You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

169 lines
4.1 KiB

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