xref: /dokuwiki/inc/Ui/Diff.php (revision 0ea1f71b7d54820e8e5d7d960b0a4a8ed4206099)
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        // ensure to have valid changelog::currentRevision property
38        $this->changelog->getCurrentRevisionInfo();
39    }
40
41    /**
42     * set class property changelog
43     */
44    abstract protected function setChangeLog();
45
46    /**
47     * Prepare revision info of comparison pair
48     */
49    abstract protected function preProcess();
50
51    /**
52     * Set a pair of revisions to be compared
53     *
54     * @param int $oldRev
55     * @param int $newRev
56     * @return $this
57     */
58    public function compare($oldRev, $newRev)
59    {
60        if ($oldRev < $newRev) {
61            [$this->oldRev, $this->newRev] = [$oldRev, $newRev];
62        } else {
63            [$this->oldRev, $this->newRev] = [$newRev, $oldRev];
64        }
65        return $this;
66    }
67
68    /**
69     * Gets or Sets preference of the Ui\Diff object
70     *
71     * @param string|array $prefs  a key name or key-value pair(s)
72     * @param mixed $value         value used when the first args is string
73     * @return array|$this
74     */
75    public function preference($prefs = null, $value = null)
76    {
77        // set
78        if (is_string($prefs) && isset($value)) {
79            $this->preference[$prefs] = $value;
80            return $this;
81        } elseif (is_array($prefs)) {
82            foreach ($prefs as $name => $value) {
83                $this->preference[$name] = $value;
84            }
85            return $this;
86        }
87        // get
88        return $this->preference;
89    }
90
91    /**
92     * Handle requested revision(s)
93     *
94     * @return void
95     */
96    protected function handle()
97    {
98        global $INPUT;
99
100        // difflink icon click, eg. ?rev=123456789&do=diff
101        if ($INPUT->has('rev')) {
102            $this->oldRev = $INPUT->int('rev');
103            $this->newRev = $this->changelog->currentRevision();
104        }
105
106        // submit button with two checked boxes
107        $rev2 = $INPUT->arr('rev2', []);
108        if (count($rev2) > 1) {
109            if ($rev2[0] < $rev2[1]) {
110                [$this->oldRev, $this->newRev] = [$rev2[0], $rev2[1]];
111            } else {
112                [$this->oldRev, $this->newRev] = [$rev2[1], $rev2[0]];
113            }
114        }
115    }
116
117
118
119
120    /**
121     * Build header of diff HTML
122     *
123     * @param string $l_rev   Left revisions
124     * @param string $r_rev   Right revision
125     * @return string[] HTML snippets for diff header
126     * @deprecated 2020-12-31
127     */
128    public function buildDiffHead($l_rev, $r_rev)
129    {
130        dbg_deprecated('not used see '. \dokuwiki\Ui\PageDiff::class .'::show()');
131    }
132
133}
134