xref: /dokuwiki/inc/Ui/Diff.php (revision d1575179188e55e8ae640896daab3b1e2297cb05)
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|string */
19    protected $oldRev;  // timestamp of older revision
20    protected $newRev;  // timestamp of newer revision, null means PageConflict or PageDraft
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     * Set a pair of revisions to be compared
48     *
49     * @param int $oldRev
50     * @param int $newRev
51     * @return $this
52     */
53    public function compare($oldRev, $newRev)
54    {
55        $this->oldRev = $oldRev;
56        $this->newRev = $newRev;
57        return $this;
58    }
59
60    /**
61     * Gets or Sets preference of the Ui\Diff object
62     *
63     * @param string|array $prefs  a key name or key-value pair(s)
64     * @param mixed $value         value used when the first args is string
65     * @return array|$this
66     */
67    public function preference($prefs = null, $value = null)
68    {
69        // set
70        if (is_string($prefs) && isset($value)) {
71            $this->preference[$prefs] = $value;
72            return $this;
73        } elseif (is_array($prefs)) {
74            foreach ($prefs as $name => $value) {
75                $this->preference[$name] = $value;
76            }
77            return $this;
78        }
79        // get
80        return $this->preference;
81    }
82
83    /**
84     * Retrieve requested revision(s) and difftype from Ui\Revisions
85     *
86     * @return void
87     */
88    protected function preProcess()
89    {
90        global $INPUT;
91        $changelog =& $this->changelog;
92
93        // difflink icon click, eg. ?rev=123456789&do=diff
94        if ($INPUT->has('rev')) {
95            $this->oldRev = $INPUT->int('rev');
96            $this->newRev = $changelog->currentRevision();
97        }
98
99        // submit button with two checked boxes
100        $rev2 = $INPUT->arr('rev2', []);
101        if (count($rev2) > 1) {
102            if ($rev2[0] == 'current') {
103                [$this->oldRev, $this->newRev] = [$rev2[1], $changelog->currentRevision()];
104            } elseif ($rev2[1] == 'current') {
105                [$this->oldRev, $this->newRev] = [$rev2[0], $changelog->currentRevision()];
106            } elseif ($rev2[0] < $rev2[1]) {
107                [$this->oldRev, $this->newRev] = [$rev2[0], $rev2[1]];
108            } else {
109                [$this->oldRev, $this->newRev] = [$rev2[1], $rev2[0]];
110            }
111        }
112
113        // diff view type
114        if ($INPUT->has('difftype')) {
115            // retrieve requested $difftype
116            $this->preference['difftype'] = $INPUT->str('difftype');
117        } else {
118            // read preference from DokuWiki cookie. PageDiff only
119            $mode = get_doku_pref('difftype', $mode = null);
120            if (isset($mode)) $this->preference['difftype'] = $mode;
121        }
122    }
123
124    /**
125     * get extended revision info
126     *
127     * @param int|string $rev  revision identifier, '' means current one, null means
128     * @return array  revision info structure of a page or media file
129     */
130    protected function getExtendedRevisionInfo($rev)
131    {
132        $changelog =& $this->changelog;
133
134        if ($rev == '' || $rev == 'current') {
135            $info = $changelog->getCurrentRevisionInfo();
136        } elseif (is_numeric($rev)) {
137            $info = $changelog->getRevisionInfo($rev);
138        } else { //if do=diff at just created page
139            $info = ['none' => true];
140        }
141        return $info;
142    }
143
144
145
146    /**
147     * Build header of diff HTML
148     *
149     * @param string $l_rev   Left revisions
150     * @param string $r_rev   Right revision
151     * @return string[] HTML snippets for diff header
152     * @deprecated 2020-12-31
153     */
154    public function buildDiffHead($l_rev, $r_rev)
155    {
156        dbg_deprecated('not used see '. \dokuwiki\Ui\PageDiff::class .'::show()');
157    }
158
159}
160