Failed Conditions
Pull Request — master (#3198)
by
unknown
03:25
created

Revisions.php$0 ➔ editor()   A

Complexity

Conditions 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\Ui;
4
5
use dokuwiki\ChangeLog\PageChangeLog;
6
use dokuwiki\ChangeLog\MediaChangeLog;
7
use dokuwiki\Extension\Event;
8
use dokuwiki\Form\Form;
9
10
/**
11
 * DokuWiki Revisions Interface
12
 *
13
 * @package dokuwiki\Ui
14
 */
15
class Revisions extends Ui
16
{
17
    protected $first;
18
    protected $media_id;
19
20
    /** 
21
     * Revisions Ui constructor
22
     *
23
     * @param int $first  skip the first n changelog lines
24
     * @param bool|string $media_id  id of media, or false for current page
25
     */
26
    public function __construct($first = 0, $media_id = false)
27
    {
28
        $this->first    = $first;
29
        $this->media_id = $media_id;
30
    }
31
32
    /**
33
     * Display list of old revisions
34
     *
35
     * @author Andreas Gohr <[email protected]>
36
     * @author Ben Coburn <[email protected]>
37
     * @author Kate Arzamastseva <[email protected]>
38
     *
39
     * @triggers HTML_REVISIONSFORM_OUTPUT
40
     * @return void
41
     */
42
    public function show()
43
    {
44
        global $ID;
45
46
        if ($this->media_id) {
47
            return $this->showMediaRevisions($this->media_id);
0 ignored issues
show
Bug introduced by
It seems like $this->media_id can also be of type boolean; however, dokuwiki\Ui\Revisions::showMediaRevisions() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
        } else {
49
            return $this->showPageRevisions($ID);
50
        }
51
    }
52
53
    /**
54
     * Display a list of Media Revisions in the MediaManager
55
     *
56
     * @param string $id  media id
57
     * @return void
58
     */
59
    protected function showMediaRevisions($id)
60
    {
61
        global $lang;
62
63
        // get revisions, and set correct pagenation parameters (first, hasNext)
64
        $first   = $this->first;
65
        $hasNext = false;
66
        $revisions = $this->getRevisions($first, $hasNext);
67
68
        // create the form
69
        $form = new Form([
70
                'id' => 'page__revisions', // must not be "media__revisions"
71
                'action' => media_managerURL(['image' => $id], '&'),
72
                'class'  => 'changes',
73
        ]);
74
        $form->setHiddenField('mediado', 'diff'); // required for media revisions
75
        $form->addTagOpen('div')->addClass('no');
76
77
        // start listing
78
        $form->addTagOpen('ul');
79
        foreach ($revisions as $info) {
80
            $rev = $info['date'];
81
            $class = ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : '';
82
            $form->addTagOpen('li')->addClass($class);
83
            $form->addTagOpen('div')->addClass('li');
84
85
            if (isset($info['current'])) {
86
               $form->addCheckbox('rev2[]')->val('current');
87
            } elseif (file_exists(mediaFN($id, $rev))) {
88
                $form->addCheckbox('rev2[]')->val($rev);
89
            } else {
90
                $form->addCheckbox('')->val($rev)->attr('disabled','disabled');
91
            }
92
            $form->addHTML(' ');
93
94
            $objRevInfo = $this->getObjRevInfo($info);
95
            $html = implode(' ', [
96
                $objRevInfo->editDate(),          // edit date and time
97
                $objRevInfo->difflink(),          // link to diffview icon
98
                $objRevInfo->itemName(),          // name of page or media
99
                '<div>',
100
                $objRevInfo->editSummary(),       // edit summary
101
                $objRevInfo->editor(),            // editor info
102
                html_sizechange($info['sizechange']), // size change indicator
103
                $objRevInfo->currentIndicator(),  // current indicator (only when k=1)
104
                '</div>',
105
            ]);
106
            $form->addHTML($html);
107
108
            $form->addTagClose('div');
109
            $form->addTagClose('li');
110
        }
111
        $form->addTagClose('ul');  // end of revision list
112
113
        // show button for diff view
114
        $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit');
115
116
        $form->addTagClose('div'); // close div class=no
117
118
        // emit HTML_REVISIONSFORM_OUTPUT event
119
        Event::createAndTrigger('HTML_REVISIONSFORM_OUTPUT', $form, null, false);
120
        print $form->toHTML();
121
122
        // provide navigation for pagenated revision list (of pages and/or media files)
123
        print $this->htmlNavigation($id, $first, $hasNext);
124
    }
125
126
    /**
127
     * Display a list of Page Revisions
128
     *
129
     * @return void
130
     */
131
    protected function showPageRevisions($id)
132
    {
133
        global $lang;
134
135
        // get revisions, and set correct pagenation parameters (first, hasNext)
136
        $first   = $this->first;
137
        $hasNext = false;
138
        $revisions = $this->getRevisions($first, $hasNext);
139
140
        // print intro
141
        print p_locale_xhtml('revisions');
142
143
        // create the form
144
        $form = new Form([
145
                'id' => 'page__revisions',
146
                'class' => 'changes',
147
        ]);
148
        $form->addTagOpen('div')->addClass('no');
149
150
        // start listing
151
        $form->addTagOpen('ul');
152
        foreach ($revisions as $info) {
153
            $rev = $info['date'];
154
            $class = ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : '';
155
            $form->addTagOpen('li')->addClass($class);
156
            $form->addTagOpen('div')->addClass('li');
157
158
            if (page_exists($id, $rev)) {
159
                $form->addCheckbox('rev2[]')->val($rev);
160
            } else {
161
                $form->addCheckbox('')->val($rev)->attr('disabled','disabled');
162
            }
163
            $form->addHTML(' ');
164
165
            $objRevInfo = $this->getObjRevInfo($info);
166
            $html = implode(' ', [
167
                $objRevInfo->editDate(),          // edit date and time
168
                $objRevInfo->difflink(),          // link to diffview icon
169
                $objRevInfo->itemName(),          // name of page or media
170
                $objRevInfo->editSummary(),       // edit summary
171
                $objRevInfo->editor(),            // editor info
172
                html_sizechange($info['sizechange']), // size change indicator
173
                $objRevInfo->currentIndicator(),  // current indicator (only when k=1)
174
            ]);
175
            $form->addHTML($html);
176
            $form->addTagClose('div');
177
            $form->addTagClose('li');
178
        }
179
        $form->addTagClose('ul');  // end of revision list
180
181
        // show button for diff view
182
        $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit');
183
184
        $form->addTagClose('div'); // close div class=no
185
186
        // emit HTML_REVISIONSFORM_OUTPUT event
187
        Event::createAndTrigger('HTML_REVISIONSFORM_OUTPUT', $form, null, false);
188
        print $form->toHTML();
189
190
        // provide navigation for pagenated revision list (of pages and/or media files)
191
        print $this->htmlNavigation($id, $first, $hasNext);
192
    }
193
194
195
    /**
196
     * Get revisions, and set correct pagenation parameters (first, hasNext)
197
     *
198
     * @param int  $first
199
     * @param bool $hasNext
200
     * @return array  revisions to be shown in a pagenated list
201
     */
202
    protected function getRevisions(&$first, &$hasNext)
203
    {
204
        global $INFO, $conf;
205
206
        if ($this->media_id) {
207
            $changelog = new MediaChangeLog($this->media_id);
0 ignored issues
show
Bug introduced by
It seems like $this->media_id can also be of type boolean; however, dokuwiki\ChangeLog\ChangeLog::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
208
        } else {
209
            $changelog = new PageChangeLog($INFO['id']);
210
        }
211
212
        $revisions = [];
213
214
        /* we need to get one additional log entry to be able to
215
         * decide if this is the last page or is there another one.
216
         * see also Ui\Recent::getRecents()
217
         */
218
        $revlist = $changelog->getRevisions($first, $conf['recent'] +1);
219
        if (count($revlist) == 0 && $first != 0) {
220
            $first = 0;
221
            $revlist = $changelog->getRevisions($first, $conf['recent'] +1);
222
        }
223
        $exists = ($this->media_id) ? file_exists(mediaFN($this->media_id)) : $INFO['exists'];
0 ignored issues
show
Bug introduced by
It seems like $this->media_id can also be of type boolean; however, mediaFN() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
224
        if ($first === 0 && $exists) {
225
            // add current page or media as revision[0]
226
            if ($this->media_id) {
227
                $rev = filemtime(fullpath(mediaFN($this->media_id)));
0 ignored issues
show
Bug introduced by
It seems like $this->media_id can also be of type boolean; however, mediaFN() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
228
                $revisions[] = $changelog->getRevisionInfo($rev) + array(
229
                        'media' => true,
230
                        'current' => true,
231
                );
232
            } else {
233
                $revisions[] = array(
234
                        'date' => $INFO['lastmod'],
235
                        'ip'   => null,
236
                        'type' => $INFO['meta']['last_change']['type'],
237
                        'id'   => $INFO['id'],
238
                        'user' => $INFO['editor'],
239
                        'sum'  => $INFO['sum'],
240
                        'extra' => null,
241
                        'sizechange' => $INFO['meta']['last_change']['sizechange'],
242
                        'current' => true,
243
                );
244
            }
245
        }
246
247
        // decide if this is the last page or is there another one
248
        $hasNext = false;
249
        if (count($revlist) > $conf['recent']) {
250
            $hasNext = true;
251
            array_pop($revlist); // remove one additional log entry
252
        }
253
254
        // append each revison info array to the revisions
255
        foreach ($revlist as $rev) {
256
            if ($this->media_id) {
257
                $revisions[] = $changelog->getRevisionInfo($rev) + array('media' => true);
258
            } else {
259
                $revisions[] = $changelog->getRevisionInfo($rev);
260
            }
261
        }
262
        return $revisions;
263
    }
264
265
    /**
266
     * Navigation buttons for Pagenation (prev/next)
267
     *
268
     * @param string $id  page id or media id
269
     * @param int  $first
270
     * @param bool $hasNext
271
     * @return array  html
272
     */
273
    protected function htmlNavigation($id, $first, $hasNext)
274
    {
275
        global $conf;
276
277
        $html = '<div class="pagenav">';
278
        $last = $first + $conf['recent'];
279
        if ($first > 0) {
280
            $first = max($first - $conf['recent'], 0);
281
            $html.= '<div class="pagenav-prev">';
282
            if ($this->media_id) {
283
                $html.= html_btn('newer', $id, "p", media_managerURL(['first' => $first], '&', false, true));
0 ignored issues
show
Documentation introduced by
media_managerURL(array('...rst), '&', false, true) is of type string|array, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
284
            } else {
285
                $html.= html_btn('newer', $id, "p" ,['do' => 'revisions', 'first' => $first]);
286
            }
287
            $html.= '</div>';
288
        }
289
        if ($hasNext) {
290
            $html.= '<div class="pagenav-next">';
291
            if ($this->media_id) {
292
                $html.= html_btn('older', $id, "n", media_managerURL(['first' => $last], '&', false, true));
0 ignored issues
show
Documentation introduced by
media_managerURL(array('...ast), '&', false, true) is of type string|array, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
293
            } else {
294
                $html.= html_btn('older', $id, "n", ['do' => 'revisions', 'first' => $last]);
295
            }
296
            $html.= '</div>';
297
        }
298
        $html.= '</div>';
299
        return $html;
300
    }
301
302
/* ----------------------------------------------------------------------------------------
303
    $html = implode(' ', [            // Ui\Recent display      // Ui\Revisions display
304
        $objRevInfo->itemIcon(),      // filetype icon
305
        $objRevInfo->editDate(),      // edit date and time     // edit date and time
306
        $objRevInfo->difflink(),      // link to diffview icon  // link to diffview icon
307
        $objRevInfo->revisionlink(),  // linkto revisions icon
308
        $objRevInfo->itemName(),      // name of page or media  // name of page
309
        $objRevInfo->editSummary(),   // edit summary           // edit summary
310
        $objRevInfo->editor(),        // editor info            // editor info
311
        html_sizechange               // size change indicator  // size change indicator
312
                                                                // current indicator (only when k=1)
313
    ]);
314
 * ---------------------------------------------------------------------------------------- */
315
316
    /**
317
     * Returns instance of objRevInfo
318
     *
319
     * @param array $info  Revision info structure of a page or media file
320
     * @return objRevInfo object (anonymous class)
321
     */
322
    protected function getObjRevInfo(array $info)
323
    {
324
        return new class ($info) // anonymous class (objRevInfo)
325
        {
326
            protected $info;
327
328
            public function __construct(array $info)
329
            {
330
                $this->info = $info;
331
            }
332
333
            // current indicator
334
            public function currentIndicator()
335
            {
336
                global $lang;
337
                return ($this->info['current']) ? '('.$lang['current'].')' : '';
338
            }
339
340
            // edit date and time of the page or media file
341
            public function editDate()
342
            {
343
                return '<span class="date">'. dformat($this->info['date']) .'</span>';
344
            }
345
346
            // edit summary
347
            public function editSummary()
348
            {
349
                return '<span class="sum">'.' – '. hsc($this->info['sum']).'</span>';
350
            }
351
352
            // editor of the page or media file
353
            public function editor()
354
            {
355
                // slightly different with display of Ui\Recent, i.e. external edit
356
                global $lang;
357
                $html = '<span class="user">';
358
                if (!$this->info['user'] && !$this->info['ip']) {
359
                    $html.= '('.$lang['external_edit'].')';
360
                } elseif ($this->info['user']) {
361
                    $html.= '<bdi>'. editorinfo($this->info['user']) .'</bdi>';
362
                    if (auth_ismanager()) $html.= ' <bdo dir="ltr">('. $this->info['ip'] .')</bdo>';
363
                } else {
364
                    $html.= '<bdo dir="ltr">'. $this->info['ip'] .'</bdo>';
365
                }
366
                $html.= '</span>';
367
                return $html;
368
            }
369
370
            // name of the page or media file
371
            public function itemName()
372
            {
373
                // slightly different with display of Ui\Recent, i.e. revison may not exists
374
                $id = $this->info['id'];
375
                $rev = $this->info['date'];
376
377
                if (isset($this->info['media'])) {
378
                    // media file revision
379
                    if (isset($this->info['current'])) {
380
                        $href = media_managerURL(['image'=> $id, 'tab_details'=> 'view'], '&');
381
                        $html = '<a href="'.$href.'" class="wikilink1">'.$id.'</a>';
382
                    } elseif (file_exists(mediaFN($id, $rev))) {
383
                        $href = media_managerURL(['image'=> $id, 'tab_details'=> 'view', 'rev'=> $rev], '&');
384
                        $html = '<a href="'.$href.'" class="wikilink1">'.$id.'</a>';
385
                    } else {
386
                        $html = $id;
387
                    }
388
                    return $html;
389
                } else {
390
                    // page revision
391
                    $display_name = useHeading('navigation') ? hsc(p_get_first_heading($id)) : $id;
392
                    if (!$display_name) $display_name = $id;
393
                    if ($this->info['current'] || page_exists($id, $rev)) {
394
                        $href = wl($id, "rev=$rev", false, '&');
395
                        $html = '<a href="'.$href.'" class="wikilink1">'.$display_name.'</a>';
396
                    } else {
397
                        $html = $display_name;
398
                    }
399
                    return $html;
400
                }
401
            }
402
403
            // icon difflink
404
            public function difflink()
405
            {
406
                global $lang;
407
                $id = $this->info['id'];
408
                $rev = $this->info['date'];
409
410
                if (isset($this->info['media'])) {
411
                    // media file revision
412
                    if (isset($this->info['current']) || !file_exists(mediaFN($id, $rev))) {
413
                        $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />';
414
                    } else {
415
                        $href = media_managerURL(['image'=> $id, 'rev'=> $rev, 'mediado'=>'diff'], '&');
416
                        $html = '<a href="'.$href.'" class="diff_link">'
417
                              . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"'
418
                              . ' title="'. $lang['diff'] .'" alt="'.$lang['diff'] .'" />'
419
                              . '</a> ';
420
                    }
421
                    return $html;
422
                } else {
423
                    // page revision
424
                    if ($this->info['current'] || !page_exists($id, $rev)) {
425
                        $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />';
426
                    } else {
427
                        $href = wl($id, "rev=$rev,do=diff", false, '&');
428
                        $html = '<a href="'.$href.'" class="diff_link">'
429
                              . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"'
430
                              . ' title="'.$lang['diff'].'" alt="'.$lang['diff'].'" />'
431
                              . '</a>';
432
                    }
433
                    return $html;
434
                }
435
            }
436
437
            // size change
438
            public function sizeChange()
439
            {
440
                $class = 'sizechange';
441
                $value = filesize_h(abs($this->info['sizechange']));
442
                if ($this->info['sizechange'] > 0) {
443
                    $class .= ' positive';
444
                    $value = '+' . $value;
445
                } elseif ($this->info['sizechange'] < 0) {
446
                    $class .= ' negative';
447
                    $value = '-' . $value;
448
                } else {
449
                    $value = '±' . $value;
450
                }
451
                return '<span class="'.$class.'">'.$value.'</span>';
452
            }
453
        }; // end of anonymous class (objRevInfo)
454
    }
455
456
}
457