xref: /dokuwiki/inc/Ui/Diff.php (revision a19054e933f74fc12f9542af6905853350003b57)
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 $rev1 older revision
53     * @param int $rev2 newer revision
54     * @return $this
55     */
56    public function compare($rev1, $rev2)
57    {
58        if ($rev2 < $rev1) [$rev1, $rev2] = [$rev2, $rev1];
59        $this->oldRev = (int)$rev1;
60        $this->newRev = (int)$this->changelog->traceCurrentRevision($rev2);
61        return $this;
62    }
63
64    /**
65     * Gets or Sets preference of the Ui\Diff object
66     *
67     * @param string|array $prefs  a key name or key-value pair(s)
68     * @param mixed $value         value used when the first args is string
69     * @return array|$this
70     */
71    public function preference($prefs = null, $value = null)
72    {
73        // set
74        if (is_string($prefs) && isset($value)) {
75            $this->preference[$prefs] = $value;
76            return $this;
77        } elseif (is_array($prefs)) {
78            foreach ($prefs as $name => $value) {
79                $this->preference[$name] = $value;
80            }
81            return $this;
82        }
83        // get
84        return $this->preference;
85    }
86
87    /**
88     * Handle requested revision(s)
89     *
90     * @return void
91     */
92    protected function handle()
93    {
94        global $INPUT;
95
96        // difflink icon click, eg. &do=diff&rev=#
97        if ($INPUT->has('rev')) {
98            $this->oldRev = $INPUT->int('rev');
99            $this->newRev = $this->changelog->currentRevision();
100        }
101
102        // submit button with two checked boxes, eg. &do=diff&rev2[0]=#&rev2[1]=#
103        $revs = $INPUT->arr('rev2', []);
104        if (count($revs) > 1) {
105            list($rev1, $rev2) = $revs;
106            if ($rev2 < $rev1) [$rev1, $rev2] = [$rev2, $rev1];
107            $this->oldRev = (int)$rev1;
108            $this->newRev = (int)$this->changelog->traceCurrentRevision($rev2);
109        }
110
111
112        if (!isset($this->oldRev, $this->newRev)) {
113            // no revision was given, compare previous to current
114            $rev2 = $this->changelog->currentRevision();
115            if ($rev2 > $this->changelog->lastRevision()) {
116                $rev1 = $this->changelog->lastRevision();
117            } else {
118                $revs = $changelog->getRevisions(0, 1);
119                $rev1 = count($revs) ? $revs[0] : false;
120            }
121            $this->oldRev = $rev1;
122            $this->newRev = $rev2;
123        }
124    }
125}
126