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 * @author Andreas Gohr <andi@splitbrain.org> 41 * @author Esther Brunner <wikidesign@gmail.com> 42 * @author Ben Coburn <btcoburn@silicodon.net> 43 */ 44function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){ 45 global $conf, $INFO; 46 47 // check for special flags as keys 48 if (!is_array($flags)) { $flags = array(); } 49 $flagExternalEdit = isset($flags['ExternalEdit']); 50 51 $id = cleanid($id); 52 $file = wikiFN($id); 53 $created = @filectime($file); 54 $minor = ($type===DOKU_CHANGE_TYPE_MINOR_EDIT); 55 $wasRemoved = ($type===DOKU_CHANGE_TYPE_DELETE); 56 57 if(!$date) $date = time(); //use current time if none supplied 58 $remote = (!$flagExternalEdit)?clientIP(true):'127.0.0.1'; 59 $user = (!$flagExternalEdit)?$_SERVER['REMOTE_USER']:''; 60 61 $strip = array("\t", "\n"); 62 $logline = array( 63 'date' => $date, 64 'ip' => $remote, 65 'type' => str_replace($strip, '', $type), 66 'id' => $id, 67 'user' => $user, 68 'sum' => str_replace($strip, '', $summary), 69 'extra' => str_replace($strip, '', $extra) 70 ); 71 72 // update metadata 73 if (!$wasRemoved) { 74 $oldmeta = p_read_metadata($id); 75 $meta = array(); 76 if (!$INFO['exists'] && empty($oldmeta['persistent']['date']['created'])){ // newly created 77 $meta['date']['created'] = $created; 78 if ($user) $meta['creator'] = $INFO['userinfo']['name']; 79 } elseif (!$INFO['exists'] && !empty($oldmeta['persistent']['date']['created'])) { // re-created / restored 80 $meta['date']['created'] = $oldmeta['persistent']['date']['created']; 81 $meta['date']['modified'] = $created; // use the files ctime here 82 $meta['creator'] = $oldmeta['persistent']['creator']; 83 if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name']; 84 } elseif (!$minor) { // non-minor modification 85 $meta['date']['modified'] = $date; 86 if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name']; 87 } 88 $meta['last_change'] = $logline; 89 p_set_metadata($id, $meta); 90 } 91 92 // add changelog lines 93 $logline = implode("\t", $logline)."\n"; 94 io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog 95 io_saveFile($conf['changelog'],$logline,true); //global changelog cache 96} 97 98/** 99 * Add's an entry to the media changelog 100 * 101 * @author Michael Hamann <michael@content-space.de> 102 * @author Andreas Gohr <andi@splitbrain.org> 103 * @author Esther Brunner <wikidesign@gmail.com> 104 * @author Ben Coburn <btcoburn@silicodon.net> 105 */ 106function addMediaLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){ 107 global $conf; 108 109 $id = cleanid($id); 110 111 if(!$date) $date = time(); //use current time if none supplied 112 $remote = clientIP(true); 113 $user = $_SERVER['REMOTE_USER']; 114 115 $strip = array("\t", "\n"); 116 $logline = array( 117 'date' => $date, 118 'ip' => $remote, 119 'type' => str_replace($strip, '', $type), 120 'id' => $id, 121 'user' => $user, 122 'sum' => str_replace($strip, '', $summary), 123 'extra' => str_replace($strip, '', $extra) 124 ); 125 126 // add changelog lines 127 $logline = implode("\t", $logline)."\n"; 128 io_saveFile($conf['media_changelog'],$logline,true); //global media changelog cache 129} 130 131/** 132 * returns an array of recently changed files using the 133 * changelog 134 * 135 * The following constants can be used to control which changes are 136 * included. Add them together as needed. 137 * 138 * RECENTS_SKIP_DELETED - don't include deleted pages 139 * RECENTS_SKIP_MINORS - don't include minor changes 140 * RECENTS_SKIP_SUBSPACES - don't include subspaces 141 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes 142 * 143 * @param int $first number of first entry returned (for paginating 144 * @param int $num return $num entries 145 * @param string $ns restrict to given namespace 146 * @param bool $flags see above 147 * 148 * @author Ben Coburn <btcoburn@silicodon.net> 149 */ 150function getRecents($first,$num,$ns='',$flags=0){ 151 global $conf; 152 $recent = array(); 153 $count = 0; 154 155 if(!$num) 156 return $recent; 157 158 // read all recent changes. (kept short) 159 if ($flags & RECENTS_MEDIA_CHANGES) { 160 $lines = @file($conf['media_changelog']); 161 } else { 162 $lines = @file($conf['changelog']); 163 } 164 165 // handle lines 166 $seen = array(); // caches seen lines, _handleRecent() skips them 167 for($i = count($lines)-1; $i >= 0; $i--){ 168 $rec = _handleRecent($lines[$i], $ns, $flags, $seen); 169 if($rec !== false) { 170 if(--$first >= 0) continue; // skip first entries 171 $recent[] = $rec; 172 $count++; 173 // break when we have enough entries 174 if($count >= $num){ break; } 175 } 176 } 177 178 return $recent; 179} 180 181/** 182 * returns an array of files changed since a given time using the 183 * changelog 184 * 185 * The following constants can be used to control which changes are 186 * included. Add them together as needed. 187 * 188 * RECENTS_SKIP_DELETED - don't include deleted pages 189 * RECENTS_SKIP_MINORS - don't include minor changes 190 * RECENTS_SKIP_SUBSPACES - don't include subspaces 191 * RECENTS_MEDIA_CHANGES - return media changes instead of page changes 192 * 193 * @param int $from date of the oldest entry to return 194 * @param int $to date of the newest entry to return (for pagination, optional) 195 * @param string $ns restrict to given namespace (optional) 196 * @param bool $flags see above (optional) 197 * 198 * @author Michael Hamann <michael@content-space.de> 199 * @author Ben Coburn <btcoburn@silicodon.net> 200 */ 201function getRecentsSince($from,$to=null,$ns='',$flags=0){ 202 global $conf; 203 $recent = array(); 204 205 if($to && $to < $from) 206 return $recent; 207 208 // read all recent changes. (kept short) 209 if ($flags & RECENTS_MEDIA_CHANGES) { 210 $lines = @file($conf['media_changelog']); 211 } else { 212 $lines = @file($conf['changelog']); 213 } 214 215 // we start searching at the end of the list 216 $lines = array_reverse($lines); 217 218 // handle lines 219 $seen = array(); // caches seen lines, _handleRecent() skips them 220 221 foreach($lines as $line){ 222 $rec = _handleRecent($line, $ns, $flags, $seen); 223 if($rec !== false) { 224 if ($rec['date'] >= $from) { 225 if (!$to || $rec['date'] <= $to) { 226 $recent[] = $rec; 227 } 228 } else { 229 break; 230 } 231 } 232 } 233 234 return array_reverse($recent); 235} 236 237/** 238 * Internal function used by getRecents 239 * 240 * don't call directly 241 * 242 * @see getRecents() 243 * @author Andreas Gohr <andi@splitbrain.org> 244 * @author Ben Coburn <btcoburn@silicodon.net> 245 */ 246function _handleRecent($line,$ns,$flags,&$seen){ 247 if(empty($line)) return false; //skip empty lines 248 249 // split the line into parts 250 $recent = parseChangelogLine($line); 251 if ($recent===false) { return false; } 252 253 // skip seen ones 254 if(isset($seen[$recent['id']])) return false; 255 256 // skip minors 257 if($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) return false; 258 259 // remember in seen to skip additional sights 260 $seen[$recent['id']] = 1; 261 262 // check if it's a hidden page 263 if(isHiddenPage($recent['id'])) return false; 264 265 // filter namespace 266 if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false; 267 268 // exclude subnamespaces 269 if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false; 270 271 // check ACL 272 $recent['perms'] = auth_quickaclcheck($recent['id']); 273 if ($recent['perms'] < AUTH_READ) return false; 274 275 // check existance 276 $fn = (($flags & RECENTS_MEDIA_CHANGES) ? mediaFN($recent['id']) : wikiFN($recent['id'])); 277 if((!@file_exists($fn)) && ($flags & RECENTS_SKIP_DELETED)) return false; 278 279 return $recent; 280} 281 282/** 283 * Get the changelog information for a specific page id 284 * and revision (timestamp). Adjacent changelog lines 285 * are optimistically parsed and cached to speed up 286 * consecutive calls to getRevisionInfo. For large 287 * changelog files, only the chunk containing the 288 * requested changelog line is read. 289 * 290 * @author Ben Coburn <btcoburn@silicodon.net> 291 */ 292function getRevisionInfo($id, $rev, $chunk_size=8192) { 293 global $cache_revinfo; 294 $cache =& $cache_revinfo; 295 if (!isset($cache[$id])) { $cache[$id] = array(); } 296 $rev = max($rev, 0); 297 298 // check if it's already in the memory cache 299 if (isset($cache[$id]) && isset($cache[$id][$rev])) { 300 return $cache[$id][$rev]; 301 } 302 303 $file = metaFN($id, '.changes'); 304 if (!@file_exists($file)) { return false; } 305 if (filesize($file)<$chunk_size || $chunk_size==0) { 306 // read whole file 307 $lines = file($file); 308 if ($lines===false) { return false; } 309 } else { 310 // read by chunk 311 $fp = fopen($file, 'rb'); // "file pointer" 312 if ($fp===false) { return false; } 313 $head = 0; 314 fseek($fp, 0, SEEK_END); 315 $tail = ftell($fp); 316 $finger = 0; 317 $finger_rev = 0; 318 319 // find chunk 320 while ($tail-$head>$chunk_size) { 321 $finger = $head+floor(($tail-$head)/2.0); 322 fseek($fp, $finger); 323 fgets($fp); // slip the finger forward to a new line 324 $finger = ftell($fp); 325 $tmp = fgets($fp); // then read at that location 326 $tmp = parseChangelogLine($tmp); 327 $finger_rev = $tmp['date']; 328 if ($finger==$head || $finger==$tail) { break; } 329 if ($finger_rev>$rev) { 330 $tail = $finger; 331 } else { 332 $head = $finger; 333 } 334 } 335 336 if ($tail-$head<1) { 337 // cound not find chunk, assume requested rev is missing 338 fclose($fp); 339 return false; 340 } 341 342 // read chunk 343 $chunk = ''; 344 $chunk_size = max($tail-$head, 0); // found chunk size 345 $got = 0; 346 fseek($fp, $head); 347 while ($got<$chunk_size && !feof($fp)) { 348 $tmp = @fread($fp, max($chunk_size-$got, 0)); 349 if ($tmp===false) { break; } //error state 350 $got += strlen($tmp); 351 $chunk .= $tmp; 352 } 353 $lines = explode("\n", $chunk); 354 array_pop($lines); // remove trailing newline 355 fclose($fp); 356 } 357 358 // parse and cache changelog lines 359 foreach ($lines as $value) { 360 $tmp = parseChangelogLine($value); 361 if ($tmp!==false) { 362 $cache[$id][$tmp['date']] = $tmp; 363 } 364 } 365 if (!isset($cache[$id][$rev])) { return false; } 366 return $cache[$id][$rev]; 367} 368 369/** 370 * Return a list of page revisions numbers 371 * Does not guarantee that the revision exists in the attic, 372 * only that a line with the date exists in the changelog. 373 * By default the current revision is skipped. 374 * 375 * id: the page of interest 376 * first: skip the first n changelog lines 377 * num: number of revisions to return 378 * 379 * The current revision is automatically skipped when the page exists. 380 * See $INFO['meta']['last_change'] for the current revision. 381 * 382 * For efficiency, the log lines are parsed and cached for later 383 * calls to getRevisionInfo. Large changelog files are read 384 * backwards in chunks until the requested number of changelog 385 * lines are recieved. 386 * 387 * @author Ben Coburn <btcoburn@silicodon.net> 388 */ 389function getRevisions($id, $first, $num, $chunk_size=8192) { 390 global $cache_revinfo; 391 $cache =& $cache_revinfo; 392 if (!isset($cache[$id])) { $cache[$id] = array(); } 393 394 $revs = array(); 395 $lines = array(); 396 $count = 0; 397 $file = metaFN($id, '.changes'); 398 $num = max($num, 0); 399 $chunk_size = max($chunk_size, 0); 400 if ($first<0) { $first = 0; } 401 else if (@file_exists(wikiFN($id))) { 402 // skip current revision if the page exists 403 $first = max($first+1, 0); 404 } 405 406 if (!@file_exists($file)) { return $revs; } 407 if (filesize($file)<$chunk_size || $chunk_size==0) { 408 // read whole file 409 $lines = file($file); 410 if ($lines===false) { return $revs; } 411 } else { 412 // read chunks backwards 413 $fp = fopen($file, 'rb'); // "file pointer" 414 if ($fp===false) { return $revs; } 415 fseek($fp, 0, SEEK_END); 416 $tail = ftell($fp); 417 418 // chunk backwards 419 $finger = max($tail-$chunk_size, 0); 420 while ($count<$num+$first) { 421 fseek($fp, $finger); 422 if ($finger>0) { 423 fgets($fp); // slip the finger forward to a new line 424 $finger = ftell($fp); 425 } 426 427 // read chunk 428 if ($tail<=$finger) { break; } 429 $chunk = ''; 430 $read_size = max($tail-$finger, 0); // found chunk size 431 $got = 0; 432 while ($got<$read_size && !feof($fp)) { 433 $tmp = @fread($fp, max($read_size-$got, 0)); 434 if ($tmp===false) { break; } //error state 435 $got += strlen($tmp); 436 $chunk .= $tmp; 437 } 438 $tmp = explode("\n", $chunk); 439 array_pop($tmp); // remove trailing newline 440 441 // combine with previous chunk 442 $count += count($tmp); 443 $lines = array_merge($tmp, $lines); 444 445 // next chunk 446 if ($finger==0) { break; } // already read all the lines 447 else { 448 $tail = $finger; 449 $finger = max($tail-$chunk_size, 0); 450 } 451 } 452 fclose($fp); 453 } 454 455 // skip parsing extra lines 456 $num = max(min(count($lines)-$first, $num), 0); 457 if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } 458 else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } 459 else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } 460 461 // handle lines in reverse order 462 for ($i = count($lines)-1; $i >= 0; $i--) { 463 $tmp = parseChangelogLine($lines[$i]); 464 if ($tmp!==false) { 465 $cache[$id][$tmp['date']] = $tmp; 466 $revs[] = $tmp['date']; 467 } 468 } 469 470 return $revs; 471} 472 473 474