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|false 729 * if success returns array(fp, array(changeloglines), $head, $tail, $eof) 730 * where fp only defined for chuck reading, needs closing. 731 * otherwise false 732 */ 733 protected function readloglines($rev) { 734 $file = $this->getChangelogFilename(); 735 736 if(!@file_exists($file)) { 737 return false; 738 } 739 740 $fp = null; 741 $head = 0; 742 $tail = 0; 743 $eof = 0; 744 745 if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 746 // read whole file 747 $lines = file($file); 748 if($lines === false) { 749 return false; 750 } 751 } else { 752 // read by chunk 753 $fp = fopen($file, 'rb'); // "file pointer" 754 if($fp === false) { 755 return false; 756 } 757 $head = 0; 758 fseek($fp, 0, SEEK_END); 759 $eof = ftell($fp); 760 $tail = $eof; 761 762 // find chunk 763 while($tail - $head > $this->chunk_size) { 764 $finger = $head + floor(($tail - $head) / 2.0); 765 $finger = $this->getNewlinepointer($fp, $finger); 766 $tmp = fgets($fp); 767 if($finger == $head || $finger == $tail) { 768 break; 769 } 770 $tmp = parseChangelogLine($tmp); 771 $finger_rev = $tmp['date']; 772 773 if($finger_rev > $rev) { 774 $tail = $finger; 775 } else { 776 $head = $finger; 777 } 778 } 779 780 if($tail - $head < 1) { 781 // cound not find chunk, assume requested rev is missing 782 fclose($fp); 783 return false; 784 } 785 786 $lines = $this->readChunk($fp, $head, $tail); 787 } 788 return array( 789 $fp, 790 $lines, 791 $head, 792 $tail, 793 $eof 794 ); 795 } 796 797 /** 798 * Read chunk and return array with lines of given chunck. 799 * Has no check if $head and $tail are really at a new line 800 * 801 * @param resource $fp resource filepointer 802 * @param int $head start point chunck 803 * @param int $tail end point chunck 804 * @return array lines read from chunck 805 */ 806 protected function readChunk($fp, $head, $tail) { 807 $chunk = ''; 808 $chunk_size = max($tail - $head, 0); // found chunk size 809 $got = 0; 810 fseek($fp, $head); 811 while($got < $chunk_size && !feof($fp)) { 812 $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0)); 813 if($tmp === false) { //error state 814 break; 815 } 816 $got += strlen($tmp); 817 $chunk .= $tmp; 818 } 819 $lines = explode("\n", $chunk); 820 array_pop($lines); // remove trailing newline 821 return $lines; 822 } 823 824 /** 825 * Set pointer to first new line after $finger and return its position 826 * 827 * @param resource $fp filepointer 828 * @param int $finger a pointer 829 * @return int pointer 830 */ 831 protected function getNewlinepointer($fp, $finger) { 832 fseek($fp, $finger); 833 $nl = $finger; 834 if($finger > 0) { 835 fgets($fp); // slip the finger forward to a new line 836 $nl = ftell($fp); 837 } 838 return $nl; 839 } 840 841 /** 842 * Check whether given revision is the current page 843 * 844 * @param int $rev timestamp of current page 845 * @return bool true if $rev is current revision, otherwise false 846 */ 847 public function isCurrentRevision($rev) { 848 return $rev == @filemtime($this->getFilename()); 849 } 850 851 /** 852 * Return an existing revision for a specific date which is 853 * the current one or younger or equal then the date 854 * 855 * @param string $id 856 * @param number $date_at timestamp 857 * @return string revision ('' for current) 858 */ 859 function getLastRevisionAt($date_at){ 860 //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current 861 if($date_at >= @filemtime($this->getFilename())) { 862 return ''; 863 } else if ($rev = $this->getRelativeRevision($date_at+1, -1)) { //+1 to get also the requested date revision 864 return $rev; 865 } else { 866 return false; 867 } 868 } 869 870 /** 871 * Returns the next lines of the changelog of the chunck before head or after tail 872 * 873 * @param resource $fp filepointer 874 * @param int $head position head of last chunk 875 * @param int $tail position tail of last chunk 876 * @param int $direction positive forward, negative backward 877 * @return array with entries: 878 * - $lines: changelog lines of readed chunk 879 * - $head: head of chunk 880 * - $tail: tail of chunk 881 */ 882 protected function readAdjacentChunk($fp, $head, $tail, $direction) { 883 if(!$fp) return array(array(), $head, $tail); 884 885 if($direction > 0) { 886 //read forward 887 $head = $tail; 888 $tail = $head + floor($this->chunk_size * (2 / 3)); 889 $tail = $this->getNewlinepointer($fp, $tail); 890 } else { 891 //read backward 892 $tail = $head; 893 $head = max($tail - $this->chunk_size, 0); 894 while(true) { 895 $nl = $this->getNewlinepointer($fp, $head); 896 // was the chunk big enough? if not, take another bite 897 if($nl > 0 && $tail <= $nl) { 898 $head = max($head - $this->chunk_size, 0); 899 } else { 900 $head = $nl; 901 break; 902 } 903 } 904 } 905 906 //load next chunck 907 $lines = $this->readChunk($fp, $head, $tail); 908 return array($lines, $head, $tail); 909 } 910 911 /** 912 * Collect the $max revisions near to the timestamp $rev 913 * 914 * @param int $rev revision timestamp 915 * @param int $max maximum number of revisions to be returned 916 * @return bool|array 917 * return array with entries: 918 * - $requestedrevs: array of with $max revision timestamps 919 * - $revs: all parsed revision timestamps 920 * - $fp: filepointer only defined for chuck reading, needs closing. 921 * - $lines: non-parsed changelog lines before the parsed revisions 922 * - $head: position of first readed changelogline 923 * - $lasttail: position of end of last readed changelogline 924 * otherwise false 925 */ 926 protected function retrieveRevisionsAround($rev, $max) { 927 //get lines from changelog 928 list($fp, $lines, $starthead, $starttail, /* $eof */) = $this->readloglines($rev); 929 if(empty($lines)) return false; 930 931 //parse chunk containing $rev, and read forward more chunks until $max/2 is reached 932 $head = $starthead; 933 $tail = $starttail; 934 $revs = array(); 935 $aftercount = $beforecount = 0; 936 while(count($lines) > 0) { 937 foreach($lines as $line) { 938 $tmp = parseChangelogLine($line); 939 if($tmp !== false) { 940 $this->cache[$this->id][$tmp['date']] = $tmp; 941 $revs[] = $tmp['date']; 942 if($tmp['date'] >= $rev) { 943 //count revs after reference $rev 944 $aftercount++; 945 if($aftercount == 1) $beforecount = count($revs); 946 } 947 //enough revs after reference $rev? 948 if($aftercount > floor($max / 2)) break 2; 949 } 950 } 951 //retrieve next chunk 952 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1); 953 } 954 if($aftercount == 0) return false; 955 956 $lasttail = $tail; 957 958 //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max 959 $lines = array(); 960 $i = 0; 961 if($aftercount > 0) { 962 $head = $starthead; 963 $tail = $starttail; 964 while($head > 0) { 965 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 966 967 for($i = count($lines) - 1; $i >= 0; $i--) { 968 $tmp = parseChangelogLine($lines[$i]); 969 if($tmp !== false) { 970 $this->cache[$this->id][$tmp['date']] = $tmp; 971 $revs[] = $tmp['date']; 972 $beforecount++; 973 //enough revs before reference $rev? 974 if($beforecount > max(floor($max / 2), $max - $aftercount)) break 2; 975 } 976 } 977 } 978 } 979 sort($revs); 980 981 //keep only non-parsed lines 982 $lines = array_slice($lines, 0, $i); 983 //trunk desired selection 984 $requestedrevs = array_slice($revs, -$max, $max); 985 986 return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail); 987 } 988} 989 990/** 991 * Class PageChangelog handles changelog of a wiki page 992 */ 993class PageChangelog extends ChangeLog { 994 995 /** 996 * Returns path to changelog 997 * 998 * @return string path to file 999 */ 1000 protected function getChangelogFilename() { 1001 return metaFN($this->id, '.changes'); 1002 } 1003 1004 /** 1005 * Returns path to current page/media 1006 * 1007 * @return string path to file 1008 */ 1009 protected function getFilename() { 1010 return wikiFN($this->id); 1011 } 1012} 1013 1014/** 1015 * Class MediaChangelog handles changelog of a media file 1016 */ 1017class MediaChangelog extends ChangeLog { 1018 1019 /** 1020 * Returns path to changelog 1021 * 1022 * @return string path to file 1023 */ 1024 protected function getChangelogFilename() { 1025 return mediaMetaFN($this->id, '.changes'); 1026 } 1027 1028 /** 1029 * Returns path to current page/media 1030 * 1031 * @return string path to file 1032 */ 1033 protected function getFilename() { 1034 return mediaFN($this->id); 1035 } 1036} 1037 1038/** 1039 * Get the changelog information for a specific page id 1040 * and revision (timestamp). Adjacent changelog lines 1041 * are optimistically parsed and cached to speed up 1042 * consecutive calls to getRevisionInfo. For large 1043 * changelog files, only the chunk containing the 1044 * requested changelog line is read. 1045 * 1046 * @deprecated 2013-11-20 1047 * 1048 * @author Ben Coburn <btcoburn@silicodon.net> 1049 * @author Kate Arzamastseva <pshns@ukr.net> 1050 */ 1051function getRevisionInfo($id, $rev, $chunk_size = 8192, $media = false) { 1052 dbg_deprecated('class PageChangeLog or class MediaChangelog'); 1053 if($media) { 1054 $changelog = new MediaChangeLog($id, $chunk_size); 1055 } else { 1056 $changelog = new PageChangeLog($id, $chunk_size); 1057 } 1058 return $changelog->getRevisionInfo($rev); 1059} 1060 1061/** 1062 * Return a list of page revisions numbers 1063 * Does not guarantee that the revision exists in the attic, 1064 * only that a line with the date exists in the changelog. 1065 * By default the current revision is skipped. 1066 * 1067 * The current revision is automatically skipped when the page exists. 1068 * See $INFO['meta']['last_change'] for the current revision. 1069 * 1070 * For efficiency, the log lines are parsed and cached for later 1071 * calls to getRevisionInfo. Large changelog files are read 1072 * backwards in chunks until the requested number of changelog 1073 * lines are recieved. 1074 * 1075 * @deprecated 2013-11-20 1076 * 1077 * @author Ben Coburn <btcoburn@silicodon.net> 1078 * @author Kate Arzamastseva <pshns@ukr.net> 1079 * 1080 * @param string $id the page of interest 1081 * @param int $first skip the first n changelog lines 1082 * @param int $num number of revisions to return 1083 * @param int $chunk_size 1084 * @param bool $media 1085 * @return array 1086 */ 1087function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false) { 1088 dbg_deprecated('class PageChangeLog or class MediaChangelog'); 1089 if($media) { 1090 $changelog = new MediaChangeLog($id, $chunk_size); 1091 } else { 1092 $changelog = new PageChangeLog($id, $chunk_size); 1093 } 1094 return $changelog->getRevisions($first, $num); 1095} 1096 1097