1<?php 2/** 3 * Changelog handling functions 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9// Constants for known core changelog line types. 10// Use these in place of string literals for more readable code. 11define('DOKU_CHANGE_TYPE_CREATE', 'C'); 12define('DOKU_CHANGE_TYPE_EDIT', 'E'); 13define('DOKU_CHANGE_TYPE_MINOR_EDIT', 'e'); 14define('DOKU_CHANGE_TYPE_DELETE', 'D'); 15define('DOKU_CHANGE_TYPE_REVERT', 'R'); 16 17/** 18 * parses a changelog line into it's components 19 * 20 * @author Ben Coburn <btcoburn@silicodon.net> 21 * 22 * @param string $line changelog line 23 * @return array|bool parsed line or false 24 */ 25function parseChangelogLine($line) { 26 $tmp = explode("\t", $line); 27 if ($tmp!==false && count($tmp)>1) { 28 $info = array(); 29 $info['date'] = (int)$tmp[0]; // unix timestamp 30 $info['ip'] = $tmp[1]; // IPv4 address (127.0.0.1) 31 $info['type'] = $tmp[2]; // log line type 32 $info['id'] = $tmp[3]; // page id 33 $info['user'] = $tmp[4]; // user name 34 $info['sum'] = $tmp[5]; // edit summary (or action reason) 35 $info['extra'] = rtrim($tmp[6], "\n"); // extra data (varies by line type) 36 return $info; 37 } else { return false; } 38} 39 40/** 41 * Add's an entry to the changelog and saves the metadata for the page 42 * 43 * @param int $date Timestamp of the change 44 * @param String $id Name of the affected page 45 * @param String $type Type of the change see DOKU_CHANGE_TYPE_* 46 * @param String $summary Summary of the change 47 * @param mixed $extra In case of a revert the revision (timestmp) of the reverted page 48 * @param array $flags Additional flags in a key value array. 49 * Available flags: 50 * - ExternalEdit - mark as an external edit. 51 * 52 * @author Andreas Gohr <andi@splitbrain.org> 53 * @author Esther Brunner <wikidesign@gmail.com> 54 * @author Ben Coburn <btcoburn@silicodon.net> 55 */ 56function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){ 57 global $conf, $INFO; 58 /** @var Input $INPUT */ 59 global $INPUT; 60 61 // check for special flags as keys 62 if (!is_array($flags)) { $flags = array(); } 63 $flagExternalEdit = isset($flags['ExternalEdit']); 64 65 $id = cleanid($id); 66 $file = wikiFN($id); 67 $created = @filectime($file); 68 $minor = ($type===DOKU_CHANGE_TYPE_MINOR_EDIT); 69 $wasRemoved = ($type===DOKU_CHANGE_TYPE_DELETE); 70 71 if(!$date) $date = time(); //use current time if none supplied 72 $remote = (!$flagExternalEdit)?clientIP(true):'127.0.0.1'; 73 $user = (!$flagExternalEdit)?$INPUT->server->str('REMOTE_USER'):''; 74 75 $strip = array("\t", "\n"); 76 $logline = array( 77 'date' => $date, 78 'ip' => $remote, 79 'type' => str_replace($strip, '', $type), 80 'id' => $id, 81 'user' => $user, 82 'sum' => utf8_substr(str_replace($strip, '', $summary),0,255), 83 'extra' => str_replace($strip, '', $extra) 84 ); 85 86 $wasCreated = ($type===DOKU_CHANGE_TYPE_CREATE); 87 $wasReverted = ($type===DOKU_CHANGE_TYPE_REVERT); 88 // update metadata 89 if (!$wasRemoved) { 90 $oldmeta = p_read_metadata($id); 91 $meta = array(); 92 if ($wasCreated && empty($oldmeta['persistent']['date']['created'])){ // newly created 93 $meta['date']['created'] = $created; 94 if ($user){ 95 $meta['creator'] = $INFO['userinfo']['name']; 96 $meta['user'] = $user; 97 } 98 } elseif (($wasCreated || $wasReverted) && !empty($oldmeta['persistent']['date']['created'])) { // re-created / restored 99 $meta['date']['created'] = $oldmeta['persistent']['date']['created']; 100 $meta['date']['modified'] = $created; // use the files ctime here 101 $meta['creator'] = $oldmeta['persistent']['creator']; 102 if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name']; 103 } elseif (!$minor) { // non-minor modification 104 $meta['date']['modified'] = $date; 105 if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name']; 106 } 107 $meta['last_change'] = $logline; 108 p_set_metadata($id, $meta); 109 } 110 111 // add changelog lines 112 $logline = implode("\t", $logline)."\n"; 113 io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog 114 io_saveFile($conf['changelog'],$logline,true); //global changelog cache 115} 116 117/** 118 * Add's an entry to the media changelog 119 * 120 * @author Michael Hamann <michael@content-space.de> 121 * @author Andreas Gohr <andi@splitbrain.org> 122 * @author Esther Brunner <wikidesign@gmail.com> 123 * @author Ben Coburn <btcoburn@silicodon.net> 124 * 125 * @param int $date Timestamp of the change 126 * @param String $id Name of the affected page 127 * @param String $type Type of the change see DOKU_CHANGE_TYPE_* 128 * @param String $summary Summary of the change 129 * @param mixed $extra In case of a revert the revision (timestmp) of the reverted page 130 * @param array $flags Additional flags in a key value array. 131 * Available flags: 132 * - (none, so far) 133 */ 134function addMediaLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){ 135 global $conf; 136 /** @var Input $INPUT */ 137 global $INPUT; 138 139 $id = cleanid($id); 140 141 if(!$date) $date = time(); //use current time if none supplied 142 $remote = clientIP(true); 143 $user = $INPUT->server->str('REMOTE_USER'); 144 145 $strip = array("\t", "\n"); 146 $logline = array( 147 'date' => $date, 148 'ip' => $remote, 149 'type' => str_replace($strip, '', $type), 150 'id' => $id, 151 'user' => $user, 152 'sum' => utf8_substr(str_replace($strip, '', $summary),0,255), 153 'extra' => str_replace($strip, '', $extra) 154 ); 155 156 // add changelog lines 157 $logline = implode("\t", $logline)."\n"; 158 io_saveFile($conf['media_changelog'],$logline,true); //global media changelog cache 159 io_saveFile(mediaMetaFN($id,'.changes'),$logline,true); //media file's changelog 160} 161 162/** 163 * returns an array of recently changed files using the 164 * changelog 165 * 166 * The following constants can be used to control which changes are 167 * included. Add them together as needed. 168 * 169 * RECENTS_SKIP_DELETED - don't include deleted pages 170 * RECENTS_SKIP_MINORS - don't include minor changes 171 * RECENTS_SKIP_SUBSPACES - don't include subspaces 172 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes 173 * RECENTS_MEDIA_PAGES_MIXED - return both media changes and page changes 174 * 175 * @param int $first number of first entry returned (for paginating 176 * @param int $num return $num entries 177 * @param string $ns restrict to given namespace 178 * @param int $flags see above 179 * @return array recently changed files 180 * 181 * @author Ben Coburn <btcoburn@silicodon.net> 182 * @author Kate Arzamastseva <pshns@ukr.net> 183 */ 184function getRecents($first,$num,$ns='',$flags=0){ 185 global $conf; 186 $recent = array(); 187 $count = 0; 188 189 if(!$num) 190 return $recent; 191 192 // read all recent changes. (kept short) 193 if ($flags & RECENTS_MEDIA_CHANGES) { 194 $lines = @file($conf['media_changelog']); 195 } else { 196 $lines = @file($conf['changelog']); 197 } 198 $lines_position = count($lines)-1; 199 $media_lines_position = 0; 200 $media_lines = array(); 201 202 if ($flags & RECENTS_MEDIA_PAGES_MIXED) { 203 $media_lines = @file($conf['media_changelog']); 204 $media_lines_position = count($media_lines)-1; 205 } 206 207 $seen = array(); // caches seen lines, _handleRecent() skips them 208 209 // handle lines 210 while ($lines_position >= 0 || (($flags & RECENTS_MEDIA_PAGES_MIXED) && $media_lines_position >=0)) { 211 if (empty($rec) && $lines_position >= 0) { 212 $rec = _handleRecent(@$lines[$lines_position], $ns, $flags, $seen); 213 if (!$rec) { 214 $lines_position --; 215 continue; 216 } 217 } 218 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && empty($media_rec) && $media_lines_position >= 0) { 219 $media_rec = _handleRecent(@$media_lines[$media_lines_position], $ns, $flags | RECENTS_MEDIA_CHANGES, $seen); 220 if (!$media_rec) { 221 $media_lines_position --; 222 continue; 223 } 224 } 225 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && @$media_rec['date'] >= @$rec['date']) { 226 $media_lines_position--; 227 $x = $media_rec; 228 $x['media'] = true; 229 $media_rec = false; 230 } else { 231 $lines_position--; 232 $x = $rec; 233 if ($flags & RECENTS_MEDIA_CHANGES) $x['media'] = true; 234 $rec = false; 235 } 236 if(--$first >= 0) continue; // skip first entries 237 $recent[] = $x; 238 $count++; 239 // break when we have enough entries 240 if($count >= $num){ break; } 241 } 242 return $recent; 243} 244 245/** 246 * returns an array of files changed since a given time using the 247 * changelog 248 * 249 * The following constants can be used to control which changes are 250 * included. Add them together as needed. 251 * 252 * RECENTS_SKIP_DELETED - don't include deleted pages 253 * RECENTS_SKIP_MINORS - don't include minor changes 254 * RECENTS_SKIP_SUBSPACES - don't include subspaces 255 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes 256 * 257 * @param int $from date of the oldest entry to return 258 * @param int $to date of the newest entry to return (for pagination, optional) 259 * @param string $ns restrict to given namespace (optional) 260 * @param int $flags see above (optional) 261 * @return array of files 262 * 263 * @author Michael Hamann <michael@content-space.de> 264 * @author Ben Coburn <btcoburn@silicodon.net> 265 */ 266function getRecentsSince($from,$to=null,$ns='',$flags=0){ 267 global $conf; 268 $recent = array(); 269 270 if($to && $to < $from) 271 return $recent; 272 273 // read all recent changes. (kept short) 274 if ($flags & RECENTS_MEDIA_CHANGES) { 275 $lines = @file($conf['media_changelog']); 276 } else { 277 $lines = @file($conf['changelog']); 278 } 279 if(!$lines) return $recent; 280 281 // we start searching at the end of the list 282 $lines = array_reverse($lines); 283 284 // handle lines 285 $seen = array(); // caches seen lines, _handleRecent() skips them 286 287 foreach($lines as $line){ 288 $rec = _handleRecent($line, $ns, $flags, $seen); 289 if($rec !== false) { 290 if ($rec['date'] >= $from) { 291 if (!$to || $rec['date'] <= $to) { 292 $recent[] = $rec; 293 } 294 } else { 295 break; 296 } 297 } 298 } 299 300 return array_reverse($recent); 301} 302 303/** 304 * Internal function used by getRecents 305 * 306 * don't call directly 307 * 308 * @see getRecents() 309 * @author Andreas Gohr <andi@splitbrain.org> 310 * @author Ben Coburn <btcoburn@silicodon.net> 311 * 312 * @param string $line changelog line 313 * @param string $ns restrict to given namespace 314 * @param int $flags flags to control which changes are included 315 * @param array $seen listing of seen pages 316 * @return array|bool false or array with info about a change 317 */ 318function _handleRecent($line,$ns,$flags,&$seen){ 319 if(empty($line)) return false; //skip empty lines 320 321 // split the line into parts 322 $recent = parseChangelogLine($line); 323 if ($recent===false) { return false; } 324 325 // skip seen ones 326 if(isset($seen[$recent['id']])) return false; 327 328 // skip minors 329 if($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false; 330 331 // remember in seen to skip additional sights 332 $seen[$recent['id']] = 1; 333 334 // check if it's a hidden page 335 if(isHiddenPage($recent['id'])) return false; 336 337 // filter namespace 338 if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false; 339 340 // exclude subnamespaces 341 if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false; 342 343 // check ACL 344 if ($flags & RECENTS_MEDIA_CHANGES) { 345 $recent['perms'] = auth_quickaclcheck(getNS($recent['id']).':*'); 346 } else { 347 $recent['perms'] = auth_quickaclcheck($recent['id']); 348 } 349 if ($recent['perms'] < AUTH_READ) return false; 350 351 // check existance 352 if($flags & RECENTS_SKIP_DELETED){ 353 $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id'])); 354 if(!file_exists($fn)) return false; 355 } 356 357 return $recent; 358} 359 360/** 361 * Class ChangeLog 362 * methods for handling of changelog of pages or media files 363 */ 364abstract class ChangeLog { 365 366 /** @var string */ 367 protected $id; 368 /** @var int */ 369 protected $chunk_size; 370 /** @var array */ 371 protected $cache; 372 373 /** 374 * Constructor 375 * 376 * @param string $id page id 377 * @param int $chunk_size maximum block size read from file 378 */ 379 public function __construct($id, $chunk_size = 8192) { 380 global $cache_revinfo; 381 382 $this->cache =& $cache_revinfo; 383 if(!isset($this->cache[$id])) { 384 $this->cache[$id] = array(); 385 } 386 387 $this->id = $id; 388 $this->setChunkSize($chunk_size); 389 390 } 391 392 /** 393 * Set chunk size for file reading 394 * Chunk size zero let read whole file at once 395 * 396 * @param int $chunk_size maximum block size read from file 397 */ 398 public function setChunkSize($chunk_size) { 399 if(!is_numeric($chunk_size)) $chunk_size = 0; 400 401 $this->chunk_size = (int) max($chunk_size, 0); 402 } 403 404 /** 405 * Returns path to changelog 406 * 407 * @return string path to file 408 */ 409 abstract protected function getChangelogFilename(); 410 411 /** 412 * Returns path to current page/media 413 * 414 * @return string path to file 415 */ 416 abstract protected function getFilename(); 417 418 /** 419 * Get the changelog information for a specific page id and revision (timestamp) 420 * 421 * Adjacent changelog lines are optimistically parsed and cached to speed up 422 * consecutive calls to getRevisionInfo. For large changelog files, only the chunk 423 * containing the requested changelog line is read. 424 * 425 * @param int $rev revision timestamp 426 * @return bool|array false or array with entries: 427 * - date: unix timestamp 428 * - ip: IPv4 address (127.0.0.1) 429 * - type: log line type 430 * - id: page id 431 * - user: user name 432 * - sum: edit summary (or action reason) 433 * - extra: extra data (varies by line type) 434 * 435 * @author Ben Coburn <btcoburn@silicodon.net> 436 * @author Kate Arzamastseva <pshns@ukr.net> 437 */ 438 public function getRevisionInfo($rev) { 439 $rev = max($rev, 0); 440 441 // check if it's already in the memory cache 442 if(isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) { 443 return $this->cache[$this->id][$rev]; 444 } 445 446 //read lines from changelog 447 list($fp, $lines) = $this->readloglines($rev); 448 if($fp) { 449 fclose($fp); 450 } 451 if(empty($lines)) return false; 452 453 // parse and cache changelog lines 454 foreach($lines as $value) { 455 $tmp = parseChangelogLine($value); 456 if($tmp !== false) { 457 $this->cache[$this->id][$tmp['date']] = $tmp; 458 } 459 } 460 if(!isset($this->cache[$this->id][$rev])) { 461 return false; 462 } 463 return $this->cache[$this->id][$rev]; 464 } 465 466 /** 467 * Return a list of page revisions numbers 468 * 469 * Does not guarantee that the revision exists in the attic, 470 * only that a line with the date exists in the changelog. 471 * By default the current revision is skipped. 472 * 473 * The current revision is automatically skipped when the page exists. 474 * See $INFO['meta']['last_change'] for the current revision. 475 * A negative $first let read the current revision too. 476 * 477 * For efficiency, the log lines are parsed and cached for later 478 * calls to getRevisionInfo. Large changelog files are read 479 * backwards in chunks until the requested number of changelog 480 * lines are recieved. 481 * 482 * @param int $first skip the first n changelog lines 483 * @param int $num number of revisions to return 484 * @return array with the revision timestamps 485 * 486 * @author Ben Coburn <btcoburn@silicodon.net> 487 * @author Kate Arzamastseva <pshns@ukr.net> 488 */ 489 public function getRevisions($first, $num) { 490 $revs = array(); 491 $lines = array(); 492 $count = 0; 493 494 $num = max($num, 0); 495 if($num == 0) { 496 return $revs; 497 } 498 499 if($first < 0) { 500 $first = 0; 501 } else if(file_exists($this->getFilename())) { 502 // skip current revision if the page exists 503 $first = max($first + 1, 0); 504 } 505 506 $file = $this->getChangelogFilename(); 507 508 if(!file_exists($file)) { 509 return $revs; 510 } 511 if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 512 // read whole file 513 $lines = file($file); 514 if($lines === false) { 515 return $revs; 516 } 517 } else { 518 // read chunks backwards 519 $fp = fopen($file, 'rb'); // "file pointer" 520 if($fp === false) { 521 return $revs; 522 } 523 fseek($fp, 0, SEEK_END); 524 $tail = ftell($fp); 525 526 // chunk backwards 527 $finger = max($tail - $this->chunk_size, 0); 528 while($count < $num + $first) { 529 $nl = $this->getNewlinepointer($fp, $finger); 530 531 // was the chunk big enough? if not, take another bite 532 if($nl > 0 && $tail <= $nl) { 533 $finger = max($finger - $this->chunk_size, 0); 534 continue; 535 } else { 536 $finger = $nl; 537 } 538 539 // read chunk 540 $chunk = ''; 541 $read_size = max($tail - $finger, 0); // found chunk size 542 $got = 0; 543 while($got < $read_size && !feof($fp)) { 544 $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0)); 545 if($tmp === false) { 546 break; 547 } //error state 548 $got += strlen($tmp); 549 $chunk .= $tmp; 550 } 551 $tmp = explode("\n", $chunk); 552 array_pop($tmp); // remove trailing newline 553 554 // combine with previous chunk 555 $count += count($tmp); 556 $lines = array_merge($tmp, $lines); 557 558 // next chunk 559 if($finger == 0) { 560 break; 561 } // already read all the lines 562 else { 563 $tail = $finger; 564 $finger = max($tail - $this->chunk_size, 0); 565 } 566 } 567 fclose($fp); 568 } 569 570 // skip parsing extra lines 571 $num = max(min(count($lines) - $first, $num), 0); 572 if ($first > 0 && $num > 0) { $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num); } 573 else if($first > 0 && $num == 0) { $lines = array_slice($lines, 0, max(count($lines) - $first, 0)); } 574 else if($first == 0 && $num > 0) { $lines = array_slice($lines, max(count($lines) - $num, 0)); } 575 576 // handle lines in reverse order 577 for($i = count($lines) - 1; $i >= 0; $i--) { 578 $tmp = parseChangelogLine($lines[$i]); 579 if($tmp !== false) { 580 $this->cache[$this->id][$tmp['date']] = $tmp; 581 $revs[] = $tmp['date']; 582 } 583 } 584 585 return $revs; 586 } 587 588 /** 589 * Get the nth revision left or right handside for a specific page id and revision (timestamp) 590 * 591 * For large changelog files, only the chunk containing the 592 * reference revision $rev is read and sometimes a next chunck. 593 * 594 * Adjacent changelog lines are optimistically parsed and cached to speed up 595 * consecutive calls to getRevisionInfo. 596 * 597 * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) 598 * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev 599 * @return bool|int 600 * timestamp of the requested revision 601 * otherwise false 602 */ 603 public function getRelativeRevision($rev, $direction) { 604 $rev = max($rev, 0); 605 $direction = (int) $direction; 606 607 //no direction given or last rev, so no follow-up 608 if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) { 609 return false; 610 } 611 612 //get lines from changelog 613 list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev); 614 if(empty($lines)) return false; 615 616 // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached 617 // also parse and cache changelog lines for getRevisionInfo(). 618 $revcounter = 0; 619 $relativerev = false; 620 $checkotherchunck = true; //always runs once 621 while(!$relativerev && $checkotherchunck) { 622 $tmp = array(); 623 //parse in normal or reverse order 624 $count = count($lines); 625 if($direction > 0) { 626 $start = 0; 627 $step = 1; 628 } else { 629 $start = $count - 1; 630 $step = -1; 631 } 632 for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { 633 $tmp = parseChangelogLine($lines[$i]); 634 if($tmp !== false) { 635 $this->cache[$this->id][$tmp['date']] = $tmp; 636 //look for revs older/earlier then reference $rev and select $direction-th one 637 if(($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) { 638 $revcounter++; 639 if($revcounter == abs($direction)) { 640 $relativerev = $tmp['date']; 641 } 642 } 643 } 644 } 645 646 //true when $rev is found, but not the wanted follow-up. 647 $checkotherchunck = $fp 648 && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev)) 649 && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); 650 651 if($checkotherchunck) { 652 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction); 653 654 if(empty($lines)) break; 655 } 656 } 657 if($fp) { 658 fclose($fp); 659 } 660 661 return $relativerev; 662 } 663 664 /** 665 * Returns revisions around rev1 and rev2 666 * When available it returns $max entries for each revision 667 * 668 * @param int $rev1 oldest revision timestamp 669 * @param int $rev2 newest revision timestamp (0 looks up last revision) 670 * @param int $max maximum number of revisions returned 671 * @return array with two arrays with revisions surrounding rev1 respectively rev2 672 */ 673 public function getRevisionsAround($rev1, $rev2, $max = 50) { 674 $max = floor(abs($max) / 2)*2 + 1; 675 $rev1 = max($rev1, 0); 676 $rev2 = max($rev2, 0); 677 678 if($rev2) { 679 if($rev2 < $rev1) { 680 $rev = $rev2; 681 $rev2 = $rev1; 682 $rev1 = $rev; 683 } 684 } else { 685 //empty right side means a removed page. Look up last revision. 686 $revs = $this->getRevisions(-1, 1); 687 $rev2 = $revs[0]; 688 } 689 //collect revisions around rev2 690 list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max); 691 692 if(empty($revs2)) return array(array(), array()); 693 694 //collect revisions around rev1 695 $index = array_search($rev1, $allrevs); 696 if($index === false) { 697 //no overlapping revisions 698 list($revs1,,,,,) = $this->retrieveRevisionsAround($rev1, $max); 699 if(empty($revs1)) $revs1 = array(); 700 } else { 701 //revisions overlaps, reuse revisions around rev2 702 $revs1 = $allrevs; 703 while($head > 0) { 704 for($i = count($lines) - 1; $i >= 0; $i--) { 705 $tmp = parseChangelogLine($lines[$i]); 706 if($tmp !== false) { 707 $this->cache[$this->id][$tmp['date']] = $tmp; 708 $revs1[] = $tmp['date']; 709 $index++; 710 711 if($index > floor($max / 2)) break 2; 712 } 713 } 714 715 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 716 } 717 sort($revs1); 718 //return wanted selection 719 $revs1 = array_slice($revs1, max($index - floor($max/2), 0), $max); 720 } 721 722 return array(array_reverse($revs1), array_reverse($revs2)); 723 } 724 725 /** 726 * Returns lines from changelog. 727 * If file larger than $chuncksize, only chunck is read that could contain $rev. 728 * 729 * @param int $rev revision timestamp 730 * @return array|false 731 * if success returns array(fp, array(changeloglines), $head, $tail, $eof) 732 * where fp only defined for chuck reading, needs closing. 733 * otherwise false 734 */ 735 protected function readloglines($rev) { 736 $file = $this->getChangelogFilename(); 737 738 if(!file_exists($file)) { 739 return false; 740 } 741 742 $fp = null; 743 $head = 0; 744 $tail = 0; 745 $eof = 0; 746 747 if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 748 // read whole file 749 $lines = file($file); 750 if($lines === false) { 751 return false; 752 } 753 } else { 754 // read by chunk 755 $fp = fopen($file, 'rb'); // "file pointer" 756 if($fp === false) { 757 return false; 758 } 759 $head = 0; 760 fseek($fp, 0, SEEK_END); 761 $eof = ftell($fp); 762 $tail = $eof; 763 764 // find chunk 765 while($tail - $head > $this->chunk_size) { 766 $finger = $head + floor(($tail - $head) / 2.0); 767 $finger = $this->getNewlinepointer($fp, $finger); 768 $tmp = fgets($fp); 769 if($finger == $head || $finger == $tail) { 770 break; 771 } 772 $tmp = parseChangelogLine($tmp); 773 $finger_rev = $tmp['date']; 774 775 if($finger_rev > $rev) { 776 $tail = $finger; 777 } else { 778 $head = $finger; 779 } 780 } 781 782 if($tail - $head < 1) { 783 // cound not find chunk, assume requested rev is missing 784 fclose($fp); 785 return false; 786 } 787 788 $lines = $this->readChunk($fp, $head, $tail); 789 } 790 return array( 791 $fp, 792 $lines, 793 $head, 794 $tail, 795 $eof 796 ); 797 } 798 799 /** 800 * Read chunk and return array with lines of given chunck. 801 * Has no check if $head and $tail are really at a new line 802 * 803 * @param resource $fp resource filepointer 804 * @param int $head start point chunck 805 * @param int $tail end point chunck 806 * @return array lines read from chunck 807 */ 808 protected function readChunk($fp, $head, $tail) { 809 $chunk = ''; 810 $chunk_size = max($tail - $head, 0); // found chunk size 811 $got = 0; 812 fseek($fp, $head); 813 while($got < $chunk_size && !feof($fp)) { 814 $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0)); 815 if($tmp === false) { //error state 816 break; 817 } 818 $got += strlen($tmp); 819 $chunk .= $tmp; 820 } 821 $lines = explode("\n", $chunk); 822 array_pop($lines); // remove trailing newline 823 return $lines; 824 } 825 826 /** 827 * Set pointer to first new line after $finger and return its position 828 * 829 * @param resource $fp filepointer 830 * @param int $finger a pointer 831 * @return int pointer 832 */ 833 protected function getNewlinepointer($fp, $finger) { 834 fseek($fp, $finger); 835 $nl = $finger; 836 if($finger > 0) { 837 fgets($fp); // slip the finger forward to a new line 838 $nl = ftell($fp); 839 } 840 return $nl; 841 } 842 843 /** 844 * Check whether given revision is the current page 845 * 846 * @param int $rev timestamp of current page 847 * @return bool true if $rev is current revision, otherwise false 848 */ 849 public function isCurrentRevision($rev) { 850 return $rev == @filemtime($this->getFilename()); 851 } 852 853 /** 854 * Return an existing revision for a specific date which is 855 * the current one or younger or equal then the date 856 * 857 * @param number $date_at timestamp 858 * @return string revision ('' for current) 859 */ 860 function getLastRevisionAt($date_at){ 861 //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current 862 if($date_at >= @filemtime($this->getFilename())) { 863 return ''; 864 } else if ($rev = $this->getRelativeRevision($date_at+1, -1)) { //+1 to get also the requested date revision 865 return $rev; 866 } else { 867 return false; 868 } 869 } 870 871 /** 872 * Returns the next lines of the changelog of the chunck before head or after tail 873 * 874 * @param resource $fp filepointer 875 * @param int $head position head of last chunk 876 * @param int $tail position tail of last chunk 877 * @param int $direction positive forward, negative backward 878 * @return array with entries: 879 * - $lines: changelog lines of readed chunk 880 * - $head: head of chunk 881 * - $tail: tail of chunk 882 */ 883 protected function readAdjacentChunk($fp, $head, $tail, $direction) { 884 if(!$fp) return array(array(), $head, $tail); 885 886 if($direction > 0) { 887 //read forward 888 $head = $tail; 889 $tail = $head + floor($this->chunk_size * (2 / 3)); 890 $tail = $this->getNewlinepointer($fp, $tail); 891 } else { 892 //read backward 893 $tail = $head; 894 $head = max($tail - $this->chunk_size, 0); 895 while(true) { 896 $nl = $this->getNewlinepointer($fp, $head); 897 // was the chunk big enough? if not, take another bite 898 if($nl > 0 && $tail <= $nl) { 899 $head = max($head - $this->chunk_size, 0); 900 } else { 901 $head = $nl; 902 break; 903 } 904 } 905 } 906 907 //load next chunck 908 $lines = $this->readChunk($fp, $head, $tail); 909 return array($lines, $head, $tail); 910 } 911 912 /** 913 * Collect the $max revisions near to the timestamp $rev 914 * 915 * @param int $rev revision timestamp 916 * @param int $max maximum number of revisions to be returned 917 * @return bool|array 918 * return array with entries: 919 * - $requestedrevs: array of with $max revision timestamps 920 * - $revs: all parsed revision timestamps 921 * - $fp: filepointer only defined for chuck reading, needs closing. 922 * - $lines: non-parsed changelog lines before the parsed revisions 923 * - $head: position of first readed changelogline 924 * - $lasttail: position of end of last readed changelogline 925 * otherwise false 926 */ 927 protected function retrieveRevisionsAround($rev, $max) { 928 //get lines from changelog 929 list($fp, $lines, $starthead, $starttail, /* $eof */) = $this->readloglines($rev); 930 if(empty($lines)) return false; 931 932 //parse chunk containing $rev, and read forward more chunks until $max/2 is reached 933 $head = $starthead; 934 $tail = $starttail; 935 $revs = array(); 936 $aftercount = $beforecount = 0; 937 while(count($lines) > 0) { 938 foreach($lines as $line) { 939 $tmp = parseChangelogLine($line); 940 if($tmp !== false) { 941 $this->cache[$this->id][$tmp['date']] = $tmp; 942 $revs[] = $tmp['date']; 943 if($tmp['date'] >= $rev) { 944 //count revs after reference $rev 945 $aftercount++; 946 if($aftercount == 1) $beforecount = count($revs); 947 } 948 //enough revs after reference $rev? 949 if($aftercount > floor($max / 2)) break 2; 950 } 951 } 952 //retrieve next chunk 953 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1); 954 } 955 if($aftercount == 0) return false; 956 957 $lasttail = $tail; 958 959 //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max 960 $lines = array(); 961 $i = 0; 962 if($aftercount > 0) { 963 $head = $starthead; 964 $tail = $starttail; 965 while($head > 0) { 966 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 967 968 for($i = count($lines) - 1; $i >= 0; $i--) { 969 $tmp = parseChangelogLine($lines[$i]); 970 if($tmp !== false) { 971 $this->cache[$this->id][$tmp['date']] = $tmp; 972 $revs[] = $tmp['date']; 973 $beforecount++; 974 //enough revs before reference $rev? 975 if($beforecount > max(floor($max / 2), $max - $aftercount)) break 2; 976 } 977 } 978 } 979 } 980 sort($revs); 981 982 //keep only non-parsed lines 983 $lines = array_slice($lines, 0, $i); 984 //trunk desired selection 985 $requestedrevs = array_slice($revs, -$max, $max); 986 987 return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail); 988 } 989} 990 991/** 992 * Class PageChangelog handles changelog of a wiki page 993 */ 994class PageChangelog extends ChangeLog { 995 996 /** 997 * Returns path to changelog 998 * 999 * @return string path to file 1000 */ 1001 protected function getChangelogFilename() { 1002 return metaFN($this->id, '.changes'); 1003 } 1004 1005 /** 1006 * Returns path to current page/media 1007 * 1008 * @return string path to file 1009 */ 1010 protected function getFilename() { 1011 return wikiFN($this->id); 1012 } 1013} 1014 1015/** 1016 * Class MediaChangelog handles changelog of a media file 1017 */ 1018class MediaChangelog extends ChangeLog { 1019 1020 /** 1021 * Returns path to changelog 1022 * 1023 * @return string path to file 1024 */ 1025 protected function getChangelogFilename() { 1026 return mediaMetaFN($this->id, '.changes'); 1027 } 1028 1029 /** 1030 * Returns path to current page/media 1031 * 1032 * @return string path to file 1033 */ 1034 protected function getFilename() { 1035 return mediaFN($this->id); 1036 } 1037} 1038 1039/** 1040 * Get the changelog information for a specific page id 1041 * and revision (timestamp). Adjacent changelog lines 1042 * are optimistically parsed and cached to speed up 1043 * consecutive calls to getRevisionInfo. For large 1044 * changelog files, only the chunk containing the 1045 * requested changelog line is read. 1046 * 1047 * @deprecated 2013-11-20 1048 * 1049 * @author Ben Coburn <btcoburn@silicodon.net> 1050 * @author Kate Arzamastseva <pshns@ukr.net> 1051 * 1052 * @param string $id 1053 * @param int $rev 1054 * @param int $chunk_size 1055 * @param bool $media 1056 * @return array|bool 1057 */ 1058function getRevisionInfo($id, $rev, $chunk_size = 8192, $media = false) { 1059 dbg_deprecated('class PageChangeLog or class MediaChangelog'); 1060 if($media) { 1061 $changelog = new MediaChangeLog($id, $chunk_size); 1062 } else { 1063 $changelog = new PageChangeLog($id, $chunk_size); 1064 } 1065 return $changelog->getRevisionInfo($rev); 1066} 1067 1068/** 1069 * Return a list of page revisions numbers 1070 * Does not guarantee that the revision exists in the attic, 1071 * only that a line with the date exists in the changelog. 1072 * By default the current revision is skipped. 1073 * 1074 * The current revision is automatically skipped when the page exists. 1075 * See $INFO['meta']['last_change'] for the current revision. 1076 * 1077 * For efficiency, the log lines are parsed and cached for later 1078 * calls to getRevisionInfo. Large changelog files are read 1079 * backwards in chunks until the requested number of changelog 1080 * lines are recieved. 1081 * 1082 * @deprecated 2013-11-20 1083 * 1084 * @author Ben Coburn <btcoburn@silicodon.net> 1085 * @author Kate Arzamastseva <pshns@ukr.net> 1086 * 1087 * @param string $id the page of interest 1088 * @param int $first skip the first n changelog lines 1089 * @param int $num number of revisions to return 1090 * @param int $chunk_size 1091 * @param bool $media 1092 * @return array 1093 */ 1094function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false) { 1095 dbg_deprecated('class PageChangeLog or class MediaChangelog'); 1096 if($media) { 1097 $changelog = new MediaChangeLog($id, $chunk_size); 1098 } else { 1099 $changelog = new PageChangeLog($id, $chunk_size); 1100 } 1101 return $changelog->getRevisions($first, $num); 1102} 1103 1104