xref: /dokuwiki/inc/Ui/Recent.php (revision 76ab675a01bb63b077df1c43f92ca924a42afcda)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\PageChangeLog;
6use dokuwiki\ChangeLog\MediaChangeLog;
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     * @author Satoshi Sahara <sahara.satoshi@gmail.com>
39     *
40     * @return void
41     */
42    public function show()
43    {
44        global $conf, $lang;
45        global $ID;
46
47        // get recent items, and set correct pagenation parameters (first, hasNext)
48        $first = $this->first;
49        $hasNext = false;
50        $recents = $this->getRecents($first, $hasNext);
51
52        // print intro
53        print p_locale_xhtml('recent');
54
55        if (getNS($ID) != '') {
56            print '<div class="level1"><p>'
57                . sprintf($lang['recent_global'], getNS($ID), wl('', 'do=recent'))
58                .'</p></div>';
59        }
60
61        // create the form
62        $form = new Form(['id'=>'dw__recent', 'method'=>'GET', 'action'=> wl($ID), 'class'=>'changes']);
63        $form->addTagOpen('div')->addClass('no');
64        $form->setHiddenField('sectok', null);
65        $form->setHiddenField('do', 'recent');
66        $form->setHiddenField('id', $ID);
67
68        // show dropdown selector, whether include not only recent pages but also recent media files?
69        if ($conf['mediarevisions']) {
70            $this->addRecentItemSelector($form);
71        }
72
73        // start listing of recent items
74        $form->addTagOpen('ul');
75        foreach ($recents as $recent) {
76            // check possible external edition for current page or media
77            $this->checkCurrentRevision($recent);
78            $recent['current'] = true;
79
80            $objRevInfo = $this->getObjRevInfo($recent);
81            $class = ($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor': '';
82            $form->addTagOpen('li')->addClass($class);
83            $form->addTagOpen('div')->addClass('li');
84            $html = implode(' ', [
85                $objRevInfo->itemIcon(),          // filetype icon
86                $objRevInfo->editDate(),          // edit date and time
87                $objRevInfo->difflink(),          // link to diffview icon
88                $objRevInfo->revisionlink(),      // linkto revisions icon
89                $objRevInfo->itemName(),          // name of page or media
90                $objRevInfo->editSummary(),       // edit summary
91                $objRevInfo->editor(),            // editor info
92                $objRevInfo->sizechange(),        // size change indicator
93            ]);
94            $form->addHTML($html);
95            $form->addTagClose('div');
96            $form->addTagClose('li');
97        }
98        $form->addTagClose('ul');
99
100        $form->addTagClose('div'); // close div class=no
101
102        // provide navigation for pagenated recent list (of pages and/or media files)
103        $form->addHTML($this->htmlNavigation($first, $hasNext));
104
105        print $form->toHTML('Recent');
106    }
107
108    /**
109     * Get recent items, and set correct pagenation parameters (first, hasNext)
110     *
111     * @param int  $first
112     * @param bool $hasNext
113     * @return array  recent items to be shown in a pagenated list
114     *
115     * @see also dokuwiki\Changelog::getRevisionInfo()
116     */
117    protected function getRecents(&$first, &$hasNext)
118    {
119        global $ID, $conf;
120
121        $flags = 0;
122        if ($this->show_changes == 'mediafiles' && $conf['mediarevisions']) {
123            $flags = RECENTS_MEDIA_CHANGES;
124        } elseif ($this->show_changes == 'pages') {
125            $flags = 0;
126        } elseif ($conf['mediarevisions']) {
127            $flags = RECENTS_MEDIA_PAGES_MIXED;
128        }
129
130        /* we need to get one additionally log entry to be able to
131         * decide if this is the last page or is there another one.
132         * This is the cheapest solution to get this information.
133         */
134        $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
135        if (count($recents) == 0 && $first != 0) {
136            $first = 0;
137            $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
138        }
139
140        $hasNext = false;
141        if (count($recents) > $conf['recent']) {
142            $hasNext = true;
143            array_pop($recents); // remove extra log entry
144        }
145        return $recents;
146    }
147
148    /**
149     * Check possible external deletion for current page or media
150     *
151     * To keep sort order in the recent list, we ignore externally modification.
152     * It is not possible to know when external deletion had happened,
153     * $info['date'] is to be incremented 1 second when such deletion detected.
154     */
155    protected function checkCurrentRevision(array &$info)
156    {
157        $itemType = strrpos($info['id'], '.') ? 'media' : 'page';
158        if ($itemType == 'page') {
159            $changelog = new PageChangelog($info['id']);
160        } else {
161            $changelog = new MediaChangelog($info['id']);
162        }
163        if (!$changelog->isCurrentRevision($info['date'])) {
164            // the page or media file was externally created, edited or deleted
165            $currentRevInfo = $changelog->getCurrentRevisionInfo();
166            $info = array_merge($info, $currentRevInfo);
167        }
168        unset($changelog);
169    }
170
171    /**
172     * Navigation buttons for Pagenation (prev/next)
173     *
174     * @param int  $first
175     * @param bool $hasNext
176     * @return string html
177     */
178    protected function htmlNavigation($first, $hasNext)
179    {
180        global $conf, $lang;
181
182        $last = $first + $conf['recent'];
183        $html = '<div class="pagenav">';
184        if ($first > 0) {
185            $first = max($first - $conf['recent'], 0);
186            $html.= '<div class="pagenav-prev">';
187            $html.= '<button type="submit" name="first['.$first.']" accesskey="n"'
188                  . ' title="'.$lang['btn_newer'].' [N]" class="button show">'
189                  . $lang['btn_newer']
190                  . '</button>';
191            $html.= '</div>';
192        }
193        if ($hasNext) {
194            $html.= '<div class="pagenav-next">';
195            $html.= '<button type="submit" name="first['.$last.']" accesskey="p"'
196                  . ' title="'.$lang['btn_older'].' [P]" class="button show">'
197                  . $lang['btn_older']
198                  . '</button>';
199            $html.= '</div>';
200        }
201        $html.= '</div>';
202        return $html;
203    }
204
205    /**
206     * Add dropdown selector of item types to the form instance
207     *
208     * @param Form $form
209     * @return void
210     */
211    protected function addRecentItemSelector(Form $form)
212    {
213        global $lang;
214
215        $form->addTagOpen('div')->addClass('changeType');
216        $options = array(
217                    'pages'      => $lang['pages_changes'],
218                    'mediafiles' => $lang['media_changes'],
219                    'both'       => $lang['both_changes'],
220        );
221        $form->addDropdown('show_changes', $options, $lang['changes_type'])
222                ->val($this->show_changes)->addClass('quickselect');
223        $form->addButton('do[recent]', $lang['btn_apply'])->attr('type','submit');
224        $form->addTagClose('div');
225    }
226
227    /**
228     * Returns instance of objRevInfo
229     * @param array $info  Revision info structure of page or media
230     * @return objRevInfo object (anonymous class)
231     */
232    protected function getObjRevInfo(array $info)
233    {
234        return new class ($info) // anonymous class (objRevInfo)
235        {
236            protected $info;
237
238            public function __construct(array $info)
239            {
240                $info['item'] = strrpos($info['id'], '.') ? 'media' : 'page';
241                $info['current'] = $info['current'] ?? false;
242                $this->info = $info;
243            }
244
245            // fileicon of the page or media file
246            public function itemIcon()
247            {
248                $id = $this->info['id'];
249                switch ($this->info['item']) {
250                    case 'media': // media file revision
251                        $html = media_printicon($id);
252                        break;
253                    case 'page': // page revision
254                        $html = '<img class="icon" src="'.DOKU_BASE.'lib/images/fileicons/file.png" alt="'.$id.'" />';
255                }
256                return $html;
257            }
258
259            // edit date and time of the page or media file
260            public function editDate()
261            {
262                return '<span class="date">'. dformat($this->info['date']) .'</span>';
263            }
264
265            // edit summary
266            public function editSummary()
267            {
268                return '<span class="sum">'.' – '. hsc($this->info['sum']).'</span>';
269            }
270
271            // editor of the page or media file
272            public function editor()
273            {
274                $html = '<span class="user">';
275                if ($this->info['user']) {
276                    $html.= '<bdi>'. editorinfo($this->info['user']) .'</bdi>';
277                    if (auth_ismanager()) $html.= ' <bdo dir="ltr">('. $this->info['ip'] .')</bdo>';
278                } else {
279                    $html.= '<bdo dir="ltr">'. $this->info['ip'] .'</bdo>';
280                }
281                $html.= '</span>';
282                return $html;
283            }
284
285            // name of the page or media file
286            public function itemName()
287            {
288                $id = $this->info['id'];
289                switch ($this->info['item']) {
290                    case 'media': // media file revision
291                        $href = media_managerURL(['tab_details'=>'view', 'image'=> $id, 'ns'=> getNS($id)], '&');
292                        $class = file_exists(mediaFN($id)) ? 'wikilink1' : 'wikilink2';
293                        return '<a href="'.$href.'" class="'.$class.'">'.$id.'</a>';
294                    case 'page': // page revision
295                        return html_wikilink(':'.$id, (useHeading('navigation') ? null : $id));
296                }
297                return '';
298            }
299
300            // icon difflink
301            public function difflink()
302            {
303                global $lang;
304                $id = $this->info['id'];
305
306                $href = '';
307                switch ($this->info['item']) {
308                    case 'media': // media file revision
309                        $revs = (new MediaChangeLog($id))->getRevisions(0, 1);
310                        $showLink = (count($revs) && file_exists(mediaFN($id)));
311                        if ($showLink) {
312                            $href = media_managerURL(
313                                ['tab_details'=>'history', 'mediado'=>'diff', 'image'=> $id, 'ns'=> getNS($id)], '&'
314                            );
315                        }
316                        break;
317                    case 'page': // page revision
318                        if($this->info['type'] !== DOKU_CHANGE_TYPE_CREATE) {
319                            $href = wl($id, "do=diff", false, '&');
320                        }
321                }
322
323                if ($href) {
324                    $html = '<a href="'.$href.'" class="diff_link">'
325                          . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"'
326                          . ' title="'.$lang['diff'].'" alt="'.$lang['diff'].'" />'
327                          . '</a>';
328                } else {
329                    $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />';
330                }
331                return $html;
332            }
333
334            // icon revision link
335            public function revisionlink()
336            {
337                global $lang;
338                $id = $this->info['id'];
339                switch ($this->info['item']) {
340                    case 'media': // media file revision
341                        $href = media_managerURL(['tab_details'=>'history', 'image'=> $id, 'ns'=> getNS($id)], '&');
342                        break;
343                    case 'page': // page revision
344                        $href = wl($id, "do=revisions", false, '&');
345                }
346                return '<a href="'.$href.'" class="revisions_link">'
347                      . '<img src="'.DOKU_BASE.'lib/images/history.png" width="12" height="14"'
348                      . ' title="'.$lang['btn_revs'].'" alt="'.$lang['btn_revs'].'" />'
349                      . '</a>';
350            }
351
352            // size change
353            public function sizeChange()
354            {
355                $class = 'sizechange';
356                $value = filesize_h(abs($this->info['sizechange']));
357                if ($this->info['sizechange'] > 0) {
358                    $class .= ' positive';
359                    $value = '+' . $value;
360                } elseif ($this->info['sizechange'] < 0) {
361                    $class .= ' negative';
362                    $value = '-' . $value;
363                } else {
364                    $value = '±' . $value;
365                }
366                return '<span class="'.$class.'">'.$value.'</span>';
367            }
368        }; // end of anonymous class (objRevInfo)
369    }
370
371}
372