10c3a5702SAndreas Gohr<?php 20c3a5702SAndreas Gohr 30c3a5702SAndreas Gohrnamespace dokuwiki\ChangeLog; 40c3a5702SAndreas Gohr 50c3a5702SAndreas Gohr/** 60c3a5702SAndreas Gohr * methods for handling of changelog of pages or media files 70c3a5702SAndreas Gohr */ 80c3a5702SAndreas Gohrabstract class ChangeLog 90c3a5702SAndreas Gohr{ 100c3a5702SAndreas Gohr 110c3a5702SAndreas Gohr /** @var string */ 120c3a5702SAndreas Gohr protected $id; 130c3a5702SAndreas Gohr /** @var int */ 140c3a5702SAndreas Gohr protected $chunk_size; 150c3a5702SAndreas Gohr /** @var array */ 160c3a5702SAndreas Gohr protected $cache; 170c3a5702SAndreas Gohr 180c3a5702SAndreas Gohr /** 190c3a5702SAndreas Gohr * Constructor 200c3a5702SAndreas Gohr * 210c3a5702SAndreas Gohr * @param string $id page id 220c3a5702SAndreas Gohr * @param int $chunk_size maximum block size read from file 230c3a5702SAndreas Gohr */ 240c3a5702SAndreas Gohr public function __construct($id, $chunk_size = 8192) 250c3a5702SAndreas Gohr { 260c3a5702SAndreas Gohr global $cache_revinfo; 270c3a5702SAndreas Gohr 280c3a5702SAndreas Gohr $this->cache =& $cache_revinfo; 290c3a5702SAndreas Gohr if (!isset($this->cache[$id])) { 300c3a5702SAndreas Gohr $this->cache[$id] = array(); 310c3a5702SAndreas Gohr } 320c3a5702SAndreas Gohr 330c3a5702SAndreas Gohr $this->id = $id; 340c3a5702SAndreas Gohr $this->setChunkSize($chunk_size); 350c3a5702SAndreas Gohr 360c3a5702SAndreas Gohr } 370c3a5702SAndreas Gohr 380c3a5702SAndreas Gohr /** 390c3a5702SAndreas Gohr * Set chunk size for file reading 400c3a5702SAndreas Gohr * Chunk size zero let read whole file at once 410c3a5702SAndreas Gohr * 420c3a5702SAndreas Gohr * @param int $chunk_size maximum block size read from file 430c3a5702SAndreas Gohr */ 440c3a5702SAndreas Gohr public function setChunkSize($chunk_size) 450c3a5702SAndreas Gohr { 460c3a5702SAndreas Gohr if (!is_numeric($chunk_size)) $chunk_size = 0; 470c3a5702SAndreas Gohr 480c3a5702SAndreas Gohr $this->chunk_size = (int)max($chunk_size, 0); 490c3a5702SAndreas Gohr } 500c3a5702SAndreas Gohr 510c3a5702SAndreas Gohr /** 520c3a5702SAndreas Gohr * Returns path to changelog 530c3a5702SAndreas Gohr * 540c3a5702SAndreas Gohr * @return string path to file 550c3a5702SAndreas Gohr */ 560c3a5702SAndreas Gohr abstract protected function getChangelogFilename(); 570c3a5702SAndreas Gohr 580c3a5702SAndreas Gohr /** 590c3a5702SAndreas Gohr * Returns path to current page/media 600c3a5702SAndreas Gohr * 610c3a5702SAndreas Gohr * @return string path to file 620c3a5702SAndreas Gohr */ 630c3a5702SAndreas Gohr abstract protected function getFilename(); 640c3a5702SAndreas Gohr 650c3a5702SAndreas Gohr /** 660c3a5702SAndreas Gohr * Get the changelog information for a specific page id and revision (timestamp) 670c3a5702SAndreas Gohr * 680c3a5702SAndreas Gohr * Adjacent changelog lines are optimistically parsed and cached to speed up 690c3a5702SAndreas Gohr * consecutive calls to getRevisionInfo. For large changelog files, only the chunk 700c3a5702SAndreas Gohr * containing the requested changelog line is read. 710c3a5702SAndreas Gohr * 720c3a5702SAndreas Gohr * @param int $rev revision timestamp 730c3a5702SAndreas Gohr * @return bool|array false or array with entries: 740c3a5702SAndreas Gohr * - date: unix timestamp 750c3a5702SAndreas Gohr * - ip: IPv4 address (127.0.0.1) 760c3a5702SAndreas Gohr * - type: log line type 770c3a5702SAndreas Gohr * - id: page id 780c3a5702SAndreas Gohr * - user: user name 790c3a5702SAndreas Gohr * - sum: edit summary (or action reason) 800c3a5702SAndreas Gohr * - extra: extra data (varies by line type) 810c3a5702SAndreas Gohr * 820c3a5702SAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 830c3a5702SAndreas Gohr * @author Kate Arzamastseva <pshns@ukr.net> 840c3a5702SAndreas Gohr */ 850c3a5702SAndreas Gohr public function getRevisionInfo($rev) 860c3a5702SAndreas Gohr { 870c3a5702SAndreas Gohr $rev = max($rev, 0); 880c3a5702SAndreas Gohr 890c3a5702SAndreas Gohr // check if it's already in the memory cache 900c3a5702SAndreas Gohr if (isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) { 910c3a5702SAndreas Gohr return $this->cache[$this->id][$rev]; 920c3a5702SAndreas Gohr } 930c3a5702SAndreas Gohr 940c3a5702SAndreas Gohr //read lines from changelog 950c3a5702SAndreas Gohr list($fp, $lines) = $this->readloglines($rev); 960c3a5702SAndreas Gohr if ($fp) { 970c3a5702SAndreas Gohr fclose($fp); 980c3a5702SAndreas Gohr } 990c3a5702SAndreas Gohr if (empty($lines)) return false; 1000c3a5702SAndreas Gohr 1010c3a5702SAndreas Gohr // parse and cache changelog lines 1020c3a5702SAndreas Gohr foreach ($lines as $value) { 1030c3a5702SAndreas Gohr $tmp = parseChangelogLine($value); 1040c3a5702SAndreas Gohr if ($tmp !== false) { 1050c3a5702SAndreas Gohr $this->cache[$this->id][$tmp['date']] = $tmp; 1060c3a5702SAndreas Gohr } 1070c3a5702SAndreas Gohr } 1080c3a5702SAndreas Gohr if (!isset($this->cache[$this->id][$rev])) { 1090c3a5702SAndreas Gohr return false; 1100c3a5702SAndreas Gohr } 1110c3a5702SAndreas Gohr return $this->cache[$this->id][$rev]; 1120c3a5702SAndreas Gohr } 1130c3a5702SAndreas Gohr 1140c3a5702SAndreas Gohr /** 1150c3a5702SAndreas Gohr * Return a list of page revisions numbers 1160c3a5702SAndreas Gohr * 1170c3a5702SAndreas Gohr * Does not guarantee that the revision exists in the attic, 1180c3a5702SAndreas Gohr * only that a line with the date exists in the changelog. 1190c3a5702SAndreas Gohr * By default the current revision is skipped. 1200c3a5702SAndreas Gohr * 1210c3a5702SAndreas Gohr * The current revision is automatically skipped when the page exists. 1220c3a5702SAndreas Gohr * See $INFO['meta']['last_change'] for the current revision. 1230c3a5702SAndreas Gohr * A negative $first let read the current revision too. 1240c3a5702SAndreas Gohr * 1250c3a5702SAndreas Gohr * For efficiency, the log lines are parsed and cached for later 1260c3a5702SAndreas Gohr * calls to getRevisionInfo. Large changelog files are read 1270c3a5702SAndreas Gohr * backwards in chunks until the requested number of changelog 1280c3a5702SAndreas Gohr * lines are recieved. 1290c3a5702SAndreas Gohr * 1300c3a5702SAndreas Gohr * @param int $first skip the first n changelog lines 1310c3a5702SAndreas Gohr * @param int $num number of revisions to return 1320c3a5702SAndreas Gohr * @return array with the revision timestamps 1330c3a5702SAndreas Gohr * 1340c3a5702SAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 1350c3a5702SAndreas Gohr * @author Kate Arzamastseva <pshns@ukr.net> 1360c3a5702SAndreas Gohr */ 1370c3a5702SAndreas Gohr public function getRevisions($first, $num) 1380c3a5702SAndreas Gohr { 1390c3a5702SAndreas Gohr $revs = array(); 1400c3a5702SAndreas Gohr $lines = array(); 1410c3a5702SAndreas Gohr $count = 0; 1420c3a5702SAndreas Gohr 1430c3a5702SAndreas Gohr $num = max($num, 0); 1440c3a5702SAndreas Gohr if ($num == 0) { 1450c3a5702SAndreas Gohr return $revs; 1460c3a5702SAndreas Gohr } 1470c3a5702SAndreas Gohr 1480c3a5702SAndreas Gohr if ($first < 0) { 1490c3a5702SAndreas Gohr $first = 0; 1500c3a5702SAndreas Gohr } else { 1510c3a5702SAndreas Gohr if (file_exists($this->getFilename())) { 1520c3a5702SAndreas Gohr // skip current revision if the page exists 1530c3a5702SAndreas Gohr $first = max($first + 1, 0); 1540c3a5702SAndreas Gohr } 1550c3a5702SAndreas Gohr } 1560c3a5702SAndreas Gohr 1570c3a5702SAndreas Gohr $file = $this->getChangelogFilename(); 1580c3a5702SAndreas Gohr 1590c3a5702SAndreas Gohr if (!file_exists($file)) { 1600c3a5702SAndreas Gohr return $revs; 1610c3a5702SAndreas Gohr } 1620c3a5702SAndreas Gohr if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 1630c3a5702SAndreas Gohr // read whole file 1640c3a5702SAndreas Gohr $lines = file($file); 1650c3a5702SAndreas Gohr if ($lines === false) { 1660c3a5702SAndreas Gohr return $revs; 1670c3a5702SAndreas Gohr } 1680c3a5702SAndreas Gohr } else { 1690c3a5702SAndreas Gohr // read chunks backwards 1700c3a5702SAndreas Gohr $fp = fopen($file, 'rb'); // "file pointer" 1710c3a5702SAndreas Gohr if ($fp === false) { 1720c3a5702SAndreas Gohr return $revs; 1730c3a5702SAndreas Gohr } 1740c3a5702SAndreas Gohr fseek($fp, 0, SEEK_END); 1750c3a5702SAndreas Gohr $tail = ftell($fp); 1760c3a5702SAndreas Gohr 1770c3a5702SAndreas Gohr // chunk backwards 1780c3a5702SAndreas Gohr $finger = max($tail - $this->chunk_size, 0); 1790c3a5702SAndreas Gohr while ($count < $num + $first) { 1800c3a5702SAndreas Gohr $nl = $this->getNewlinepointer($fp, $finger); 1810c3a5702SAndreas Gohr 1820c3a5702SAndreas Gohr // was the chunk big enough? if not, take another bite 1830c3a5702SAndreas Gohr if ($nl > 0 && $tail <= $nl) { 1840c3a5702SAndreas Gohr $finger = max($finger - $this->chunk_size, 0); 1850c3a5702SAndreas Gohr continue; 1860c3a5702SAndreas Gohr } else { 1870c3a5702SAndreas Gohr $finger = $nl; 1880c3a5702SAndreas Gohr } 1890c3a5702SAndreas Gohr 1900c3a5702SAndreas Gohr // read chunk 1910c3a5702SAndreas Gohr $chunk = ''; 1920c3a5702SAndreas Gohr $read_size = max($tail - $finger, 0); // found chunk size 1930c3a5702SAndreas Gohr $got = 0; 1940c3a5702SAndreas Gohr while ($got < $read_size && !feof($fp)) { 1950c3a5702SAndreas Gohr $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0)); 1960c3a5702SAndreas Gohr if ($tmp === false) { 1970c3a5702SAndreas Gohr break; 1980c3a5702SAndreas Gohr } //error state 1990c3a5702SAndreas Gohr $got += strlen($tmp); 2000c3a5702SAndreas Gohr $chunk .= $tmp; 2010c3a5702SAndreas Gohr } 2020c3a5702SAndreas Gohr $tmp = explode("\n", $chunk); 2030c3a5702SAndreas Gohr array_pop($tmp); // remove trailing newline 2040c3a5702SAndreas Gohr 2050c3a5702SAndreas Gohr // combine with previous chunk 2060c3a5702SAndreas Gohr $count += count($tmp); 2070c3a5702SAndreas Gohr $lines = array_merge($tmp, $lines); 2080c3a5702SAndreas Gohr 2090c3a5702SAndreas Gohr // next chunk 2100c3a5702SAndreas Gohr if ($finger == 0) { 2110c3a5702SAndreas Gohr break; 212*e24a74c0SAndreas Gohr } else { // already read all the lines 2130c3a5702SAndreas Gohr $tail = $finger; 2140c3a5702SAndreas Gohr $finger = max($tail - $this->chunk_size, 0); 2150c3a5702SAndreas Gohr } 2160c3a5702SAndreas Gohr } 2170c3a5702SAndreas Gohr fclose($fp); 2180c3a5702SAndreas Gohr } 2190c3a5702SAndreas Gohr 2200c3a5702SAndreas Gohr // skip parsing extra lines 2210c3a5702SAndreas Gohr $num = max(min(count($lines) - $first, $num), 0); 2220c3a5702SAndreas Gohr if ($first > 0 && $num > 0) { 2230c3a5702SAndreas Gohr $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num); 2240c3a5702SAndreas Gohr } else { 2250c3a5702SAndreas Gohr if ($first > 0 && $num == 0) { 2260c3a5702SAndreas Gohr $lines = array_slice($lines, 0, max(count($lines) - $first, 0)); 2270c3a5702SAndreas Gohr } elseif ($first == 0 && $num > 0) { 2280c3a5702SAndreas Gohr $lines = array_slice($lines, max(count($lines) - $num, 0)); 2290c3a5702SAndreas Gohr } 2300c3a5702SAndreas Gohr } 2310c3a5702SAndreas Gohr 2320c3a5702SAndreas Gohr // handle lines in reverse order 2330c3a5702SAndreas Gohr for ($i = count($lines) - 1; $i >= 0; $i--) { 2340c3a5702SAndreas Gohr $tmp = parseChangelogLine($lines[$i]); 2350c3a5702SAndreas Gohr if ($tmp !== false) { 2360c3a5702SAndreas Gohr $this->cache[$this->id][$tmp['date']] = $tmp; 2370c3a5702SAndreas Gohr $revs[] = $tmp['date']; 2380c3a5702SAndreas Gohr } 2390c3a5702SAndreas Gohr } 2400c3a5702SAndreas Gohr 2410c3a5702SAndreas Gohr return $revs; 2420c3a5702SAndreas Gohr } 2430c3a5702SAndreas Gohr 2440c3a5702SAndreas Gohr /** 2450c3a5702SAndreas Gohr * Get the nth revision left or right handside for a specific page id and revision (timestamp) 2460c3a5702SAndreas Gohr * 2470c3a5702SAndreas Gohr * For large changelog files, only the chunk containing the 2480c3a5702SAndreas Gohr * reference revision $rev is read and sometimes a next chunck. 2490c3a5702SAndreas Gohr * 2500c3a5702SAndreas Gohr * Adjacent changelog lines are optimistically parsed and cached to speed up 2510c3a5702SAndreas Gohr * consecutive calls to getRevisionInfo. 2520c3a5702SAndreas Gohr * 2530c3a5702SAndreas Gohr * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) 2540c3a5702SAndreas Gohr * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev 2550c3a5702SAndreas Gohr * @return bool|int 2560c3a5702SAndreas Gohr * timestamp of the requested revision 2570c3a5702SAndreas Gohr * otherwise false 2580c3a5702SAndreas Gohr */ 2590c3a5702SAndreas Gohr public function getRelativeRevision($rev, $direction) 2600c3a5702SAndreas Gohr { 2610c3a5702SAndreas Gohr $rev = max($rev, 0); 2620c3a5702SAndreas Gohr $direction = (int)$direction; 2630c3a5702SAndreas Gohr 2640c3a5702SAndreas Gohr //no direction given or last rev, so no follow-up 2650c3a5702SAndreas Gohr if (!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) { 2660c3a5702SAndreas Gohr return false; 2670c3a5702SAndreas Gohr } 2680c3a5702SAndreas Gohr 2690c3a5702SAndreas Gohr //get lines from changelog 2700c3a5702SAndreas Gohr list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev); 2710c3a5702SAndreas Gohr if (empty($lines)) return false; 2720c3a5702SAndreas Gohr 2730c3a5702SAndreas Gohr // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached 2740c3a5702SAndreas Gohr // also parse and cache changelog lines for getRevisionInfo(). 2750c3a5702SAndreas Gohr $revcounter = 0; 2760c3a5702SAndreas Gohr $relativerev = false; 2770c3a5702SAndreas Gohr $checkotherchunck = true; //always runs once 2780c3a5702SAndreas Gohr while (!$relativerev && $checkotherchunck) { 2790c3a5702SAndreas Gohr $tmp = array(); 2800c3a5702SAndreas Gohr //parse in normal or reverse order 2810c3a5702SAndreas Gohr $count = count($lines); 2820c3a5702SAndreas Gohr if ($direction > 0) { 2830c3a5702SAndreas Gohr $start = 0; 2840c3a5702SAndreas Gohr $step = 1; 2850c3a5702SAndreas Gohr } else { 2860c3a5702SAndreas Gohr $start = $count - 1; 2870c3a5702SAndreas Gohr $step = -1; 2880c3a5702SAndreas Gohr } 2890c3a5702SAndreas Gohr for ($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { 2900c3a5702SAndreas Gohr $tmp = parseChangelogLine($lines[$i]); 2910c3a5702SAndreas Gohr if ($tmp !== false) { 2920c3a5702SAndreas Gohr $this->cache[$this->id][$tmp['date']] = $tmp; 2930c3a5702SAndreas Gohr //look for revs older/earlier then reference $rev and select $direction-th one 2940c3a5702SAndreas Gohr if (($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) { 2950c3a5702SAndreas Gohr $revcounter++; 2960c3a5702SAndreas Gohr if ($revcounter == abs($direction)) { 2970c3a5702SAndreas Gohr $relativerev = $tmp['date']; 2980c3a5702SAndreas Gohr } 2990c3a5702SAndreas Gohr } 3000c3a5702SAndreas Gohr } 3010c3a5702SAndreas Gohr } 3020c3a5702SAndreas Gohr 3030c3a5702SAndreas Gohr //true when $rev is found, but not the wanted follow-up. 3040c3a5702SAndreas Gohr $checkotherchunck = $fp 3050c3a5702SAndreas Gohr && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev)) 3060c3a5702SAndreas Gohr && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); 3070c3a5702SAndreas Gohr 3080c3a5702SAndreas Gohr if ($checkotherchunck) { 3090c3a5702SAndreas Gohr list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction); 3100c3a5702SAndreas Gohr 3110c3a5702SAndreas Gohr if (empty($lines)) break; 3120c3a5702SAndreas Gohr } 3130c3a5702SAndreas Gohr } 3140c3a5702SAndreas Gohr if ($fp) { 3150c3a5702SAndreas Gohr fclose($fp); 3160c3a5702SAndreas Gohr } 3170c3a5702SAndreas Gohr 3180c3a5702SAndreas Gohr return $relativerev; 3190c3a5702SAndreas Gohr } 3200c3a5702SAndreas Gohr 3210c3a5702SAndreas Gohr /** 3220c3a5702SAndreas Gohr * Returns revisions around rev1 and rev2 3230c3a5702SAndreas Gohr * When available it returns $max entries for each revision 3240c3a5702SAndreas Gohr * 3250c3a5702SAndreas Gohr * @param int $rev1 oldest revision timestamp 3260c3a5702SAndreas Gohr * @param int $rev2 newest revision timestamp (0 looks up last revision) 3270c3a5702SAndreas Gohr * @param int $max maximum number of revisions returned 3280c3a5702SAndreas Gohr * @return array with two arrays with revisions surrounding rev1 respectively rev2 3290c3a5702SAndreas Gohr */ 3300c3a5702SAndreas Gohr public function getRevisionsAround($rev1, $rev2, $max = 50) 3310c3a5702SAndreas Gohr { 3320c3a5702SAndreas Gohr $max = floor(abs($max) / 2) * 2 + 1; 3330c3a5702SAndreas Gohr $rev1 = max($rev1, 0); 3340c3a5702SAndreas Gohr $rev2 = max($rev2, 0); 3350c3a5702SAndreas Gohr 3360c3a5702SAndreas Gohr if ($rev2) { 3370c3a5702SAndreas Gohr if ($rev2 < $rev1) { 3380c3a5702SAndreas Gohr $rev = $rev2; 3390c3a5702SAndreas Gohr $rev2 = $rev1; 3400c3a5702SAndreas Gohr $rev1 = $rev; 3410c3a5702SAndreas Gohr } 3420c3a5702SAndreas Gohr } else { 3430c3a5702SAndreas Gohr //empty right side means a removed page. Look up last revision. 3440c3a5702SAndreas Gohr $revs = $this->getRevisions(-1, 1); 3450c3a5702SAndreas Gohr $rev2 = $revs[0]; 3460c3a5702SAndreas Gohr } 3470c3a5702SAndreas Gohr //collect revisions around rev2 3480c3a5702SAndreas Gohr list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max); 3490c3a5702SAndreas Gohr 3500c3a5702SAndreas Gohr if (empty($revs2)) return array(array(), array()); 3510c3a5702SAndreas Gohr 3520c3a5702SAndreas Gohr //collect revisions around rev1 3530c3a5702SAndreas Gohr $index = array_search($rev1, $allrevs); 3540c3a5702SAndreas Gohr if ($index === false) { 3550c3a5702SAndreas Gohr //no overlapping revisions 3560c3a5702SAndreas Gohr list($revs1, , , , ,) = $this->retrieveRevisionsAround($rev1, $max); 3570c3a5702SAndreas Gohr if (empty($revs1)) $revs1 = array(); 3580c3a5702SAndreas Gohr } else { 3590c3a5702SAndreas Gohr //revisions overlaps, reuse revisions around rev2 3600c3a5702SAndreas Gohr $revs1 = $allrevs; 3610c3a5702SAndreas Gohr while ($head > 0) { 3620c3a5702SAndreas Gohr for ($i = count($lines) - 1; $i >= 0; $i--) { 3630c3a5702SAndreas Gohr $tmp = parseChangelogLine($lines[$i]); 3640c3a5702SAndreas Gohr if ($tmp !== false) { 3650c3a5702SAndreas Gohr $this->cache[$this->id][$tmp['date']] = $tmp; 3660c3a5702SAndreas Gohr $revs1[] = $tmp['date']; 3670c3a5702SAndreas Gohr $index++; 3680c3a5702SAndreas Gohr 3690c3a5702SAndreas Gohr if ($index > floor($max / 2)) break 2; 3700c3a5702SAndreas Gohr } 3710c3a5702SAndreas Gohr } 3720c3a5702SAndreas Gohr 3730c3a5702SAndreas Gohr list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 3740c3a5702SAndreas Gohr } 3750c3a5702SAndreas Gohr sort($revs1); 3760c3a5702SAndreas Gohr //return wanted selection 3770c3a5702SAndreas Gohr $revs1 = array_slice($revs1, max($index - floor($max / 2), 0), $max); 3780c3a5702SAndreas Gohr } 3790c3a5702SAndreas Gohr 3800c3a5702SAndreas Gohr return array(array_reverse($revs1), array_reverse($revs2)); 3810c3a5702SAndreas Gohr } 3820c3a5702SAndreas Gohr 383923e149aSMichael Große 384923e149aSMichael Große /** 385923e149aSMichael Große * Checks if the ID has old revisons 386923e149aSMichael Große * @return boolean 387923e149aSMichael Große */ 388923e149aSMichael Große public function hasRevisions() { 389923e149aSMichael Große $file = $this->getChangelogFilename(); 390923e149aSMichael Große return file_exists($file); 391923e149aSMichael Große } 392923e149aSMichael Große 3930c3a5702SAndreas Gohr /** 3940c3a5702SAndreas Gohr * Returns lines from changelog. 3950c3a5702SAndreas Gohr * If file larger than $chuncksize, only chunck is read that could contain $rev. 3960c3a5702SAndreas Gohr * 3970c3a5702SAndreas Gohr * @param int $rev revision timestamp 3980c3a5702SAndreas Gohr * @return array|false 3990c3a5702SAndreas Gohr * if success returns array(fp, array(changeloglines), $head, $tail, $eof) 4000c3a5702SAndreas Gohr * where fp only defined for chuck reading, needs closing. 4010c3a5702SAndreas Gohr * otherwise false 4020c3a5702SAndreas Gohr */ 4030c3a5702SAndreas Gohr protected function readloglines($rev) 4040c3a5702SAndreas Gohr { 4050c3a5702SAndreas Gohr $file = $this->getChangelogFilename(); 4060c3a5702SAndreas Gohr 4070c3a5702SAndreas Gohr if (!file_exists($file)) { 4080c3a5702SAndreas Gohr return false; 4090c3a5702SAndreas Gohr } 4100c3a5702SAndreas Gohr 4110c3a5702SAndreas Gohr $fp = null; 4120c3a5702SAndreas Gohr $head = 0; 4130c3a5702SAndreas Gohr $tail = 0; 4140c3a5702SAndreas Gohr $eof = 0; 4150c3a5702SAndreas Gohr 4160c3a5702SAndreas Gohr if (filesize($file) < $this->chunk_size || $this->chunk_size == 0) { 4170c3a5702SAndreas Gohr // read whole file 4180c3a5702SAndreas Gohr $lines = file($file); 4190c3a5702SAndreas Gohr if ($lines === false) { 4200c3a5702SAndreas Gohr return false; 4210c3a5702SAndreas Gohr } 4220c3a5702SAndreas Gohr } else { 4230c3a5702SAndreas Gohr // read by chunk 4240c3a5702SAndreas Gohr $fp = fopen($file, 'rb'); // "file pointer" 4250c3a5702SAndreas Gohr if ($fp === false) { 4260c3a5702SAndreas Gohr return false; 4270c3a5702SAndreas Gohr } 4280c3a5702SAndreas Gohr $head = 0; 4290c3a5702SAndreas Gohr fseek($fp, 0, SEEK_END); 4300c3a5702SAndreas Gohr $eof = ftell($fp); 4310c3a5702SAndreas Gohr $tail = $eof; 4320c3a5702SAndreas Gohr 4330c3a5702SAndreas Gohr // find chunk 4340c3a5702SAndreas Gohr while ($tail - $head > $this->chunk_size) { 4350c3a5702SAndreas Gohr $finger = $head + floor(($tail - $head) / 2.0); 4360c3a5702SAndreas Gohr $finger = $this->getNewlinepointer($fp, $finger); 4370c3a5702SAndreas Gohr $tmp = fgets($fp); 4380c3a5702SAndreas Gohr if ($finger == $head || $finger == $tail) { 4390c3a5702SAndreas Gohr break; 4400c3a5702SAndreas Gohr } 4410c3a5702SAndreas Gohr $tmp = parseChangelogLine($tmp); 4420c3a5702SAndreas Gohr $finger_rev = $tmp['date']; 4430c3a5702SAndreas Gohr 4440c3a5702SAndreas Gohr if ($finger_rev > $rev) { 4450c3a5702SAndreas Gohr $tail = $finger; 4460c3a5702SAndreas Gohr } else { 4470c3a5702SAndreas Gohr $head = $finger; 4480c3a5702SAndreas Gohr } 4490c3a5702SAndreas Gohr } 4500c3a5702SAndreas Gohr 4510c3a5702SAndreas Gohr if ($tail - $head < 1) { 4520c3a5702SAndreas Gohr // cound not find chunk, assume requested rev is missing 4530c3a5702SAndreas Gohr fclose($fp); 4540c3a5702SAndreas Gohr return false; 4550c3a5702SAndreas Gohr } 4560c3a5702SAndreas Gohr 4570c3a5702SAndreas Gohr $lines = $this->readChunk($fp, $head, $tail); 4580c3a5702SAndreas Gohr } 4590c3a5702SAndreas Gohr return array( 4600c3a5702SAndreas Gohr $fp, 4610c3a5702SAndreas Gohr $lines, 4620c3a5702SAndreas Gohr $head, 4630c3a5702SAndreas Gohr $tail, 4640c3a5702SAndreas Gohr $eof, 4650c3a5702SAndreas Gohr ); 4660c3a5702SAndreas Gohr } 4670c3a5702SAndreas Gohr 4680c3a5702SAndreas Gohr /** 4690c3a5702SAndreas Gohr * Read chunk and return array with lines of given chunck. 4700c3a5702SAndreas Gohr * Has no check if $head and $tail are really at a new line 4710c3a5702SAndreas Gohr * 4720c3a5702SAndreas Gohr * @param resource $fp resource filepointer 4730c3a5702SAndreas Gohr * @param int $head start point chunck 4740c3a5702SAndreas Gohr * @param int $tail end point chunck 4750c3a5702SAndreas Gohr * @return array lines read from chunck 4760c3a5702SAndreas Gohr */ 4770c3a5702SAndreas Gohr protected function readChunk($fp, $head, $tail) 4780c3a5702SAndreas Gohr { 4790c3a5702SAndreas Gohr $chunk = ''; 4800c3a5702SAndreas Gohr $chunk_size = max($tail - $head, 0); // found chunk size 4810c3a5702SAndreas Gohr $got = 0; 4820c3a5702SAndreas Gohr fseek($fp, $head); 4830c3a5702SAndreas Gohr while ($got < $chunk_size && !feof($fp)) { 4840c3a5702SAndreas Gohr $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0)); 4850c3a5702SAndreas Gohr if ($tmp === false) { //error state 4860c3a5702SAndreas Gohr break; 4870c3a5702SAndreas Gohr } 4880c3a5702SAndreas Gohr $got += strlen($tmp); 4890c3a5702SAndreas Gohr $chunk .= $tmp; 4900c3a5702SAndreas Gohr } 4910c3a5702SAndreas Gohr $lines = explode("\n", $chunk); 4920c3a5702SAndreas Gohr array_pop($lines); // remove trailing newline 4930c3a5702SAndreas Gohr return $lines; 4940c3a5702SAndreas Gohr } 4950c3a5702SAndreas Gohr 4960c3a5702SAndreas Gohr /** 4970c3a5702SAndreas Gohr * Set pointer to first new line after $finger and return its position 4980c3a5702SAndreas Gohr * 4990c3a5702SAndreas Gohr * @param resource $fp filepointer 5000c3a5702SAndreas Gohr * @param int $finger a pointer 5010c3a5702SAndreas Gohr * @return int pointer 5020c3a5702SAndreas Gohr */ 5030c3a5702SAndreas Gohr protected function getNewlinepointer($fp, $finger) 5040c3a5702SAndreas Gohr { 5050c3a5702SAndreas Gohr fseek($fp, $finger); 5060c3a5702SAndreas Gohr $nl = $finger; 5070c3a5702SAndreas Gohr if ($finger > 0) { 5080c3a5702SAndreas Gohr fgets($fp); // slip the finger forward to a new line 5090c3a5702SAndreas Gohr $nl = ftell($fp); 5100c3a5702SAndreas Gohr } 5110c3a5702SAndreas Gohr return $nl; 5120c3a5702SAndreas Gohr } 5130c3a5702SAndreas Gohr 5140c3a5702SAndreas Gohr /** 5150c3a5702SAndreas Gohr * Check whether given revision is the current page 5160c3a5702SAndreas Gohr * 5170c3a5702SAndreas Gohr * @param int $rev timestamp of current page 5180c3a5702SAndreas Gohr * @return bool true if $rev is current revision, otherwise false 5190c3a5702SAndreas Gohr */ 5200c3a5702SAndreas Gohr public function isCurrentRevision($rev) 5210c3a5702SAndreas Gohr { 5220c3a5702SAndreas Gohr return $rev == @filemtime($this->getFilename()); 5230c3a5702SAndreas Gohr } 5240c3a5702SAndreas Gohr 5250c3a5702SAndreas Gohr /** 5260c3a5702SAndreas Gohr * Return an existing revision for a specific date which is 5270c3a5702SAndreas Gohr * the current one or younger or equal then the date 5280c3a5702SAndreas Gohr * 5290c3a5702SAndreas Gohr * @param number $date_at timestamp 5300c3a5702SAndreas Gohr * @return string revision ('' for current) 5310c3a5702SAndreas Gohr */ 5320c3a5702SAndreas Gohr public function getLastRevisionAt($date_at) 5330c3a5702SAndreas Gohr { 5340c3a5702SAndreas Gohr //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current 5350c3a5702SAndreas Gohr if (file_exists($this->getFilename()) && $date_at >= @filemtime($this->getFilename())) { 5360c3a5702SAndreas Gohr return ''; 5370c3a5702SAndreas Gohr } else { 5380c3a5702SAndreas Gohr if ($rev = $this->getRelativeRevision($date_at + 1, -1)) { //+1 to get also the requested date revision 5390c3a5702SAndreas Gohr return $rev; 5400c3a5702SAndreas Gohr } else { 5410c3a5702SAndreas Gohr return false; 5420c3a5702SAndreas Gohr } 5430c3a5702SAndreas Gohr } 5440c3a5702SAndreas Gohr } 5450c3a5702SAndreas Gohr 5460c3a5702SAndreas Gohr /** 5470c3a5702SAndreas Gohr * Returns the next lines of the changelog of the chunck before head or after tail 5480c3a5702SAndreas Gohr * 5490c3a5702SAndreas Gohr * @param resource $fp filepointer 5500c3a5702SAndreas Gohr * @param int $head position head of last chunk 5510c3a5702SAndreas Gohr * @param int $tail position tail of last chunk 5520c3a5702SAndreas Gohr * @param int $direction positive forward, negative backward 5530c3a5702SAndreas Gohr * @return array with entries: 5540c3a5702SAndreas Gohr * - $lines: changelog lines of readed chunk 5550c3a5702SAndreas Gohr * - $head: head of chunk 5560c3a5702SAndreas Gohr * - $tail: tail of chunk 5570c3a5702SAndreas Gohr */ 5580c3a5702SAndreas Gohr protected function readAdjacentChunk($fp, $head, $tail, $direction) 5590c3a5702SAndreas Gohr { 5600c3a5702SAndreas Gohr if (!$fp) return array(array(), $head, $tail); 5610c3a5702SAndreas Gohr 5620c3a5702SAndreas Gohr if ($direction > 0) { 5630c3a5702SAndreas Gohr //read forward 5640c3a5702SAndreas Gohr $head = $tail; 5650c3a5702SAndreas Gohr $tail = $head + floor($this->chunk_size * (2 / 3)); 5660c3a5702SAndreas Gohr $tail = $this->getNewlinepointer($fp, $tail); 5670c3a5702SAndreas Gohr } else { 5680c3a5702SAndreas Gohr //read backward 5690c3a5702SAndreas Gohr $tail = $head; 5700c3a5702SAndreas Gohr $head = max($tail - $this->chunk_size, 0); 5710c3a5702SAndreas Gohr while (true) { 5720c3a5702SAndreas Gohr $nl = $this->getNewlinepointer($fp, $head); 5730c3a5702SAndreas Gohr // was the chunk big enough? if not, take another bite 5740c3a5702SAndreas Gohr if ($nl > 0 && $tail <= $nl) { 5750c3a5702SAndreas Gohr $head = max($head - $this->chunk_size, 0); 5760c3a5702SAndreas Gohr } else { 5770c3a5702SAndreas Gohr $head = $nl; 5780c3a5702SAndreas Gohr break; 5790c3a5702SAndreas Gohr } 5800c3a5702SAndreas Gohr } 5810c3a5702SAndreas Gohr } 5820c3a5702SAndreas Gohr 5830c3a5702SAndreas Gohr //load next chunck 5840c3a5702SAndreas Gohr $lines = $this->readChunk($fp, $head, $tail); 5850c3a5702SAndreas Gohr return array($lines, $head, $tail); 5860c3a5702SAndreas Gohr } 5870c3a5702SAndreas Gohr 5880c3a5702SAndreas Gohr /** 5890c3a5702SAndreas Gohr * Collect the $max revisions near to the timestamp $rev 5900c3a5702SAndreas Gohr * 5910c3a5702SAndreas Gohr * @param int $rev revision timestamp 5920c3a5702SAndreas Gohr * @param int $max maximum number of revisions to be returned 5930c3a5702SAndreas Gohr * @return bool|array 5940c3a5702SAndreas Gohr * return array with entries: 5950c3a5702SAndreas Gohr * - $requestedrevs: array of with $max revision timestamps 5960c3a5702SAndreas Gohr * - $revs: all parsed revision timestamps 5970c3a5702SAndreas Gohr * - $fp: filepointer only defined for chuck reading, needs closing. 5980c3a5702SAndreas Gohr * - $lines: non-parsed changelog lines before the parsed revisions 5990c3a5702SAndreas Gohr * - $head: position of first readed changelogline 6000c3a5702SAndreas Gohr * - $lasttail: position of end of last readed changelogline 6010c3a5702SAndreas Gohr * otherwise false 6020c3a5702SAndreas Gohr */ 6030c3a5702SAndreas Gohr protected function retrieveRevisionsAround($rev, $max) 6040c3a5702SAndreas Gohr { 6050c3a5702SAndreas Gohr //get lines from changelog 6060c3a5702SAndreas Gohr list($fp, $lines, $starthead, $starttail, /* $eof */) = $this->readloglines($rev); 6070c3a5702SAndreas Gohr if (empty($lines)) return false; 6080c3a5702SAndreas Gohr 6090c3a5702SAndreas Gohr //parse chunk containing $rev, and read forward more chunks until $max/2 is reached 6100c3a5702SAndreas Gohr $head = $starthead; 6110c3a5702SAndreas Gohr $tail = $starttail; 6120c3a5702SAndreas Gohr $revs = array(); 6130c3a5702SAndreas Gohr $aftercount = $beforecount = 0; 6140c3a5702SAndreas Gohr while (count($lines) > 0) { 6150c3a5702SAndreas Gohr foreach ($lines as $line) { 6160c3a5702SAndreas Gohr $tmp = parseChangelogLine($line); 6170c3a5702SAndreas Gohr if ($tmp !== false) { 6180c3a5702SAndreas Gohr $this->cache[$this->id][$tmp['date']] = $tmp; 6190c3a5702SAndreas Gohr $revs[] = $tmp['date']; 6200c3a5702SAndreas Gohr if ($tmp['date'] >= $rev) { 6210c3a5702SAndreas Gohr //count revs after reference $rev 6220c3a5702SAndreas Gohr $aftercount++; 6230c3a5702SAndreas Gohr if ($aftercount == 1) $beforecount = count($revs); 6240c3a5702SAndreas Gohr } 6250c3a5702SAndreas Gohr //enough revs after reference $rev? 6260c3a5702SAndreas Gohr if ($aftercount > floor($max / 2)) break 2; 6270c3a5702SAndreas Gohr } 6280c3a5702SAndreas Gohr } 6290c3a5702SAndreas Gohr //retrieve next chunk 6300c3a5702SAndreas Gohr list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1); 6310c3a5702SAndreas Gohr } 6320c3a5702SAndreas Gohr if ($aftercount == 0) return false; 6330c3a5702SAndreas Gohr 6340c3a5702SAndreas Gohr $lasttail = $tail; 6350c3a5702SAndreas Gohr 6360c3a5702SAndreas Gohr //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max 6370c3a5702SAndreas Gohr $lines = array(); 6380c3a5702SAndreas Gohr $i = 0; 6390c3a5702SAndreas Gohr if ($aftercount > 0) { 6400c3a5702SAndreas Gohr $head = $starthead; 6410c3a5702SAndreas Gohr $tail = $starttail; 6420c3a5702SAndreas Gohr while ($head > 0) { 6430c3a5702SAndreas Gohr list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); 6440c3a5702SAndreas Gohr 6450c3a5702SAndreas Gohr for ($i = count($lines) - 1; $i >= 0; $i--) { 6460c3a5702SAndreas Gohr $tmp = parseChangelogLine($lines[$i]); 6470c3a5702SAndreas Gohr if ($tmp !== false) { 6480c3a5702SAndreas Gohr $this->cache[$this->id][$tmp['date']] = $tmp; 6490c3a5702SAndreas Gohr $revs[] = $tmp['date']; 6500c3a5702SAndreas Gohr $beforecount++; 6510c3a5702SAndreas Gohr //enough revs before reference $rev? 6520c3a5702SAndreas Gohr if ($beforecount > max(floor($max / 2), $max - $aftercount)) break 2; 6530c3a5702SAndreas Gohr } 6540c3a5702SAndreas Gohr } 6550c3a5702SAndreas Gohr } 6560c3a5702SAndreas Gohr } 6570c3a5702SAndreas Gohr sort($revs); 6580c3a5702SAndreas Gohr 6590c3a5702SAndreas Gohr //keep only non-parsed lines 6600c3a5702SAndreas Gohr $lines = array_slice($lines, 0, $i); 6610c3a5702SAndreas Gohr //trunk desired selection 6620c3a5702SAndreas Gohr $requestedrevs = array_slice($revs, -$max, $max); 6630c3a5702SAndreas Gohr 6640c3a5702SAndreas Gohr return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail); 6650c3a5702SAndreas Gohr } 6660c3a5702SAndreas Gohr} 667