1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Ui\Editor;
6use dokuwiki\Action\Exception\ActionAbort;
7use dokuwiki\Ui;
8
9/**
10 * Class Edit
11 *
12 * Handle editing
13 *
14 * @package dokuwiki\Action
15 */
16class Edit extends AbstractAction
17{
18    /** @inheritdoc */
19    public function minimumPermission()
20    {
21        global $INFO;
22        if ($INFO['exists']) {
23            return AUTH_READ; // we check again below
24        } else {
25            return AUTH_CREATE;
26        }
27    }
28
29    /**
30     * @inheritdoc falls back to 'source' if page not writable
31     */
32    public function checkPreconditions()
33    {
34        parent::checkPreconditions();
35        global $INFO;
36
37        // no edit permission? view source
38        if ($INFO['exists'] && !$INFO['writable']) {
39            throw new ActionAbort('source');
40        }
41    }
42
43    /** @inheritdoc */
44    public function preProcess()
45    {
46        global $ID;
47        global $INFO;
48
49        global $TEXT;
50        global $RANGE;
51        global $PRE;
52        global $SUF;
53        global $REV;
54        global $SUM;
55        global $lang;
56        global $DATE;
57
58        if (!isset($TEXT)) {
59            if ($INFO['exists']) {
60                if ($RANGE) {
61                    [$PRE, $TEXT, $SUF] = rawWikiSlices($RANGE, $ID, $REV);
62                } else {
63                    $TEXT = rawWiki($ID, $REV);
64                }
65            } else {
66                $TEXT = pageTemplate($ID);
67            }
68        }
69
70        //set summary default
71        if (!$SUM) {
72            if ($REV) {
73                $SUM = sprintf($lang['restored'], dformat($REV));
74            } elseif (!$INFO['exists']) {
75                $SUM = $lang['created'];
76            }
77        }
78
79        // Use the date of the newest revision, not of the revision we edit
80        // This is used for conflict detection
81        if (!$DATE) $DATE = @filemtime(wikiFN($ID));
82
83        //check if locked by anyone - if not lock for my self
84        $lockedby = checklock($ID);
85        if ($lockedby) {
86            throw new ActionAbort('locked');
87        }
88        lock($ID);
89    }
90
91    /** @inheritdoc */
92    public function tplContent()
93    {
94        (new Editor())->show();
95    }
96}
97