5 use dokuwiki\Extension\Event
;
14 protected $errors = [];
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)
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))) {
39 * Get the filename for this draft (whether or not it exists)
43 public function getDraftFilename()
49 * Checks if this draft exists on the filesystem
53 public function isDraftAvailable()
55 return file_exists($this->cname
);
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']) {
77 !$INPUT->post
->has('wikitext') &&
78 !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')
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
,
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'];
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
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()
134 @unlink
($this->cname
);
135 $INFO['draft'] = null;
139 * Get a formatted message stating when the draft was saved
143 public function getDraftMessage()
146 return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname
));
150 * Retrieve the errors that occured when saving the draft
154 public function getErrors()
156 return $this->errors
;
160 * Get the timestamp when this draft was saved
164 public function getDraftDate()
166 return filemtime($this->cname
);