1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\ChangeLog\PageChangeLog; 6use dokuwiki\ChangeLog\RevisionInfo; 7use dokuwiki\Form\Form; 8use InlineDiffFormatter; 9use TableDiffFormatter; 10 11/** 12 * DokuWiki PageDiff Interface 13 * 14 * @author Andreas Gohr <andi@splitbrain.org> 15 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 16 * @package dokuwiki\Ui 17 */ 18class PageDiff extends Diff 19{ 20 /* @var PageChangeLog */ 21 protected $changelog; 22 23 /* @var RevisionInfo older revision */ 24 protected $RevInfo1; 25 /* @var RevisionInfo newer revision */ 26 protected $RevInfo2; 27 28 /* @var string */ 29 protected $text; 30 31 /** 32 * PageDiff Ui constructor 33 * 34 * @param string $id page id 35 */ 36 public function __construct($id = null) 37 { 38 global $INFO; 39 if (!isset($id)) $id = $INFO['id']; 40 41 // init preference 42 $this->preference['showIntro'] = true; 43 $this->preference['difftype'] = 'sidebyside'; // diff view type: inline or sidebyside 44 45 parent::__construct($id); 46 } 47 48 /** @inheritdoc */ 49 protected function setChangeLog() 50 { 51 $this->changelog = new PageChangeLog($this->id); 52 } 53 54 /** 55 * Set text to be compared with most current version 56 * when it has been externally edited 57 * exclusively use of the compare($old, $new) method 58 * 59 * @param string $text 60 * @return $this 61 */ 62 public function compareWith($text = null) 63 { 64 if (isset($text)) { 65 $this->text = $text; 66 $changelog =& $this->changelog; 67 68 // revision info object of older file (left side) 69 $info = $changelog->getCurrentRevisionInfo(); 70 $info['media'] = false; 71 $this->RevInfo1 = new RevisionInfo($info); 72 $this->RevInfo1->append([ 73 'current' => true, 74 'text' => rawWiki($this->id), 75 ]); 76 77 // revision info object of newer file (right side) 78 $this->RevInfo2 = new RevisionInfo(); 79 $this->RevInfo2->append([ 80 'date' => false, 81 //'ip' => '127.0.0.1', 82 //'type' => DOKU_CHANGE_TYPE_CREATE, 83 'id' => $this->id, 84 //'user' => '', 85 //'sum' => '', 86 'extra' => 'compareWith', 87 //'sizechange' => strlen($this->text) - io_getSizeFile(wikiFN($this->id)), 88 'current' => false, 89 'text' => cleanText($this->text), 90 ]); 91 } 92 return $this; 93 } 94 95 /** 96 * Handle requested revision(s) and diff view preferences 97 * 98 * @return void 99 */ 100 protected function handle() 101 { 102 global $INPUT; 103 104 // retrieve requested rev or rev2 105 if (!isset($this->RevInfo1, $this->RevInfo2)) { 106 parent::handle(); 107 } 108 109 // requested diff view type 110 $mode = ''; 111 if ($INPUT->has('difftype')) { 112 $mode = $INPUT->str('difftype'); 113 } else { 114 // read preference from DokuWiki cookie. PageDiff only 115 $mode = get_doku_pref('difftype', null); 116 } 117 if (in_array($mode, ['inline', 'sidebyside'])) $this->preference['difftype'] = $mode; 118 119 if (!$INPUT->has('rev') && !$INPUT->has('rev2')) { 120 global $INFO, $REV; 121 if ($this->id == $INFO['id']) 122 $REV = $this->rev1; // store revision back in $REV 123 } 124 } 125 126 /** 127 * Prepare revision info of comparison pair 128 */ 129 protected function preProcess() 130 { 131 global $lang; 132 133 $changelog =& $this->changelog; 134 135 // create revision info object for older and newer sides 136 // RevInfo1 : older, left side 137 // RevInfo2 : newer, right side 138 139 $changelogRev1 = $changelog->getRevisionInfo($this->rev1); 140 $changelogRev2 = $changelog->getRevisionInfo($this->rev2); 141 $changelogRev1['media'] = $changelogRev2['media'] = false; 142 143 $this->RevInfo1 = new RevisionInfo($changelogRev1); 144 $this->RevInfo2 = new RevisionInfo($changelogRev2); 145 146 foreach ([$this->RevInfo1, $this->RevInfo2] as $RevInfo) { 147 $isCurrent = $changelog->isCurrentRevision($RevInfo->val('date')); 148 $RevInfo->isCurrent($isCurrent); 149 150 if ($RevInfo->val('type') == DOKU_CHANGE_TYPE_DELETE || empty($RevInfo->val('type'))) { 151 $text = ''; 152 } else { 153 $rev = $isCurrent ? '' : $RevInfo->val('date'); 154 $text = rawWiki($this->id, $rev); 155 } 156 $RevInfo->append(['text' => $text]); 157 } 158 159 // msg could displayed only when wrong url typed in browser address bar 160 if ($this->rev2 === false) { 161 msg(sprintf( 162 $lang['page_nonexist_rev'], 163 $this->id, 164 wl($this->id, ['do' => 'edit']), 165 $this->id 166 ), -1); 167 } elseif (!$this->rev1 || $this->rev1 == $this->rev2) { 168 msg('no way to compare when less than two revisions', -1); 169 } 170 } 171 172 /** 173 * Show diff 174 * between current page version and provided $text 175 * or between the revisions provided via GET or POST 176 * 177 * @return void 178 * @author Andreas Gohr <andi@splitbrain.org> 179 * 180 */ 181 public function show() 182 { 183 global $lang; 184 185 if (!isset($this->RevInfo1, $this->RevInfo2)) { 186 // retrieve form parameters: rev, rev2, difftype 187 $this->handle(); 188 // prepare revision info of comparison pair, except PageConfrict or PageDraft 189 $this->preProcess(); 190 } 191 192 // revision title 193 $rev1Title = trim($this->RevInfo1->showRevisionTitle() . ' ' . $this->RevInfo1->showCurrentIndicator()); 194 $rev1Summary = ($this->RevInfo1->val('date')) 195 ? $this->RevInfo1->showEditSummary() . ' ' . $this->RevInfo1->showEditor() 196 : ''; 197 198 if ($this->RevInfo2->val('extra') == 'compareWith') { 199 $rev2Title = $lang['yours']; 200 $rev2Summary = ''; 201 } else { 202 $rev2Title = trim($this->RevInfo2->showRevisionTitle() . ' ' . $this->RevInfo2->showCurrentIndicator()); 203 $rev2Summary = ($this->RevInfo2->val('date')) 204 ? $this->RevInfo2->showEditSummary() . ' ' . $this->RevInfo2->showEditor() 205 : ''; 206 } 207 208 // create difference engine object 209 $Difference = new \Diff( 210 explode("\n", $this->RevInfo1->val('text')), 211 explode("\n", $this->RevInfo2->val('text')) 212 ); 213 214 // build paired navigation 215 [$rev1Navi, $rev2Navi] = $this->buildRevisionsNavigation(); 216 217 // display intro 218 if ($this->preference['showIntro']) echo p_locale_xhtml('diff'); 219 220 // print form to choose diff view type, and exact url reference to the view 221 $this->showDiffViewSelector(); 222 223 // assign minor edit checker to the variable 224 $classEditType = static fn($changeType) => ($changeType === DOKU_CHANGE_TYPE_MINOR_EDIT) 225 ? ' class="minor"' 226 : ''; 227 228 // display diff view table 229 echo '<div class="table">'; 230 echo '<table class="diff diff_' . hsc($this->preference['difftype']) . '">'; 231 232 //navigation and header 233 switch ($this->preference['difftype']) { 234 case 'inline': 235 $title1 = $rev1Title . ($rev1Summary ? '<br />' . $rev1Summary : ''); 236 $title2 = $rev2Title . ($rev2Summary ? '<br />' . $rev2Summary : ''); 237 // no navigation for PageConflict or PageDraft 238 if ($this->RevInfo2->val('extra') !== 'compareWith') { 239 echo '<tr>' 240 . '<td class="diff-lineheader">-</td>' 241 . '<td class="diffnav">' . $rev1Navi . '</td>' 242 . '</tr>'; 243 echo '<tr>' 244 . '<th class="diff-lineheader">-</th>' 245 . '<th' . $classEditType($this->RevInfo1->val('type')) . '>' . $title1 . '</th>' 246 . '</tr>'; 247 } 248 echo '<tr>' 249 . '<td class="diff-lineheader">+</td>' 250 . '<td class="diffnav">' . $rev2Navi . '</td>' 251 . '</tr>'; 252 echo '<tr>' 253 . '<th class="diff-lineheader">+</th>' 254 . '<th' . $classEditType($this->RevInfo2->val('type')) . '>' . $title2 . '</th>' 255 . '</tr>'; 256 // create formatter object 257 $DiffFormatter = new InlineDiffFormatter(); 258 break; 259 260 case 'sidebyside': 261 default: 262 $title1 = $rev1Title . ($rev1Summary ? ' ' . $rev1Summary : ''); 263 $title2 = $rev2Title . ($rev2Summary ? ' ' . $rev2Summary : ''); 264 // no navigation for PageConflict or PageDraft 265 if ($this->RevInfo2->val('extra') !== 'compareWith') { 266 echo '<tr>' 267 . '<td colspan="2" class="diffnav">' . $rev1Navi . '</td>' 268 . '<td colspan="2" class="diffnav">' . $rev2Navi . '</td>' 269 . '</tr>'; 270 } 271 echo '<tr>' 272 . '<th colspan="2"' . $classEditType($this->RevInfo1->val('type')) . '>' . $title1 . '</th>' 273 . '<th colspan="2"' . $classEditType($this->RevInfo2->val('type')) . '>' . $title2 . '</th>' 274 . '</tr>'; 275 // create formatter object 276 $DiffFormatter = new TableDiffFormatter(); 277 break; 278 } 279 280 // output formatted difference 281 echo $this->insertSoftbreaks($DiffFormatter->format($Difference)); 282 283 echo '</table>'; 284 echo '</div>'; 285 } 286 287 /** 288 * Print form to choose diff view type, and exact url reference to the view 289 */ 290 protected function showDiffViewSelector() 291 { 292 global $lang; 293 294 // no revisions selector for PageConflict or PageDraft 295 if ($this->RevInfo2->val('extra') == 'compareWith') return; 296 297 // use timestamp for current revision, date may be false when revisions < 2 298 [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')]; 299 300 echo '<div class="diffoptions group">'; 301 302 // create the form to select difftype 303 $form = new Form(['action' => wl()]); 304 $form->setHiddenField('id', $this->id); 305 $form->setHiddenField('rev2[0]', $rev1); 306 $form->setHiddenField('rev2[1]', $rev2); 307 $form->setHiddenField('do', 'diff'); 308 309 $options = ['sidebyside' => $lang['diff_side'], 'inline' => $lang['diff_inline']]; 310 $input = $form->addDropdown('difftype', $options, $lang['diff_type']) 311 ->val($this->preference['difftype']) 312 ->addClass('quickselect'); 313 $input->useInput(false); // inhibit prefillInput() during toHTML() process 314 $form->addButton('do[diff]', 'Go')->attr('type', 'submit'); 315 echo $form->toHTML(); 316 317 // show exact url reference to the view when it is meaningful 318 echo '<p>'; 319 if ($rev1 && $rev2) { 320 // link to exactly this view FS#2835 321 $viewUrl = $this->diffViewlink('difflink', $rev1, $rev2); 322 } 323 echo $viewUrl ?? '<br />'; 324 echo '</p>'; 325 326 echo '</div>'; 327 } 328 329 /** 330 * Create html for revision navigation 331 * 332 * The navigation consists of older and newer revisions selectors, each 333 * state mutually depends on the selected revision of opposite side. 334 * 335 * @return string[] html of navigation for both older and newer sides 336 */ 337 protected function buildRevisionsNavigation() 338 { 339 $changelog =& $this->changelog; 340 341 if ($this->RevInfo2->val('extra') == 'compareWith') { 342 // no revisions selector for PageConflict or PageDraft 343 return ['', '']; 344 } 345 346 // use timestamp for current revision, date may be false when revisions < 2 347 [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')]; 348 349 // retrieve revisions used in dropdown selectors, even when rev1 or rev2 is false 350 [$revs1, $revs2] = $changelog->getRevisionsAround( 351 ($rev1 ?: $changelog->currentRevision()), 352 ($rev2 ?: $changelog->currentRevision()) 353 ); 354 355 // build options for dropdown selector 356 $rev1Options = $this->buildRevisionOptions('older', $revs1); 357 $rev2Options = $this->buildRevisionOptions('newer', $revs2); 358 // determine previous/next revisions (older/left side) 359 $rev1Prev = false; 360 $rev1Next = false; 361 if (($index = array_search($rev1, $revs1)) !== false) { 362 $rev1Prev = ($index + 1 < count($revs1)) ? $revs1[$index + 1] : false; 363 $rev1Next = ($index > 0) ? $revs1[$index - 1] : false; 364 } 365 // determine previous/next revisions (newer/right side) 366 $rev2Prev = false; 367 $rev2Next = false; 368 if (($index = array_search($rev2, $revs2)) !== false) { 369 $rev2Prev = ($index + 1 < count($revs2)) ? $revs2[$index + 1] : false; 370 $rev2Next = ($index > 0) ? $revs2[$index - 1] : false; 371 } 372 373 /* 374 * navigation UI for older revisions / Left side: 375 */ 376 $rev1Navi = ''; 377 // move backward both side: ◀◀ 378 if ($rev1Prev && $rev2Prev) 379 $rev1Navi .= $this->diffViewlink('diffbothprevrev', $rev1Prev, $rev2Prev); 380 // move backward left side: ◀ 381 if ($rev1Prev) 382 $rev1Navi .= $this->diffViewlink('diffprevrev', $rev1Prev, $rev2); 383 // dropdown 384 $rev1Navi .= $this->buildDropdownSelector('older', $rev1Options); 385 // move forward left side: ▶ 386 if ($rev1Next && ($rev1Next < $rev2)) 387 $rev1Navi .= $this->diffViewlink('diffnextrev', $rev1Next, $rev2); 388 389 /* 390 * navigation UI for newer revisions / Right side: 391 */ 392 $rev2Navi = ''; 393 // move backward right side: ◀ 394 if ($rev2Prev && ($rev1 < $rev2Prev)) 395 $rev2Navi .= $this->diffViewlink('diffprevrev', $rev1, $rev2Prev); 396 // dropdown 397 $rev2Navi .= $this->buildDropdownSelector('newer', $rev2Options); 398 // move forward right side: ▶ 399 if ($rev2Next) { 400 if ($changelog->isCurrentRevision($rev2Next)) { 401 $rev2Navi .= $this->diffViewlink('difflastrev', $rev1, $rev2Next); 402 } else { 403 $rev2Navi .= $this->diffViewlink('diffnextrev', $rev1, $rev2Next); 404 } 405 } 406 // move forward both side: ▶▶ 407 if ($rev1Next && $rev2Next) 408 $rev2Navi .= $this->diffViewlink('diffbothnextrev', $rev1Next, $rev2Next); 409 410 return [$rev1Navi, $rev2Navi]; 411 } 412 413 /** 414 * prepare options for dropdwon selector 415 * 416 * @params string $side "older" or "newer" 417 * @params array $revs list of revsion 418 * @return array 419 */ 420 protected function buildRevisionOptions($side, $revs) 421 { 422 // use timestamp for current revision, date may be false when revisions < 2 423 [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')]; 424 425 $changelog =& $this->changelog; 426 $options = []; 427 428 foreach ($revs as $rev) { 429 $info = $changelog->getRevisionInfo($rev); 430 // revision info may have timestamp key when external edits occurred 431 $info['timestamp'] ??= true; 432 $date = dformat($info['date']); 433 if ($info['timestamp'] === false) { 434 // exteranlly deleted or older file restored 435 $date = preg_replace('/[0-9a-zA-Z]/', '_', $date); 436 } 437 $options[$rev] = [ 438 'label' => implode(' ', [ 439 $date, 440 editorinfo($info['user'], true), 441 $info['sum'], 442 ]), 443 'attrs' => ['title' => $rev] 444 ]; 445 if ( 446 ($side == 'older' && ($rev2 && $rev >= $rev2)) 447 || ($side == 'newer' && ($rev <= $rev1)) 448 ) { 449 $options[$rev]['attrs']['disabled'] = 'disabled'; 450 } 451 } 452 return $options; 453 } 454 455 /** 456 * build Dropdown form for revisions navigation 457 * 458 * @params string $side "older" or "newer" 459 * @params array $options dropdown options 460 * @return string 461 */ 462 protected function buildDropdownSelector($side, $options) 463 { 464 // use timestamp for current revision, date may be false when revisions < 2 465 [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')]; 466 467 $form = new Form(['action' => wl($this->id)]); 468 $form->setHiddenField('id', $this->id); 469 $form->setHiddenField('do', 'diff'); 470 $form->setHiddenField('difftype', $this->preference['difftype']); 471 472 if ($side == 'older') { 473 // left side 474 $form->setHiddenField('rev2[1]', $rev2); 475 $input = $form->addDropdown('rev2[0]', $options) 476 ->val($rev1)->addClass('quickselect'); 477 $input->useInput(false); 478 } elseif ($side == 'newer') { 479 // right side 480 $form->setHiddenField('rev2[0]', $rev1); 481 $input = $form->addDropdown('rev2[1]', $options) 482 ->val($rev2)->addClass('quickselect'); 483 $input->useInput(false); 484 } 485 $form->addButton('do[diff]', 'Go')->attr('type', 'submit'); 486 return $form->toHTML(); 487 } 488 489 /** 490 * Create html link to a diff view defined by two revisions 491 * 492 * @param string $linktype 493 * @param int $rev1 older revision 494 * @param int $rev2 newer revision or null for diff with current revision 495 * @return string html of link to a diff view 496 */ 497 protected function diffViewlink($linktype, $rev1, $rev2 = null) 498 { 499 global $lang; 500 if ($rev1 === false) return ''; 501 502 if ($rev2 === null) { 503 $urlparam = [ 504 'do' => 'diff', 505 'rev' => $rev1, 506 'difftype' => $this->preference['difftype'] 507 ]; 508 } else { 509 $urlparam = [ 510 'do' => 'diff', 511 'rev2[0]' => $rev1, 512 'rev2[1]' => $rev2, 513 'difftype' => $this->preference['difftype'] 514 ]; 515 } 516 $attr = [ 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