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 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 | RECENTS_MEDIA_CHANGES, $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 if(!$lines) return $recent; 258 259 // we start searching at the end of the list 260 $lines = array_reverse($lines); 261 262 // handle lines 263 $seen = array(); // caches seen lines, _handleRecent() skips them 264 265 foreach($lines as $line){ 266 $rec = _handleRecent($line, $ns, $flags, $seen); 267 if($rec !== false) { 268 if ($rec['date'] >= $from) { 269 if (!$to || $rec['date'] <= $to) { 270 $recent[] = $rec; 271 } 272 } else { 273 break; 274 } 275 } 276 } 277 278 return array_reverse($recent); 279} 280 281/** 282 * Internal function used by getRecents 283 * 284 * don't call directly 285 * 286 * @see getRecents() 287 * @author Andreas Gohr <andi@splitbrain.org> 288 * @author Ben Coburn <btcoburn@silicodon.net> 289 */ 290function _handleRecent($line,$ns,$flags,&$seen){ 291 if(empty($line)) return false; //skip empty lines 292 293 // split the line into parts 294 $recent = parseChangelogLine($line); 295 if ($recent===false) { return false; } 296 297 // skip seen ones 298 if(isset($seen[$recent['id']])) return false; 299 300 // skip minors 301 if($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false; 302 303 // remember in seen to skip additional sights 304 $seen[$recent['id']] = 1; 305 306 // check if it's a hidden page 307 if(isHiddenPage($recent['id'])) return false; 308 309 // filter namespace 310 if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false; 311 312 // exclude subnamespaces 313 if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false; 314 315 // check ACL 316 if ($flags & RECENTS_MEDIA_CHANGES) { 317 $recent['perms'] = auth_quickaclcheck(getNS($recent['id']).':*'); 318 } else { 319 $recent['perms'] = auth_quickaclcheck($recent['id']); 320 } 321 if ($recent['perms'] < AUTH_READ) return false; 322 323 // check existance 324 if($flags & RECENTS_SKIP_DELETED){ 325 $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id'])); 326 if(!@file_exists($fn)) return false; 327 } 328 329 return $recent; 330} 331 332/** 333 * Get the changelog information for a specific page id 334 * and revision (timestamp). Adjacent changelog lines 335 * are optimistically parsed and cached to speed up 336 * consecutive calls to getRevisionInfo. For large 337 * changelog files, only the chunk containing the 338 * requested changelog line is read. 339 * 340 * @author Ben Coburn <btcoburn@silicodon.net> 341 * @author Kate Arzamastseva <pshns@ukr.net> 342 */ 343function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { 344 global $cache_revinfo; 345 $cache =& $cache_revinfo; 346 if (!isset($cache[$id])) { $cache[$id] = array(); } 347 $rev = max($rev, 0); 348 349 // check if it's already in the memory cache 350 if (isset($cache[$id]) && isset($cache[$id][$rev])) { 351 return $cache[$id][$rev]; 352 } 353 354 if ($media) { 355 $file = mediaMetaFN($id, '.changes'); 356 } else { 357 $file = metaFN($id, '.changes'); 358 } 359 if (!@file_exists($file)) { return false; } 360 if (filesize($file)<$chunk_size || $chunk_size==0) { 361 // read whole file 362 $lines = file($file); 363 if ($lines===false) { return false; } 364 } else { 365 // read by chunk 366 $fp = fopen($file, 'rb'); // "file pointer" 367 if ($fp===false) { return false; } 368 $head = 0; 369 fseek($fp, 0, SEEK_END); 370 $tail = ftell($fp); 371 $finger = 0; 372 $finger_rev = 0; 373 374 // find chunk 375 while ($tail-$head>$chunk_size) { 376 $finger = $head+floor(($tail-$head)/2.0); 377 fseek($fp, $finger); 378 fgets($fp); // slip the finger forward to a new line 379 $finger = ftell($fp); 380 $tmp = fgets($fp); // then read at that location 381 $tmp = parseChangelogLine($tmp); 382 $finger_rev = $tmp['date']; 383 if ($finger==$head || $finger==$tail) { break; } 384 if ($finger_rev>$rev) { 385 $tail = $finger; 386 } else { 387 $head = $finger; 388 } 389 } 390 391 if ($tail-$head<1) { 392 // cound not find chunk, assume requested rev is missing 393 fclose($fp); 394 return false; 395 } 396 397 // read chunk 398 $chunk = ''; 399 $chunk_size = max($tail-$head, 0); // found chunk size 400 $got = 0; 401 fseek($fp, $head); 402 while ($got<$chunk_size && !feof($fp)) { 403 $tmp = @fread($fp, max($chunk_size-$got, 0)); 404 if ($tmp===false) { break; } //error state 405 $got += strlen($tmp); 406 $chunk .= $tmp; 407 } 408 $lines = explode("\n", $chunk); 409 array_pop($lines); // remove trailing newline 410 fclose($fp); 411 } 412 413 // parse and cache changelog lines 414 foreach ($lines as $value) { 415 $tmp = parseChangelogLine($value); 416 if ($tmp!==false) { 417 $cache[$id][$tmp['date']] = $tmp; 418 } 419 } 420 if (!isset($cache[$id][$rev])) { return false; } 421 return $cache[$id][$rev]; 422} 423 424/** 425 * Return a list of page revisions numbers 426 * Does not guarantee that the revision exists in the attic, 427 * only that a line with the date exists in the changelog. 428 * By default the current revision is skipped. 429 * 430 * id: the page of interest 431 * first: skip the first n changelog lines 432 * num: number of revisions to return 433 * 434 * The current revision is automatically skipped when the page exists. 435 * See $INFO['meta']['last_change'] for the current revision. 436 * 437 * For efficiency, the log lines are parsed and cached for later 438 * calls to getRevisionInfo. Large changelog files are read 439 * backwards in chunks until the requested number of changelog 440 * lines are recieved. 441 * 442 * @author Ben Coburn <btcoburn@silicodon.net> 443 * @author Kate Arzamastseva <pshns@ukr.net> 444 */ 445function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { 446 global $cache_revinfo; 447 $cache =& $cache_revinfo; 448 if (!isset($cache[$id])) { $cache[$id] = array(); } 449 450 $revs = array(); 451 $lines = array(); 452 $count = 0; 453 if ($media) { 454 $file = mediaMetaFN($id, '.changes'); 455 } else { 456 $file = metaFN($id, '.changes'); 457 } 458 $num = max($num, 0); 459 $chunk_size = max($chunk_size, 0); 460 if ($first<0) { 461 $first = 0; 462 } else if (!$media && @file_exists(wikiFN($id)) || $media && @file_exists(mediaFN($id))) { 463 // skip current revision if the page exists 464 $first = max($first+1, 0); 465 } 466 467 if (!@file_exists($file)) { return $revs; } 468 if (filesize($file)<$chunk_size || $chunk_size==0) { 469 // read whole file 470 $lines = file($file); 471 if ($lines===false) { return $revs; } 472 } else { 473 // read chunks backwards 474 $fp = fopen($file, 'rb'); // "file pointer" 475 if ($fp===false) { return $revs; } 476 fseek($fp, 0, SEEK_END); 477 $tail = ftell($fp); 478 479 // chunk backwards 480 $finger = max($tail-$chunk_size, 0); 481 while ($count<$num+$first) { 482 fseek($fp, $finger); 483 $nl = $finger; 484 if ($finger>0) { 485 fgets($fp); // slip the finger forward to a new line 486 $nl = ftell($fp); 487 } 488 489 // was the chunk big enough? if not, take another bite 490 if($nl > 0 && $tail <= $nl){ 491 $finger = max($finger-$chunk_size, 0); 492 continue; 493 }else{ 494 $finger = $nl; 495 } 496 497 // read chunk 498 $chunk = ''; 499 $read_size = max($tail-$finger, 0); // found chunk size 500 $got = 0; 501 while ($got<$read_size && !feof($fp)) { 502 $tmp = @fread($fp, max($read_size-$got, 0)); 503 if ($tmp===false) { break; } //error state 504 $got += strlen($tmp); 505 $chunk .= $tmp; 506 } 507 $tmp = explode("\n", $chunk); 508 array_pop($tmp); // remove trailing newline 509 510 // combine with previous chunk 511 $count += count($tmp); 512 $lines = array_merge($tmp, $lines); 513 514 // next chunk 515 if ($finger==0) { break; } // already read all the lines 516 else { 517 $tail = $finger; 518 $finger = max($tail-$chunk_size, 0); 519 } 520 } 521 fclose($fp); 522 } 523 524 // skip parsing extra lines 525 $num = max(min(count($lines)-$first, $num), 0); 526 if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } 527 else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } 528 else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } 529 530 // handle lines in reverse order 531 for ($i = count($lines)-1; $i >= 0; $i--) { 532 $tmp = parseChangelogLine($lines[$i]); 533 if ($tmp!==false) { 534 $cache[$id][$tmp['date']] = $tmp; 535 $revs[] = $tmp['date']; 536 } 537 } 538 539 return $revs; 540} 541 542 543