xref: /dokuwiki/inc/Ui/MediaRevisions.php (revision 0d48ec5cdf1a931858bcb1183b8a6f48d4543fea)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\MediaChangeLog;
6use dokuwiki\ChangeLog\RevisionInfo;
7use dokuwiki\Form\Form;
8use InvalidArgumentException;
9
10/**
11 * DokuWiki MediaRevisions Interface
12 *
13 * @package dokuwiki\Ui
14 */
15class MediaRevisions extends Revisions
16{
17    /* @var MediaChangeLog */
18    protected $changelog;
19
20    /**
21     * MediaRevisions Ui constructor
22     *
23     * @param string $id  id of media
24     */
25    public function __construct($id)
26    {
27        if (!$id) {
28            throw new InvalidArgumentException('media id should not be empty!');
29        }
30        parent::__construct($id);
31    }
32
33    /** @inheritdoc */
34    protected function setChangeLog()
35    {
36        $this->changelog = new MediaChangeLog($this->id);
37    }
38
39    /**
40     * Display a list of Media Revisions in the MediaManager
41     *
42     * @author Andreas Gohr <andi@splitbrain.org>
43     * @author Ben Coburn <btcoburn@silicodon.net>
44     * @author Kate Arzamastseva <pshns@ukr.net>
45     * @author Satoshi Sahara <sahara.satoshi@gmail.com>
46     *
47     * @param int $first  skip the first n changelog lines
48     * @return void
49     */
50    public function show($first = 0)
51    {
52        global $lang;
53        $changelog =& $this->changelog;
54
55        // get revisions, and set correct pagination parameters (first, hasNext)
56        if ($first === null) $first = 0;
57        $hasNext = false;
58        $revisions = $this->getRevisions($first, $hasNext);
59
60        // create the form
61        $form = new Form([
62                'id' => 'page__revisions', // must not be "media__revisions"
63                'action' => media_managerURL(['image' => $this->id], '&'),
64                'class'  => 'changes',
65        ]);
66        $form->setHiddenField('mediado', 'diff'); // required for media revisions
67        $form->addTagOpen('div')->addClass('no');
68
69        // start listing
70        $form->addTagOpen('ul');
71        foreach ($revisions as $info) {
72            $rev = $info['date'];
73            $info['media'] = true;
74            $RevInfo = new RevisionInfo($info);
75            $RevInfo->isCurrent($changelog->isCurrentRevision($rev));
76
77            $class = ($RevInfo->val('type') === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : '';
78            $form->addTagOpen('li')->addClass($class);
79            $form->addTagOpen('div')->addClass('li');
80
81            if ($RevInfo->isCurrent()) {
82                $form->addCheckbox('rev2[]')->val($rev);
83            } elseif (file_exists(mediaFN($this->id, $rev))) {
84                $form->addCheckbox('rev2[]')->val($rev);
85            } else {
86                $form->addCheckbox('')->val($rev)->attr('disabled','disabled');
87            }
88            $form->addHTML(' ');
89
90            $html = implode(' ', [
91                $RevInfo->showEditDate(),          // edit date and time
92                $RevInfo->showIconCompareWithCurrent(),  // link to diff view icon
93                $RevInfo->showFileName(),          // name of page or media
94                '<div>',
95                $RevInfo->showEditSummary(),       // edit summary
96                $RevInfo->showEditor(),            // editor info
97                $RevInfo->showSizechange(),        // size change indicator
98                $RevInfo->showCurrentIndicator(),  // current indicator (only when k=1)
99                '</div>',
100            ]);
101            $form->addHTML($html);
102
103            $form->addTagClose('div');
104            $form->addTagClose('li');
105        }
106        $form->addTagClose('ul');  // end of revision list
107
108        // show button for diff view
109        $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit');
110
111        $form->addTagClose('div'); // close div class=no
112
113        print $form->toHTML('Revisions');
114
115        // provide navigation for paginated revision list (of pages and/or media files)
116        print $this->navigation($first, $hasNext, function ($n) {
117            return media_managerURL(['first' => $n], '&', false, true);
118        });
119    }
120
121}
122