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