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 if ($INPUT->has('difftype')) { 109 $this->preference['difftype'] = $INPUT->str('difftype'); 110 } else { 111 // read preference from DokuWiki cookie. PageDiff only 112 $mode = get_doku_pref('difftype', null); 113 if (isset($mode)) $this->preference['difftype'] = $mode; 114 } 115 116 if (!$INPUT->has('rev') && !$INPUT->has('rev2')) { 117 global $INFO, $REV; 118 if ($this->id == $INFO['id']) 119 $REV = $this->rev1; // store revision back in $REV 120 } 121 } 122 123 /** 124 * Prepare revision info of comparison pair 125 */ 126 protected function preProcess() 127 { 128 global $lang; 129 130 $changelog =& $this->changelog; 131 132 // create revision info object for older and newer sides 133 // RevInfo1 : older, left side 134 // RevInfo2 : newer, right side 135 136 $changelogRev1 = $changelog->getRevisionInfo($this->rev1); 137 $changelogRev2 = $changelog->getRevisionInfo($this->rev2); 138 $changelogRev1['media'] = $changelogRev2['media'] = false; 139 140 $this->RevInfo1 = new RevisionInfo($changelogRev1); 141 $this->RevInfo2 = new RevisionInfo($changelogRev2); 142 143 foreach ([$this->RevInfo1, $this->RevInfo2] as $RevInfo) { 144 $isCurrent = $changelog->isCurrentRevision($RevInfo->val('date')); 145 $RevInfo->isCurrent($isCurrent); 146 147 if ($RevInfo->val('type') == DOKU_CHANGE_TYPE_DELETE || empty($RevInfo->val('type'))) { 148 $text = ''; 149 } else { 150 $rev = $isCurrent ? '' : $RevInfo->val('date'); 151 $text = rawWiki($this->id, $rev); 152 } 153 $RevInfo->append(['text' => $text]); 154 } 155 156 // msg could displayed only when wrong url typed in browser address bar 157 if ($this->rev2 === false) { 158 msg(sprintf($lang['page_nonexist_rev'], 159 $this->id, 160 wl($this->id, ['do'=>'edit']), 161 $this->id), -1); 162 } elseif (!$this->rev1 || $this->rev1 == $this->rev2) { 163 msg('no way to compare when less than two revisions', -1); 164 } 165 } 166 167 /** 168 * Show diff 169 * between current page version and provided $text 170 * or between the revisions provided via GET or POST 171 * 172 * @author Andreas Gohr <andi@splitbrain.org> 173 * 174 * @return void 175 */ 176 public function show() 177 { 178 global $lang; 179 180 if (!isset($this->RevInfo1, $this->RevInfo2)) { 181 // retrieve form parameters: rev, rev2, difftype 182 $this->handle(); 183 // prepare revision info of comparison pair, except PageConfrict or PageDraft 184 $this->preProcess(); 185 } 186 187 // revision title 188 $rev1Title = trim($this->RevInfo1->showRevisionTitle() .' '. $this->RevInfo1->showCurrentIndicator()); 189 $rev1Summary = ($this->RevInfo1->val('date')) 190 ? $this->RevInfo1->showEditSummary() .' '. $this->RevInfo1->showEditor() 191 : ''; 192 193 if ($this->RevInfo2->val('extra') == 'compareWith') { 194 $rev2Title = $lang['yours']; 195 $rev2Summary = ''; 196 } else { 197 $rev2Title = trim($this->RevInfo2->showRevisionTitle() .' '. $this->RevInfo2->showCurrentIndicator()); 198 $rev2Summary = ($this->RevInfo2->val('date')) 199 ? $this->RevInfo2->showEditSummary() .' '. $this->RevInfo2->showEditor() 200 : ''; 201 } 202 203 // create difference engine object 204 $Difference = new \Diff( 205 explode("\n", $this->RevInfo1->val('text')), 206 explode("\n", $this->RevInfo2->val('text')) 207 ); 208 209 // build paired navigation 210 [$rev1Navi, $rev2Navi] = $this->buildRevisionsNavigation(); 211 212 // display intro 213 if ($this->preference['showIntro']) echo p_locale_xhtml('diff'); 214 215 // print form to choose diff view type, and exact url reference to the view 216 $this->showDiffViewSelector(); 217 218 // assign minor edit checker to the variable 219 $classEditType = function ($changeType) { 220 return ($changeType === DOKU_CHANGE_TYPE_MINOR_EDIT) ? ' class="minor"' : ''; 221 }; 222 223 // display diff view table 224 echo '<div class="table">'; 225 echo '<table class="diff diff_'.$this->preference['difftype'] .'">'; 226 227 //navigation and header 228 switch ($this->preference['difftype']) { 229 case 'inline': 230 $title1 = $rev1Title . ($rev1Summary ? '<br />'.$rev1Summary : ''); 231 $title2 = $rev2Title . ($rev2Summary ? '<br />'.$rev2Summary : ''); 232 // no navigation for PageConflict or PageDraft 233 if ($this->RevInfo2->val('extra') !== 'compareWith') { 234 echo '<tr>' 235 .'<td class="diff-lineheader">-</td>' 236 .'<td class="diffnav">'. $rev1Navi .'</td>' 237 .'</tr>'; 238 echo '<tr>' 239 .'<th class="diff-lineheader">-</th>' 240 .'<th'.$classEditType($this->RevInfo1->val('type')).'>'. $title1 .'</th>' 241 .'</tr>'; 242 } 243 echo '<tr>' 244 .'<td class="diff-lineheader">+</td>' 245 .'<td class="diffnav">'. $rev2Navi .'</td>' 246 .'</tr>'; 247 echo '<tr>' 248 .'<th class="diff-lineheader">+</th>' 249 .'<th'.$classEditType($this->RevInfo2->val('type')).'>'. $title2 .'</th>' 250 .'</tr>'; 251 // create formatter object 252 $DiffFormatter = new InlineDiffFormatter(); 253 break; 254 255 case 'sidebyside': 256 default: 257 $title1 = $rev1Title . ($rev1Summary ? ' '.$rev1Summary : ''); 258 $title2 = $rev2Title . ($rev2Summary ? ' '.$rev2Summary : ''); 259 // no navigation for PageConflict or PageDraft 260 if ($this->RevInfo2->val('extra') !== 'compareWith') { 261 echo '<tr>' 262 .'<td colspan="2" class="diffnav">'. $rev1Navi .'</td>' 263 .'<td colspan="2" class="diffnav">'. $rev2Navi .'</td>' 264 .'</tr>'; 265 } 266 echo '<tr>' 267 .'<th colspan="2"'.$classEditType($this->RevInfo1->val('type')).'>'.$title1.'</th>' 268 .'<th colspan="2"'.$classEditType($this->RevInfo2->val('type')).'>'.$title2.'</th>' 269 .'</tr>'; 270 // create formatter object 271 $DiffFormatter = new TableDiffFormatter(); 272 break; 273 } 274 275 // output formatted difference 276 echo $this->insertSoftbreaks($DiffFormatter->format($Difference)); 277 278 echo '</table>'; 279 echo '</div>'; 280 } 281 282 /** 283 * Print form to choose diff view type, and exact url reference to the view 284 */ 285 protected function showDiffViewSelector() 286 { 287 global $lang; 288 289 // no revisions selector for PageConflict or PageDraft 290 if ($this->RevInfo2->val('extra') == 'compareWith') return; 291 292 // use timestamp for current revision, date may be false when revisions < 2 293 [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')]; 294 295 echo '<div class="diffoptions group">'; 296 297 // create the form to select difftype 298 $form = new Form(['action' => wl()]); 299 $form->setHiddenField('id', $this->id); 300 $form->setHiddenField('rev2[0]', $rev1); 301 $form->setHiddenField('rev2[1]', $rev2); 302 $form->setHiddenField('do', 'diff'); 303 $options = array( 304 'sidebyside' => $lang['diff_side'], 305 'inline' => $lang['diff_inline'], 306 ); 307 $input = $form->addDropdown('difftype', $options, $lang['diff_type']) 308 ->val($this->preference['difftype']) 309 ->addClass('quickselect'); 310 $input->useInput(false); // inhibit prefillInput() during toHTML() process 311 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 312 echo $form->toHTML(); 313 314 // show exact url reference to the view when it is meaningful 315 echo '<p>'; 316 if ($rev1 && $rev2) { 317 // link to exactly this view FS#2835 318 $viewUrl = $this->diffViewlink('difflink', $rev1, $rev2); 319 } 320 echo $viewUrl ?? '<br />'; 321 echo '</p>'; 322 323 echo '</div>'; 324 } 325 326 /** 327 * Create html for revision navigation 328 * 329 * The navigation consists of older and newer revisions selectors, each 330 * state mutually depends on the selected revision of opposite side. 331 * 332 * @return string[] html of navigation for both older and newer sides 333 */ 334 protected function buildRevisionsNavigation() 335 { 336 $changelog =& $this->changelog; 337 338 if ($this->RevInfo2->val('extra') == 'compareWith') { 339 // no revisions selector for PageConflict or PageDraft 340 return array('', ''); 341 } 342 343 // use timestamp for current revision, date may be false when revisions < 2 344 [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')]; 345 346 // retrieve revisions used in dropdown selectors, even when rev1 or rev2 is false 347 [$revs1, $revs2] = $changelog->getRevisionsAround( 348 ($rev1 ?: $changelog->currentRevision()), 349 ($rev2 ?: $changelog->currentRevision()) 350 ); 351 352 // build options for dropdown selector 353 $rev1Options = $this->buildRevisionOptions('older', $revs1); 354 $rev2Options = $this->buildRevisionOptions('newer', $revs2); 355 356 // determine previous/next revisions (older/left side) 357 $rev1Prev = $rev1Next = false; 358 if (($index = array_search($rev1, $revs1)) !== false) { 359 $rev1Prev = ($index +1 < count($revs1)) ? $revs1[$index +1] : false; 360 $rev1Next = ($index > 0) ? $revs1[$index -1] : false; 361 } 362 // determine previous/next revisions (newer/right side) 363 $rev2Prev = $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 array($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'] = $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] = array( 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 switch ($side) { 468 case 'older': // left side 469 $form->setHiddenField('rev2[1]', $rev2); 470 $input = $form->addDropdown('rev2[0]', $options) 471 ->val($rev1)->addClass('quickselect'); 472 $input->useInput(false); // inhibit prefillInput() during toHTML() process 473 break; 474 case 'newer': // right side 475 $form->setHiddenField('rev2[0]', $rev1); 476 $input = $form->addDropdown('rev2[1]', $options) 477 ->val($rev2)->addClass('quickselect'); 478 $input->useInput(false); // inhibit prefillInput() during toHTML() process 479 break; 480 } 481 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 482 return $form->toHTML(); 483 } 484 485 /** 486 * Create html link to a diff view defined by two revisions 487 * 488 * @param string $linktype 489 * @param int $rev1 older revision 490 * @param int $rev2 newer revision or null for diff with current revision 491 * @return string html of link to a diff view 492 */ 493 protected function diffViewlink($linktype, $rev1, $rev2 = null) 494 { 495 global $lang; 496 if ($rev1 === false) return ''; 497 498 if ($rev2 === null) { 499 $urlparam = array( 500 'do' => 'diff', 501 'rev' => $rev1, 502 'difftype' => $this->preference['difftype'], 503 ); 504 } else { 505 $urlparam = array( 506 'do' => 'diff', 507 'rev2[0]' => $rev1, 508 'rev2[1]' => $rev2, 509 'difftype' => $this->preference['difftype'], 510 ); 511 } 512 $attr = array( 513 'class' => $linktype, 514 'href' => wl($this->id, $urlparam, true, '&'), 515 'title' => $lang[$linktype], 516 ); 517 return '<a '. buildAttributes($attr) .'><span>'. $lang[$linktype] .'</span></a>'; 518 } 519 520 521 /** 522 * Insert soft breaks in diff html 523 * 524 * @param string $diffhtml 525 * @return string 526 */ 527 public function insertSoftbreaks($diffhtml) 528 { 529 // search the diff html string for both: 530 // - html tags, so these can be ignored 531 // - long strings of characters without breaking characters 532 return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/', function ($match) { 533 // if match is an html tag, return it intact 534 if ($match[0][0] == '<') return $match[0]; 535 // its a long string without a breaking character, 536 // make certain characters into breaking characters by inserting a 537 // word break opportunity (<wbr> tag) in front of them. 538 $regex = <<< REGEX 539(?(?= # start a conditional expression with a positive look ahead ... 540&\#?\\w{1,6};) # ... for html entities - we don't want to split them (ok to catch some invalid combinations) 541&\#?\\w{1,6}; # yes pattern - a quicker match for the html entity, since we know we have one 542| 543[?/,&\#;:] # no pattern - any other group of 'special' characters to insert a breaking character after 544)+ # end conditional expression 545REGEX; 546 return preg_replace('<'.$regex.'>xu', '\0<wbr>', $match[0]); 547 }, $diffhtml); 548 } 549 550} 551