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' => str_replace($strip, '', $summary), 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' => str_replace($strip, '', $summary), 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 bool $flags see above 161 * 162 * @author Ben Coburn <btcoburn@silicodon.net> 163 * @author Kate Arzamastseva <pshns@ukr.net> 164 */ 165function getRecents($first,$num,$ns='',$flags=0){ 166 global $conf; 167 $recent = array(); 168 $count = 0; 169 170 if(!$num) 171 return $recent; 172 173 // read all recent changes. (kept short) 174 if ($flags & RECENTS_MEDIA_CHANGES) { 175 $lines = @file($conf['media_changelog']); 176 } else { 177 $lines = @file($conf['changelog']); 178 } 179 $lines_position = count($lines)-1; 180 181 if ($flags & RECENTS_MEDIA_PAGES_MIXED) { 182 $media_lines = @file($conf['media_changelog']); 183 $media_lines_position = count($media_lines)-1; 184 } 185 186 $seen = array(); // caches seen lines, _handleRecent() skips them 187 188 // handle lines 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, $seen); 192 if (!$rec) { 193 $lines_position --; 194 continue; 195 } 196 } 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, $seen); 199 if (!$media_rec) { 200 $media_lines_position --; 201 continue; 202 } 203 } 204 if (($flags & RECENTS_MEDIA_PAGES_MIXED) && @$media_rec['date'] >= @$rec['date']) { 205 $media_lines_position--; 206 $x = $media_rec; 207 $x['media'] = true; 208 $media_rec = false; 209 } else { 210 $lines_position--; 211 $x = $rec; 212 if ($flags & RECENTS_MEDIA_CHANGES) $x['media'] = true; 213 $rec = false; 214 } 215 if(--$first >= 0) continue; // skip first entries 216 $recent[] = $x; 217 $count++; 218 // break when we have enough entries 219 if($count >= $num){ break; } 220 } 221 return $recent; 222} 223 224/** 225 * returns an array of files changed since a given time using the 226 * changelog 227 * 228 * The following constants can be used to control which changes are 229 * included. Add them together as needed. 230 * 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 235 * 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) 240 * 241 * @author Michael Hamann <michael@content-space.de> 242 * @author Ben Coburn <btcoburn@silicodon.net> 243 */ 244function getRecentsSince($from,$to=null,$ns='',$flags=0){ 245 global $conf; 246 $recent = array(); 247 248 if($to && $to < $from) 249 return $recent; 250 251 // read all recent changes. (kept short) 252 if ($flags & RECENTS_MEDIA_CHANGES) { 253 $lines = @file($conf['media_changelog']); 254 } else { 255 $lines = @file($conf['changelog']); 256 } 257 258 // we start searching at the end of the list 259 $lines = array_reverse($lines); 260 261 // handle lines 262 $seen = array(); // caches seen lines, _handleRecent() skips them 263 264 foreach($lines as $line){ 265 $rec = _handleRecent($line, $ns, $flags, $seen); 266 if($rec !== false) { 267 if ($rec['date'] >= $from) { 268 if (!$to || $rec['date'] <= $to) { 269 $recent[] = $rec; 270 } 271 } else { 272 break; 273 } 274 } 275 } 276 277 return array_reverse($recent); 278} 279 280/** 281 * Internal function used by getRecents 282 * 283 * don't call directly 284 * 285 * @see getRecents() 286 * @author Andreas Gohr <andi@splitbrain.org> 287 * @author Ben Coburn <btcoburn@silicodon.net> 288 */ 289function _handleRecent($line,$ns,$flags,&$seen){ 290 if(empty($line)) return false; //skip empty lines 291 292 // split the line into parts 293 $recent = parseChangelogLine($line); 294 if ($recent===false) { return false; } 295 296 // skip seen ones 297 if(isset($seen[$recent['id']])) return false; 298 299 // skip minors 300 if($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false; 301 302 // remember in seen to skip additional sights 303 $seen[$recent['id']] = 1; 304 305 // check if it's a hidden page 306 if(isHiddenPage($recent['id'])) return false; 307 308 // filter namespace 309 if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false; 310 311 // exclude subnamespaces 312 if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false; 313 314 // check ACL 315 $recent['perms'] = auth_quickaclcheck($recent['id']); 316 if ($recent['perms'] < AUTH_READ) return false; 317 318 // check existance 319 $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id'])); 320 if((!@file_exists($fn)) && ($flags & RECENTS_SKIP_DELETED)) return false; 321 322 return $recent; 323} 324 325/** 326 * Get the changelog information for a specific page id 327 * and revision (timestamp). Adjacent changelog lines 328 * are optimistically parsed and cached to speed up 329 * consecutive calls to getRevisionInfo. For large 330 * changelog files, only the chunk containing the 331 * requested changelog line is read. 332 * 333 * @author Ben Coburn <btcoburn@silicodon.net> 334 * @author Kate Arzamastseva <pshns@ukr.net> 335 */ 336function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { 337 global $cache_revinfo; 338 $cache =& $cache_revinfo; 339 if (!isset($cache[$id])) { $cache[$id] = array(); } 340 $rev = max($rev, 0); 341 342 // check if it's already in the memory cache 343 if (isset($cache[$id]) && isset($cache[$id][$rev])) { 344 return $cache[$id][$rev]; 345 } 346 347 if ($media) { 348 $file = mediaMetaFN($id, '.changes'); 349 } else { 350 $file = metaFN($id, '.changes'); 351 } 352 if (!@file_exists($file)) { return false; } 353 if (filesize($file)<$chunk_size || $chunk_size==0) { 354 // read whole file 355 $lines = file($file); 356 if ($lines===false) { return false; } 357 } else { 358 // read by chunk 359 $fp = fopen($file, 'rb'); // "file pointer" 360 if ($fp===false) { return false; } 361 $head = 0; 362 fseek($fp, 0, SEEK_END); 363 $tail = ftell($fp); 364 $finger = 0; 365 $finger_rev = 0; 366 367 // find chunk 368 while ($tail-$head>$chunk_size) { 369 $finger = $head+floor(($tail-$head)/2.0); 370 fseek($fp, $finger); 371 fgets($fp); // slip the finger forward to a new line 372 $finger = ftell($fp); 373 $tmp = fgets($fp); // then read at that location 374 $tmp = parseChangelogLine($tmp); 375 $finger_rev = $tmp['date']; 376 if ($finger==$head || $finger==$tail) { break; } 377 if ($finger_rev>$rev) { 378 $tail = $finger; 379 } else { 380 $head = $finger; 381 } 382 } 383 384 if ($tail-$head<1) { 385 // cound not find chunk, assume requested rev is missing 386 fclose($fp); 387 return false; 388 } 389 390 // read chunk 391 $chunk = ''; 392 $chunk_size = max($tail-$head, 0); // found chunk size 393 $got = 0; 394 fseek($fp, $head); 395 while ($got<$chunk_size && !feof($fp)) { 396 $tmp = @fread($fp, max($chunk_size-$got, 0)); 397 if ($tmp===false) { break; } //error state 398 $got += strlen($tmp); 399 $chunk .= $tmp; 400 } 401 $lines = explode("\n", $chunk); 402 array_pop($lines); // remove trailing newline 403 fclose($fp); 404 } 405 406 // parse and cache changelog lines 407 foreach ($lines as $value) { 408 $tmp = parseChangelogLine($value); 409 if ($tmp!==false) { 410 $cache[$id][$tmp['date']] = $tmp; 411 } 412 } 413 if (!isset($cache[$id][$rev])) { return false; } 414 return $cache[$id][$rev]; 415} 416 417/** 418 * Return a list of page revisions numbers 419 * Does not guarantee that the revision exists in the attic, 420 * only that a line with the date exists in the changelog. 421 * By default the current revision is skipped. 422 * 423 * id: the page of interest 424 * first: skip the first n changelog lines 425 * num: number of revisions to return 426 * 427 * The current revision is automatically skipped when the page exists. 428 * See $INFO['meta']['last_change'] for the current revision. 429 * 430 * For efficiency, the log lines are parsed and cached for later 431 * calls to getRevisionInfo. Large changelog files are read 432 * backwards in chunks until the requested number of changelog 433 * lines are recieved. 434 * 435 * @author Ben Coburn <btcoburn@silicodon.net> 436 * @author Kate Arzamastseva <pshns@ukr.net> 437 */ 438function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { 439 global $cache_revinfo; 440 $cache =& $cache_revinfo; 441 if (!isset($cache[$id])) { $cache[$id] = array(); } 442 443 $revs = array(); 444 $lines = array(); 445 $count = 0; 446 if ($media) { 447 $file = mediaMetaFN($id, '.changes'); 448 } else { 449 $file = metaFN($id, '.changes'); 450 } 451 $num = max($num, 0); 452 $chunk_size = max($chunk_size, 0); 453 if ($first<0) { $first = 0; } 454 else if (!$media && @file_exists(wikiFN($id)) || $media && @file_exists(mediaFN($id))) { 455 // skip current revision if the page exists 456 $first = max($first+1, 0); 457 } 458 459 if (!@file_exists($file)) { return $revs; } 460 if (filesize($file)<$chunk_size || $chunk_size==0) { 461 // read whole file 462 $lines = file($file); 463 if ($lines===false) { return $revs; } 464 } else { 465 // read chunks backwards 466 $fp = fopen($file, 'rb'); // "file pointer" 467 if ($fp===false) { return $revs; } 468 fseek($fp, 0, SEEK_END); 469 $tail = ftell($fp); 470 471 // chunk backwards 472 $finger = max($tail-$chunk_size, 0); 473 while ($count<$num+$first) { 474 fseek($fp, $finger); 475 if ($finger>0) { 476 fgets($fp); // slip the finger forward to a new line 477 $finger = ftell($fp); 478 } 479 480 // read chunk 481 if ($tail<=$finger) { break; } 482 $chunk = ''; 483 $read_size = max($tail-$finger, 0); // found chunk size 484 $got = 0; 485 while ($got<$read_size && !feof($fp)) { 486 $tmp = @fread($fp, max($read_size-$got, 0)); 487 if ($tmp===false) { break; } //error state 488 $got += strlen($tmp); 489 $chunk .= $tmp; 490 } 491 $tmp = explode("\n", $chunk); 492 array_pop($tmp); // remove trailing newline 493 494 // combine with previous chunk 495 $count += count($tmp); 496 $lines = array_merge($tmp, $lines); 497 498 // next chunk 499 if ($finger==0) { break; } // already read all the lines 500 else { 501 $tail = $finger; 502 $finger = max($tail-$chunk_size, 0); 503 } 504 } 505 fclose($fp); 506 } 507 508 // skip parsing extra lines 509 $num = max(min(count($lines)-$first, $num), 0); 510 if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } 511 else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } 512 else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } 513 514 // handle lines in reverse order 515 for ($i = count($lines)-1; $i >= 0; $i--) { 516 $tmp = parseChangelogLine($lines[$i]); 517 if ($tmp!==false) { 518 $cache[$id][$tmp['date']] = $tmp; 519 $revs[] = $tmp['date']; 520 } 521 } 522 523 return $revs; 524} 525 526 527