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