Merge pull request #4056 from dokuwiki-translate/lang_update_704_1694501527
[dokuwiki.git] / inc / Draft.php
blob4f108f60e63139fc9e0b85b3f16e9b436a11c1c4
1 <?php
3 namespace dokuwiki;
5 use dokuwiki\Extension\Event;
7 /**
8 * Class Draft
10 * @package dokuwiki
12 class Draft
14 protected $errors = [];
15 protected $cname;
16 protected $id;
17 protected $client;
19 /**
20 * Draft constructor.
22 * @param string $ID the page id for this draft
23 * @param string $client the client identification (username or ip or similar) for this draft
25 public function __construct($ID, $client)
27 $this->id = $ID;
28 $this->client = $client;
29 $this->cname = getCacheName("$client\n$ID", '.draft');
30 if (file_exists($this->cname) && file_exists(wikiFN($ID))) {
31 if (filemtime($this->cname) < filemtime(wikiFN($ID))) {
32 // remove stale draft
33 $this->deleteDraft();
38 /**
39 * Get the filename for this draft (whether or not it exists)
41 * @return string
43 public function getDraftFilename()
45 return $this->cname;
48 /**
49 * Checks if this draft exists on the filesystem
51 * @return bool
53 public function isDraftAvailable()
55 return file_exists($this->cname);
58 /**
59 * Save a draft of a current edit session
61 * The draft will not be saved if
62 * - drafts are deactivated in the config
63 * - or the editarea is empty and there are no event handlers registered
64 * - or the event is prevented
66 * @triggers DRAFT_SAVE
68 * @return bool whether has the draft been saved
70 public function saveDraft()
72 global $INPUT, $INFO, $EVENT_HANDLER, $conf;
73 if (!$conf['usedraft']) {
74 return false;
76 if (
77 !$INPUT->post->has('wikitext') &&
78 !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')
79 ) {
80 return false;
82 $draft = [
83 'id' => $this->id,
84 'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
85 'text' => $INPUT->post->str('wikitext'),
86 'suffix' => $INPUT->post->str('suffix'),
87 'date' => $INPUT->post->int('date'),
88 'client' => $this->client,
89 'cname' => $this->cname,
90 'errors' => [],
92 $event = new Event('DRAFT_SAVE', $draft);
93 if ($event->advise_before()) {
94 $draft['hasBeenSaved'] = io_saveFile($draft['cname'], serialize($draft));
95 if ($draft['hasBeenSaved']) {
96 $INFO['draft'] = $draft['cname'];
98 } else {
99 $draft['hasBeenSaved'] = false;
101 $event->advise_after();
103 $this->errors = $draft['errors'];
105 return $draft['hasBeenSaved'];
109 * Get the text from the draft file
111 * @throws \RuntimeException if the draft file doesn't exist
113 * @return string
115 public function getDraftText()
117 if (!file_exists($this->cname)) {
118 throw new \RuntimeException(
119 "Draft for page $this->id and user $this->client doesn't exist at $this->cname."
122 $draft = unserialize(io_readFile($this->cname, false));
123 return cleanText(con($draft['prefix'], $draft['text'], $draft['suffix'], true));
127 * Remove the draft from the filesystem
129 * Also sets $INFO['draft'] to null
131 public function deleteDraft()
133 global $INFO;
134 @unlink($this->cname);
135 $INFO['draft'] = null;
139 * Get a formatted message stating when the draft was saved
141 * @return string
143 public function getDraftMessage()
145 global $lang;
146 return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname));
150 * Retrieve the errors that occured when saving the draft
152 * @return array
154 public function getErrors()
156 return $this->errors;
160 * Get the timestamp when this draft was saved
162 * @return int
164 public function getDraftDate()
166 return filemtime($this->cname);