3 * Changelog handling functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
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');
18 * parses a changelog line into it's components
20 * @author Ben Coburn <btcoburn@silicodon.net>
22 function parseChangelogLine($line) {
23 $tmp = explode("\t", $line);
24 if ($tmp!==false && count($tmp)>1) {
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)
34 } else { return false; }
38 * Add's an entry to the changelog and saves the metadata for the page
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.
47 * - ExternalEdit - mark as an external edit.
49 * @author Andreas Gohr <andi@splitbrain.org>
50 * @author Esther Brunner <wikidesign@gmail.com>
51 * @author Ben Coburn <btcoburn@silicodon.net>
53 function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){
56 // check for special flags as keys
57 if (!is_array($flags)) { $flags = array(); }
58 $flagExternalEdit = isset($flags['ExternalEdit']);
62 $created = @filectime($file);
63 $minor = ($type===DOKU_CHANGE_TYPE_MINOR_EDIT);
64 $wasRemoved = ($type===DOKU_CHANGE_TYPE_DELETE);
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']:'';
70 $strip = array("\t", "\n");
74 'type' => str_replace($strip, '', $type),
77 'sum' => utf8_substr(str_replace($strip, '', $summary),0,255),
78 'extra' => str_replace($strip, '', $extra)
83 $oldmeta = p_read_metadata($id);
85 if (!$INFO['exists'] && empty($oldmeta['persistent']['date']['created'])){ // newly created
86 $meta['date']['created'] = $created;
88 $meta['creator'] = $INFO['userinfo']['name'];
89 $meta['user'] = $user;
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'];
100 $meta['last_change'] = $logline;
101 p_set_metadata($id, $meta);
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
111 * Add's an entry to the media changelog
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>
118 function addMediaLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){
123 if(!$date) $date = time(); //use current time if none supplied
124 $remote = clientIP(true);
125 $user = $_SERVER['REMOTE_USER'];
127 $strip = array("\t", "\n");
131 'type' => str_replace($strip, '', $type),
134 'sum' => utf8_substr(str_replace($strip, '', $summary),0,255),
135 'extra' => str_replace($strip, '', $extra)
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
145 * returns an array of recently changed files using the
148 * The following constants can be used to control which changes are
149 * included. Add them together as needed.
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
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 bool $flags see above
162 * @author Ben Coburn <btcoburn@silicodon.net>
163 * @author Kate Arzamastseva <pshns@ukr.net>
165 function getRecents($first,$num,$ns='',$flags=0){
173 // read all recent changes. (kept short)
174 if ($flags & RECENTS_MEDIA_CHANGES) {
175 $lines = @file($conf['media_changelog']);
177 $lines = @file($conf['changelog']);
179 $lines_position = count($lines)-1;
181 if ($flags & RECENTS_MEDIA_PAGES_MIXED) {
182 $media_lines = @file($conf['media_changelog']);
183 $media_lines_position = count($media_lines)-1;
186 $seen = array(); // caches seen lines, _handleRecent() skips them
189 while ($lines_position >= 0 || (($flags & RECENTS_MEDIA_PAGES_MIXED) && $media_lines_position >=0)) {
190 if (empty($rec) && $lines_position >= 0) {
191 $rec = _handleRecent(@$lines[$lines_position], $ns, $flags & ~RECENTS_MEDIA_CHANGES, $seen);
197 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && empty($media_rec) && $media_lines_position >= 0) {
198 $media_rec = _handleRecent(@$media_lines[$media_lines_position], $ns, $flags | RECENTS_MEDIA_CHANGES, $seen);
200 $media_lines_position --;
204 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && @$media_rec['date'] >= @$rec['date']) {
205 $media_lines_position--;
212 if ($flags & RECENTS_MEDIA_CHANGES) $x['media'] = true;
215 if(--$first >= 0) continue; // skip first entries
218 // break when we have enough entries
219 if($count >= $num){ break; }
225 * returns an array of files changed since a given time using the
228 * The following constants can be used to control which changes are
229 * included. Add them together as needed.
231 * RECENTS_SKIP_DELETED - don't include deleted pages
232 * RECENTS_SKIP_MINORS - don't include minor changes
233 * RECENTS_SKIP_SUBSPACES - don't include subspaces
234 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes
236 * @param int $from date of the oldest entry to return
237 * @param int $to date of the newest entry to return (for pagination, optional)
238 * @param string $ns restrict to given namespace (optional)
239 * @param bool $flags see above (optional)
241 * @author Michael Hamann <michael@content-space.de>
242 * @author Ben Coburn <btcoburn@silicodon.net>
244 function getRecentsSince($from,$to=null,$ns='',$flags=0){
248 if($to && $to < $from)
251 // read all recent changes. (kept short)
252 if ($flags & RECENTS_MEDIA_CHANGES) {
253 $lines = @file($conf['media_changelog']);
255 $lines = @file($conf['changelog']);
258 // we start searching at the end of the list
259 $lines = array_reverse($lines);
262 $seen = array(); // caches seen lines, _handleRecent() skips them
264 foreach($lines as $line){
265 $rec = _handleRecent($line, $ns, $flags, $seen);
267 if ($rec['date'] >= $from) {
268 if (!$to || $rec['date'] <= $to) {
277 return array_reverse($recent);
281 * Internal function used by getRecents
283 * don't call directly
286 * @author Andreas Gohr <andi@splitbrain.org>
287 * @author Ben Coburn <btcoburn@silicodon.net>
289 function _handleRecent($line,$ns,$flags,&$seen){
290 if(empty($line)) return false; //skip empty lines
292 // split the line into parts
293 $recent = parseChangelogLine($line);
294 if ($recent===false) { return false; }
297 if(isset($seen[$recent['id']])) return false;
300 if($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false;
302 // remember in seen to skip additional sights
303 $seen[$recent['id']] = 1;
305 // check if it's a hidden page
306 if(isHiddenPage($recent['id'])) return false;
309 if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false;
311 // exclude subnamespaces
312 if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false;
315 if ($flags & RECENTS_MEDIA_CHANGES) {
316 $recent['perms'] = auth_quickaclcheck(getNS($recent['id']).':*');
318 $recent['perms'] = auth_quickaclcheck($recent['id']);
320 if ($recent['perms'] < AUTH_READ) return false;
323 $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id']));
324 if((!@file_exists($fn)) && ($flags & RECENTS_SKIP_DELETED)) return false;
330 * Get the changelog information for a specific page id
331 * and revision (timestamp). Adjacent changelog lines
332 * are optimistically parsed and cached to speed up
333 * consecutive calls to getRevisionInfo. For large
334 * changelog files, only the chunk containing the
335 * requested changelog line is read.
337 * @author Ben Coburn <btcoburn@silicodon.net>
338 * @author Kate Arzamastseva <pshns@ukr.net>
340 function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) {
341 global $cache_revinfo;
342 $cache =& $cache_revinfo;
343 if (!isset($cache[$id])) { $cache[$id] = array(); }
346 // check if it's already in the memory cache
347 if (isset($cache[$id]) && isset($cache[$id][$rev])) {
348 return $cache[$id][$rev];
352 $file = mediaMetaFN($id, '.changes');
354 $file = metaFN($id, '.changes');
356 if (!@file_exists($file)) { return false; }
357 if (filesize($file)<$chunk_size || $chunk_size==0) {
359 $lines = file($file);
360 if ($lines===false) { return false; }
363 $fp = fopen($file, 'rb'); // "file pointer"
364 if ($fp===false) { return false; }
366 fseek($fp, 0, SEEK_END);
372 while ($tail-$head>$chunk_size) {
373 $finger = $head+floor(($tail-$head)/2.0);
375 fgets($fp); // slip the finger forward to a new line
376 $finger = ftell($fp);
377 $tmp = fgets($fp); // then read at that location
378 $tmp = parseChangelogLine($tmp);
379 $finger_rev = $tmp['date'];
380 if ($finger==$head || $finger==$tail) { break; }
381 if ($finger_rev>$rev) {
389 // cound not find chunk, assume requested rev is missing
396 $chunk_size = max($tail-$head, 0); // found chunk size
399 while ($got<$chunk_size && !feof($fp)) {
400 $tmp = @fread($fp, max($chunk_size-$got, 0));
401 if ($tmp===false) { break; } //error state
402 $got += strlen($tmp);
405 $lines = explode("\n", $chunk);
406 array_pop($lines); // remove trailing newline
410 // parse and cache changelog lines
411 foreach ($lines as $value) {
412 $tmp = parseChangelogLine($value);
414 $cache[$id][$tmp['date']] = $tmp;
417 if (!isset($cache[$id][$rev])) { return false; }
418 return $cache[$id][$rev];
422 * Return a list of page revisions numbers
423 * Does not guarantee that the revision exists in the attic,
424 * only that a line with the date exists in the changelog.
425 * By default the current revision is skipped.
427 * id: the page of interest
428 * first: skip the first n changelog lines
429 * num: number of revisions to return
431 * The current revision is automatically skipped when the page exists.
432 * See $INFO['meta']['last_change'] for the current revision.
434 * For efficiency, the log lines are parsed and cached for later
435 * calls to getRevisionInfo. Large changelog files are read
436 * backwards in chunks until the requested number of changelog
437 * lines are recieved.
439 * @author Ben Coburn <btcoburn@silicodon.net>
440 * @author Kate Arzamastseva <pshns@ukr.net>
442 function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) {
443 global $cache_revinfo;
444 $cache =& $cache_revinfo;
445 if (!isset($cache[$id])) { $cache[$id] = array(); }
451 $file = mediaMetaFN($id, '.changes');
453 $file = metaFN($id, '.changes');
456 $chunk_size = max($chunk_size, 0);
459 } else if (!$media && @file_exists(wikiFN($id)) || $media && @file_exists(mediaFN($id))) {
460 // skip current revision if the page exists
461 $first = max($first+1, 0);
464 if (!@file_exists($file)) { return $revs; }
465 if (filesize($file)<$chunk_size || $chunk_size==0) {
467 $lines = file($file);
468 if ($lines===false) { return $revs; }
470 // read chunks backwards
471 $fp = fopen($file, 'rb'); // "file pointer"
472 if ($fp===false) { return $revs; }
473 fseek($fp, 0, SEEK_END);
477 $finger = max($tail-$chunk_size, 0);
478 while ($count<$num+$first) {
482 fgets($fp); // slip the finger forward to a new line
486 // was the chunk big enough? if not, take another bite
487 if($nl > 0 && $tail <= $nl){
488 $finger = max($finger-$chunk_size, 0);
496 $read_size = max($tail-$finger, 0); // found chunk size
498 while ($got<$read_size && !feof($fp)) {
499 $tmp = @fread($fp, max($read_size-$got, 0));
500 if ($tmp===false) { break; } //error state
501 $got += strlen($tmp);
504 $tmp = explode("\n", $chunk);
505 array_pop($tmp); // remove trailing newline
507 // combine with previous chunk
508 $count += count($tmp);
509 $lines = array_merge($tmp, $lines);
512 if ($finger==0) { break; } // already read all the lines
515 $finger = max($tail-$chunk_size, 0);
521 // skip parsing extra lines
522 $num = max(min(count($lines)-$first, $num), 0);
523 if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); }
524 else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); }
525 else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); }
527 // handle lines in reverse order
528 for ($i = count($lines)-1; $i >= 0; $i--) {
529 $tmp = parseChangelogLine($lines[$i]);
531 $cache[$id][$tmp['date']] = $tmp;
532 $revs[] = $tmp['date'];