10aabe6f8SMichael Große<?php 20aabe6f8SMichael Große 30aabe6f8SMichael Großenamespace dokuwiki; 40aabe6f8SMichael Große 50aabe6f8SMichael Große/** 60aabe6f8SMichael Große * Class Draft 70aabe6f8SMichael Große * 80aabe6f8SMichael Große * @package dokuwiki 90aabe6f8SMichael Große */ 100aabe6f8SMichael Großeclass Draft 110aabe6f8SMichael Große{ 120aabe6f8SMichael Große 130aabe6f8SMichael Große protected $errors = []; 140aabe6f8SMichael Große protected $cname; 150aabe6f8SMichael Große protected $id; 160aabe6f8SMichael Große protected $client; 170aabe6f8SMichael Große 18e432cc89SMichael Große /** 19e432cc89SMichael Große * Draft constructor. 20e432cc89SMichael Große * 21e432cc89SMichael Große * @param string $ID the page id for this draft 22e432cc89SMichael Große * @param string $client the client identification (username or ip or similar) for this draft 23e432cc89SMichael Große */ 240aabe6f8SMichael Große public function __construct($ID, $client) 250aabe6f8SMichael Große { 260aabe6f8SMichael Große $this->id = $ID; 270aabe6f8SMichael Große $this->client = $client; 280aabe6f8SMichael Große $this->cname = getCacheName($client.$ID, '.draft'); 290aabe6f8SMichael Große if(file_exists($this->cname) && file_exists(wikiFN($ID))) { 300aabe6f8SMichael Große if (filemtime($this->cname) < filemtime(wikiFN($ID))) { 310aabe6f8SMichael Große // remove stale draft 320aabe6f8SMichael Große $this->deleteDraft(); 330aabe6f8SMichael Große } 340aabe6f8SMichael Große } 350aabe6f8SMichael Große } 360aabe6f8SMichael Große 370aabe6f8SMichael Große /** 380aabe6f8SMichael Große * Get the filename for this draft (whether or not it exists) 390aabe6f8SMichael Große * 400aabe6f8SMichael Große * @return string 410aabe6f8SMichael Große */ 420aabe6f8SMichael Große public function getDraftFilename() 430aabe6f8SMichael Große { 440aabe6f8SMichael Große return $this->cname; 450aabe6f8SMichael Große } 460aabe6f8SMichael Große 470aabe6f8SMichael Große /** 480aabe6f8SMichael Große * Checks if this draft exists on the filesystem 490aabe6f8SMichael Große * 500aabe6f8SMichael Große * @return bool 510aabe6f8SMichael Große */ 520aabe6f8SMichael Große public function isDraftAvailable() 530aabe6f8SMichael Große { 540aabe6f8SMichael Große return file_exists($this->cname); 550aabe6f8SMichael Große } 560aabe6f8SMichael Große 570aabe6f8SMichael Große /** 580aabe6f8SMichael Große * Save a draft of a current edit session 590aabe6f8SMichael Große * 600aabe6f8SMichael Große * The draft will not be saved if 610aabe6f8SMichael Große * - drafts are deactivated in the config 620aabe6f8SMichael Große * - or the editarea is empty and there are no event handlers registered 630aabe6f8SMichael Große * - or the event is prevented 640aabe6f8SMichael Große * 650aabe6f8SMichael Große * @triggers DRAFT_SAVE 660aabe6f8SMichael Große * 670aabe6f8SMichael Große * @return bool whether has the draft been saved 680aabe6f8SMichael Große */ 690aabe6f8SMichael Große public function saveDraft() 700aabe6f8SMichael Große { 710aabe6f8SMichael Große global $INPUT, $INFO, $EVENT_HANDLER, $conf; 720aabe6f8SMichael Große if (!$conf['usedraft']) { 730aabe6f8SMichael Große return false; 740aabe6f8SMichael Große } 750aabe6f8SMichael Große if (!$INPUT->post->has('wikitext') && 760aabe6f8SMichael Große !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')) { 770aabe6f8SMichael Große return false; 780aabe6f8SMichael Große } 790aabe6f8SMichael Große $draft = [ 800aabe6f8SMichael Große 'id' => $this->id, 810aabe6f8SMichael Große 'prefix' => substr($INPUT->post->str('prefix'), 0, -1), 820aabe6f8SMichael Große 'text' => $INPUT->post->str('wikitext'), 830aabe6f8SMichael Große 'suffix' => $INPUT->post->str('suffix'), 840aabe6f8SMichael Große 'date' => $INPUT->post->int('date'), 850aabe6f8SMichael Große 'client' => $this->client, 860aabe6f8SMichael Große 'cname' => $this->cname, 870aabe6f8SMichael Große 'errors' => [], 880aabe6f8SMichael Große ]; 890aabe6f8SMichael Große $event = new \Doku_Event('DRAFT_SAVE', $draft); 900aabe6f8SMichael Große if ($event->advise_before()) { 910aabe6f8SMichael Große $draft['hasBeenSaved'] = io_saveFile($draft['cname'], serialize($draft)); 920aabe6f8SMichael Große if ($draft['hasBeenSaved']) { 930aabe6f8SMichael Große $INFO['draft'] = $draft['cname']; 940aabe6f8SMichael Große } 950aabe6f8SMichael Große } else { 960aabe6f8SMichael Große $draft['hasBeenSaved'] = false; 970aabe6f8SMichael Große } 980aabe6f8SMichael Große $event->advise_after(); 990aabe6f8SMichael Große 1000aabe6f8SMichael Große $this->errors = $draft['errors']; 1010aabe6f8SMichael Große 1020aabe6f8SMichael Große return $draft['hasBeenSaved']; 1030aabe6f8SMichael Große } 1040aabe6f8SMichael Große 1050aabe6f8SMichael Große /** 1060aabe6f8SMichael Große * Get the text from the draft file 1070aabe6f8SMichael Große * 1080aabe6f8SMichael Große * @throws \RuntimeException if the draft file doesn't exist 1090aabe6f8SMichael Große * 1100aabe6f8SMichael Große * @return string 1110aabe6f8SMichael Große */ 1120aabe6f8SMichael Große public function getDraftText() 1130aabe6f8SMichael Große { 1140aabe6f8SMichael Große if (!file_exists($this->cname)) { 1150aabe6f8SMichael Große throw new \RuntimeException( 1160aabe6f8SMichael Große "Draft for page $this->id and user $this->client doesn't exist at $this->cname." 1170aabe6f8SMichael Große ); 1180aabe6f8SMichael Große } 1190aabe6f8SMichael Große $draft = unserialize(io_readFile($this->cname,false)); 1200aabe6f8SMichael Große return cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true)); 1210aabe6f8SMichael Große } 1220aabe6f8SMichael Große 1230aabe6f8SMichael Große /** 1240aabe6f8SMichael Große * Remove the draft from the filesystem 1250aabe6f8SMichael Große * 1260aabe6f8SMichael Große * Also sets $INFO['draft'] to null 1270aabe6f8SMichael Große */ 1280aabe6f8SMichael Große public function deleteDraft() 1290aabe6f8SMichael Große { 1300aabe6f8SMichael Große global $INFO; 1310aabe6f8SMichael Große @unlink($this->cname); 1320aabe6f8SMichael Große $INFO['draft'] = null; 1330aabe6f8SMichael Große } 1340aabe6f8SMichael Große 1350aabe6f8SMichael Große /** 1360aabe6f8SMichael Große * Get a formatted message stating when the draft was saved 1370aabe6f8SMichael Große * 1380aabe6f8SMichael Große * @return string 1390aabe6f8SMichael Große */ 1400aabe6f8SMichael Große public function getDraftMessage() 1410aabe6f8SMichael Große { 1420aabe6f8SMichael Große global $lang; 1430aabe6f8SMichael Große return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname)); 1440aabe6f8SMichael Große } 1450aabe6f8SMichael Große 1460aabe6f8SMichael Große /** 1470aabe6f8SMichael Große * Retrieve the errors that occured when saving the draft 1480aabe6f8SMichael Große * 1490aabe6f8SMichael Große * @return array 1500aabe6f8SMichael Große */ 1510aabe6f8SMichael Große public function getErrors() 1520aabe6f8SMichael Große { 1530aabe6f8SMichael Große return $this->errors; 1540aabe6f8SMichael Große } 155*520438b3SMichael Große 156*520438b3SMichael Große /** 157*520438b3SMichael Große * Get the timestamp when this draft was saved 158*520438b3SMichael Große * 159*520438b3SMichael Große * @return int 160*520438b3SMichael Große */ 161*520438b3SMichael Große public function getDraftDate() 162*520438b3SMichael Große { 163*520438b3SMichael Große return filemtime($this->cname); 164*520438b3SMichael Große } 1650aabe6f8SMichael Große} 166