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