xref: /dokuwiki/inc/Draft.php (revision 0aabe6f83fad7d8ce23367ad642b6979fc65b4f0)
1*0aabe6f8SMichael Große<?php
2*0aabe6f8SMichael Große
3*0aabe6f8SMichael Großenamespace dokuwiki;
4*0aabe6f8SMichael Große
5*0aabe6f8SMichael Große/**
6*0aabe6f8SMichael Große * Class Draft
7*0aabe6f8SMichael Große *
8*0aabe6f8SMichael Große * @package dokuwiki
9*0aabe6f8SMichael Große */
10*0aabe6f8SMichael Großeclass Draft
11*0aabe6f8SMichael Große{
12*0aabe6f8SMichael Große
13*0aabe6f8SMichael Große    protected $errors = [];
14*0aabe6f8SMichael Große    protected $cname;
15*0aabe6f8SMichael Große    protected $id;
16*0aabe6f8SMichael Große    protected $client;
17*0aabe6f8SMichael Große
18*0aabe6f8SMichael Große    public function __construct($ID, $client)
19*0aabe6f8SMichael Große    {
20*0aabe6f8SMichael Große        $this->id = $ID;
21*0aabe6f8SMichael Große        $this->client = $client;
22*0aabe6f8SMichael Große        $this->cname = getCacheName($client.$ID, '.draft');
23*0aabe6f8SMichael Große        if(file_exists($this->cname) && file_exists(wikiFN($ID))) {
24*0aabe6f8SMichael Große            if (filemtime($this->cname) < filemtime(wikiFN($ID))) {
25*0aabe6f8SMichael Große                // remove stale draft
26*0aabe6f8SMichael Große                $this->deleteDraft();
27*0aabe6f8SMichael Große            }
28*0aabe6f8SMichael Große        }
29*0aabe6f8SMichael Große    }
30*0aabe6f8SMichael Große
31*0aabe6f8SMichael Große    /**
32*0aabe6f8SMichael Große     * Get the filename for this draft (whether or not it exists)
33*0aabe6f8SMichael Große     *
34*0aabe6f8SMichael Große     * @return string
35*0aabe6f8SMichael Große     */
36*0aabe6f8SMichael Große    public function getDraftFilename()
37*0aabe6f8SMichael Große    {
38*0aabe6f8SMichael Große        return $this->cname;
39*0aabe6f8SMichael Große    }
40*0aabe6f8SMichael Große
41*0aabe6f8SMichael Große    /**
42*0aabe6f8SMichael Große     * Checks if this draft exists on the filesystem
43*0aabe6f8SMichael Große     *
44*0aabe6f8SMichael Große     * @return bool
45*0aabe6f8SMichael Große     */
46*0aabe6f8SMichael Große    public function isDraftAvailable()
47*0aabe6f8SMichael Große    {
48*0aabe6f8SMichael Große        return file_exists($this->cname);
49*0aabe6f8SMichael Große    }
50*0aabe6f8SMichael Große
51*0aabe6f8SMichael Große    /**
52*0aabe6f8SMichael Große     * Save a draft of a current edit session
53*0aabe6f8SMichael Große     *
54*0aabe6f8SMichael Große     * The draft will not be saved if
55*0aabe6f8SMichael Große     *   - drafts are deactivated in the config
56*0aabe6f8SMichael Große     *   - or the editarea is empty and there are no event handlers registered
57*0aabe6f8SMichael Große     *   - or the event is prevented
58*0aabe6f8SMichael Große     *
59*0aabe6f8SMichael Große     * @triggers DRAFT_SAVE
60*0aabe6f8SMichael Große     *
61*0aabe6f8SMichael Große     * @return bool whether has the draft been saved
62*0aabe6f8SMichael Große     */
63*0aabe6f8SMichael Große    public function saveDraft()
64*0aabe6f8SMichael Große    {
65*0aabe6f8SMichael Große        global $INPUT, $INFO, $EVENT_HANDLER, $conf;
66*0aabe6f8SMichael Große        if (!$conf['usedraft']) {
67*0aabe6f8SMichael Große            return false;
68*0aabe6f8SMichael Große        }
69*0aabe6f8SMichael Große        if (!$INPUT->post->has('wikitext') &&
70*0aabe6f8SMichael Große            !$EVENT_HANDLER->hasHandlerForEvent('DRAFT_SAVE')) {
71*0aabe6f8SMichael Große            return false;
72*0aabe6f8SMichael Große        }
73*0aabe6f8SMichael Große        $draft = [
74*0aabe6f8SMichael Große            'id' => $this->id,
75*0aabe6f8SMichael Große            'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
76*0aabe6f8SMichael Große            'text' => $INPUT->post->str('wikitext'),
77*0aabe6f8SMichael Große            'suffix' => $INPUT->post->str('suffix'),
78*0aabe6f8SMichael Große            'date' => $INPUT->post->int('date'),
79*0aabe6f8SMichael Große            'client' => $this->client,
80*0aabe6f8SMichael Große            'cname' => $this->cname,
81*0aabe6f8SMichael Große            'errors' => [],
82*0aabe6f8SMichael Große        ];
83*0aabe6f8SMichael Große        $event = new \Doku_Event('DRAFT_SAVE', $draft);
84*0aabe6f8SMichael Große        if ($event->advise_before()) {
85*0aabe6f8SMichael Große            $draft['hasBeenSaved'] = io_saveFile($draft['cname'], serialize($draft));
86*0aabe6f8SMichael Große            if ($draft['hasBeenSaved']) {
87*0aabe6f8SMichael Große                $INFO['draft'] = $draft['cname'];
88*0aabe6f8SMichael Große            }
89*0aabe6f8SMichael Große        } else {
90*0aabe6f8SMichael Große            $draft['hasBeenSaved'] = false;
91*0aabe6f8SMichael Große        }
92*0aabe6f8SMichael Große        $event->advise_after();
93*0aabe6f8SMichael Große
94*0aabe6f8SMichael Große        $this->errors = $draft['errors'];
95*0aabe6f8SMichael Große
96*0aabe6f8SMichael Große        return $draft['hasBeenSaved'];
97*0aabe6f8SMichael Große    }
98*0aabe6f8SMichael Große
99*0aabe6f8SMichael Große    /**
100*0aabe6f8SMichael Große     * Get the text from the draft file
101*0aabe6f8SMichael Große     *
102*0aabe6f8SMichael Große     * @throws \RuntimeException if the draft file doesn't exist
103*0aabe6f8SMichael Große     *
104*0aabe6f8SMichael Große     * @return string
105*0aabe6f8SMichael Große     */
106*0aabe6f8SMichael Große    public function getDraftText()
107*0aabe6f8SMichael Große    {
108*0aabe6f8SMichael Große        if (!file_exists($this->cname)) {
109*0aabe6f8SMichael Große            throw new \RuntimeException(
110*0aabe6f8SMichael Große                "Draft for page $this->id and user $this->client doesn't exist at $this->cname."
111*0aabe6f8SMichael Große            );
112*0aabe6f8SMichael Große        }
113*0aabe6f8SMichael Große        $draft = unserialize(io_readFile($this->cname,false));
114*0aabe6f8SMichael Große        return cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
115*0aabe6f8SMichael Große    }
116*0aabe6f8SMichael Große
117*0aabe6f8SMichael Große    /**
118*0aabe6f8SMichael Große     * Remove the draft from the filesystem
119*0aabe6f8SMichael Große     *
120*0aabe6f8SMichael Große     * Also sets $INFO['draft'] to null
121*0aabe6f8SMichael Große     */
122*0aabe6f8SMichael Große    public function deleteDraft()
123*0aabe6f8SMichael Große    {
124*0aabe6f8SMichael Große        global $INFO;
125*0aabe6f8SMichael Große        @unlink($this->cname);
126*0aabe6f8SMichael Große        $INFO['draft'] = null;
127*0aabe6f8SMichael Große    }
128*0aabe6f8SMichael Große
129*0aabe6f8SMichael Große    /**
130*0aabe6f8SMichael Große     * Get a formatted message stating when the draft was saved
131*0aabe6f8SMichael Große     *
132*0aabe6f8SMichael Große     * @return string
133*0aabe6f8SMichael Große     */
134*0aabe6f8SMichael Große    public function getDraftMessage()
135*0aabe6f8SMichael Große    {
136*0aabe6f8SMichael Große        global $lang;
137*0aabe6f8SMichael Große        return $lang['draftdate'] . ' ' . dformat(filemtime($this->cname));
138*0aabe6f8SMichael Große    }
139*0aabe6f8SMichael Große
140*0aabe6f8SMichael Große    /**
141*0aabe6f8SMichael Große     * Retrieve the errors that occured when saving the draft
142*0aabe6f8SMichael Große     *
143*0aabe6f8SMichael Große     * @return array
144*0aabe6f8SMichael Große     */
145*0aabe6f8SMichael Große    public function getErrors()
146*0aabe6f8SMichael Große    {
147*0aabe6f8SMichael Große        return $this->errors;
148*0aabe6f8SMichael Große    }
149*0aabe6f8SMichael Große}
150