1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\ChangeLog\PageChangeLog; 6use dokuwiki\Form\Form; 7 8/** 9 * DokuWiki PageDiff Interface 10 * 11 * @package dokuwiki\Ui 12 */ 13class PageDiff extends Diff 14{ 15 /* @var string */ 16 protected $text = ''; 17 18 /** 19 * PageDiff Ui constructor 20 * 21 * @param string $id page id 22 */ 23 public function __construct($id = null) 24 { 25 global $INFO; 26 $this->id = isset($id) ? $id : $INFO['id']; 27 $this->item = 'page'; 28 29 // init preference 30 $this->preference['showIntro'] = true; 31 $this->preference['difftype'] = 'sidebyside'; // diff view type: inline or sidebyside 32 33 $this->setChangeLog(); 34 } 35 36 /** @inheritdoc */ 37 protected function setChangeLog() 38 { 39 $this->changelog = new PageChangeLog($this->id); 40 } 41 42 /** 43 * Set text to be compared with most current version 44 * exclusively use of the compare($old, $new) method 45 * 46 * @param string $text 47 * @return $this 48 */ 49 public function compareWith($text = null) 50 { 51 if (isset($text)) { 52 $this->text = $text; 53 $this->old_rev = ''; 54 } 55 return $this; 56 } 57 58 /** @inheritdoc */ 59 protected function preProcess() 60 { 61 parent::preProcess(); 62 if (!isset($this->old_rev, $this->new_rev)) { 63 // no revision was given, compare previous to current 64 $this->old_rev = $this->changelog->getRevisions(0, 1)[0]; 65 $this->new_rev = ''; 66 67 global $INFO, $REV; 68 if ($this->id == $INFO['id']) 69 $REV = $this->old_rev; // store revision back in $REV 70 } 71 } 72 73 /** 74 * Show diff 75 * between current page version and provided $text 76 * or between the revisions provided via GET or POST 77 * 78 * @author Andreas Gohr <andi@splitbrain.org> 79 * 80 * @return void 81 */ 82 public function show() 83 { 84 global $INFO; 85 86 // determine left and right revision 87 if (!isset($this->old_rev, $this->new_rev)) $this->preProcess(); 88 [$l_rev, $r_rev] = [$this->old_rev, $this->new_rev]; 89 90 // determine the last revision, which is usually the timestamp of current page, 91 // however which might be the last revision if the page had removed. 92 if ($this->id == $INFO['id']) { 93 $this->last_rev = $INFO['currentrev'] ?? $INFO['meta']['last_change']['date'] ?? 0; 94 } else { 95 $last_rev = $this->changelog->getRevisions(-1, 1) // empty array for removed page 96 ?: $this->changelog->getRevisions(0, 1); 97 $this->last_rev = count($last_rev) > 0 ? $last_rev[0] : 0; 98 } 99 100 // build html diff view components 101 list( 102 $l_minor, $r_minor, 103 $l_head, $r_head, 104 $l_text, $r_text, 105 $l_nav, $r_nav, 106 ) = $this->buildDiffViewComponents($l_rev, $r_rev); 107 108 // create difference engine object 109 $Difference = new \Diff(explode("\n", $l_text), explode("\n", $r_text)); 110 111 // display intro 112 if ($this->preference['showIntro']) echo p_locale_xhtml('diff'); 113 114 // print form to choose diff view type, and exact url reference to the view 115 if (!$this->text) { 116 $this->showDiffViewSelector(); 117 } 118 119 // display diff view table 120 echo '<div class="table">'; 121 echo '<table class="diff diff_'.$this->preference['difftype'] .'">'; 122 123 //navigation and header 124 switch ($this->preference['difftype']) { 125 case 'inline': 126 if (!$this->text) { 127 echo '<tr>' 128 .'<td class="diff-lineheader">-</td>' 129 .'<td class="diffnav">'. $l_nav .'</td>' 130 .'</tr>'; 131 echo '<tr>' 132 .'<th class="diff-lineheader">-</th>' 133 .'<th'. $l_minor .'>'. $l_head .'</th>' 134 .'</tr>'; 135 } 136 echo '<tr>' 137 .'<td class="diff-lineheader">+</td>' 138 .'<td class="diffnav">'. $r_nav .'</td>' 139 .'</tr>'; 140 echo '<tr>' 141 .'<th class="diff-lineheader">+</th>' 142 .'<th'. $r_minor .'>'. $r_head .'</th>' 143 .'</tr>'; 144 // create formatter object 145 $DiffFormatter = new \InlineDiffFormatter(); 146 break; 147 148 case 'sidebyside': 149 default: 150 if (!$this->text) { 151 echo '<tr>' 152 .'<td colspan="2" class="diffnav">'. $l_nav .'</td>' 153 .'<td colspan="2" class="diffnav">'. $r_nav .'</td>' 154 .'</tr>'; 155 } 156 echo '<tr>' 157 .'<th colspan="2"'. $l_minor .'>'. $l_head .'</th>' 158 .'<th colspan="2"'. $r_minor .'>'. $r_head .'</th>' 159 .'</tr>'; 160 // create formatter object 161 $DiffFormatter = new \TableDiffFormatter(); 162 break; 163 } 164 165 // output formatted difference 166 echo $this->insertSoftbreaks($DiffFormatter->format($Difference)); 167 168 echo '</table>'; 169 echo '</div>'; 170 } 171 172 /** 173 * Print form to choose diff view type, and exact url reference to the view 174 */ 175 protected function showDiffViewSelector() 176 { 177 global $INFO, $lang; 178 179 echo '<div class="diffoptions group">'; 180 181 // create the form to select difftype 182 $form = new Form(['action' => wl()]); 183 $form->setHiddenField('id', $this->id); 184 $form->setHiddenField('rev2[0]', $this->old_rev ?: 'current'); 185 $form->setHiddenField('rev2[1]', $this->new_rev ?: 'current'); 186 $form->setHiddenField('do', 'diff'); 187 $options = array( 188 'sidebyside' => $lang['diff_side'], 189 'inline' => $lang['diff_inline'] 190 ); 191 $input = $form->addDropdown('difftype', $options, $lang['diff_type']) 192 ->val($this->preference['difftype']) 193 ->addClass('quickselect'); 194 $input->useInput(false); // inhibit prefillInput() during toHTML() process 195 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 196 echo $form->toHTML(); 197 198 // show exact url reference to the view 199 if ($this->id == $INFO['id']) { 200 echo '<p>'; 201 // link to exactly this view FS#2835 202 echo $this->diffViewlink('difflink', $this->old_rev, ($this->new_rev ?: $this->last_rev)); 203 echo '</p>'; 204 } 205 206 echo '</div>'; // .diffoptions 207 } 208 209 /** 210 * Build html diff view components 211 * 212 * @param int $l_rev revision timestamp of left side 213 * @param int $r_rev revision timestamp of right side 214 * @return array 215 * $l_minor, $r_minor, // string class attributes 216 * $l_head, $r_head, // string html snippet 217 * $l_text, $r_text, // string raw wiki text 218 * $l_nav, $r_nav, // string html snippet 219 */ 220 protected function buildDiffViewComponents($l_rev, $r_rev) 221 { 222 global $lang; 223 224 if ($this->text) { // compare text to the most current revision 225 $l_minor = ''; 226 $l_head = '<a class="wikilink1" href="'. wl($this->id) .'">' 227 . $this->id .' '. dformat((int) @filemtime(wikiFN($this->id))) .'</a> ' 228 . $lang['current']; 229 $l_text = rawWiki($this->id, ''); 230 231 $r_minor = ''; 232 $r_head = $lang['yours']; 233 $r_text = cleanText($this->text); 234 235 } else { 236 // when both revisions are empty then the page was created just now 237 if (!$l_rev && !$r_rev) { 238 $l_text = ''; 239 } else { 240 $l_text = rawWiki($this->id, $l_rev); 241 } 242 $r_text = rawWiki($this->id, $r_rev); 243 244 // get header of diff HTML 245 list( 246 $l_head, $r_head, 247 $l_minor, $r_minor, 248 ) = $this->buildDiffHead($l_rev, $r_rev); 249 } 250 // build navigation 251 $l_nav = ''; 252 $r_nav = ''; 253 if (!$this->text) { 254 list($l_nav, $r_nav) = $this->buildDiffNavigation($l_rev, $r_rev); 255 } 256 257 return array( 258 $l_minor, $r_minor, 259 $l_head, $r_head, 260 $l_text, $r_text, 261 $l_nav, $r_nav, 262 ); 263 } 264 265 /** 266 * Create html for revision navigation 267 * 268 * @param int $l_rev left revision timestamp 269 * @param int $r_rev right revision timestamp 270 * @return string[] html of left and right navigation elements 271 */ 272 protected function buildDiffNavigation($l_rev, $r_rev) 273 { 274 global $INFO; 275 276 // last timestamp is not in changelog, retrieve timestamp from metadata 277 // note: when page is removed, the metadata timestamp is zero 278 if (!$r_rev) { 279 $r_rev = $this->last_rev; 280 } 281 282 //retrieve revisions with additional info 283 list($l_revs, $r_revs) = $this->changelog->getRevisionsAround($l_rev, $r_rev); 284 285 $l_revisions = array(); 286 if (!$l_rev) { 287 //no left revision given, add dummy 288 $l_revisions[0] = array('label' => '', 'attrs' => []); 289 } 290 foreach ($l_revs as $rev) { 291 $info = $this->changelog->getRevisionInfo($rev); 292 $l_revisions[$rev] = array( 293 'label' => implode(' ', array( 294 dformat($info['date']), 295 editorinfo($info['user'], true), 296 $info['sum'], 297 )), 298 'attrs' => ['title' => $rev], 299 ); 300 if ($r_rev ? $rev >= $r_rev : false) 301 $l_revisions[$rev]['attrs']['disabled'] = 'disabled'; 302 } 303 304 $r_revisions = array(); 305 if (!$r_rev) { 306 //no right revision given, add dummy 307 $r_revisions[0] = array('label' => '', 'attrs' => []); 308 } 309 foreach ($r_revs as $rev) { 310 $info = $this->changelog->getRevisionInfo($rev); 311 $r_revisions[$rev] = array( 312 'label' => implode(' ', array( 313 dformat($info['date']), 314 editorinfo($info['user'], true), 315 $info['sum'], 316 )), 317 'attrs' => ['title' => $rev], 318 ); 319 if ($rev <= $l_rev) 320 $r_revisions[$rev]['attrs']['disabled'] = 'disabled'; 321 } 322 323 //determine previous/next revisions 324 $l_index = array_search($l_rev, $l_revs); 325 $l_prev = $l_revs[$l_index + 1]; 326 $l_next = $l_revs[$l_index - 1]; 327 if ($r_rev) { 328 $r_index = array_search($r_rev, $r_revs); 329 $r_prev = $r_revs[$r_index + 1]; 330 $r_next = $r_revs[$r_index - 1]; 331 } else { 332 //removed page 333 $r_prev = ($l_next) ? $r_revs[0] : null; 334 $r_next = null; 335 } 336 337 /* 338 * Left side: 339 */ 340 $l_nav = ''; 341 //move back 342 if ($l_prev) { 343 $l_nav .= $this->diffViewlink('diffbothprevrev', $l_prev, $r_prev); 344 $l_nav .= $this->diffViewlink('diffprevrev', $l_prev, $r_rev); 345 } 346 //dropdown 347 $form = new Form(['action' => wl()]); 348 $form->setHiddenField('id', $this->id); 349 $form->setHiddenField('difftype', $this->preference['difftype']); 350 $form->setHiddenField('rev2[1]', $r_rev ?: 'current'); 351 $form->setHiddenField('do', 'diff'); 352 $input = $form->addDropdown('rev2[0]', $l_revisions) 353 ->val($l_rev ?: 'current')->addClass('quickselect'); 354 $input->useInput(false); // inhibit prefillInput() during toHTML() process 355 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 356 $l_nav .= $form->toHTML(); 357 //move forward 358 if ($l_next && ($l_next < $r_rev || !$r_rev)) { 359 $l_nav .= $this->diffViewlink('diffnextrev', $l_next, $r_rev); 360 } 361 362 /* 363 * Right side: 364 */ 365 $r_nav = ''; 366 //move back 367 if ($l_rev < $r_prev) { 368 $r_nav .= $this->diffViewlink('diffprevrev', $l_rev, $r_prev); 369 } 370 //dropdown 371 $form = new Form(['action' => wl()]); 372 $form->setHiddenField('id', $this->id); 373 $form->setHiddenField('rev2[0]', $l_rev ?: 'current'); 374 $form->setHiddenField('difftype', $this->preference['difftype']); 375 $form->setHiddenField('do', 'diff'); 376 $input = $form->addDropdown('rev2[1]', $r_revisions) 377 ->val($r_rev ?: 'current')->addClass('quickselect'); 378 $input->useInput(false); // inhibit prefillInput() during toHTML() process 379 $form->addButton('do[diff]', 'Go')->attr('type','submit'); 380 $r_nav .= $form->toHTML(); 381 //move forward 382 if ($r_next) { 383 if ($this->changelog->isCurrentRevision($r_next)) { 384 //last revision is diff with current page 385 $r_nav .= $this->diffViewlink('difflastrev', $l_rev); 386 } else { 387 $r_nav .= $this->diffViewlink('diffnextrev', $l_rev, $r_next); 388 } 389 $r_nav .= $this->diffViewlink('diffbothnextrev', $l_next, $r_next); 390 } 391 return array($l_nav, $r_nav); 392 } 393 394 /** 395 * Create html link to a diff view defined by two revisions 396 * 397 * @param string $linktype 398 * @param int $lrev oldest revision 399 * @param int $rrev newest revision or null for diff with current revision 400 * @return string html of link to a diff view 401 */ 402 protected function diffViewlink($linktype, $lrev, $rrev = null) 403 { 404 global $lang; 405 if ($rrev === null) { 406 $urlparam = array( 407 'do' => 'diff', 408 'rev' => $lrev, 409 'difftype' => $this->preference['difftype'], 410 ); 411 } else { 412 $urlparam = array( 413 'do' => 'diff', 414 'rev2[0]' => $lrev, 415 'rev2[1]' => $rrev, 416 'difftype' => $this->preference['difftype'], 417 ); 418 } 419 $attr = array( 420 'class' => $linktype, 421 'href' => wl($this->id, $urlparam, true, '&'), 422 'title' => $lang[$linktype], 423 ); 424 return '<a '. buildAttributes($attr) .'><span>'. $lang[$linktype] .'</span></a>'; 425 } 426 427 428 /** 429 * Insert soft breaks in diff html 430 * 431 * @param string $diffhtml 432 * @return string 433 */ 434 public function insertSoftbreaks($diffhtml) 435 { 436 // search the diff html string for both: 437 // - html tags, so these can be ignored 438 // - long strings of characters without breaking characters 439 return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/', function ($match) { 440 // if match is an html tag, return it intact 441 if ($match[0][0] == '<') return $match[0]; 442 // its a long string without a breaking character, 443 // make certain characters into breaking characters by inserting a 444 // word break opportunity (<wbr> tag) in front of them. 445 $regex = <<< REGEX 446(?(?= # start a conditional expression with a positive look ahead ... 447&\#?\\w{1,6};) # ... for html entities - we don't want to split them (ok to catch some invalid combinations) 448&\#?\\w{1,6}; # yes pattern - a quicker match for the html entity, since we know we have one 449| 450[?/,&\#;:] # no pattern - any other group of 'special' characters to insert a breaking character after 451)+ # end conditional expression 452REGEX; 453 return preg_replace('<'.$regex.'>xu', '\0<wbr>', $match[0]); 454 }, $diffhtml); 455 } 456 457} 458