1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\ChangeLog\PageChangeLog; 6use dokuwiki\ChangeLog\MediaChangeLog; 7use dokuwiki\Form\Form; 8 9/** 10 * DokuWiki Revisions Interface 11 * 12 * @package dokuwiki\Ui 13 */ 14class Revisions extends Ui 15{ 16 protected $first; 17 protected $media_id; 18 19 /** 20 * Revisions Ui constructor 21 * 22 * @param int $first skip the first n changelog lines 23 * @param bool|string $media_id id of media, or false for current page 24 */ 25 public function __construct($first = 0, $media_id = false) 26 { 27 $this->first = $first; 28 $this->media_id = $media_id; 29 } 30 31 /** 32 * Display list of old revisions 33 * 34 * @author Andreas Gohr <andi@splitbrain.org> 35 * @author Ben Coburn <btcoburn@silicodon.net> 36 * @author Kate Arzamastseva <pshns@ukr.net> 37 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 38 * 39 * @return void 40 */ 41 public function show() 42 { 43 global $ID; 44 45 if ($this->media_id) { 46 return $this->showMediaRevisions($this->media_id); 47 } else { 48 return $this->showPageRevisions($ID); 49 } 50 } 51 52 /** 53 * Display a list of Media Revisions in the MediaManager 54 * 55 * @param string $id media id 56 * @return void 57 */ 58 protected function showMediaRevisions($id) 59 { 60 global $lang; 61 62 // get revisions, and set correct pagenation parameters (first, hasNext) 63 $first = $this->first; 64 $hasNext = false; 65 $revisions = $this->getRevisions($first, $hasNext); 66 67 // create the form 68 $form = new Form([ 69 'id' => 'page__revisions', // must not be "media__revisions" 70 'action' => media_managerURL(['image' => $id], '&'), 71 'class' => 'changes', 72 ]); 73 $form->setHiddenField('mediado', 'diff'); // required for media revisions 74 $form->addTagOpen('div')->addClass('no'); 75 76 // start listing 77 $form->addTagOpen('ul'); 78 foreach ($revisions as $info) { 79 $rev = $info['date']; 80 $class = ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : ''; 81 $form->addTagOpen('li')->addClass($class); 82 $form->addTagOpen('div')->addClass('li'); 83 84 if (isset($info['current'])) { 85 $form->addCheckbox('rev2[]')->val('current'); 86 } elseif (file_exists(mediaFN($id, $rev))) { 87 $form->addCheckbox('rev2[]')->val($rev); 88 } else { 89 $form->addCheckbox('')->val($rev)->attr('disabled','disabled'); 90 } 91 $form->addHTML(' '); 92 93 $objRevInfo = $this->getObjRevInfo($info); 94 $html = implode(' ', [ 95 $objRevInfo->editDate(), // edit date and time 96 $objRevInfo->difflink(), // link to diffview icon 97 $objRevInfo->itemName(), // name of page or media 98 '<div>', 99 $objRevInfo->editSummary(), // edit summary 100 $objRevInfo->editor(), // editor info 101 html_sizechange($info['sizechange']), // size change indicator 102 $objRevInfo->currentIndicator(), // current indicator (only when k=1) 103 '</div>', 104 ]); 105 $form->addHTML($html); 106 107 $form->addTagClose('div'); 108 $form->addTagClose('li'); 109 } 110 $form->addTagClose('ul'); // end of revision list 111 112 // show button for diff view 113 $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit'); 114 115 $form->addTagClose('div'); // close div class=no 116 117 print $form->toHTML('Revisions'); 118 119 // provide navigation for pagenated revision list (of pages and/or media files) 120 print $this->htmlNavigation($id, $first, $hasNext); 121 } 122 123 /** 124 * Display a list of Page Revisions 125 * 126 * @return void 127 */ 128 protected function showPageRevisions($id) 129 { 130 global $lang; 131 132 // get revisions, and set correct pagenation parameters (first, hasNext) 133 $first = $this->first; 134 $hasNext = false; 135 $revisions = $this->getRevisions($first, $hasNext); 136 137 // print intro 138 print p_locale_xhtml('revisions'); 139 140 // create the form 141 $form = new Form([ 142 'id' => 'page__revisions', 143 'class' => 'changes', 144 ]); 145 $form->addTagOpen('div')->addClass('no'); 146 147 // start listing 148 $form->addTagOpen('ul'); 149 foreach ($revisions as $info) { 150 $rev = $info['date']; 151 $class = ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? 'minor' : ''; 152 $form->addTagOpen('li')->addClass($class); 153 $form->addTagOpen('div')->addClass('li'); 154 155 if (page_exists($id, $rev)) { 156 $form->addCheckbox('rev2[]')->val($rev); 157 } else { 158 $form->addCheckbox('')->val($rev)->attr('disabled','disabled'); 159 } 160 $form->addHTML(' '); 161 162 $objRevInfo = $this->getObjRevInfo($info); 163 $html = implode(' ', [ 164 $objRevInfo->editDate(), // edit date and time 165 $objRevInfo->difflink(), // link to diffview icon 166 $objRevInfo->itemName(), // name of page or media 167 $objRevInfo->editSummary(), // edit summary 168 $objRevInfo->editor(), // editor info 169 $objRevInfo->sizechange(), // size change indicator 170 $objRevInfo->currentIndicator(), // current indicator (only when k=1) 171 ]); 172 $form->addHTML($html); 173 $form->addTagClose('div'); 174 $form->addTagClose('li'); 175 } 176 $form->addTagClose('ul'); // end of revision list 177 178 // show button for diff view 179 $form->addButton('do[diff]', $lang['diff2'])->attr('type', 'submit'); 180 181 $form->addTagClose('div'); // close div class=no 182 183 print $form->toHTML('Revisions'); 184 185 // provide navigation for pagenated revision list (of pages and/or media files) 186 print $this->htmlNavigation($id, $first, $hasNext); 187 } 188 189 190 /** 191 * Get revisions, and set correct pagenation parameters (first, hasNext) 192 * 193 * @param int $first 194 * @param bool $hasNext 195 * @return array revisions to be shown in a pagenated list 196 */ 197 protected function getRevisions(&$first, &$hasNext) 198 { 199 global $INFO, $conf; 200 201 if ($this->media_id) { 202 $changelog = new MediaChangeLog($this->media_id); 203 } else { 204 $changelog = new PageChangeLog($INFO['id']); 205 } 206 207 $revisions = []; 208 209 /* we need to get one additional log entry to be able to 210 * decide if this is the last page or is there another one. 211 * see also Ui\Recent::getRecents() 212 */ 213 $revlist = $changelog->getRevisions($first, $conf['recent'] +1); 214 if (count($revlist) == 0 && $first != 0) { 215 $first = 0; 216 $revlist = $changelog->getRevisions($first, $conf['recent'] +1); 217 } 218 $exists = ($this->media_id) ? file_exists(mediaFN($this->media_id)) : $INFO['exists']; 219 if ($first === 0 && $exists) { 220 // add current page or media as revision[0] 221 if ($this->media_id) { 222 $rev = filemtime(fullpath(mediaFN($this->media_id))); 223 $revisions[] = $changelog->getRevisionInfo($rev) + array( 224 'media' => true, 225 'current' => true, 226 ); 227 } else { 228 $revisions[] = array( 229 'date' => $INFO['lastmod'], 230 'ip' => null, 231 'type' => $INFO['meta']['last_change']['type'], 232 'id' => $INFO['id'], 233 'user' => $INFO['editor'], 234 'sum' => $INFO['sum'], 235 'extra' => null, 236 'sizechange' => $INFO['meta']['last_change']['sizechange'], 237 'current' => true, 238 ); 239 } 240 } 241 242 // decide if this is the last page or is there another one 243 $hasNext = false; 244 if (count($revlist) > $conf['recent']) { 245 $hasNext = true; 246 array_pop($revlist); // remove one additional log entry 247 } 248 249 // append each revison info array to the revisions 250 foreach ($revlist as $rev) { 251 if ($this->media_id) { 252 $revisions[] = $changelog->getRevisionInfo($rev) + array('media' => true); 253 } else { 254 $revisions[] = $changelog->getRevisionInfo($rev); 255 } 256 } 257 return $revisions; 258 } 259 260 /** 261 * Navigation buttons for Pagenation (prev/next) 262 * 263 * @param string $id page id or media id 264 * @param int $first 265 * @param bool $hasNext 266 * @return array html 267 */ 268 protected function htmlNavigation($id, $first, $hasNext) 269 { 270 global $conf; 271 272 $html = '<div class="pagenav">'; 273 $last = $first + $conf['recent']; 274 if ($first > 0) { 275 $first = max($first - $conf['recent'], 0); 276 $html.= '<div class="pagenav-prev">'; 277 if ($this->media_id) { 278 $html.= html_btn('newer', $id, "p", media_managerURL(['first' => $first], '&', false, true)); 279 } else { 280 $html.= html_btn('newer', $id, "p" ,['do' => 'revisions', 'first' => $first]); 281 } 282 $html.= '</div>'; 283 } 284 if ($hasNext) { 285 $html.= '<div class="pagenav-next">'; 286 if ($this->media_id) { 287 $html.= html_btn('older', $id, "n", media_managerURL(['first' => $last], '&', false, true)); 288 } else { 289 $html.= html_btn('older', $id, "n", ['do' => 'revisions', 'first' => $last]); 290 } 291 $html.= '</div>'; 292 } 293 $html.= '</div>'; 294 return $html; 295 } 296 297 /** 298 * Returns instance of objRevInfo 299 * 300 * @param array $info Revision info structure of a page or media file 301 * @return objRevInfo object (anonymous class) 302 */ 303 protected function getObjRevInfo(array $info) 304 { 305 return new class ($info) // anonymous class (objRevInfo) 306 { 307 protected $info; 308 309 public function __construct(array $info) 310 { 311 $this->info = $info; 312 } 313 314 // current indicator 315 public function currentIndicator() 316 { 317 global $lang; 318 return ($this->info['current']) ? '('.$lang['current'].')' : ''; 319 } 320 321 // edit date and time of the page or media file 322 public function editDate() 323 { 324 return '<span class="date">'. dformat($this->info['date']) .'</span>'; 325 } 326 327 // edit summary 328 public function editSummary() 329 { 330 return '<span class="sum">'.' – '. hsc($this->info['sum']).'</span>'; 331 } 332 333 // editor of the page or media file 334 public function editor() 335 { 336 // slightly different with display of Ui\Recent, i.e. external edit 337 global $lang; 338 $html = '<span class="user">'; 339 if (!$this->info['user'] && !$this->info['ip']) { 340 $html.= '('.$lang['external_edit'].')'; 341 } elseif ($this->info['user']) { 342 $html.= '<bdi>'. editorinfo($this->info['user']) .'</bdi>'; 343 if (auth_ismanager()) $html.= ' <bdo dir="ltr">('. $this->info['ip'] .')</bdo>'; 344 } else { 345 $html.= '<bdo dir="ltr">'. $this->info['ip'] .'</bdo>'; 346 } 347 $html.= '</span>'; 348 return $html; 349 } 350 351 // name of the page or media file 352 public function itemName() 353 { 354 // slightly different with display of Ui\Recent, i.e. revison may not exists 355 $id = $this->info['id']; 356 $rev = $this->info['date']; 357 358 if (isset($this->info['media'])) { 359 // media file revision 360 if (isset($this->info['current'])) { 361 $href = media_managerURL(['image'=> $id, 'tab_details'=> 'view'], '&'); 362 $html = '<a href="'.$href.'" class="wikilink1">'.$id.'</a>'; 363 } elseif (file_exists(mediaFN($id, $rev))) { 364 $href = media_managerURL(['image'=> $id, 'tab_details'=> 'view', 'rev'=> $rev], '&'); 365 $html = '<a href="'.$href.'" class="wikilink1">'.$id.'</a>'; 366 } else { 367 $html = $id; 368 } 369 return $html; 370 } else { 371 // page revision 372 $display_name = useHeading('navigation') ? hsc(p_get_first_heading($id)) : $id; 373 if (!$display_name) $display_name = $id; 374 if ($this->info['current'] || page_exists($id, $rev)) { 375 $href = wl($id, "rev=$rev", false, '&'); 376 $html = '<a href="'.$href.'" class="wikilink1">'.$display_name.'</a>'; 377 } else { 378 $html = $display_name; 379 } 380 return $html; 381 } 382 } 383 384 // icon difflink 385 public function difflink() 386 { 387 global $lang; 388 $id = $this->info['id']; 389 $rev = $this->info['date']; 390 391 if (isset($this->info['media'])) { 392 // media file revision 393 if (isset($this->info['current']) || !file_exists(mediaFN($id, $rev))) { 394 $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />'; 395 } else { 396 $href = media_managerURL(['image'=> $id, 'rev'=> $rev, 'mediado'=>'diff'], '&'); 397 $html = '<a href="'.$href.'" class="diff_link">' 398 . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"' 399 . ' title="'. $lang['diff'] .'" alt="'.$lang['diff'] .'" />' 400 . '</a> '; 401 } 402 return $html; 403 } else { 404 // page revision 405 if ($this->info['current'] || !page_exists($id, $rev)) { 406 $html = '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />'; 407 } else { 408 $href = wl($id, "rev=$rev,do=diff", false, '&'); 409 $html = '<a href="'.$href.'" class="diff_link">' 410 . '<img src="'.DOKU_BASE.'lib/images/diff.png" width="15" height="11"' 411 . ' title="'.$lang['diff'].'" alt="'.$lang['diff'].'" />' 412 . '</a>'; 413 } 414 return $html; 415 } 416 } 417 418 // size change 419 public function sizeChange() 420 { 421 $class = 'sizechange'; 422 $value = filesize_h(abs($this->info['sizechange'])); 423 if ($this->info['sizechange'] > 0) { 424 $class .= ' positive'; 425 $value = '+' . $value; 426 } elseif ($this->info['sizechange'] < 0) { 427 $class .= ' negative'; 428 $value = '-' . $value; 429 } else { 430 $value = '±' . $value; 431 } 432 return '<span class="'.$class.'">'.$value.'</span>'; 433 } 434 }; // end of anonymous class (objRevInfo) 435 } 436 437} 438