Merge pull request #3969 from dokuwiki-translate/lang_update_635_1684167422
[dokuwiki.git] / inc / Draft.php
blobea9431069e90baaeeef9d9f46913bdf734c61c13
1 <?php
3 namespace dokuwiki;
5 /**
6 * Class Draft
8 * @package dokuwiki
9 */
10 class Draft
13 protected $errors = [];
14 protected $cname;
15 protected $id;
16 protected $client;
18 /**
19 * Draft constructor.
21 * @param string $ID the page id for this draft
22 * @param string $client the client identification (username or ip or similar) for this draft
24 public function __construct($ID, $client)
26 $this->id = $ID;
27 $this->client = $client;
28 $this->cname = getCacheName("$client\n$ID", '.draft');
29 if(file_exists($this->cname) && file_exists(wikiFN($ID))) {
30 if (filemtime($this->cname) < filemtime(wikiFN($ID))) {
31 // remove stale draft
32 $this->deleteDraft();
37 /**
38 * Get the filename for this draft (whether or not it exists)
40 * @return string
42 public function getDraftFilename()
44 return $this->cname;
47 /**
48 * Checks if this draft exists on the filesystem
50 * @return bool
52 public function isDraftAvailable()
54 return file_exists($this->cname);
57 /**
58 * Save a draft of a current edit session
60 * The draft will not be saved if
61 * - drafts are deactivated in the config
62 * - or the editarea is empty and there are no event handlers registered
63 * - or the event is prevented
65 * @triggers DRAFT_SAVE
67 * @return bool whether has the draft been saved
69 public function saveDraft()
71 global $INPUT, $INFO, $EVENT_HANDLER, $conf;
72 if (!$conf['usedraft']) {
73 return false;
75 if (!$INPUT->post->has('wikitext') &&
76 !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')) {
77 return false;
79 $draft = [
80 'id' => $this->id,
81 'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
82 'text' => $INPUT->post->str('wikitext'),
83 'suffix' => $INPUT->post->str('suffix'),
84 'date' => $INPUT->post->int('date'),
85 'client' => $this->client,
86 'cname' => $this->cname,
87 'errors' => [],
89 $event = new Extension\Event('DRAFT_SAVE', $draft);
90 if ($event->advise_before()) {
91 $draft['hasBeenSaved'] = io_saveFile($draft['cname'], serialize($draft));
92 if ($draft['hasBeenSaved']) {
93 $INFO['draft'] = $draft['cname'];
95 } else {
96 $draft['hasBeenSaved'] = false;
98 $event->advise_after();
100 $this->errors = $draft['errors'];
102 return $draft['hasBeenSaved'];
106 * Get the text from the draft file
108 * @throws \RuntimeException if the draft file doesn't exist
110 * @return string
112 public function getDraftText()
114 if (!file_exists($this->cname)) {
115 throw new \RuntimeException(
116 "Draft for page $this->id and user $this->client doesn't exist at $this->cname."
119 $draft = unserialize(io_readFile($this->cname,false));
120 return cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
124 * Remove the draft from the filesystem
126 * Also sets $INFO['draft'] to null
128 public function deleteDraft()
130 global $INFO;
131 @unlink($this->cname);
132 $INFO['draft'] = null;
136 * Get a formatted message stating when the draft was saved
138 * @return string
140 public function getDraftMessage()
142 global $lang;
143 return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname));
147 * Retrieve the errors that occured when saving the draft
149 * @return array
151 public function getErrors()
153 return $this->errors;
157 * Get the timestamp when this draft was saved
159 * @return int
161 public function getDraftDate()
163 return filemtime($this->cname);