13 protected $errors = [];
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)
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))) {
38 * Get the filename for this draft (whether or not it exists)
42 public function getDraftFilename()
48 * Checks if this draft exists on the filesystem
52 public function isDraftAvailable()
54 return file_exists($this->cname
);
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']) {
75 if (!$INPUT->post
->has('wikitext') &&
76 !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')) {
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
,
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'];
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
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()
131 @unlink
($this->cname
);
132 $INFO['draft'] = null;
136 * Get a formatted message stating when the draft was saved
140 public function getDraftMessage()
143 return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname
));
147 * Retrieve the errors that occured when saving the draft
151 public function getErrors()
153 return $this->errors
;
157 * Get the timestamp when this draft was saved
161 public function getDraftDate()
163 return filemtime($this->cname
);