xref: /dokuwiki/inc/Ui/Revisions.php (revision 2d198df195d4a030538783bafbb57d0abedfc3eb)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\ChangeLog;
6
7/**
8 * DokuWiki Revisions Interface
9 * parent class of PageRevisions and MediaRevisions
10 *
11 * @package dokuwiki\Ui
12 */
13abstract class Revisions extends Ui
14{
15    /* @var string */
16    protected $id;   // page id or media id
17
18    /* @var ChangeLog */
19    protected $changelog; // PageChangeLog or MediaChangeLog object
20
21    /**
22     * Revisions Ui constructor
23     *
24     * @param string $id  page id or media id
25     */
26    public function __construct($id)
27    {
28        $this->id = $id;
29        $this->setChangeLog();
30    }
31
32    /**
33     * set class property changelog
34     */
35    abstract protected function setChangeLog();
36
37    /**
38     * Get revisions, and set correct pagination parameters (first, hasNext)
39     *
40     * @param int  $first
41     * @param bool $hasNext
42     * @return array  revisions to be shown in a paginated list
43     * @see also https://www.dokuwiki.org/devel:changelog
44     */
45    protected function getRevisions(&$first, &$hasNext)
46    {
47        global $conf;
48
49        $changelog =& $this->changelog;
50        $revisions = [];
51
52        $currentRevInfo = $changelog->getCurrentRevisionInfo();
53        if (!$currentRevInfo) return $revisions;
54
55        $num = $conf['recent'];
56        if ($first == 0) {
57            // add external or existing last revision that is excluded from $changelog->getRevisions()
58            if (array_key_exists('timestamp', $currentRevInfo) || (
59                $currentRevInfo['type'] != DOKU_CHANGE_TYPE_DELETE &&
60                $currentRevInfo['date'] == $changelog->lastRevision() )
61            ) {
62                $revisions[] = $currentRevInfo;
63                $num = $num - 1;
64            }
65        }
66        /* we need to get one additional log entry to be able to
67         * decide if this is the last page or is there another one.
68         * see also Ui\Recent::getRecents()
69         */
70        $revlist = $changelog->getRevisions($first, $num + 1);
71        if (count($revlist) == 0 && $first > 0) {
72            // resets to zero if $first requested a too high number
73            $first = 0;
74            return $this->getRevisions($first, $hasNext);
75        }
76
77        // decide if this is the last page or is there another one
78        $hasNext = false;
79        if (count($revlist) > $num) {
80            $hasNext = true;
81            array_pop($revlist); // remove one additional log entry
82        }
83
84        // append each revision info array to the revisions
85        foreach ($revlist as $rev) {
86            $revisions[] = $changelog->getRevisionInfo($rev);
87        }
88        return $revisions;
89    }
90
91    /**
92     * Navigation buttons for Pagination (prev/next)
93     *
94     * @param int  $first
95     * @param bool $hasNext
96     * @param callable $callback returns array of hidden fields for the form button
97     * @return string html
98     */
99    protected function navigation($first, $hasNext, $callback)
100    {
101        global $conf;
102
103        $html = '<div class="pagenav">';
104        $last = $first + $conf['recent'];
105        if ($first > 0) {
106            $first = max($first - $conf['recent'], 0);
107            $html.= '<div class="pagenav-prev">';
108            $html.= html_btn('newer', $this->id, "p", $callback($first));
109            $html.= '</div>';
110        }
111        if ($hasNext) {
112            $html.= '<div class="pagenav-next">';
113            $html.= html_btn('older', $this->id, "n", $callback($last));
114            $html.= '</div>';
115        }
116        $html.= '</div>';
117        return $html;
118    }
119
120}
121