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 PageRevisionLog 338 */ 339class PageRevisionLog { 340 341 /** @var string */ 342 private $id; 343 /** @var int */ 344 private $chunk_size; 345 /** @var array */ 346 private $cache; 347 348 /** 349 * Constructor 350 * 351 * @param string $id page id 352 * @param int $chunk_size maximum block size read from file 353 */ 354 public function __construct($id, $chunk_size = 8192) { 355 global $cache_revinfo; 356 357 $this->cache =& $cache_revinfo; 358 if(!isset($this->cache[$id])) { 359 $this->cache[$id] = array(); 360 } 361 362 $this->id = $id; 363 $this->setChunkSize($chunk_size); 364 365 } 366 367 /** 368 * Set chunk size for file reading 369 * 370 * @param int $chunk_size maximum block size read from file 371 */ 372 public function setChunkSize($chunk_size) { 373 if(!is_numeric($chunk_size)) $chunk_size = 0; 374 375 $this->chunk_size = (int) max($chunk_size, 0); 376 } 377 378 /** 379 * Get the changelog information for a specific page id and revision (timestamp) 380 * 381 * Adjacent changelog lines are optimistically parsed and cached to speed up 382 * consecutive calls to getRevisionInfo. For large changelog files, only the chunk 383 * containing the requested changelog line is read. 384 * 385 * @param int $rev revision timestamp 386 * @param bool $media look into media log? 387 * @return bool|array false or array with entries: 388 * - date: unix timestamp 389 * - ip: IPv4 address (127.0.0.1) 390 * - type: log line type 391 * - id: page id 392 * - user: user name 393 * - sum: edit summary (or action reason) 394 * - extra: extra data (varies by line type) 395 * 396 * @author Ben Coburn <btcoburn@silicodon.net> 397 * @author Kate Arzamastseva <pshns@ukr.net> 398 */ 399 public function getRevisionInfo($rev, $media = false) { 400 $rev = max($rev, 0); 401 402 // check if it's already in the memory cache 403 if(isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) { 404 return $this->cache[$this->id][$rev]; 405 } 406 407 //read lines from changelog 408 list($fp, $lines) = $this->readloglines($media, $rev); 409 if($fp) { 410 fclose($fp); 411 } 412 if(empty($lines)) return false; 413 414 // parse and cache changelog lines 415 foreach($lines as $value) { 416 $tmp = parseChangelogLine($value); 417 if($tmp !== false) { 418 $this->cache[$this->id][$tmp['date']] = $tmp; 419 } 420 } 421 if(!isset($this->cache[$this->id][$rev])) { 422 return false; 423 } 424 return $this->cache[$this->id][$rev]; 425 } 426 427 /** 428 * Return a list of page revisions numbers 429 * 430 * Does not guarantee that the revision exists in the attic, 431 * only that a line with the date exists in the changelog. 432 * By default the current revision is skipped. 433 * 434 * The current revision is automatically skipped when the page exists. 435 * See $INFO['meta']['last_change'] for the current revision. 436 * A negative $first let read the current revision too. 437 * 438 * For efficiency, the log lines are parsed and cached for later 439 * calls to getRevisionInfo. Large changelog files are read 440 * backwards in chunks until the requested number of changelog 441 * lines are recieved. 442 * 443 * @param int $first skip the first n changelog lines 444 * @param int $num number of revisions to return 445 * @param bool $media look into media log? 446 * @return array with the revision timestamps 447 * 448 * @author Ben Coburn <btcoburn@silicodon.net> 449 * @author Kate Arzamastseva <pshns@ukr.net> 450 */ 451 public function getRevisions($first, $num, $media = false) { 452 $revs = array(); 453 $lines = array(); 454 $count = 0; 455 if ($media) { 456 $file = mediaMetaFN($this->id, '.changes'); 457 } else { 458 $file = metaFN($this->id, '.changes'); 459 } 460 $num = max($num, 0); 461 if ($num == 0) { return $revs; } 462 463 $this->chunk_size = max($this->chunk_size, 0); 464 if ($first<0) { 465 $first = 0; 466 } else if (!$media && @file_exists(wikiFN($this->id)) || $media && @file_exists(mediaFN($this->id))) { 467 // skip current revision if the page exists 468 $first = max($first+1, 0); 469 } 470 471 if (!@file_exists($file)) { return $revs; } 472 if (filesize($file)<$this->chunk_size || $this->chunk_size==0) { 473 // read whole file 474 $lines = file($file); 475 if ($lines===false) { return $revs; } 476 } else { 477 // read chunks backwards 478 $fp = fopen($file, 'rb'); // "file pointer" 479 if ($fp===false) { return $revs; } 480 fseek($fp, 0, SEEK_END); 481 $tail = ftell($fp); 482 483 // chunk backwards 484 $finger = max($tail-$this->chunk_size, 0); 485 while ($count<$num+$first) { 486 fseek($fp, $finger); 487 $nl = $finger; 488 if ($finger>0) { 489 fgets($fp); // slip the finger forward to a new line 490 $nl = ftell($fp); 491 } 492 493 // was the chunk big enough? if not, take another bite 494 if($nl > 0 && $tail <= $nl){ 495 $finger = max($finger-$this->chunk_size, 0); 496 continue; 497 }else{ 498 $finger = $nl; 499 } 500 501 // read chunk 502 $chunk = ''; 503 $read_size = max($tail-$finger, 0); // found chunk size 504 $got = 0; 505 while ($got<$read_size && !feof($fp)) { 506 $tmp = @fread($fp, max($read_size-$got, 0)); 507 if ($tmp===false) { break; } //error state 508 $got += strlen($tmp); 509 $chunk .= $tmp; 510 } 511 $tmp = explode("\n", $chunk); 512 array_pop($tmp); // remove trailing newline 513 514 // combine with previous chunk 515 $count += count($tmp); 516 $lines = array_merge($tmp, $lines); 517 518 // next chunk 519 if ($finger==0) { break; } // already read all the lines 520 else { 521 $tail = $finger; 522 $finger = max($tail-$this->chunk_size, 0); 523 } 524 } 525 fclose($fp); 526 } 527 528 // skip parsing extra lines 529 $num = max(min(count($lines)-$first, $num), 0); 530 if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } 531 else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } 532 else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } 533 534 // handle lines in reverse order 535 for ($i = count($lines)-1; $i >= 0; $i--) { 536 $tmp = parseChangelogLine($lines[$i]); 537 if ($tmp!==false) { 538 $this->cache[$this->id][$tmp['date']] = $tmp; 539 $revs[] = $tmp['date']; 540 } 541 } 542 543 return $revs; 544 } 545 546 /** 547 * Get the nth revision left or right handside for a specific page id and revision (timestamp) 548 * 549 * For large changelog files, only the chunk containing the 550 * reference revision $rev is read and sometimes a next chunck. 551 * 552 * Adjacent changelog lines are optimistically parsed and cached to speed up 553 * consecutive calls to getRevisionInfo. 554 * 555 * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) 556 * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev 557 * @param bool $media look into media log? 558 * @return bool|int 559 * timestamp of the requested revision 560 * otherwise false 561 */ 562 public function getRelativeRevision($rev, $direction, $media = false) { 563 $rev = max($rev, 0); 564 $direction = (int) $direction; 565 566 //no direction given or last rev, so no follow-up 567 if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev)) ) { 568 return false; 569 } 570 571 //get lines from changelog 572 list($fp, $lines, $head, $tail, $eof) = $this->readloglines($media, $rev); 573 if(empty($lines)) return false; 574 575 // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached 576 // also parse and cache changelog lines for getRevisionInfo(). 577 $revcounter = 0; 578 $relativerev = false; 579 $checkotherchunck = true; //always runs once 580 while(!$relativerev && $checkotherchunck) { 581 $tmp = array(); 582 //parse in normal or reverse order 583 $count = count($lines); 584 if($direction > 0) { 585 $start = 0; 586 $step = 1; 587 } else { 588 $start = $count - 1; 589 $step = -1; 590 } 591 for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { 592 $tmp = parseChangelogLine($lines[$i]); 593 if($tmp !== false) { 594 $this->cache[$this->id][$tmp['date']] = $tmp; 595 //look for revs older/earlier then reference $rev and select $direction-th one 596 if(($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) { 597 $revcounter++; 598 if($revcounter == abs($direction)) { 599 $relativerev = $tmp['date']; 600 } 601 } 602 } 603 } 604 605 //true when $rev is found, but not the wanted follow-up. 606 $checkotherchunck = $fp 607 && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev)) 608 && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); 609 610 if($checkotherchunck) { 611 //search bounds of chunck, rounded on new line, but smaller than $chunck_size 612 if($direction > 0) { 613 $head = $tail; 614 $lookpointer = true; 615 $tail = $head + floor($this->chunk_size * (2 / 3)); 616 while($lookpointer) { 617 $tail = min($tail, $eof); 618 $tail = $this->getNewlinepointer($fp, $tail); 619 $lookpointer = $tail - $head > $this->chunk_size; 620 if($lookpointer) { 621 $tail = $head + floor(($tail - $head) / 2); 622 } 623 if($tail == $head) break; 624 } 625 } else { 626 $tail = $head; 627 $head = max($tail - $this->chunk_size, 0); 628 $head = $this->getNewlinepointer($fp, $head); 629 } 630 631 //load next chunck 632 $lines = $this->readChunk($fp, $head, $tail); 633 if(empty($lines)) break; 634 } 635 } 636 if($fp) { 637 fclose($fp); 638 } 639 640 return $relativerev; 641 } 642 643 644 /** 645 * Returns lines from changelog. 646 * If file larger than $chuncksize, only chunck is read that could contain $rev. 647 * 648 * @param bool $media look into media log? 649 * @param int $rev revision timestamp 650 * @return array(fp, array(changeloglines), $head, $tail, $eof)|bool 651 * returns false when not succeed. fp only defined for chuck reading, needs closing. 652 */ 653 protected function readloglines($media, $rev) { 654 if($media) { 655 $file = mediaMetaFN($this->id, '.changes'); 656 } else { 657 $file = metaFN($this->id, '.changes'); 658 } 659 660 if(!@file_exists($file)) { 661 return false; 662 } 663 664 $fp = null; 665 $head = 0; 666 $tail = 0; 667 $eof = 0; 668 669 if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 670 // read whole file 671 $lines = file($file); 672 if($lines === false) { 673 return false; 674 } 675 } else { 676 // read by chunk 677 $fp = fopen($file, 'rb'); // "file pointer" 678 if($fp === false) { 679 return false; 680 } 681 $head = 0; 682 fseek($fp, 0, SEEK_END); 683 $eof = ftell($fp); 684 $tail = $eof; 685 686 // find chunk 687 while($tail - $head > $this->chunk_size) { 688 $finger = $head + floor(($tail - $head) / 2.0); 689 $finger = $this->getNewlinepointer($fp, $finger); 690 $tmp = fgets($fp); 691 $tmp = parseChangelogLine($tmp); 692 $finger_rev = $tmp['date']; 693 if($finger == $head || $finger == $tail) { 694 break; 695 } 696 if($finger_rev > $rev) { 697 $tail = $finger; 698 } else { 699 $head = $finger; 700 } 701 } 702 703 if($tail - $head < 1) { 704 // cound not find chunk, assume requested rev is missing 705 fclose($fp); 706 return false; 707 } 708 709 $lines = $this->readChunk($fp, $head, $tail); 710 } 711 return array( 712 $fp, 713 $lines, 714 $head, 715 $tail, 716 $eof 717 ); 718 } 719 720 /** 721 * Read chunk and return array with lines of given chunck. 722 * Has no check if $head and $tail are really at a new line 723 * 724 * @param $fp resource filepointer 725 * @param $head int start point chunck 726 * @param $tail int end point chunck 727 * @return array lines read from chunck 728 */ 729 protected function readChunk($fp, $head, $tail) { 730 $chunk = ''; 731 $chunk_size = max($tail - $head, 0); // found chunk size 732 $got = 0; 733 fseek($fp, $head); 734 while($got < $chunk_size && !feof($fp)) { 735 $tmp = @fread($fp, max($chunk_size - $got, 0)); 736 if($tmp === false) { //error state 737 break; 738 } 739 $got += strlen($tmp); 740 $chunk .= $tmp; 741 } 742 $lines = explode("\n", $chunk); 743 array_pop($lines); // remove trailing newline 744 return $lines; 745 } 746 747 /** 748 * Set pointer to first new line after $finger and return its position 749 * 750 * @param $fp resource filepointer 751 * @param $finger int a pointer 752 * @return int pointer 753 */ 754 protected function getNewlinepointer($fp, $finger) { 755 fseek($fp, $finger); 756 fgets($fp); // slip the finger forward to a new line 757 return ftell($fp); 758 } 759 760 /** 761 * Check whether given revision is the current page 762 * 763 * @param int $rev timestamp of current page 764 * @return bool true if $rev is current revision, otherwise false 765 */ 766 public function isCurrentRevision($rev){ 767 return isset($INFO['meta']['last_change']) && $rev == $INFO['meta']['last_change']['date']; 768 } 769} 770 771/** 772 * Get the changelog information for a specific page id 773 * and revision (timestamp). Adjacent changelog lines 774 * are optimistically parsed and cached to speed up 775 * consecutive calls to getRevisionInfo. For large 776 * changelog files, only the chunk containing the 777 * requested changelog line is read. 778 * 779 * @deprecated 20-11-2013 780 * 781 * @author Ben Coburn <btcoburn@silicodon.net> 782 * @author Kate Arzamastseva <pshns@ukr.net> 783 */ 784function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { 785 786 $log = new PageRevisionLog($id, $chunk_size); 787 return $log->getRevisionInfo($rev, $media); 788} 789 790/** 791 * Return a list of page revisions numbers 792 * Does not guarantee that the revision exists in the attic, 793 * only that a line with the date exists in the changelog. 794 * By default the current revision is skipped. 795 * 796 * id: the page of interest 797 * first: skip the first n changelog lines 798 * num: number of revisions to return 799 * 800 * The current revision is automatically skipped when the page exists. 801 * See $INFO['meta']['last_change'] for the current revision. 802 * 803 * For efficiency, the log lines are parsed and cached for later 804 * calls to getRevisionInfo. Large changelog files are read 805 * backwards in chunks until the requested number of changelog 806 * lines are recieved. 807 * 808 * @deprecated 20-11-2013 809 * 810 * @author Ben Coburn <btcoburn@silicodon.net> 811 * @author Kate Arzamastseva <pshns@ukr.net> 812 */ 813function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { 814 $log = new PageRevisionLog($id, $chunk_size); 815 return $log->getRevisions($first, $num, $media); 816} 817 818/** 819* Return an existing revision for a specific date which is 820* the current one or less or equal then the date 821* 822* @param string $id 823* @param number $date_at 824* @param boolean $media 825* @return string revision ('' for current) 826*/ 827function getProperRevision($id,$date_at,$media = false){ 828 $create_time = @filemtime($media?mediaFN($id):wikiFN($id)); 829 if(((int)$date_at) >= $create_time) { //requestet REV older then time($id) => load current 830 return ''; 831 } else { 832 $log = new PageRevisionLog($id); 833 if($rev = $log->getRelativeRevision($date_at+1, -1,$media)) { 834 return $rev; 835 } else { 836 return false; 837 } 838 } 839}