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