Failed Conditions
Pull Request — master (#3361)
by
unknown
03:02
created

Diff   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 159
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
setChangeLog() 0 1 ?
itemFN() 0 1 ?
A compare() 0 6 1
A preference() 0 15 5
B preProcess() 0 34 8
A getExtendedRevisionInfo() 0 18 3
A buildDiffHead() 0 4 1
1
<?php
2
3
namespace dokuwiki\Ui;
4
5
/**
6
 * DokuWiki Diff Interface
7
 * parent class of PageDiff and MediaDiff
8
 *
9
 * @package dokuwiki\Ui
10
 */
11
abstract 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|string */
18
    protected $oldRev;  // timestamp of older revision, '' means current one
19
    protected $newRev;  // timestamp of newer revision, '' means current one
20
21
    /* @var array */
22
    protected $preference = [];
23
24
    /* @var ChangeLog */
25
    protected $changelog; // PageChangeLog or MediaChangeLog object
26
27
    /**
28
     * Diff Ui constructor
29
     *
30
     * @param string $id  page id or media id
31
     */
32
    public function __construct($id)
33
    {
34
        $this->id = $id;
35
        $this->setChangeLog();
36
    }
37
38
    /**
39
     * set class property changelog
40
     */
41
    abstract protected function setChangeLog();
42
43
    /**
44
     * item filename resolver
45
     *
46
     * @param string $id  page id or media id
47
     * @param int|string $rev revision timestamp, or empty string for current one
48
     * @return string full path
49
     */
50
    abstract protected function itemFN($id, $rev = '');
51
52
    /**
53
     * Set a pair of revisions to be compared
54
     *
55
     * @param int $oldRev
56
     * @param int $newRev
57
     * @return $this
58
     */
59
    public function compare($oldRev, $newRev)
60
    {
61
        $this->oldRev = $oldRev;
62
        $this->newRev = $newRev;
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
     * Retrieve requested revision(s) and difftype from Ui\Revisions
91
     *
92
     * @return void
93
     */
94
    protected function preProcess()
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 = ''; // current revision
102
        }
103
104
        // submit button with two checked boxes
105
        $rev2 = $INPUT->arr('rev2', []);
106
        if (count($rev2) > 1) {
107
            if ($rev2[0] == 'current') {
108
                [$this->oldRev, $this->newRev] = [$rev2[1], ''];
109
            } elseif ($rev2[1] == 'current') {
110
                [$this->oldRev, $this->newRev] = [$rev2[0], ''];
111
            } elseif ($rev2[0] < $rev2[1]) {
112
                [$this->oldRev, $this->newRev] = [$rev2[0], $rev2[1]];
113
            } else {
114
                [$this->oldRev, $this->newRev] = [$rev2[1], $rev2[0]];
115
            }
116
        }
117
118
        // diff view type
119
        if ($INPUT->has('difftype')) {
120
            // retrieve requested $difftype
121
            $this->preference['difftype'] = $INPUT->str('difftype');
122
        } else {
123
            // read preference from DokuWiki cookie. PageDiff only
124
            get_doku_pref('difftype', $mode);
0 ignored issues
show
Bug introduced by
The variable $mode does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
125
            if (isset($mode)) $this->preference['difftype'] = $mode;
126
        }
127
    }
128
129
    /**
130
     * get extended revision info
131
     *
132
     * @param int|string $rev  revision identifier, '' means current one
133
     * @return array  revision info structure of a page or media file
134
     */
135
    protected function getExtendedRevisionInfo($rev)
136
    {
137
        $changelog =& $this->changelog;
138
139
        if ($rev) {
140
            $info = $changelog->getRevisionInfo($rev);
141
        } elseif (file_exists($filename = $this->itemFN($this->id))) {
142
            $rev = filemtime(fullpath($filename));
143
            $info = $changelog->getRevisionInfo($rev) + array(
144
                'current' => true,
145
            );
146
        } else { // once exists, but now removed
147
            $info = array(
148
                'current' => true,
149
            );
150
        }
151
        return array('item' => $this->item) + $info;
152
    }
153
154
155
156
    /**
157
     * Build header of diff HTML
158
     *
159
     * @param string $l_rev   Left revisions
160
     * @param string $r_rev   Right revision
161
     * @return string[] HTML snippets for diff header
162
     * @deprecated 2020-12-31
163
     */
164
    public function buildDiffHead($l_rev, $r_rev)
0 ignored issues
show
Unused Code introduced by
The parameter $l_rev is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $r_rev is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
    {
166
        dbg_deprecated('not used see '. \dokuwiki\Ui\PageDiff::class .'::show()');
167
    }
168
169
}
170