xref: /dokuwiki/inc/Ui/Diff.php (revision d9c75b22c52ca1b35878418dff1a583d3968d8ad)
1<?php
2
3namespace dokuwiki\Ui;
4
5/**
6 * DokuWiki Diff Interface
7 * parent class of PageDiff and MediaDiff
8 *
9 * @package dokuwiki\Ui
10 */
11abstract class Diff extends Ui
12{
13    /* @var string */
14    protected $id;   // page id or media id
15    protected $item; // page or media
16
17    /* @var int */
18    protected $oldRev;  // timestamp of older revision, left side
19    protected $newRev;  // timestamp of newer revision, right side
20    protected $last_rev; // current revision, or last revision when it had removed
21
22    /* @var array */
23    protected $preference = [];
24
25    /* @var ChangeLog */
26    protected $changelog; // PageChangeLog or MediaChangeLog object
27
28    /**
29     * Diff Ui constructor
30     *
31     * @param string $id  page id or media id
32     */
33    public function __construct($id)
34    {
35        $this->id = $id;
36        $this->setChangeLog();
37    }
38
39    /**
40     * set class property changelog
41     */
42    abstract protected function setChangeLog();
43
44    /**
45     * item filename resolver
46     *
47     * @param string $id  page id or media id
48     * @param int|string $rev revision timestamp, or empty string for current one
49     * @return string full path
50     */
51    abstract protected function itemFN($id, $rev = '');
52
53    /**
54     * Set a pair of revisions to be compared
55     *
56     * @param int $oldRev
57     * @param int $newRev
58     * @return $this
59     */
60    public function compare($oldRev, $newRev)
61    {
62        $this->oldRev = $oldRev;
63        $this->newRev = $newRev;
64        return $this;
65    }
66
67    /**
68     * Gets or Sets preference of the Ui\Diff object
69     *
70     * @param string|array $prefs  a key name or key-value pair(s)
71     * @param mixed $value         value used when the first args is string
72     * @return array|$this
73     */
74    public function preference($prefs = null, $value = null)
75    {
76        // set
77        if (is_string($prefs) && isset($value)) {
78            $this->preference[$prefs] = $value;
79            return $this;
80        } elseif (is_array($prefs)) {
81            foreach ($prefs as $name => $value) {
82                $this->preference[$name] = $value;
83            }
84            return $this;
85        }
86        // get
87        return $this->preference;
88    }
89
90    /**
91     * Retrieve requested revision(s) and difftype from Ui\Revisions
92     *
93     * @return void
94     */
95    protected function preProcess()
96    {
97        global $INPUT;
98
99        // difflink icon click, eg. ?rev=123456789&do=diff
100        if ($INPUT->has('rev')) {
101            $this->oldRev = $INPUT->int('rev');
102            $this->newRev = ''; // current revision
103        }
104
105        // submit button with two checked boxes
106        $rev2 = $INPUT->arr('rev2', []);
107        if (count($rev2) > 1) {
108            if ($rev2[0] == 'current') {
109                [$this->oldRev, $this->newRev] = [$rev2[1], ''];
110            } elseif ($rev2[1] == 'current') {
111                [$this->oldRev, $this->newRev] = [$rev2[0], ''];
112            } elseif ($rev2[0] < $rev2[1]) {
113                [$this->oldRev, $this->newRev] = [$rev2[0], $rev2[1]];
114            } else {
115                [$this->oldRev, $this->newRev] = [$rev2[1], $rev2[0]];
116            }
117        }
118
119        // diff view type
120        if ($INPUT->has('difftype')) {
121            // retrieve requested $difftype
122            $this->preference['difftype'] = $INPUT->str('difftype');
123        } else {
124            // read preference from DokuWiki cookie. PageDiff only
125            get_doku_pref('difftype', $mode);
126            if (isset($mode)) $this->preference['difftype'] = $mode;
127        }
128    }
129
130    /**
131     * get extended revision info
132     *
133     * @param int|string $rev  revision identifier, '' means current one
134     * @return array  revision info structure of a page or media file
135     */
136    protected function getExtendedRevisionInfo($rev)
137    {
138        $changelog =& $this->changelog;
139
140        if ($rev) {
141            $info = $changelog->getRevisionInfo($rev);
142        } elseif (file_exists($filename = $this->itemFN($this->id))) {
143            $rev = filemtime(fullpath($filename));
144            $info = $changelog->getRevisionInfo($rev) + array(
145                'current' => true,
146            );
147        } else { // once exists, but now removed
148            $info = array(
149                'current' => true,
150            );
151        }
152        return array('item' => $this->item) + $info;
153    }
154
155
156
157    /**
158     * Build header of diff HTML
159     *
160     * @param string $l_rev   Left revisions
161     * @param string $r_rev   Right revision
162     * @return string[] HTML snippets for diff header
163     * @deprecated 2020-12-31
164     */
165    public function buildDiffHead($l_rev, $r_rev)
166    {
167        dbg_deprecated('not used see '. \dokuwiki\Ui\PageDiff::class .'::show()');
168    }
169
170}
171