xref: /dokuwiki/inc/Ui/PageRevisions.php (revision e71e09a62e4986d588e0afb8128f2eb7cfa41a12)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\PageChangeLog;
6use dokuwiki\Form\Form;
7
8/**
9 * DokuWiki PageRevisions Interface
10 *
11 * @package dokuwiki\Ui
12 */
13class PageRevisions extends Revisions
14{
15    /**
16     * PageRevisions Ui constructor
17     *
18     * @param string $id  id of page
19     */
20    public function __construct($id)
21    {
22        global $INFO;
23        if (!$id) $id = $INFO['id'];
24        $this->item = 'page';
25        parent::__construct($id);
26    }
27
28    /** @inheritdoc */
29    protected function setChangeLog()
30    {
31        $this->changelog = new PageChangeLog($this->id);
32    }
33
34    /**
35     * Display list of old revisions of the page
36     *
37     * @author Andreas Gohr <andi@splitbrain.org>
38     * @author Ben Coburn <btcoburn@silicodon.net>
39     * @author Kate Arzamastseva <pshns@ukr.net>
40     * @author Satoshi Sahara <sahara.satoshi@gmail.com>
41     *
42     * @param int $first  skip the first n changelog lines
43     * @return void
44     */
45    public function show($first = 0)
46    {
47        global $lang, $REV;
48
49        // get revisions, and set correct pagenation parameters (first, hasNext)
50        if ($first === null) $first = 0;
51        $hasNext = false;
52        $revisions = $this->getRevisions($first, $hasNext);
53
54        // print intro
55        print p_locale_xhtml('revisions');
56
57        // create the form
58        $form = new Form([
59                'id' => 'page__revisions',
60                'class' => 'changes',
61        ]);
62        $form->addTagOpen('div')->addClass('no');
63
64        // start listing
65        $form->addTagOpen('ul');
66        foreach ($revisions as $info) {
67            $rev = $info['date'];
68            $class = ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : '';
69            $form->addTagOpen('li')->addClass($class);
70            $form->addTagOpen('div')->addClass('li');
71
72            if (isset($info['current'])) {
73                $form->addCheckbox('rev2[]')->val('current');
74            } elseif ($rev == $REV) {
75                $form->addCheckbox('rev2[]')->val($rev)->attr('checked','checked');
76            } elseif (page_exists($this->id, $rev)) {
77                $form->addCheckbox('rev2[]')->val($rev);
78            } else {
79                $form->addCheckbox('')->val($rev)->attr('disabled','disabled');
80            }
81            $form->addHTML(' ');
82
83            $objRevInfo = $this->getObjRevInfo($info);
84            $html = implode(' ', [
85                $objRevInfo->editDate(),          // edit date and time
86                $objRevInfo->difflink(),          // link to diffview icon
87                $objRevInfo->itemName(),          // name of page or media
88                $objRevInfo->editSummary(),       // edit summary
89                $objRevInfo->editor(),            // editor info
90                $objRevInfo->sizechange(),        // size change indicator
91                $objRevInfo->currentIndicator(),  // current indicator (only when k=1)
92            ]);
93            $form->addHTML($html);
94            $form->addTagClose('div');
95            $form->addTagClose('li');
96        }
97        $form->addTagClose('ul');  // end of revision list
98
99        // show button for diff view
100        $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit');
101
102        $form->addTagClose('div'); // close div class=no
103
104        print $form->toHTML('Revisions');
105
106        // provide navigation for pagenated revision list (of pages and/or media files)
107        print $this->navigation($first, $hasNext, function ($n) {
108            return array('do' => 'revisions', 'first' => $n);
109        });
110    }
111
112    /**
113     * Get revisions, and set correct pagenation parameters (first, hasNext)
114     *
115     * @param int  $first
116     * @param bool $hasNext
117     * @return array  revisions to be shown in a pagenated list
118     * @see also https://www.dokuwiki.org/devel:changelog
119     */
120    protected function getRevisions(&$first, &$hasNext)
121    {
122        global $INFO, $conf;
123
124        if ($this->id != $INFO['id']) {
125            return parent::getRevisions($first, $hasNext);
126        }
127
128        $changelog =& $this->changelog;
129
130        $revisions = [];
131
132        /* we need to get one additional log entry to be able to
133         * decide if this is the last page or is there another one.
134         * see also Ui\Recent::getRecents()
135         */
136        $revlist = $changelog->getRevisions($first, $conf['recent'] +1);
137        if (count($revlist) == 0 && $first != 0) {
138            $first = 0;
139            $revlist = $changelog->getRevisions($first, $conf['recent'] +1);
140        }
141        $exists = $INFO['exists'];
142        if ($first == 0 && $exists) {
143            // add current page as revision[0]
144            $revisions[] = array(
145                'date' => $INFO['lastmod'],
146                'ip'   => null,
147                'type' => $INFO['meta']['last_change']['type'],
148                'id'   => $INFO['id'],
149                'user' => $INFO['editor'],
150                'sum'  => $INFO['sum'],
151                'extra' => null,
152                'sizechange' => $INFO['meta']['last_change']['sizechange'],
153                'item' => $this->item,
154                'current' => true,
155            );
156        }
157
158        // decide if this is the last page or is there another one
159        $hasNext = false;
160        if (count($revlist) > $conf['recent']) {
161            $hasNext = true;
162            array_pop($revlist); // remove one additional log entry
163        }
164
165        // append each revison info array to the revisions
166        foreach ($revlist as $rev) {
167            $revisions[] = $changelog->getRevisionInfo($rev) + array('item' => $this->item);
168        }
169        return $revisions;
170    }
171
172}
173