Release 2015-08-10 "Detritus"
[dokuwiki.git] / inc / changelog.php
blobf4731021ca48c63e14eae6d414eb02132cf72866
1 <?php
2 /**
3 * Changelog handling functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 // Constants for known core changelog line types.
10 // Use these in place of string literals for more readable code.
11 define('DOKU_CHANGE_TYPE_CREATE', 'C');
12 define('DOKU_CHANGE_TYPE_EDIT', 'E');
13 define('DOKU_CHANGE_TYPE_MINOR_EDIT', 'e');
14 define('DOKU_CHANGE_TYPE_DELETE', 'D');
15 define('DOKU_CHANGE_TYPE_REVERT', 'R');
17 /**
18 * parses a changelog line into it's components
20 * @author Ben Coburn <btcoburn@silicodon.net>
22 * @param string $line changelog line
23 * @return array|bool parsed line or false
25 function 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; }
40 /**
41 * Add's an entry to the changelog and saves the metadata for the page
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.
52 * @author Andreas Gohr <andi@splitbrain.org>
53 * @author Esther Brunner <wikidesign@gmail.com>
54 * @author Ben Coburn <btcoburn@silicodon.net>
56 function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){
57 global $conf, $INFO;
58 /** @var Input $INPUT */
59 global $INPUT;
61 // check for special flags as keys
62 if (!is_array($flags)) { $flags = array(); }
63 $flagExternalEdit = isset($flags['ExternalEdit']);
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);
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'):'';
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)
86 $wasCreated = ($type===DOKU_CHANGE_TYPE_CREATE);
87 $wasReverted = ($type===DOKU_CHANGE_TYPE_REVERT);
88 // update metadata
89 if (!$wasRemoved) {
90 $oldmeta = p_read_metadata($id);
91 $meta = array();
92 if ($wasCreated && empty($oldmeta['persistent']['date']['created'])){ // newly created
93 $meta['date']['created'] = $created;
94 if ($user){
95 $meta['creator'] = $INFO['userinfo']['name'];
96 $meta['user'] = $user;
98 } elseif (($wasCreated || $wasReverted) && !empty($oldmeta['persistent']['date']['created'])) { // re-created / restored
99 $meta['date']['created'] = $oldmeta['persistent']['date']['created'];
100 $meta['date']['modified'] = $created; // use the files ctime here
101 $meta['creator'] = $oldmeta['persistent']['creator'];
102 if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name'];
103 } elseif (!$minor) { // non-minor modification
104 $meta['date']['modified'] = $date;
105 if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name'];
107 $meta['last_change'] = $logline;
108 p_set_metadata($id, $meta);
111 // add changelog lines
112 $logline = implode("\t", $logline)."\n";
113 io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog
114 io_saveFile($conf['changelog'],$logline,true); //global changelog cache
118 * Add's an entry to the media changelog
120 * @author Michael Hamann <michael@content-space.de>
121 * @author Andreas Gohr <andi@splitbrain.org>
122 * @author Esther Brunner <wikidesign@gmail.com>
123 * @author Ben Coburn <btcoburn@silicodon.net>
125 * @param int $date Timestamp of the change
126 * @param String $id Name of the affected page
127 * @param String $type Type of the change see DOKU_CHANGE_TYPE_*
128 * @param String $summary Summary of the change
129 * @param mixed $extra In case of a revert the revision (timestmp) of the reverted page
130 * @param array $flags Additional flags in a key value array.
131 * Available flags:
132 * - (none, so far)
134 function addMediaLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){
135 global $conf;
136 /** @var Input $INPUT */
137 global $INPUT;
139 $id = cleanid($id);
141 if(!$date) $date = time(); //use current time if none supplied
142 $remote = clientIP(true);
143 $user = $INPUT->server->str('REMOTE_USER');
145 $strip = array("\t", "\n");
146 $logline = array(
147 'date' => $date,
148 'ip' => $remote,
149 'type' => str_replace($strip, '', $type),
150 'id' => $id,
151 'user' => $user,
152 'sum' => utf8_substr(str_replace($strip, '', $summary),0,255),
153 'extra' => str_replace($strip, '', $extra)
156 // add changelog lines
157 $logline = implode("\t", $logline)."\n";
158 io_saveFile($conf['media_changelog'],$logline,true); //global media changelog cache
159 io_saveFile(mediaMetaFN($id,'.changes'),$logline,true); //media file's changelog
163 * returns an array of recently changed files using the
164 * changelog
166 * The following constants can be used to control which changes are
167 * included. Add them together as needed.
169 * RECENTS_SKIP_DELETED - don't include deleted pages
170 * RECENTS_SKIP_MINORS - don't include minor changes
171 * RECENTS_SKIP_SUBSPACES - don't include subspaces
172 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes
173 * RECENTS_MEDIA_PAGES_MIXED - return both media changes and page changes
175 * @param int $first number of first entry returned (for paginating
176 * @param int $num return $num entries
177 * @param string $ns restrict to given namespace
178 * @param int $flags see above
179 * @return array recently changed files
181 * @author Ben Coburn <btcoburn@silicodon.net>
182 * @author Kate Arzamastseva <pshns@ukr.net>
184 function getRecents($first,$num,$ns='',$flags=0){
185 global $conf;
186 $recent = array();
187 $count = 0;
189 if(!$num)
190 return $recent;
192 // read all recent changes. (kept short)
193 if ($flags & RECENTS_MEDIA_CHANGES) {
194 $lines = @file($conf['media_changelog']);
195 } else {
196 $lines = @file($conf['changelog']);
198 $lines_position = count($lines)-1;
199 $media_lines_position = 0;
200 $media_lines = array();
202 if ($flags & RECENTS_MEDIA_PAGES_MIXED) {
203 $media_lines = @file($conf['media_changelog']);
204 $media_lines_position = count($media_lines)-1;
207 $seen = array(); // caches seen lines, _handleRecent() skips them
209 // handle lines
210 while ($lines_position >= 0 || (($flags & RECENTS_MEDIA_PAGES_MIXED) && $media_lines_position >=0)) {
211 if (empty($rec) && $lines_position >= 0) {
212 $rec = _handleRecent(@$lines[$lines_position], $ns, $flags, $seen);
213 if (!$rec) {
214 $lines_position --;
215 continue;
218 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && empty($media_rec) && $media_lines_position >= 0) {
219 $media_rec = _handleRecent(@$media_lines[$media_lines_position], $ns, $flags | RECENTS_MEDIA_CHANGES, $seen);
220 if (!$media_rec) {
221 $media_lines_position --;
222 continue;
225 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && @$media_rec['date'] >= @$rec['date']) {
226 $media_lines_position--;
227 $x = $media_rec;
228 $x['media'] = true;
229 $media_rec = false;
230 } else {
231 $lines_position--;
232 $x = $rec;
233 if ($flags & RECENTS_MEDIA_CHANGES) $x['media'] = true;
234 $rec = false;
236 if(--$first >= 0) continue; // skip first entries
237 $recent[] = $x;
238 $count++;
239 // break when we have enough entries
240 if($count >= $num){ break; }
242 return $recent;
246 * returns an array of files changed since a given time using the
247 * changelog
249 * The following constants can be used to control which changes are
250 * included. Add them together as needed.
252 * RECENTS_SKIP_DELETED - don't include deleted pages
253 * RECENTS_SKIP_MINORS - don't include minor changes
254 * RECENTS_SKIP_SUBSPACES - don't include subspaces
255 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes
257 * @param int $from date of the oldest entry to return
258 * @param int $to date of the newest entry to return (for pagination, optional)
259 * @param string $ns restrict to given namespace (optional)
260 * @param int $flags see above (optional)
261 * @return array of files
263 * @author Michael Hamann <michael@content-space.de>
264 * @author Ben Coburn <btcoburn@silicodon.net>
266 function getRecentsSince($from,$to=null,$ns='',$flags=0){
267 global $conf;
268 $recent = array();
270 if($to && $to < $from)
271 return $recent;
273 // read all recent changes. (kept short)
274 if ($flags & RECENTS_MEDIA_CHANGES) {
275 $lines = @file($conf['media_changelog']);
276 } else {
277 $lines = @file($conf['changelog']);
279 if(!$lines) return $recent;
281 // we start searching at the end of the list
282 $lines = array_reverse($lines);
284 // handle lines
285 $seen = array(); // caches seen lines, _handleRecent() skips them
287 foreach($lines as $line){
288 $rec = _handleRecent($line, $ns, $flags, $seen);
289 if($rec !== false) {
290 if ($rec['date'] >= $from) {
291 if (!$to || $rec['date'] <= $to) {
292 $recent[] = $rec;
294 } else {
295 break;
300 return array_reverse($recent);
304 * Internal function used by getRecents
306 * don't call directly
308 * @see getRecents()
309 * @author Andreas Gohr <andi@splitbrain.org>
310 * @author Ben Coburn <btcoburn@silicodon.net>
312 * @param string $line changelog line
313 * @param string $ns restrict to given namespace
314 * @param int $flags flags to control which changes are included
315 * @param array $seen listing of seen pages
316 * @return array|bool false or array with info about a change
318 function _handleRecent($line,$ns,$flags,&$seen){
319 if(empty($line)) return false; //skip empty lines
321 // split the line into parts
322 $recent = parseChangelogLine($line);
323 if ($recent===false) { return false; }
325 // skip seen ones
326 if(isset($seen[$recent['id']])) return false;
328 // skip minors
329 if($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false;
331 // remember in seen to skip additional sights
332 $seen[$recent['id']] = 1;
334 // check if it's a hidden page
335 if(isHiddenPage($recent['id'])) return false;
337 // filter namespace
338 if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false;
340 // exclude subnamespaces
341 if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false;
343 // check ACL
344 if ($flags & RECENTS_MEDIA_CHANGES) {
345 $recent['perms'] = auth_quickaclcheck(getNS($recent['id']).':*');
346 } else {
347 $recent['perms'] = auth_quickaclcheck($recent['id']);
349 if ($recent['perms'] < AUTH_READ) return false;
351 // check existance
352 if($flags & RECENTS_SKIP_DELETED){
353 $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id']));
354 if(!file_exists($fn)) return false;
357 return $recent;
361 * Class ChangeLog
362 * methods for handling of changelog of pages or media files
364 abstract class ChangeLog {
366 /** @var string */
367 protected $id;
368 /** @var int */
369 protected $chunk_size;
370 /** @var array */
371 protected $cache;
374 * Constructor
376 * @param string $id page id
377 * @param int $chunk_size maximum block size read from file
379 public function __construct($id, $chunk_size = 8192) {
380 global $cache_revinfo;
382 $this->cache =& $cache_revinfo;
383 if(!isset($this->cache[$id])) {
384 $this->cache[$id] = array();
387 $this->id = $id;
388 $this->setChunkSize($chunk_size);
393 * Set chunk size for file reading
394 * Chunk size zero let read whole file at once
396 * @param int $chunk_size maximum block size read from file
398 public function setChunkSize($chunk_size) {
399 if(!is_numeric($chunk_size)) $chunk_size = 0;
401 $this->chunk_size = (int) max($chunk_size, 0);
405 * Returns path to changelog
407 * @return string path to file
409 abstract protected function getChangelogFilename();
412 * Returns path to current page/media
414 * @return string path to file
416 abstract protected function getFilename();
419 * Get the changelog information for a specific page id and revision (timestamp)
421 * Adjacent changelog lines are optimistically parsed and cached to speed up
422 * consecutive calls to getRevisionInfo. For large changelog files, only the chunk
423 * containing the requested changelog line is read.
425 * @param int $rev revision timestamp
426 * @return bool|array false or array with entries:
427 * - date: unix timestamp
428 * - ip: IPv4 address (127.0.0.1)
429 * - type: log line type
430 * - id: page id
431 * - user: user name
432 * - sum: edit summary (or action reason)
433 * - extra: extra data (varies by line type)
435 * @author Ben Coburn <btcoburn@silicodon.net>
436 * @author Kate Arzamastseva <pshns@ukr.net>
438 public function getRevisionInfo($rev) {
439 $rev = max($rev, 0);
441 // check if it's already in the memory cache
442 if(isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) {
443 return $this->cache[$this->id][$rev];
446 //read lines from changelog
447 list($fp, $lines) = $this->readloglines($rev);
448 if($fp) {
449 fclose($fp);
451 if(empty($lines)) return false;
453 // parse and cache changelog lines
454 foreach($lines as $value) {
455 $tmp = parseChangelogLine($value);
456 if($tmp !== false) {
457 $this->cache[$this->id][$tmp['date']] = $tmp;
460 if(!isset($this->cache[$this->id][$rev])) {
461 return false;
463 return $this->cache[$this->id][$rev];
467 * Return a list of page revisions numbers
469 * Does not guarantee that the revision exists in the attic,
470 * only that a line with the date exists in the changelog.
471 * By default the current revision is skipped.
473 * The current revision is automatically skipped when the page exists.
474 * See $INFO['meta']['last_change'] for the current revision.
475 * A negative $first let read the current revision too.
477 * For efficiency, the log lines are parsed and cached for later
478 * calls to getRevisionInfo. Large changelog files are read
479 * backwards in chunks until the requested number of changelog
480 * lines are recieved.
482 * @param int $first skip the first n changelog lines
483 * @param int $num number of revisions to return
484 * @return array with the revision timestamps
486 * @author Ben Coburn <btcoburn@silicodon.net>
487 * @author Kate Arzamastseva <pshns@ukr.net>
489 public function getRevisions($first, $num) {
490 $revs = array();
491 $lines = array();
492 $count = 0;
494 $num = max($num, 0);
495 if($num == 0) {
496 return $revs;
499 if($first < 0) {
500 $first = 0;
501 } else if(file_exists($this->getFilename())) {
502 // skip current revision if the page exists
503 $first = max($first + 1, 0);
506 $file = $this->getChangelogFilename();
508 if(!file_exists($file)) {
509 return $revs;
511 if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
512 // read whole file
513 $lines = file($file);
514 if($lines === false) {
515 return $revs;
517 } else {
518 // read chunks backwards
519 $fp = fopen($file, 'rb'); // "file pointer"
520 if($fp === false) {
521 return $revs;
523 fseek($fp, 0, SEEK_END);
524 $tail = ftell($fp);
526 // chunk backwards
527 $finger = max($tail - $this->chunk_size, 0);
528 while($count < $num + $first) {
529 $nl = $this->getNewlinepointer($fp, $finger);
531 // was the chunk big enough? if not, take another bite
532 if($nl > 0 && $tail <= $nl) {
533 $finger = max($finger - $this->chunk_size, 0);
534 continue;
535 } else {
536 $finger = $nl;
539 // read chunk
540 $chunk = '';
541 $read_size = max($tail - $finger, 0); // found chunk size
542 $got = 0;
543 while($got < $read_size && !feof($fp)) {
544 $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0));
545 if($tmp === false) {
546 break;
547 } //error state
548 $got += strlen($tmp);
549 $chunk .= $tmp;
551 $tmp = explode("\n", $chunk);
552 array_pop($tmp); // remove trailing newline
554 // combine with previous chunk
555 $count += count($tmp);
556 $lines = array_merge($tmp, $lines);
558 // next chunk
559 if($finger == 0) {
560 break;
561 } // already read all the lines
562 else {
563 $tail = $finger;
564 $finger = max($tail - $this->chunk_size, 0);
567 fclose($fp);
570 // skip parsing extra lines
571 $num = max(min(count($lines) - $first, $num), 0);
572 if ($first > 0 && $num > 0) { $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num); }
573 else if($first > 0 && $num == 0) { $lines = array_slice($lines, 0, max(count($lines) - $first, 0)); }
574 else if($first == 0 && $num > 0) { $lines = array_slice($lines, max(count($lines) - $num, 0)); }
576 // handle lines in reverse order
577 for($i = count($lines) - 1; $i >= 0; $i--) {
578 $tmp = parseChangelogLine($lines[$i]);
579 if($tmp !== false) {
580 $this->cache[$this->id][$tmp['date']] = $tmp;
581 $revs[] = $tmp['date'];
585 return $revs;
589 * Get the nth revision left or right handside for a specific page id and revision (timestamp)
591 * For large changelog files, only the chunk containing the
592 * reference revision $rev is read and sometimes a next chunck.
594 * Adjacent changelog lines are optimistically parsed and cached to speed up
595 * consecutive calls to getRevisionInfo.
597 * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber)
598 * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev
599 * @return bool|int
600 * timestamp of the requested revision
601 * otherwise false
603 public function getRelativeRevision($rev, $direction) {
604 $rev = max($rev, 0);
605 $direction = (int) $direction;
607 //no direction given or last rev, so no follow-up
608 if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) {
609 return false;
612 //get lines from changelog
613 list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev);
614 if(empty($lines)) return false;
616 // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached
617 // also parse and cache changelog lines for getRevisionInfo().
618 $revcounter = 0;
619 $relativerev = false;
620 $checkotherchunck = true; //always runs once
621 while(!$relativerev && $checkotherchunck) {
622 $tmp = array();
623 //parse in normal or reverse order
624 $count = count($lines);
625 if($direction > 0) {
626 $start = 0;
627 $step = 1;
628 } else {
629 $start = $count - 1;
630 $step = -1;
632 for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) {
633 $tmp = parseChangelogLine($lines[$i]);
634 if($tmp !== false) {
635 $this->cache[$this->id][$tmp['date']] = $tmp;
636 //look for revs older/earlier then reference $rev and select $direction-th one
637 if(($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) {
638 $revcounter++;
639 if($revcounter == abs($direction)) {
640 $relativerev = $tmp['date'];
646 //true when $rev is found, but not the wanted follow-up.
647 $checkotherchunck = $fp
648 && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev))
649 && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0));
651 if($checkotherchunck) {
652 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction);
654 if(empty($lines)) break;
657 if($fp) {
658 fclose($fp);
661 return $relativerev;
665 * Returns revisions around rev1 and rev2
666 * When available it returns $max entries for each revision
668 * @param int $rev1 oldest revision timestamp
669 * @param int $rev2 newest revision timestamp (0 looks up last revision)
670 * @param int $max maximum number of revisions returned
671 * @return array with two arrays with revisions surrounding rev1 respectively rev2
673 public function getRevisionsAround($rev1, $rev2, $max = 50) {
674 $max = floor(abs($max) / 2)*2 + 1;
675 $rev1 = max($rev1, 0);
676 $rev2 = max($rev2, 0);
678 if($rev2) {
679 if($rev2 < $rev1) {
680 $rev = $rev2;
681 $rev2 = $rev1;
682 $rev1 = $rev;
684 } else {
685 //empty right side means a removed page. Look up last revision.
686 $revs = $this->getRevisions(-1, 1);
687 $rev2 = $revs[0];
689 //collect revisions around rev2
690 list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max);
692 if(empty($revs2)) return array(array(), array());
694 //collect revisions around rev1
695 $index = array_search($rev1, $allrevs);
696 if($index === false) {
697 //no overlapping revisions
698 list($revs1,,,,,) = $this->retrieveRevisionsAround($rev1, $max);
699 if(empty($revs1)) $revs1 = array();
700 } else {
701 //revisions overlaps, reuse revisions around rev2
702 $revs1 = $allrevs;
703 while($head > 0) {
704 for($i = count($lines) - 1; $i >= 0; $i--) {
705 $tmp = parseChangelogLine($lines[$i]);
706 if($tmp !== false) {
707 $this->cache[$this->id][$tmp['date']] = $tmp;
708 $revs1[] = $tmp['date'];
709 $index++;
711 if($index > floor($max / 2)) break 2;
715 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1);
717 sort($revs1);
718 //return wanted selection
719 $revs1 = array_slice($revs1, max($index - floor($max/2), 0), $max);
722 return array(array_reverse($revs1), array_reverse($revs2));
726 * Returns lines from changelog.
727 * If file larger than $chuncksize, only chunck is read that could contain $rev.
729 * @param int $rev revision timestamp
730 * @return array|false
731 * if success returns array(fp, array(changeloglines), $head, $tail, $eof)
732 * where fp only defined for chuck reading, needs closing.
733 * otherwise false
735 protected function readloglines($rev) {
736 $file = $this->getChangelogFilename();
738 if(!file_exists($file)) {
739 return false;
742 $fp = null;
743 $head = 0;
744 $tail = 0;
745 $eof = 0;
747 if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) {
748 // read whole file
749 $lines = file($file);
750 if($lines === false) {
751 return false;
753 } else {
754 // read by chunk
755 $fp = fopen($file, 'rb'); // "file pointer"
756 if($fp === false) {
757 return false;
759 $head = 0;
760 fseek($fp, 0, SEEK_END);
761 $eof = ftell($fp);
762 $tail = $eof;
764 // find chunk
765 while($tail - $head > $this->chunk_size) {
766 $finger = $head + floor(($tail - $head) / 2.0);
767 $finger = $this->getNewlinepointer($fp, $finger);
768 $tmp = fgets($fp);
769 if($finger == $head || $finger == $tail) {
770 break;
772 $tmp = parseChangelogLine($tmp);
773 $finger_rev = $tmp['date'];
775 if($finger_rev > $rev) {
776 $tail = $finger;
777 } else {
778 $head = $finger;
782 if($tail - $head < 1) {
783 // cound not find chunk, assume requested rev is missing
784 fclose($fp);
785 return false;
788 $lines = $this->readChunk($fp, $head, $tail);
790 return array(
791 $fp,
792 $lines,
793 $head,
794 $tail,
795 $eof
800 * Read chunk and return array with lines of given chunck.
801 * Has no check if $head and $tail are really at a new line
803 * @param resource $fp resource filepointer
804 * @param int $head start point chunck
805 * @param int $tail end point chunck
806 * @return array lines read from chunck
808 protected function readChunk($fp, $head, $tail) {
809 $chunk = '';
810 $chunk_size = max($tail - $head, 0); // found chunk size
811 $got = 0;
812 fseek($fp, $head);
813 while($got < $chunk_size && !feof($fp)) {
814 $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0));
815 if($tmp === false) { //error state
816 break;
818 $got += strlen($tmp);
819 $chunk .= $tmp;
821 $lines = explode("\n", $chunk);
822 array_pop($lines); // remove trailing newline
823 return $lines;
827 * Set pointer to first new line after $finger and return its position
829 * @param resource $fp filepointer
830 * @param int $finger a pointer
831 * @return int pointer
833 protected function getNewlinepointer($fp, $finger) {
834 fseek($fp, $finger);
835 $nl = $finger;
836 if($finger > 0) {
837 fgets($fp); // slip the finger forward to a new line
838 $nl = ftell($fp);
840 return $nl;
844 * Check whether given revision is the current page
846 * @param int $rev timestamp of current page
847 * @return bool true if $rev is current revision, otherwise false
849 public function isCurrentRevision($rev) {
850 return $rev == @filemtime($this->getFilename());
854 * Return an existing revision for a specific date which is
855 * the current one or younger or equal then the date
857 * @param number $date_at timestamp
858 * @return string revision ('' for current)
860 function getLastRevisionAt($date_at){
861 //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current
862 if($date_at >= @filemtime($this->getFilename())) {
863 return '';
864 } else if ($rev = $this->getRelativeRevision($date_at+1, -1)) { //+1 to get also the requested date revision
865 return $rev;
866 } else {
867 return false;
872 * Returns the next lines of the changelog of the chunck before head or after tail
874 * @param resource $fp filepointer
875 * @param int $head position head of last chunk
876 * @param int $tail position tail of last chunk
877 * @param int $direction positive forward, negative backward
878 * @return array with entries:
879 * - $lines: changelog lines of readed chunk
880 * - $head: head of chunk
881 * - $tail: tail of chunk
883 protected function readAdjacentChunk($fp, $head, $tail, $direction) {
884 if(!$fp) return array(array(), $head, $tail);
886 if($direction > 0) {
887 //read forward
888 $head = $tail;
889 $tail = $head + floor($this->chunk_size * (2 / 3));
890 $tail = $this->getNewlinepointer($fp, $tail);
891 } else {
892 //read backward
893 $tail = $head;
894 $head = max($tail - $this->chunk_size, 0);
895 while(true) {
896 $nl = $this->getNewlinepointer($fp, $head);
897 // was the chunk big enough? if not, take another bite
898 if($nl > 0 && $tail <= $nl) {
899 $head = max($head - $this->chunk_size, 0);
900 } else {
901 $head = $nl;
902 break;
907 //load next chunck
908 $lines = $this->readChunk($fp, $head, $tail);
909 return array($lines, $head, $tail);
913 * Collect the $max revisions near to the timestamp $rev
915 * @param int $rev revision timestamp
916 * @param int $max maximum number of revisions to be returned
917 * @return bool|array
918 * return array with entries:
919 * - $requestedrevs: array of with $max revision timestamps
920 * - $revs: all parsed revision timestamps
921 * - $fp: filepointer only defined for chuck reading, needs closing.
922 * - $lines: non-parsed changelog lines before the parsed revisions
923 * - $head: position of first readed changelogline
924 * - $lasttail: position of end of last readed changelogline
925 * otherwise false
927 protected function retrieveRevisionsAround($rev, $max) {
928 //get lines from changelog
929 list($fp, $lines, $starthead, $starttail, /* $eof */) = $this->readloglines($rev);
930 if(empty($lines)) return false;
932 //parse chunk containing $rev, and read forward more chunks until $max/2 is reached
933 $head = $starthead;
934 $tail = $starttail;
935 $revs = array();
936 $aftercount = $beforecount = 0;
937 while(count($lines) > 0) {
938 foreach($lines as $line) {
939 $tmp = parseChangelogLine($line);
940 if($tmp !== false) {
941 $this->cache[$this->id][$tmp['date']] = $tmp;
942 $revs[] = $tmp['date'];
943 if($tmp['date'] >= $rev) {
944 //count revs after reference $rev
945 $aftercount++;
946 if($aftercount == 1) $beforecount = count($revs);
948 //enough revs after reference $rev?
949 if($aftercount > floor($max / 2)) break 2;
952 //retrieve next chunk
953 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1);
955 if($aftercount == 0) return false;
957 $lasttail = $tail;
959 //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max
960 $lines = array();
961 $i = 0;
962 if($aftercount > 0) {
963 $head = $starthead;
964 $tail = $starttail;
965 while($head > 0) {
966 list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1);
968 for($i = count($lines) - 1; $i >= 0; $i--) {
969 $tmp = parseChangelogLine($lines[$i]);
970 if($tmp !== false) {
971 $this->cache[$this->id][$tmp['date']] = $tmp;
972 $revs[] = $tmp['date'];
973 $beforecount++;
974 //enough revs before reference $rev?
975 if($beforecount > max(floor($max / 2), $max - $aftercount)) break 2;
980 sort($revs);
982 //keep only non-parsed lines
983 $lines = array_slice($lines, 0, $i);
984 //trunk desired selection
985 $requestedrevs = array_slice($revs, -$max, $max);
987 return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail);
992 * Class PageChangelog handles changelog of a wiki page
994 class PageChangelog extends ChangeLog {
997 * Returns path to changelog
999 * @return string path to file
1001 protected function getChangelogFilename() {
1002 return metaFN($this->id, '.changes');
1006 * Returns path to current page/media
1008 * @return string path to file
1010 protected function getFilename() {
1011 return wikiFN($this->id);
1016 * Class MediaChangelog handles changelog of a media file
1018 class MediaChangelog extends ChangeLog {
1021 * Returns path to changelog
1023 * @return string path to file
1025 protected function getChangelogFilename() {
1026 return mediaMetaFN($this->id, '.changes');
1030 * Returns path to current page/media
1032 * @return string path to file
1034 protected function getFilename() {
1035 return mediaFN($this->id);
1040 * Get the changelog information for a specific page id
1041 * and revision (timestamp). Adjacent changelog lines
1042 * are optimistically parsed and cached to speed up
1043 * consecutive calls to getRevisionInfo. For large
1044 * changelog files, only the chunk containing the
1045 * requested changelog line is read.
1047 * @deprecated 2013-11-20
1049 * @author Ben Coburn <btcoburn@silicodon.net>
1050 * @author Kate Arzamastseva <pshns@ukr.net>
1052 * @param string $id
1053 * @param int $rev
1054 * @param int $chunk_size
1055 * @param bool $media
1056 * @return array|bool
1058 function getRevisionInfo($id, $rev, $chunk_size = 8192, $media = false) {
1059 dbg_deprecated('class PageChangeLog or class MediaChangelog');
1060 if($media) {
1061 $changelog = new MediaChangeLog($id, $chunk_size);
1062 } else {
1063 $changelog = new PageChangeLog($id, $chunk_size);
1065 return $changelog->getRevisionInfo($rev);
1069 * Return a list of page revisions numbers
1070 * Does not guarantee that the revision exists in the attic,
1071 * only that a line with the date exists in the changelog.
1072 * By default the current revision is skipped.
1074 * The current revision is automatically skipped when the page exists.
1075 * See $INFO['meta']['last_change'] for the current revision.
1077 * For efficiency, the log lines are parsed and cached for later
1078 * calls to getRevisionInfo. Large changelog files are read
1079 * backwards in chunks until the requested number of changelog
1080 * lines are recieved.
1082 * @deprecated 2013-11-20
1084 * @author Ben Coburn <btcoburn@silicodon.net>
1085 * @author Kate Arzamastseva <pshns@ukr.net>
1087 * @param string $id the page of interest
1088 * @param int $first skip the first n changelog lines
1089 * @param int $num number of revisions to return
1090 * @param int $chunk_size
1091 * @param bool $media
1092 * @return array
1094 function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false) {
1095 dbg_deprecated('class PageChangeLog or class MediaChangelog');
1096 if($media) {
1097 $changelog = new MediaChangeLog($id, $chunk_size);
1098 } else {
1099 $changelog = new PageChangeLog($id, $chunk_size);
1101 return $changelog->getRevisions($first, $num);