Failed Conditions
Pull Request — master (#3198)
by
unknown
02:51
created

Recent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\Ui;
4
5
use dokuwiki\ChangeLog\MediaChangeLog;
6
use dokuwiki\Extension\Event;
7
use dokuwiki\Form\Form;
8
9
/**
10
 * DokuWiki Recent Interface
11
 *
12
 * @package dokuwiki\Ui
13
 */
14
class Recent extends Ui
15
{
16
    protected $first;
17
    protected $show_changes;
18
19
    /** 
20
     * Recent Ui constructor
21
     *
22
     * @param int $first  skip the first n changelog lines
23
     * @param string $show_changes  type of changes to show; pages, mediafiles, or both
24
     */
25
    public function __construct($first = 0, $show_changes = 'both')
26
    {
27
        $this->first        = $first;
28
        $this->show_changes = $show_changes;
29
    }
30
31
    /**
32
     * Display recent changes
33
     *
34
     * @author Andreas Gohr <[email protected]>
35
     * @author Matthias Grimm <[email protected]>
36
     * @author Ben Coburn <[email protected]>
37
     * @author Kate Arzamastseva <[email protected]>
38
     *
39
     * @triggers HTML_RECENTFORM_OUTPUT
40
     * @return void
41
     */
42
    public function show()
43
    {
44
        global $conf;
45
        global $lang;
46
        global $ID;
47
48
        $first = $this->first;
49
        $show_changes = $this->show_changes;
50
51
        /* we need to get one additionally log entry to be able to
52
         * decide if this is the last page or is there another one.
53
         * This is the cheapest solution to get this information.
54
         */
55
        $flags = 0;
56
        if ($show_changes == 'mediafiles' && $conf['mediarevisions']) {
57
            $flags = RECENTS_MEDIA_CHANGES;
58
        } elseif ($show_changes == 'pages') {
59
            $flags = 0;
60
        } elseif ($conf['mediarevisions']) {
61
            $show_changes = 'both';
62
            $flags = RECENTS_MEDIA_PAGES_MIXED;
63
        }
64
65
        $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
0 ignored issues
show
Security Bug introduced by
It seems like getNS($ID) targeting getNS() can also be of type false; however, getRecents() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
66
        if (count($recents) == 0 && $first != 0) {
67
            $first = 0;
68
            $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
0 ignored issues
show
Security Bug introduced by
It seems like getNS($ID) targeting getNS() can also be of type false; however, getRecents() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
69
        }
70
71
        $hasNext = false;
72
        if (count($recents) > $conf['recent']) {
73
            $hasNext = true;
74
            array_pop($recents); // remove extra log entry
75
        }
76
77
        // print intro
78
        print p_locale_xhtml('recent');
79
80
        if (getNS($ID) != '') {
81
            print '<div class="level1"><p>'
82
                . sprintf($lang['recent_global'], getNS($ID), wl('', 'do=recent'))
83
                .'</p></div>';
84
        }
85
86
        // create the form
87
        $form = new Form(['id' => 'dw__recent', 'method' => 'GET', 'action'=>wl($ID)]);
88
        $form->addClass('changes');
89
        $form->addTagOpen('div')->addClass('no');
90
        $form->setHiddenField('sectok', null);
91
        $form->setHiddenField('do', 'recent');
92
        $form->setHiddenField('id', $ID);
93
94
        // show dropdown selector, whether include not only recent pages but also recent media files?
95
        if ($conf['mediarevisions']) {
96
            $form->addTagOpen('div')->addClass('changeType');
97
            $options = array(
98
                            'pages'      => $lang['pages_changes'],
99
                            'mediafiles' => $lang['media_changes'],
100
                            'both'       => $lang['both_changes'],
101
            );
102
            $form->addDropdown('show_changes', $options, $lang['changes_type'])
103
                ->val($show_changes)->addClass('quickselect');
104
            $form->addButton('do[recent]', $lang['btn_apply'])->attr('type','submit');
105
            $form->addTagClose('div');
106
        }
107
108
        // start listing
109
        $form->addTagOpen('ul');
110
111
        foreach ($recents as $recent) {
112
            $date = dformat($recent['date']);
113
            $class = ($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor': '';
114
115
            $form->addTagOpen('li')->addClass($class);
116
            $form->addTagOpen('div')->addClass('li');
117
118
            if (!empty($recent['media'])) {
119
                $form->addHTML(media_printicon($recent['id']));
120
            } else {
121
                $form->addTag('img')->attrs([
122
                        'src' => DOKU_BASE .'lib/images/fileicons/file.png',
123
                        'alt' => $recent['id']
124
                ])->addClass('icon');
125
            }
126
            $form->addHTML(' ');
127
128
            $form->addTagOpen('span')->addClass('date');
129
            $form->addHTML($date);
130
            $form->addTagClose('span');
131
            $form->addHTML(' ');
132
133
            $diff = false;
134
            $href = '';
135
136
            if (!empty($recent['media'])) {
137
                $changelog = new MediaChangeLog($recent['id']);
138
                $revs = $changelog->getRevisions(0, 1);
139
                $diff = (count($revs) && file_exists(mediaFN($recent['id'])));
140
                if ($diff) {
141
                    $href = media_managerURL(
142
                        array(
143
                            'tab_details' => 'history',
144
                            'mediado' => 'diff',
145
                            'image' => $recent['id'],
146
                            'ns' => getNS($recent['id'])
147
                        ), '&'
148
                    );
149
                }
150
            } else {
151
                $href = wl($recent['id'], "do=diff", false, '&');
152
            }
153
154
            if (!empty($recent['media']) && !$diff) {
155
                $form->addTag('img')->attrs([
156
                        'src'    => DOKU_BASE .'lib/images/blank.gif',
157
                        'width'  => 15,
158
                        'height' => 11,
159
                        'alt' => '',
160
                ]);
161
            } else {
162
                $form->addTagOpen('a')->attr('href', $href)->addClass('diff_link');
0 ignored issues
show
Bug introduced by
It seems like $href defined by media_managerURL(array('...S($recent['id'])), '&') on line 141 can also be of type array; however, dokuwiki\Form\Element::attr() does only seem to accept null|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...
163
                $form->addTag('img')->attrs([
164
                        'src'    => DOKU_BASE .'lib/images/diff.png',
165
                        'width'  => 15,
166
                        'height' => 11,
167
                        'title'  => $lang['diff'],
168
                        'alt'    => $lang['diff'],
169
                ]);
170
                $form->addTagClose('a');
171
            }
172
            $form->addHTML(' ');
173
174
            if (!empty($recent['media'])) {
175
                $href = media_managerURL(
176
                    array(
177
                        'tab_details' => 'history',
178
                        'image' => $recent['id'],
179
                        'ns' => getNS($recent['id'])
180
                    ), '&'
181
                );
182
            } else {
183
                $href = wl($recent['id'], "do=revisions", false, '&');
184
            }
185
            $form->addTagOpen('a')->attr('href', $href)->addClass('revisions_link');
0 ignored issues
show
Bug introduced by
It seems like $href defined by media_managerURL(array('...S($recent['id'])), '&') on line 175 can also be of type array; however, dokuwiki\Form\Element::attr() does only seem to accept null|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...
186
            $form->addTag('img')->attrs([
187
                    'src'    => DOKU_BASE .'lib/images/history.png',
188
                    'width'  => 12,
189
                    'height' => 14,
190
                    'title'  => $lang['btn_revs'],
191
                    'alt'    => $lang['btn_revs']
192
            ]);
193
            $form->addTagClose('a');
194
            $form->addHTML(' ');
195
196
            if (!empty($recent['media'])) {
197
                $href = media_managerURL(
198
                    array(
199
                        'tab_details' => 'view',
200
                        'image' => $recent['id'],
201
                        'ns' => getNS($recent['id'])
202
                    ), '&'
203
                );
204
                $class = file_exists(mediaFN($recent['id'])) ? 'wikilink1' : 'wikilink2';
205
                $form->addTagOpen('a')->attr('href', $href)->addClass($class);
0 ignored issues
show
Bug introduced by
It seems like $href defined by media_managerURL(array('...S($recent['id'])), '&') on line 197 can also be of type array; however, dokuwiki\Form\Element::attr() does only seem to accept null|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...
206
                $form->addHTML($recent['id']);
207
                $form->addTagClose('a');
208
            } else {
209
                $form->addHTML(html_wikilink(':'. $recent['id'], useHeading('navigation') ? null : $recent['id']));
210
            }
211
            $form->addTagOpen('span')->addClass('sum');
212
            $form->addHTML(' – '. hsc($recent['sum']));
213
            $form->addTagClose('span');
214
215
            $form->addTagOPen('span')->addClass('user');
216
            if ($recent['user']) {
217
                $form->addHTML('<bdi>'. editorinfo($recent['user']) .'</bdi>');
218
                if (auth_ismanager()) {
219
                    $form->addHTML(' <bdo dir="ltr">('. $recent['ip'] .')</bdo>');
220
                }
221
            } else {
222
                $form->addHTML('<bdo dir="ltr">'. $recent['ip'] .'</bdo>');
223
            }
224
            $form->addTagClose('span');
225
            $form->addHTML(' ');
226
227
            $form->addHTML(html_sizechange($recent['sizechange']));
228
229
            $form->addTagClose('div');
230
            $form->addTagClose('li');
231
        }
232
233
        $form->addTagClose('ul');
234
235
        // provide navigation for pagenated cecent list (of pages and/or media files)
236
        $form->addTagOpen('div')->addClass('pagenav');
237
        $last = $first + $conf['recent'];
238
        if ($first > 0) {
239
            $first = $first - $conf['recent'];
240
            if ($first < 0) $first = 0;
241
            $form->addTagOpen('div')->addClass('pagenav-prev');
242
            $form->addTagOpen('button')->attrs([
243
                    'type'      => 'submit',
244
                    'name'      => 'first['. $first .']',
245
                    'accesskey' => 'n',
246
                    'title'     => $lang['btn_newer'] .' [N]',
247
            ])->addClass('button show');
248
            $form->addHTML($lang['btn_newer']);
249
            $form->addTagClose('button');
250
            $form->addTagClose('div');
251
        }
252
        if ($hasNext) {
253
            $form->addTagOpen('div')->addClass('pagenav-next');
254
            $form->addTagOpen('button')->attrs([
255
                    'type'      => 'submit',
256
                    'name'      => 'first['. $last .']',
257
                    'accesskey' => 'p',
258
                    'title'     => $lang['btn_older'] .' [P]',
259
            ])->addClass('button show');
260
            $form->addHTML($lang['btn_older']);
261
            $form->addTagClose('button');
262
            $form->addTagClose('div');
263
        }
264
        $form->addTagClose('div');
265
266
        $form->addTagClose('div'); // close div class=no
267
268
        // emit HTML_CRECENTFORM_OUTPUT event
269
        Event::createAndTrigger('HTML_RECENTFORM_OUTPUT', $form, null, false);
270
        print $form->toHTML();
271
272
        print DOKU_LF;
273
    }
274
275
}
276