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     * @param int $first skip the first n changelog lines
43     * @return void
44     * @author Kate Arzamastseva <pshns@ukr.net>
45     * @author Satoshi Sahara <sahara.satoshi@gmail.com>
46     *
47     * @author Andreas Gohr <andi@splitbrain.org>
48     * @author Ben Coburn <btcoburn@silicodon.net>
49     */
50    public function show($first = -1)
51    {
52        global $lang;
53        $changelog =& $this->changelog;
54
55        // get revisions, and set correct pagination parameters (first, hasNext)
56        if ($first === null) $first = -1;
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            $RevInfo = new RevisionInfo($info);
74            $RevInfo->isCurrent($changelog->isCurrentRevision($rev));
75
76            $class = ($RevInfo->val('type') === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : '';
77            $form->addTagOpen('li')->addClass($class);
78            $form->addTagOpen('div')->addClass('li');
79
80            if ($RevInfo->isCurrent()) {
81                $form->addCheckbox('rev2[]')->val($rev);
82            } elseif (file_exists(mediaFN($this->id, $rev))) {
83                $form->addCheckbox('rev2[]')->val($rev);
84            } else {
85                $form->addCheckbox('')->val($rev)->attr('disabled', 'disabled');
86            }
87            $form->addHTML(' ');
88
89            $html = implode(' ', [
90                $RevInfo->showEditDate(),          // edit date and time
91                $RevInfo->showIconCompareWithCurrent(),  // link to diff view icon
92                $RevInfo->showFileName(),          // name of page or media
93                '<div>',
94                $RevInfo->showEditSummary(),       // edit summary
95                $RevInfo->showEditor(),            // editor info
96                $RevInfo->showSizechange(),        // size change indicator
97                $RevInfo->showCurrentIndicator(),  // current indicator (only when k=1)
98                '</div>',
99            ]);
100            $form->addHTML($html);
101
102            $form->addTagClose('div');
103            $form->addTagClose('li');
104        }
105        $form->addTagClose('ul');  // end of revision list
106
107        // show button for diff view
108        $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit');
109
110        $form->addTagClose('div'); // close div class=no
111
112        echo $form->toHTML('Revisions');
113
114        // provide navigation for paginated revision list (of pages and/or media files)
115        echo $this->navigation(
116            $first,
117            $hasNext,
118            static fn($n) => media_managerURL(['first' => $n], '&', false, true)
119        );
120    }
121}
122