1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\ChangeLog\PageChangeLog; 6use dokuwiki\Form\Form; 7 8/** 9 * DokuWiki PageDiff Interface 10 * 11 * @author Andreas Gohr <andi@splitbrain.org> 12 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 13 * @package dokuwiki\Ui 14 */ 15class PageDiff extends Diff 16{ 17 /* @var PageChangeLog */ 18 protected $changelog; 19 20 /* @var array */ 21 protected $oldRevInfo; 22 protected $newRevInfo; 23 24 /* @var string */ 25 protected $text; 26 27 /** 28 * PageDiff Ui constructor 29 * 30 * @param string $id page id 31 */ 32 public function __construct($id = null) 33 { 34 global $INFO; 35 if (!isset($id)) $id = $INFO['id']; 36 37 // init preference 38 $this->preference['showIntro'] = true; 39 $this->preference['difftype'] = 'sidebyside'; // diff view type: inline or sidebyside 40 41 parent::__construct($id); 42 } 43 44 /** @inheritdoc */ 45 protected function setChangeLog() 46 { 47 $this->changelog = new PageChangeLog($this->id); 48 } 49 50 /** 51 * Set text to be compared with most current version 52 * when it has been externally edited 53 * exclusively use of the compare($old, $new) method 54 * 55 * @param string $text 56 * @return $this 57 */ 58 public function compareWith($text = null) 59 { 60 global $lang; 61 62 if (isset($text)) { 63 $this->text = $text; 64 $changelog =& $this->changelog; 65 66 // revision info of older file (left side) 67 $this->oldRevInfo = $changelog->getCurrentRevisionInfo() + [ 68 'current' => true, 69 'rev' => '', 70 'navTitle' => $this->revisionTitle($changelog->getCurrentRevisionInfo()), 71 'text' => rawWiki($this->id, ''), 72 ]; 73 74 // revision info of newer file (right side) 75 $this->newRevInfo = [ 76 'date' => null, 77 //'ip' => '127.0.0.1', 78 //'type' => DOKU_CHANGE_TYPE_CREATE, 79 'id' => $this->id, 80 //'user' => '', 81 //'sum' => '', 82 //'extra' => '', 83 'sizechange' => strlen($this->text) - io_getSizeFile(wikiFN($this->id, '')), 84 'timestamp' => 'unknown', 85 'current' => false, 86 'rev' => false, 87 'navTitle' => $lang['yours'], 88 'text' => cleanText($this->text), 89 ]; 90 } 91 return $this; 92 } 93 94 /** 95 * Handle requested revision(s) and diff view preferences 96 * 97 * @return void 98 */ 99 protected function handle() 100 { 101 global $INPUT; 102 103 // requested rev or rev2 104 if (!isset($this->oldRevInfo, $this->newRevInfo)) { 105 parent::handle(); 106 } 107 108 // requested diff view type 109 if ($INPUT->has('difftype')) { 110 $this->preference['difftype'] = $INPUT->str('difftype'); 111 } else { 112 // read preference from DokuWiki cookie. PageDiff only 113 $mode = get_doku_pref('difftype', $mode = null); 114 if (isset($mode)) $this->preference['difftype'] = $mode; 115 } 116 117 if (!isset($this->oldRev, $this->newRev)) { 118 // no revision was given, compare previous to current 119 $changelog =& $this->changelog; 120 $this->oldRev = $changelog->getRevisions(0, 1)[0]; 121 $this->newRev = $changelog->currentRevision(); 122 123 global $INFO, $REV; 124 if ($this->id == $INFO['id']) 125 $REV = $this->oldRev; // store revision back in $REV 126 } 127 } 128 129 /** 130 * Prepare revision info of comparison pair 131 */ 132 protected function preProcess() 133 { 134 $changelog =& $this->changelog; 135 136 // revision info of older file (left side) 137 $this->oldRevInfo = $changelog->getRevisionInfo($this->oldRev); 138 // revision info of newer file (right side) 139 $this->newRevInfo = $changelog->getRevisionInfo($this->newRev); 140 141 foreach ([&$this->oldRevInfo, &$this->newRevInfo] as &$revInfo) { 142 // use timestamp and '' properly as $rev for the current file 143 $isCurrent = $changelog->isCurrentRevision($revInfo['date']); 144 $revInfo += [ 145 'current' => $isCurrent, 146 'rev' => $isCurrent ? '' : $revInfo['date'], 147 ]; 148 149 // headline in the Diff view navigation 150 $revInfo['navTitle'] = $this->revisionTitle($revInfo); 151 152 if ($revInfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 153 //attic stores complete last page version for a deleted page 154 $revInfo['text'] = ''; 155 } else { 156 $revInfo['text'] = rawWiki($this->id, $revInfo['rev']); 157 } 158 } 159 } 160 161 /** 162 * Show diff 163 * between current page version and provided $text 164 * or between the revisions provided via GET or POST 165 * 166 * @author Andreas Gohr <andi@splitbrain.org> 167 * 168 * @return void 169 */ 170 public function show() 171 { 172 $changelog =& $this->changelog; 173 174 if (!isset($this->oldRevInfo, $this->newRevInfo)) { 175 // retrieve form parameters: rev, rev2, difftype 176 $this->handle(); 177 // prepare revision info of comparison pair, except PageConfrict or PageDraft 178 $this->preProcess(); 179 } 180 181 // create difference engine object 182 $Difference = new \Diff( 183 explode("\n", $this->oldRevInfo['text']), 184 explode("\n", $this->newRevInfo['text']) 185 ); 186 187 // build paired navigation 188 [$navOlderRevisions, $navNewerRevisions] = $this->buildRevisionsNavigation(); 189 190 // display intro 191 if ($this->preference['showIntro']) echo p_locale_xhtml('diff'); 192 193 // print form to choose diff view type, and exact url reference to the view 194 if ($this->newRevInfo['rev'] !== false) { 195 $this->showDiffViewSelector(); 196 } 197 198 // assign minor edit checker to the variable 199 $classEditType = function ($info) { 200 return ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? ' class="minor"' : ''; 201 }; 202 203 // display diff view table 204 echo '<div class="table">'; 205 echo '<table class="diff diff_'.$this->preference['difftype'] .'">'; 206 207 //navigation and header 208 switch ($this->preference['difftype']) { 209 case 'inline': 210 if ($this->newRevInfo['rev'] !== false) { 211 echo '<tr>' 212 .'<td class="diff-lineheader">-</td>' 213 .'<td class="diffnav">'. $navOlderRevisions .'</td>' 214 .'</tr>'; 215 echo '<tr>' 216 .'<th class="diff-lineheader">-</th>' 217 .'<th'.$classEditType($this->oldRevInfo).'>'.$this->oldRevInfo['navTitle'].'</th>' 218 .'</tr>'; 219 } 220 echo '<tr>' 221 .'<td class="diff-lineheader">+</td>' 222 .'<td class="diffnav">'. $navNewerRevisions .'</td>' 223 .'</tr>'; 224 echo '<tr>' 225 .'<th class="diff-lineheader">+</th>' 226 .'<th'.$classEditType($this->newRevInfo).'>'.$this->newRevInfo['navTitle'].'</th>' 227 .'</tr>'; 228 // create formatter object 229 $DiffFormatter = new \InlineDiffFormatter(); 230 break; 231 232 case 'sidebyside': 233 default: 234 if ($this->newRevInfo['rev'] !== false) { 235 echo '<tr>' 236 .'<td colspan="2" class="diffnav">'. $navOlderRevisions .'</td>' 237 .'<td colspan="2" class="diffnav">'. $navNewerRevisions .'</td>' 238 .'</tr>'; 239 } 240 echo '<tr>' 241 .'<th colspan="2"'.$classEditType($this->oldRevInfo).'>'.$this->oldRevInfo['navTitle'].'</th>' 242 .'<th colspan="2"'.$classEditType($this->newRevInfo).'>'.$this->newRevInfo['navTitle'].'</th>' 243 .'</tr>'; 244 // create formatter object 245 $DiffFormatter = new \TableDiffFormatter(); 246 break; 247 } 248 249 // output formatted difference 250 echo $this->insertSoftbreaks($DiffFormatter->format($Difference)); 251 252 echo '</table>'; 253 echo '</div>'; 254 } 255 256 /** 257 * Revision Title for PageDiff table headline 258 * 259 * @param array $info Revision info structure of a page 260 * @return string 261 */ 262 protected function revisionTitle(array $info) 263 { 264 global $lang; 265 266 // use designated title when compare current page source with given text 267 if (array_key_exists('date', $info) && is_null($info['date'])) { 268 return $lang['yours']; 269 } 270 271 if (isset($info['date'])) { 272 $rev = $info['date']; 273 if (($info['timestamp'] ?? '') == 'unknown') { 274 // exteranlly deleted or older file restored 275 $title = '<bdi><a class="wikilink2" href="'.wl($this->id).'">' 276 . $this->id .' ['. $lang['unknowndate'] .']'.'</a></bdi>'; 277 } else { 278 $title = '<bdi><a class="wikilink1" href="'.wl($this->id, ['rev' => $rev]).'">' 279 . $this->id .' ['. dformat($rev) .']'.'</a></bdi>'; 280 } 281 } else { 282 $rev = false; 283 $title = '—'; 284 } 285 if (isset($info['current'])) { 286 $title .= ' ('.$lang['current'].')'; 287 } 288 289 // append separator 290 $title .= ($this->preference['difftype'] === 'inline') ? ' ' : '<br />'; 291 292 // supplement 293 if (isset($info['date'])) { 294 $objRevInfo = (new PageRevisions($this->id))->getObjRevInfo($info); 295 $title .= $objRevInfo->editSummary().' '.$objRevInfo->editor(); 296 } 297 return $title; 298 } 299 300 /** 301 * Print form to choose diff view type, and exact url reference to the view 302 */ 303 protected function showDiffViewSelector() 304 { 305 global $lang; 306 307 // use timestamp for current revision 308 [$oldRev, $newRev] = [(int)$this->oldRevInfo['date'], (int)$this->newRevInfo['date']]; 309 310 echo '<div class="diffoptions group">'; 311 312 // create the form to select difftype 313 $form = new Form(['action' => wl()]); 314 $form->setHiddenField('id', $this->id); 315 $form->setHiddenField('rev2[0]', $oldRev); 316 $form->setHiddenField('rev2[1]', $newRev); 317 $form->setHiddenField('do', 'diff'); 318 $options = array( 319 'sidebyside' => $lang['diff_side'], 320 'inline' => $lang['diff_inline'], 321 ); 322 $input = $form->addDropdown('difftype', $options, $lang['diff_type']) 323 ->val($this->preference['difftype']) 324 ->addClass('quickselect'); 325 $input->useInput(false); // inhibit prefillInput() during toHTML() process 326 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 327 echo $form->toHTML(); 328 329 // show exact url reference to the view when it is meaningful 330 echo '<p>'; 331 if ($oldRev && $newRev) { 332 // link to exactly this view FS#2835 333 $viewUrl = $this->diffViewlink('difflink', $oldRev, $newRev); 334 } 335 echo $viewUrl ?? '<br />'; 336 echo '</p>'; 337 338 echo '</div>'; // .diffoptions 339 } 340 341 /** 342 * Create html for revision navigation 343 * 344 * The navigation consists of older and newer revisions selectors, each 345 * state mutually depends on the selected revision of opposite side. 346 * 347 * @return string[] html of navigation for both older and newer sides 348 */ 349 protected function buildRevisionsNavigation() 350 { 351 $changelog =& $this->changelog; 352 353 if ($this->newRevInfo['rev'] === false) { 354 // no revisions selector for PageConflict or PageDraft 355 return array('', ''); 356 } 357 358 // use timestamp for current revision 359 [$oldRev, $newRev] = [(int)$this->oldRevInfo['date'], (int)$this->newRevInfo['date']]; 360 361 // retrieve revisions with additional info 362 [$oldRevs, $newRevs] = $changelog->getRevisionsAround($oldRev, $newRev); 363 364 // build options for dropdown selector 365 $olderRevisions = $this->buildRevisionOptions('older', $oldRevs); 366 $newerRevisions = $this->buildRevisionOptions('newer', $newRevs); 367 368 // determine previous/next revisions 369 $index = array_search($oldRev, $oldRevs); 370 $oldPrevRev = ($index +1 < count($oldRevs)) ? $oldRevs[$index +1] : false; 371 $oldNextRev = ($index > 0) ? $oldRevs[$index -1] : false; 372 $index = array_search($newRev, $newRevs); 373 $newPrevRev = ($index +1 < count($newRevs)) ? $newRevs[$index +1] : false; 374 $newNextRev = ($index > 0) ? $newRevs[$index -1] : false; 375 376 /* 377 * navigation UI for older revisions / Left side: 378 */ 379 $navOlderRevs = ''; 380 // move backward both side: ◀◀ 381 if ($oldPrevRev && $newPrevRev) 382 $navOlderRevs .= $this->diffViewlink('diffbothprevrev', $oldPrevRev, $newPrevRev); 383 // move backward left side: ◀ 384 if ($oldPrevRev) 385 $navOlderRevs .= $this->diffViewlink('diffprevrev', $oldPrevRev, $newRev); 386 // dropdown 387 $navOlderRevs .= $this->buildDropdownSelector('older', $olderRevisions); 388 // move forward left side: ▶ 389 if ($oldNextRev && ($oldNextRev < $newRev)) 390 $navOlderRevs .= $this->diffViewlink('diffnextrev', $oldNextRev, $newRev); 391 392 /* 393 * navigation UI for newer revisions / Right side: 394 */ 395 $navNewerRevs = ''; 396 // move backward right side: ◀ 397 if ($newPrevRev && ($oldRev < $newPrevRev)) 398 $navNewerRevs .= $this->diffViewlink('diffprevrev', $oldRev, $newPrevRev); 399 // dropdown 400 $navNewerRevs .= $this->buildDropdownSelector('newer', $newerRevisions); 401 // move forward right side: ▶ 402 if ($newNextRev) { 403 if ($changelog->isCurrentRevision($newNextRev)) { 404 $navNewerRevs .= $this->diffViewlink('difflastrev', $oldRev, $newNextRev); 405 } else { 406 $navNewerRevs .= $this->diffViewlink('diffnextrev', $oldRev, $newNextRev); 407 } 408 } 409 // move forward both side: ▶▶ 410 if ($oldNextRev && $newNextRev) 411 $navNewerRevs .= $this->diffViewlink('diffbothnextrev', $oldNextRev, $newNextRev); 412 413 return array($navOlderRevs, $navNewerRevs); 414 } 415 416 /** 417 * prepare options for dropdwon selector 418 * 419 * @params string $side "older" or "newer" 420 * @params array $revs list of revsion 421 * @return array 422 */ 423 protected function buildRevisionOptions($side, $revs) 424 { 425 global $lang; 426 $changelog =& $this->changelog; 427 $revisions = array(); 428 429 // use timestamp for current revision 430 [$oldRev, $newRev] = [(int)$this->oldRevInfo['date'], (int)$this->newRevInfo['date']]; 431 432 foreach ($revs as $rev) { 433 $info = $changelog->getRevisionInfo($rev); 434 $date = dformat($info['date']); 435 if (($info['timestamp'] ?? '') == 'unknown') { 436 // exteranlly deleted or older file restored 437 $date = preg_replace('/[0-9a-zA-Z]/','_', $date); 438 } 439 $revisions[$rev] = array( 440 'label' => implode(' ', [ 441 $date, 442 editorinfo($info['user'], true), 443 $info['sum'], 444 ]), 445 'attrs' => ['title' => $rev], 446 ); 447 if (($side == 'older' && ($newRev && $rev >= $newRev)) 448 ||($side == 'newer' && ($rev <= $oldRev)) 449 ) { 450 $revisions[$rev]['attrs']['disabled'] = 'disabled'; 451 } 452 } 453 return $revisions; 454 } 455 456 /** 457 * build Dropdown form for revisions navigation 458 * 459 * @params string $side "older" or "newer" 460 * @params array $options dropdown options 461 * @return string 462 */ 463 protected function buildDropdownSelector($side, $options) 464 { 465 $form = new Form(['action' => wl($this->id)]); 466 $form->setHiddenField('id', $this->id); 467 $form->setHiddenField('do', 'diff'); 468 $form->setHiddenField('difftype', $this->preference['difftype']); 469 470 // use timestamp for current revision 471 [$oldRev, $newRev] = [(int)$this->oldRevInfo['date'], (int)$this->newRevInfo['date']]; 472 473 switch ($side) { 474 case 'older': // left side 475 $form->setHiddenField('rev2[1]', $newRev); 476 $input = $form->addDropdown('rev2[0]', $options) 477 ->val($oldRev)->addClass('quickselect'); 478 $input->useInput(false); // inhibit prefillInput() during toHTML() process 479 break; 480 case 'newer': // right side 481 $form->setHiddenField('rev2[0]', $oldRev); 482 $input = $form->addDropdown('rev2[1]', $options) 483 ->val($newRev)->addClass('quickselect'); 484 $input->useInput(false); // inhibit prefillInput() during toHTML() process 485 break; 486 } 487 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 488 return $form->toHTML(); 489 } 490 491 /** 492 * Create html link to a diff view defined by two revisions 493 * 494 * @param string $linktype 495 * @param int $oldRev older revision 496 * @param int $newRev newer revision or null for diff with current revision 497 * @return string html of link to a diff view 498 */ 499 protected function diffViewlink($linktype, $oldRev, $newRev = null) 500 { 501 global $lang; 502 if ($newRev === null) { 503 $urlparam = array( 504 'do' => 'diff', 505 'rev' => $oldRev, 506 'difftype' => $this->preference['difftype'], 507 ); 508 } else { 509 $urlparam = array( 510 'do' => 'diff', 511 'rev2[0]' => $oldRev, 512 'rev2[1]' => $newRev, 513 'difftype' => $this->preference['difftype'], 514 ); 515 } 516 $attr = array( 517 'class' => $linktype, 518 'href' => wl($this->id, $urlparam, true, '&'), 519 'title' => $lang[$linktype], 520 ); 521 return '<a '. buildAttributes($attr) .'><span>'. $lang[$linktype] .'</span></a>'; 522 } 523 524 525 /** 526 * Insert soft breaks in diff html 527 * 528 * @param string $diffhtml 529 * @return string 530 */ 531 public function insertSoftbreaks($diffhtml) 532 { 533 // search the diff html string for both: 534 // - html tags, so these can be ignored 535 // - long strings of characters without breaking characters 536 return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/', function ($match) { 537 // if match is an html tag, return it intact 538 if ($match[0][0] == '<') return $match[0]; 539 // its a long string without a breaking character, 540 // make certain characters into breaking characters by inserting a 541 // word break opportunity (<wbr> tag) in front of them. 542 $regex = <<< REGEX 543(?(?= # start a conditional expression with a positive look ahead ... 544&\#?\\w{1,6};) # ... for html entities - we don't want to split them (ok to catch some invalid combinations) 545&\#?\\w{1,6}; # yes pattern - a quicker match for the html entity, since we know we have one 546| 547[?/,&\#;:] # no pattern - any other group of 'special' characters to insert a breaking character after 548)+ # end conditional expression 549REGEX; 550 return preg_replace('<'.$regex.'>xu', '\0<wbr>', $match[0]); 551 }, $diffhtml); 552 } 553 554} 555