xref: /dokuwiki/inc/Ui/Diff.php (revision 591ebe4523e0e3be14097d6d097eb5796628dcba)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\ChangeLog;
6
7/**
8 * DokuWiki Diff Interface
9 * parent class of PageDiff and MediaDiff
10 *
11 * @package dokuwiki\Ui
12 */
13abstract class Diff extends Ui
14{
15    /* @var string */
16    protected $id;   // page id or media id
17
18    /* @var int */
19    protected $oldRev;  // timestamp of older revision
20    protected $newRev;  // timestamp of newer revision
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     * Prepare revision info of comparison pair
46     */
47    abstract protected function preProcess();
48
49    /**
50     * Set a pair of revisions to be compared
51     *
52     * @param int $oldRev
53     * @param int $newRev
54     * @return $this
55     */
56    public function compare($oldRev, $newRev)
57    {
58        if ($oldRev < $newRev) {
59            [$this->oldRev, $this->newRev] = [$oldRev, $newRev];
60        } else {
61            [$this->oldRev, $this->newRev] = [$newRev, $oldRev];
62        }
63        return $this;
64    }
65
66    /**
67     * Gets or Sets preference of the Ui\Diff object
68     *
69     * @param string|array $prefs  a key name or key-value pair(s)
70     * @param mixed $value         value used when the first args is string
71     * @return array|$this
72     */
73    public function preference($prefs = null, $value = null)
74    {
75        // set
76        if (is_string($prefs) && isset($value)) {
77            $this->preference[$prefs] = $value;
78            return $this;
79        } elseif (is_array($prefs)) {
80            foreach ($prefs as $name => $value) {
81                $this->preference[$name] = $value;
82            }
83            return $this;
84        }
85        // get
86        return $this->preference;
87    }
88
89    /**
90     * Handle requested revision(s)
91     *
92     * @return void
93     */
94    protected function handle()
95    {
96        global $INPUT;
97
98        // difflink icon click, eg. ?rev=123456789&do=diff
99        if ($INPUT->has('rev')) {
100            $this->oldRev = $INPUT->int('rev');
101            $this->newRev = $this->changelog->currentRevision();
102        }
103
104        // submit button with two checked boxes
105        $rev2 = $INPUT->arr('rev2', []);
106        if (count($rev2) > 1) {
107            if ($rev2[0] < $rev2[1]) {
108                [$this->oldRev, $this->newRev] = [$rev2[0], $rev2[1]];
109            } else {
110                [$this->oldRev, $this->newRev] = [$rev2[1], $rev2[0]];
111            }
112        }
113
114        if (!isset($this->oldRev, $this->newRev)) {
115            // no revision was given, compare previous to current
116            $revs = $this->changelog->getRevisions(-1, 2);
117            $this->newRev = $this->changelog->currentRevision();
118            $this->oldRev = ($revs[0] == $this->newRev) ? $revs[1] : $revs[0];
119        }
120    }
121
122
123
124
125    /**
126     * Build header of diff HTML
127     *
128     * @param string $l_rev   Left revisions
129     * @param string $r_rev   Right revision
130     * @return string[] HTML snippets for diff header
131     * @deprecated 2020-12-31
132     */
133    public function buildDiffHead($l_rev, $r_rev)
134    {
135        dbg_deprecated('not used see '. \dokuwiki\Ui\PageDiff::class .'::show()');
136    }
137
138}
139