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