xref: /dokuwiki/inc/Ui/Revisions.php (revision 48d75c0057e32072269dab43372c65184dcf6649)
1<?php
2
3namespace dokuwiki\Ui;
4
5/**
6 * DokuWiki Revisions Interface
7 * parent class of PageRevisions and MediaRevisions
8 *
9 * @package dokuwiki\Ui
10 */
11abstract class Revisions extends Ui
12{
13    /* @var string */
14    protected $id;
15
16    /**
17     * Revisions Ui constructor
18     *
19     * @param string $id  id of page or media
20     */
21    public function __construct($id)
22    {
23        $this->id = $id;
24    }
25
26    /**
27     * Get revisions, and set correct pagenation parameters (first, hasNext)
28     *
29     * @param int  $first
30     * @param bool $hasNext
31     * @return array  revisions to be shown in a pagenated list
32     * @see also https://www.dokuwiki.org/devel:changelog
33     */
34    abstract protected function getRevisions(&$first, &$hasNext);
35
36    /**
37     * Navigation buttons for Pagenation (prev/next)
38     *
39     * @param int  $first
40     * @param bool $hasNext
41     * @param callable $callback returns array of hidden fields for the form button
42     * @return array  html
43     */
44    protected function navigation($first, $hasNext, $callback)
45    {
46        global $conf;
47
48        $html = '<div class="pagenav">';
49        $last = $first + $conf['recent'];
50        if ($first > 0) {
51            $first = max($first - $conf['recent'], 0);
52            $html.= '<div class="pagenav-prev">';
53            $html.= html_btn('newer', $this->id, "p", $callback($first));
54            $html.= '</div>';
55        }
56        if ($hasNext) {
57            $html.= '<div class="pagenav-next">';
58            $html.= html_btn('older', $this->id, "n", $callback($last));
59            $html.= '</div>';
60        }
61        $html.= '</div>';
62        return $html;
63    }
64
65    /**
66     * Returns instance of objRevInfo
67     *
68     * @param array $info  Revision info structure of a page or media file
69     * @return objRevInfo object (anonymous class)
70     */
71    protected function getObjRevInfo(array $info)
72    {
73        return new class ($info) // anonymous class (objRevInfo)
74        {
75            protected $info;
76
77            public function __construct(array $info)
78            {
79                $this->info = $info;
80            }
81
82            // current indicator
83            public function currentIndicator()
84            {
85                global $lang;
86                return ($this->info['current']) ? '('.$lang['current'].')' : '';
87            }
88
89            // edit date and time of the page or media file
90            public function editDate()
91            {
92                return '<span class="date">'. dformat($this->info['date']) .'</span>';
93            }
94
95            // edit summary
96            public function editSummary()
97            {
98                return '<span class="sum">'.' – '. hsc($this->info['sum']).'</span>';
99            }
100
101            // editor of the page or media file
102            public function editor()
103            {
104                // slightly different with display of Ui\Recent, i.e. external edit
105                global $lang;
106                $html = '<span class="user">';
107                if (!$this->info['user'] && !$this->info['ip']) {
108                    $html.= '('.$lang['external_edit'].')';
109                } elseif ($this->info['user']) {
110                    $html.= '<bdi>'. editorinfo($this->info['user']) .'</bdi>';
111                    if (auth_ismanager()) $html.= ' <bdo dir="ltr">('. $this->info['ip'] .')</bdo>';
112                } else {
113                    $html.= '<bdo dir="ltr">'. $this->info['ip'] .'</bdo>';
114                }
115                $html.= '</span>';
116                return $html;
117            }
118
119            // name of the page or media file
120            public function itemName()
121            {
122                // slightly different with display of Ui\Recent, i.e. revison may not exists
123                $id = $this->info['id'];
124                $rev = $this->info['date'];
125
126                if (isset($this->info['media'])) {
127                    // media file revision
128                    if (isset($this->info['current'])) {
129                        $href = media_managerURL(['image'=> $id, 'tab_details'=> 'view'], '&');
130                        $html = '<a href="'.$href.'" class="wikilink1">'.$id.'</a>';
131                    } elseif (file_exists(mediaFN($id, $rev))) {
132                        $href = media_managerURL(['image'=> $id, 'tab_details'=> 'view', 'rev'=> $rev], '&');
133                        $html = '<a href="'.$href.'" class="wikilink1">'.$id.'</a>';
134                    } else {
135                        $html = $id;
136                    }
137                    return $html;
138                } else {
139                    // page revision
140                    $display_name = useHeading('navigation') ? hsc(p_get_first_heading($id)) : $id;
141                    if (!$display_name) $display_name = $id;
142                    if ($this->info['current'] || page_exists($id, $rev)) {
143                        $href = wl($id, "rev=$rev", false, '&');
144                        $html = '<a href="'.$href.'" class="wikilink1">'.$display_name.'</a>';
145                    } else {
146                        $html = $display_name;
147                    }
148                    return $html;
149                }
150            }
151
152            // icon difflink
153            public function difflink()
154            {
155                global $lang;
156                $id = $this->info['id'];
157                $rev = $this->info['date'];
158
159                if (isset($this->info['media'])) {
160                    // media file revision
161                    if (isset($this->info['current']) || !file_exists(mediaFN($id, $rev))) {
162                        $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />';
163                    } else {
164                        $href = media_managerURL(['image'=> $id, 'rev'=> $rev, 'mediado'=>'diff'], '&');
165                        $html = '<a href="'.$href.'" class="diff_link">'
166                              . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"'
167                              . ' title="'. $lang['diff'] .'" alt="'.$lang['diff'] .'" />'
168                              . '</a> ';
169                    }
170                    return $html;
171                } else {
172                    // page revision
173                    if ($this->info['current'] || !page_exists($id, $rev)) {
174                        $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />';
175                    } else {
176                        $href = wl($id, "rev=$rev,do=diff", false, '&');
177                        $html = '<a href="'.$href.'" class="diff_link">'
178                              . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"'
179                              . ' title="'.$lang['diff'].'" alt="'.$lang['diff'].'" />'
180                              . '</a>';
181                    }
182                    return $html;
183                }
184            }
185
186            // size change
187            public function sizeChange()
188            {
189                $class = 'sizechange';
190                $value = filesize_h(abs($this->info['sizechange']));
191                if ($this->info['sizechange'] > 0) {
192                    $class .= ' positive';
193                    $value = '+' . $value;
194                } elseif ($this->info['sizechange'] < 0) {
195                    $class .= ' negative';
196                    $value = '-' . $value;
197                } else {
198                    $value = '±' . $value;
199                }
200                return '<span class="'.$class.'">'.$value.'</span>';
201            }
202        }; // end of anonymous class (objRevInfo)
203    }
204
205}
206