Draft.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace dokuwiki;
  3. /**
  4. * Class Draft
  5. *
  6. * @package dokuwiki
  7. */
  8. class Draft
  9. {
  10. protected $errors = [];
  11. protected $cname;
  12. protected $id;
  13. protected $client;
  14. /**
  15. * Draft constructor.
  16. *
  17. * @param string $ID the page id for this draft
  18. * @param string $client the client identification (username or ip or similar) for this draft
  19. */
  20. public function __construct($ID, $client)
  21. {
  22. $this->id = $ID;
  23. $this->client = $client;
  24. $this->cname = getCacheName("$client\n$ID", '.draft');
  25. if(file_exists($this->cname) && file_exists(wikiFN($ID))) {
  26. if (filemtime($this->cname) < filemtime(wikiFN($ID))) {
  27. // remove stale draft
  28. $this->deleteDraft();
  29. }
  30. }
  31. }
  32. /**
  33. * Get the filename for this draft (whether or not it exists)
  34. *
  35. * @return string
  36. */
  37. public function getDraftFilename()
  38. {
  39. return $this->cname;
  40. }
  41. /**
  42. * Checks if this draft exists on the filesystem
  43. *
  44. * @return bool
  45. */
  46. public function isDraftAvailable()
  47. {
  48. return file_exists($this->cname);
  49. }
  50. /**
  51. * Save a draft of a current edit session
  52. *
  53. * The draft will not be saved if
  54. * - drafts are deactivated in the config
  55. * - or the editarea is empty and there are no event handlers registered
  56. * - or the event is prevented
  57. *
  58. * @triggers DRAFT_SAVE
  59. *
  60. * @return bool whether has the draft been saved
  61. */
  62. public function saveDraft()
  63. {
  64. global $INPUT, $INFO, $EVENT_HANDLER, $conf;
  65. if (!$conf['usedraft']) {
  66. return false;
  67. }
  68. if (!$INPUT->post->has('wikitext') &&
  69. !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')) {
  70. return false;
  71. }
  72. $draft = [
  73. 'id' => $this->id,
  74. 'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
  75. 'text' => $INPUT->post->str('wikitext'),
  76. 'suffix' => $INPUT->post->str('suffix'),
  77. 'date' => $INPUT->post->int('date'),
  78. 'client' => $this->client,
  79. 'cname' => $this->cname,
  80. 'errors' => [],
  81. ];
  82. $event = new Extension\Event('DRAFT_SAVE', $draft);
  83. if ($event->advise_before()) {
  84. $draft['hasBeenSaved'] = io_saveFile($draft['cname'], serialize($draft));
  85. if ($draft['hasBeenSaved']) {
  86. $INFO['draft'] = $draft['cname'];
  87. }
  88. } else {
  89. $draft['hasBeenSaved'] = false;
  90. }
  91. $event->advise_after();
  92. $this->errors = $draft['errors'];
  93. return $draft['hasBeenSaved'];
  94. }
  95. /**
  96. * Get the text from the draft file
  97. *
  98. * @throws \RuntimeException if the draft file doesn't exist
  99. *
  100. * @return string
  101. */
  102. public function getDraftText()
  103. {
  104. if (!file_exists($this->cname)) {
  105. throw new \RuntimeException(
  106. "Draft for page $this->id and user $this->client doesn't exist at $this->cname."
  107. );
  108. }
  109. $draft = unserialize(io_readFile($this->cname,false));
  110. return cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
  111. }
  112. /**
  113. * Remove the draft from the filesystem
  114. *
  115. * Also sets $INFO['draft'] to null
  116. */
  117. public function deleteDraft()
  118. {
  119. global $INFO;
  120. @unlink($this->cname);
  121. $INFO['draft'] = null;
  122. }
  123. /**
  124. * Get a formatted message stating when the draft was saved
  125. *
  126. * @return string
  127. */
  128. public function getDraftMessage()
  129. {
  130. global $lang;
  131. return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname));
  132. }
  133. /**
  134. * Retrieve the errors that occured when saving the draft
  135. *
  136. * @return array
  137. */
  138. public function getErrors()
  139. {
  140. return $this->errors;
  141. }
  142. /**
  143. * Get the timestamp when this draft was saved
  144. *
  145. * @return int
  146. */
  147. public function getDraftDate()
  148. {
  149. return filemtime($this->cname);
  150. }
  151. }