xref: /dokuwiki/inc/Ui/Recent.php (revision 9e3166413adffd9d067b4cde8da8d04c44d9c8bf)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\MediaChangeLog;
6use dokuwiki\Extension\Event;
7use dokuwiki\Form\Form;
8
9/**
10 * DokuWiki Recent Interface
11 *
12 * @package dokuwiki\Ui
13 */
14class 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 <andi@splitbrain.org>
35     * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
36     * @author Ben Coburn <btcoburn@silicodon.net>
37     * @author Kate Arzamastseva <pshns@ukr.net>
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);
66        if (count($recents) == 0 && $first != 0) {
67            $first = 0;
68            $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
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
127            $form->addTagOpen('span')->addClass('date');
128            $form->addHTML($date);
129            $form->addTagClose('span');
130
131            $diff = false;
132            $href = '';
133
134            if (!empty($recent['media'])) {
135                $changelog = new MediaChangeLog($recent['id']);
136                $revs = $changelog->getRevisions(0, 1);
137                $diff = (count($revs) && file_exists(mediaFN($recent['id'])));
138                if ($diff) {
139                    $href = media_managerURL(
140                        array(
141                            'tab_details' => 'history',
142                            'mediado' => 'diff',
143                            'image' => $recent['id'],
144                            'ns' => getNS($recent['id'])
145                        ), '&'
146                    );
147                }
148            } else {
149                $href = wl($recent['id'], "do=diff", false, '&');
150            }
151
152            if (!empty($recent['media']) && !$diff) {
153                $form->addTag('img')->attrs([
154                        'src'    => DOKU_BASE .'lib/images/blank.gif',
155                        'width'  => 15,
156                        'height' => 11,
157                        'alt' => '',
158                ]);
159            } else {
160                $form->addTagOpen('a')->attr('href', $href)->addClass('diff_link');
161                $form->addTag('img')->attrs([
162                        'src'    => DOKU_BASE .'lib/images/diff.png',
163                        'width'  => 15,
164                        'height' => 11,
165                        'title'  => $lang['diff'],
166                        'alt'    => $lang['diff'],
167                ]);
168                $form->addTagClose('a');
169            }
170
171            if (!empty($recent['media'])) {
172                $href = media_managerURL(
173                    array(
174                        'tab_details' => 'history',
175                        'image' => $recent['id'],
176                        'ns' => getNS($recent['id'])
177                    ), '&'
178                );
179            } else {
180                $href = wl($recent['id'], "do=revisions", false, '&');
181            }
182            $form->addTagOpen('a')->attr('href', $href)->addClass('revisions_link');
183            $form->addTag('img')->attrs([
184                    'src'    => DOKU_BASE .'lib/images/history.png',
185                    'width'  => 12,
186                    'height' => 14,
187                    'title'  => $lang['btn_revs'],
188                    'alt'    => $lang['btn_revs']
189            ]);
190            $form->addTagClose('a');
191
192            if (!empty($recent['media'])) {
193                $href = media_managerURL(
194                    array(
195                        'tab_details' => 'view',
196                        'image' => $recent['id'],
197                        'ns' => getNS($recent['id'])
198                    ), '&'
199                );
200                $class = file_exists(mediaFN($recent['id'])) ? 'wikilink1' : 'wikilink2';
201                $form->addTagOpen('a')->attr('href', $href)->addClass($class);
202                $form->addHTML($recent['id']);
203                $form->addTagClose('a');
204            } else {
205                $form->addHTML(html_wikilink(':'. $recent['id'], useHeading('navigation') ? null : $recent['id']));
206            }
207            $form->addTagOpen('span')->addClass('sum');
208            $form->addHTML(' – '. hsc($recent['sum']));
209            $form->addTagClose('span');
210
211            $form->addTagOPen('span')->addClass('user');
212            if ($recent['user']) {
213                $form->addHTML('<bdi>'. editorinfo($recent['user']) .'</bdi>');
214                if (auth_ismanager()) {
215                    $form->addHTML(' <bdo dir="ltr">('. $recent['ip'] .')</bdo>');
216                }
217            } else {
218                $form->addHTML('<bdo dir="ltr">'. $recent['ip'] .'</bdo>');
219            }
220            $form->addTagClose('span');
221
222            html_sizechange($recent['sizechange'], $form);
223
224            $form->addTagClose('div');
225            $form->addTagClose('li');
226        }
227
228        $form->addTagClose('ul');
229
230        // provide navigation for pagenated cecent list (of pages and/or media files)
231        $form->addTagOpen('div')->addClass('pagenav');
232        $last = $first + $conf['recent'];
233        if ($first > 0) {
234            $first = $first - $conf['recent'];
235            if ($first < 0) $first = 0;
236            $form->addTagOpen('div')->addClass('pagenav-prev');
237            $form->addTagOpen('button')->attrs([
238                    'type'      => 'submit',
239                    'name'      => 'first['. $first .']',
240                    'accesskey' => 'n',
241                    'title'     => $lang['btn_newer'] .' [N]',
242            ])->addClass('button show');
243            $form->addHTML($lang['btn_newer']);
244            $form->addTagClose('button');
245            $form->addTagClose('div');
246        }
247        if ($hasNext) {
248            $form->addTagOpen('div')->addClass('pagenav-next');
249            $form->addTagOpen('button')->attrs([
250                    'type'      => 'submit',
251                    'name'      => 'first['. $last .']',
252                    'accesskey' => 'p',
253                    'title'     => $lang['btn_older'] .' [P]',
254            ])->addClass('button show');
255            $form->addHTML($lang['btn_older']);
256            $form->addTagClose('button');
257            $form->addTagClose('div');
258        }
259        $form->addTagClose('div');
260
261        $form->addTagClose('div'); // close div class=no
262
263        // emit HTML_CRECENTFORM_OUTPUT event, print the form
264        Event::createAndTrigger('HTML_RECENTFORM_OUTPUT', $form, 'html_form_output', false);
265
266        print DOKU_LF;
267    }
268
269}
270