10c3a5702SAndreas Gohr<?php 20c3a5702SAndreas Gohr 30c3a5702SAndreas Gohrnamespace dokuwiki\ChangeLog; 40c3a5702SAndreas Gohr 566f4cdd4SSatoshi Saharause dokuwiki\Logger; 666f4cdd4SSatoshi Sahara 70c3a5702SAndreas Gohr/** 81d11f1d3SSatoshi Sahara * ChangeLog Prototype; methods for handling changelog 90c3a5702SAndreas Gohr */ 100c3a5702SAndreas Gohrabstract class ChangeLog 110c3a5702SAndreas Gohr{ 121d11f1d3SSatoshi Sahara use ChangeLogTrait; 131d11f1d3SSatoshi Sahara 140c3a5702SAndreas Gohr /** @var string */ 150c3a5702SAndreas Gohr protected $id; 1679a2d784SGerrit Uitslag /** @var false|int */ 17bd17ac90SSatoshi Sahara protected $currentRevision; 180c3a5702SAndreas Gohr /** @var array */ 190603e565SAndreas Gohr protected $cache = []; 200c3a5702SAndreas Gohr 210c3a5702SAndreas Gohr /** 220c3a5702SAndreas Gohr * Constructor 230c3a5702SAndreas Gohr * 240c3a5702SAndreas Gohr * @param string $id page id 250c3a5702SAndreas Gohr * @param int $chunk_size maximum block size read from file 260c3a5702SAndreas Gohr */ 270c3a5702SAndreas Gohr public function __construct($id, $chunk_size = 8192) 280c3a5702SAndreas Gohr { 290c3a5702SAndreas Gohr global $cache_revinfo; 300c3a5702SAndreas Gohr 310c3a5702SAndreas Gohr $this->cache =& $cache_revinfo; 320c3a5702SAndreas Gohr if (!isset($this->cache[$id])) { 330603e565SAndreas Gohr $this->cache[$id] = []; 340c3a5702SAndreas Gohr } 350c3a5702SAndreas Gohr 360c3a5702SAndreas Gohr $this->id = $id; 370c3a5702SAndreas Gohr $this->setChunkSize($chunk_size); 380c3a5702SAndreas Gohr } 390c3a5702SAndreas Gohr 400c3a5702SAndreas Gohr /** 410c3a5702SAndreas Gohr * Returns path to current page/media 420c3a5702SAndreas Gohr * 4383bec475SAndreas Gohr * @param string|int $rev empty string or revision timestamp 440c3a5702SAndreas Gohr * @return string path to file 450c3a5702SAndreas Gohr */ 4683bec475SAndreas Gohr abstract protected function getFilename($rev = ''); 470c3a5702SAndreas Gohr 48df7627d6SSatoshi Sahara /** 49a835c93aSGerrit Uitslag * Returns mode 50a835c93aSGerrit Uitslag * 51a835c93aSGerrit Uitslag * @return string RevisionInfo::MODE_MEDIA or RevisionInfo::MODE_PAGE 52a835c93aSGerrit Uitslag */ 53a835c93aSGerrit Uitslag abstract protected function getMode(); 54a835c93aSGerrit Uitslag 55a835c93aSGerrit Uitslag /** 56df7627d6SSatoshi Sahara * Check whether given revision is the current page 57df7627d6SSatoshi Sahara * 58df7627d6SSatoshi Sahara * @param int $rev timestamp of current page 59df7627d6SSatoshi Sahara * @return bool true if $rev is current revision, otherwise false 60df7627d6SSatoshi Sahara */ 61df7627d6SSatoshi Sahara public function isCurrentRevision($rev) 62df7627d6SSatoshi Sahara { 63df7627d6SSatoshi Sahara return $rev == $this->currentRevision(); 64df7627d6SSatoshi Sahara } 65df7627d6SSatoshi Sahara 66df7627d6SSatoshi Sahara /** 67df7627d6SSatoshi Sahara * Checks if the revision is last revision 68df7627d6SSatoshi Sahara * 69df7627d6SSatoshi Sahara * @param int $rev revision timestamp 70df7627d6SSatoshi Sahara * @return bool true if $rev is last revision, otherwise false 71df7627d6SSatoshi Sahara */ 72df7627d6SSatoshi Sahara public function isLastRevision($rev = null) 73df7627d6SSatoshi Sahara { 74df7627d6SSatoshi Sahara return $rev === $this->lastRevision(); 75df7627d6SSatoshi Sahara } 76df7627d6SSatoshi Sahara 77df7627d6SSatoshi Sahara /** 78eeda7adaSGerrit Uitslag * Return the current revision identifier 7905282e9fSSatoshi Sahara * 8005282e9fSSatoshi Sahara * The "current" revision means current version of the page or media file. It is either 8105282e9fSSatoshi Sahara * identical with or newer than the "last" revision, that depends on whether the file 8205282e9fSSatoshi Sahara * has modified, created or deleted outside of DokuWiki. 8305282e9fSSatoshi Sahara * The value of identifier can be determined by timestamp as far as the file exists, 8405282e9fSSatoshi Sahara * otherwise it must be assigned larger than any other revisions to keep them sortable. 8505282e9fSSatoshi Sahara * 8605282e9fSSatoshi Sahara * @return int|false revision timestamp 87df7627d6SSatoshi Sahara */ 88df7627d6SSatoshi Sahara public function currentRevision() 89df7627d6SSatoshi Sahara { 90df7627d6SSatoshi Sahara if (!isset($this->currentRevision)) { 91df7627d6SSatoshi Sahara // set ChangeLog::currentRevision property 92df7627d6SSatoshi Sahara $this->getCurrentRevisionInfo(); 93df7627d6SSatoshi Sahara } 94df7627d6SSatoshi Sahara return $this->currentRevision; 95df7627d6SSatoshi Sahara } 96df7627d6SSatoshi Sahara 97df7627d6SSatoshi Sahara /** 98eeda7adaSGerrit Uitslag * Return the last revision identifier, date value of the last entry of the changelog 99d154755dSSatoshi Sahara * 10005282e9fSSatoshi Sahara * @return int|false revision timestamp 101df7627d6SSatoshi Sahara */ 102df7627d6SSatoshi Sahara public function lastRevision() 103df7627d6SSatoshi Sahara { 104df7627d6SSatoshi Sahara $revs = $this->getRevisions(-1, 1); 105df7627d6SSatoshi Sahara return empty($revs) ? false : $revs[0]; 106df7627d6SSatoshi Sahara } 107df7627d6SSatoshi Sahara 1080c3a5702SAndreas Gohr /** 109a835c93aSGerrit Uitslag * Parses a changelog line into its components and save revision info to the cache pool 110b82f2411SSatoshi Sahara * 111a835c93aSGerrit Uitslag * @param string $value changelog line 112a835c93aSGerrit Uitslag * @return array|bool parsed line or false 113b82f2411SSatoshi Sahara */ 114a835c93aSGerrit Uitslag protected function parseAndCacheLogLine($value) 115b82f2411SSatoshi Sahara { 116a835c93aSGerrit Uitslag $info = static::parseLogLine($value); 117a835c93aSGerrit Uitslag if (is_array($info)) { 118a835c93aSGerrit Uitslag $info['mode'] = $this->getMode(); 1190603e565SAndreas Gohr $this->cache[$this->id][$info['date']] ??= $info; 120a835c93aSGerrit Uitslag return $info; 121a835c93aSGerrit Uitslag } 122a835c93aSGerrit Uitslag return false; 123b82f2411SSatoshi Sahara } 124b82f2411SSatoshi Sahara 125b82f2411SSatoshi Sahara /** 126d154755dSSatoshi Sahara * Get the changelog information for a specific revision (timestamp) 1270c3a5702SAndreas Gohr * 1280c3a5702SAndreas Gohr * Adjacent changelog lines are optimistically parsed and cached to speed up 1290c3a5702SAndreas Gohr * consecutive calls to getRevisionInfo. For large changelog files, only the chunk 1300c3a5702SAndreas Gohr * containing the requested changelog line is read. 1310c3a5702SAndreas Gohr * 1320c3a5702SAndreas Gohr * @param int $rev revision timestamp 13386216bf0SGerrit Uitslag * @param bool $retrieveCurrentRevInfo allows to skip for getting other revision info in the 13486216bf0SGerrit Uitslag * getCurrentRevisionInfo() where $currentRevision is not yet determined 1350c3a5702SAndreas Gohr * @return bool|array false or array with entries: 1360c3a5702SAndreas Gohr * - date: unix timestamp 1370c3a5702SAndreas Gohr * - ip: IPv4 address (127.0.0.1) 1380c3a5702SAndreas Gohr * - type: log line type 1390c3a5702SAndreas Gohr * - id: page id 1400c3a5702SAndreas Gohr * - user: user name 1410c3a5702SAndreas Gohr * - sum: edit summary (or action reason) 1420c3a5702SAndreas Gohr * - extra: extra data (varies by line type) 143bd17ac90SSatoshi Sahara * - sizechange: change of filesize 144a835c93aSGerrit Uitslag * additional: 145a835c93aSGerrit Uitslag * - mode: page or media 1460c3a5702SAndreas Gohr * 1470c3a5702SAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 1480c3a5702SAndreas Gohr * @author Kate Arzamastseva <pshns@ukr.net> 1490c3a5702SAndreas Gohr */ 15086216bf0SGerrit Uitslag public function getRevisionInfo($rev, $retrieveCurrentRevInfo = true) 1510c3a5702SAndreas Gohr { 152a3984ddfSSatoshi Sahara $rev = max(0, $rev); 153a3984ddfSSatoshi Sahara if (!$rev) return false; 1540c3a5702SAndreas Gohr 15586216bf0SGerrit Uitslag //ensure the external edits are cached as well 15686216bf0SGerrit Uitslag if (!isset($this->currentRevision) && $retrieveCurrentRevInfo) { 15786216bf0SGerrit Uitslag $this->getCurrentRevisionInfo(); 15886216bf0SGerrit Uitslag } 15986216bf0SGerrit Uitslag 1600c3a5702SAndreas Gohr // check if it's already in the memory cache 16185160059SGerrit Uitslag if (isset($this->cache[$this->id][$rev])) { 1620c3a5702SAndreas Gohr return $this->cache[$this->id][$rev]; 1630c3a5702SAndreas Gohr } 1640c3a5702SAndreas Gohr 1650c3a5702SAndreas Gohr //read lines from changelog 166*d41f5a8fSAndreas Gohr $result = $this->readloglines($rev); 167*d41f5a8fSAndreas Gohr if ($result === false) return false; 168*d41f5a8fSAndreas Gohr [$fp, $lines] = $result; 1690c3a5702SAndreas Gohr if ($fp) { 1700c3a5702SAndreas Gohr fclose($fp); 1710c3a5702SAndreas Gohr } 1720c3a5702SAndreas Gohr if (empty($lines)) return false; 1730c3a5702SAndreas Gohr 1740c3a5702SAndreas Gohr // parse and cache changelog lines 175a835c93aSGerrit Uitslag foreach ($lines as $line) { 176a835c93aSGerrit Uitslag $this->parseAndCacheLogLine($line); 1770c3a5702SAndreas Gohr } 17885160059SGerrit Uitslag 17985160059SGerrit Uitslag return $this->cache[$this->id][$rev] ?? false; 1800c3a5702SAndreas Gohr } 1810c3a5702SAndreas Gohr 1820c3a5702SAndreas Gohr /** 1830c3a5702SAndreas Gohr * Return a list of page revisions numbers 1840c3a5702SAndreas Gohr * 1850c3a5702SAndreas Gohr * Does not guarantee that the revision exists in the attic, 1860c3a5702SAndreas Gohr * only that a line with the date exists in the changelog. 1870c3a5702SAndreas Gohr * By default the current revision is skipped. 1880c3a5702SAndreas Gohr * 1890c3a5702SAndreas Gohr * The current revision is automatically skipped when the page exists. 1900c3a5702SAndreas Gohr * See $INFO['meta']['last_change'] for the current revision. 1910c3a5702SAndreas Gohr * A negative $first let read the current revision too. 1920c3a5702SAndreas Gohr * 1930c3a5702SAndreas Gohr * For efficiency, the log lines are parsed and cached for later 1940c3a5702SAndreas Gohr * calls to getRevisionInfo. Large changelog files are read 1950c3a5702SAndreas Gohr * backwards in chunks until the requested number of changelog 196eeda7adaSGerrit Uitslag * lines are received. 1970c3a5702SAndreas Gohr * 1980c3a5702SAndreas Gohr * @param int $first skip the first n changelog lines 1990c3a5702SAndreas Gohr * @param int $num number of revisions to return 2000c3a5702SAndreas Gohr * @return array with the revision timestamps 2010c3a5702SAndreas Gohr * 2020c3a5702SAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 2030c3a5702SAndreas Gohr * @author Kate Arzamastseva <pshns@ukr.net> 2040c3a5702SAndreas Gohr */ 2050c3a5702SAndreas Gohr public function getRevisions($first, $num) 2060c3a5702SAndreas Gohr { 2070603e565SAndreas Gohr $revs = []; 2080603e565SAndreas Gohr $lines = []; 2090c3a5702SAndreas Gohr $count = 0; 2100c3a5702SAndreas Gohr 211d154755dSSatoshi Sahara $logfile = $this->getChangelogFilename(); 212d154755dSSatoshi Sahara if (!file_exists($logfile)) return $revs; 213d154755dSSatoshi Sahara 2140c3a5702SAndreas Gohr $num = max($num, 0); 2150c3a5702SAndreas Gohr if ($num == 0) { 2160c3a5702SAndreas Gohr return $revs; 2170c3a5702SAndreas Gohr } 2180c3a5702SAndreas Gohr 2190c3a5702SAndreas Gohr if ($first < 0) { 2200c3a5702SAndreas Gohr $first = 0; 2210c3a5702SAndreas Gohr } else { 222df7627d6SSatoshi Sahara $fileLastMod = $this->getFilename(); 223df7627d6SSatoshi Sahara if (file_exists($fileLastMod) && $this->isLastRevision(filemtime($fileLastMod))) { 224df7627d6SSatoshi Sahara // skip last revision if the page exists 2250c3a5702SAndreas Gohr $first = max($first + 1, 0); 2260c3a5702SAndreas Gohr } 2270c3a5702SAndreas Gohr } 2280c3a5702SAndreas Gohr 229d154755dSSatoshi Sahara if (filesize($logfile) < $this->chunk_size || $this->chunk_size == 0) { 2300c3a5702SAndreas Gohr // read whole file 231d154755dSSatoshi Sahara $lines = file($logfile); 2320c3a5702SAndreas Gohr if ($lines === false) { 2330c3a5702SAndreas Gohr return $revs; 2340c3a5702SAndreas Gohr } 2350c3a5702SAndreas Gohr } else { 2360c3a5702SAndreas Gohr // read chunks backwards 237d154755dSSatoshi Sahara $fp = fopen($logfile, 'rb'); // "file pointer" 2380c3a5702SAndreas Gohr if ($fp === false) { 2390c3a5702SAndreas Gohr return $revs; 2400c3a5702SAndreas Gohr } 2410c3a5702SAndreas Gohr fseek($fp, 0, SEEK_END); 2420c3a5702SAndreas Gohr $tail = ftell($fp); 2430c3a5702SAndreas Gohr 2440c3a5702SAndreas Gohr // chunk backwards 2450c3a5702SAndreas Gohr $finger = max($tail - $this->chunk_size, 0); 2460c3a5702SAndreas Gohr while ($count < $num + $first) { 2470c3a5702SAndreas Gohr $nl = $this->getNewlinepointer($fp, $finger); 2480c3a5702SAndreas Gohr 2490c3a5702SAndreas Gohr // was the chunk big enough? if not, take another bite 2500c3a5702SAndreas Gohr if ($nl > 0 && $tail <= $nl) { 2510c3a5702SAndreas Gohr $finger = max($finger - $this->chunk_size, 0); 2520c3a5702SAndreas Gohr continue; 2530c3a5702SAndreas Gohr } else { 2540c3a5702SAndreas Gohr $finger = $nl; 2550c3a5702SAndreas Gohr } 2560c3a5702SAndreas Gohr 2570c3a5702SAndreas Gohr // read chunk 2580c3a5702SAndreas Gohr $chunk = ''; 2590c3a5702SAndreas Gohr $read_size = max($tail - $finger, 0); // found chunk size 2600c3a5702SAndreas Gohr $got = 0; 2610c3a5702SAndreas Gohr while ($got < $read_size && !feof($fp)) { 2620c3a5702SAndreas Gohr $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0)); 2630c3a5702SAndreas Gohr if ($tmp === false) { 2640c3a5702SAndreas Gohr break; 2650c3a5702SAndreas Gohr } //error state 2660c3a5702SAndreas Gohr $got += strlen($tmp); 2670c3a5702SAndreas Gohr $chunk .= $tmp; 2680c3a5702SAndreas Gohr } 2690c3a5702SAndreas Gohr $tmp = explode("\n", $chunk); 2700c3a5702SAndreas Gohr array_pop($tmp); // remove trailing newline 2710c3a5702SAndreas Gohr 2720c3a5702SAndreas Gohr // combine with previous chunk 2730c3a5702SAndreas Gohr $count += count($tmp); 2740603e565SAndreas Gohr $lines = [...$tmp, ...$lines]; 2750c3a5702SAndreas Gohr 2760c3a5702SAndreas Gohr // next chunk 2770c3a5702SAndreas Gohr if ($finger == 0) { 2780c3a5702SAndreas Gohr break; 279e24a74c0SAndreas Gohr } else { // already read all the lines 2800c3a5702SAndreas Gohr $tail = $finger; 2810c3a5702SAndreas Gohr $finger = max($tail - $this->chunk_size, 0); 2820c3a5702SAndreas Gohr } 2830c3a5702SAndreas Gohr } 2840c3a5702SAndreas Gohr fclose($fp); 2850c3a5702SAndreas Gohr } 2860c3a5702SAndreas Gohr 2870c3a5702SAndreas Gohr // skip parsing extra lines 2880c3a5702SAndreas Gohr $num = max(min(count($lines) - $first, $num), 0); 2890c3a5702SAndreas Gohr if ($first > 0 && $num > 0) { 2900c3a5702SAndreas Gohr $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num); 291df7627d6SSatoshi Sahara } elseif ($first > 0 && $num == 0) { 2920c3a5702SAndreas Gohr $lines = array_slice($lines, 0, max(count($lines) - $first, 0)); 2930c3a5702SAndreas Gohr } elseif ($first == 0 && $num > 0) { 2940c3a5702SAndreas Gohr $lines = array_slice($lines, max(count($lines) - $num, 0)); 2950c3a5702SAndreas Gohr } 2960c3a5702SAndreas Gohr 2970c3a5702SAndreas Gohr // handle lines in reverse order 2980c3a5702SAndreas Gohr for ($i = count($lines) - 1; $i >= 0; $i--) { 299a835c93aSGerrit Uitslag $info = $this->parseAndCacheLogLine($lines[$i]); 300a835c93aSGerrit Uitslag if (is_array($info)) { 301bd17ac90SSatoshi Sahara $revs[] = $info['date']; 3020c3a5702SAndreas Gohr } 3030c3a5702SAndreas Gohr } 3040c3a5702SAndreas Gohr 3050c3a5702SAndreas Gohr return $revs; 3060c3a5702SAndreas Gohr } 3070c3a5702SAndreas Gohr 3080c3a5702SAndreas Gohr /** 309eeda7adaSGerrit Uitslag * Get the nth revision left or right-hand side for a specific page id and revision (timestamp) 3100c3a5702SAndreas Gohr * 3110c3a5702SAndreas Gohr * For large changelog files, only the chunk containing the 312eeda7adaSGerrit Uitslag * reference revision $rev is read and sometimes a next chunk. 3130c3a5702SAndreas Gohr * 3140c3a5702SAndreas Gohr * Adjacent changelog lines are optimistically parsed and cached to speed up 3150c3a5702SAndreas Gohr * consecutive calls to getRevisionInfo. 3160c3a5702SAndreas Gohr * 317d154755dSSatoshi Sahara * @param int $rev revision timestamp used as start date 318d154755dSSatoshi Sahara * (doesn't need to be exact revision number) 319d154755dSSatoshi Sahara * @param int $direction give position of returned revision with respect to $rev; 320d154755dSSatoshi Sahara positive=next, negative=prev 3210c3a5702SAndreas Gohr * @return bool|int 3220c3a5702SAndreas Gohr * timestamp of the requested revision 3230c3a5702SAndreas Gohr * otherwise false 3240c3a5702SAndreas Gohr */ 3250c3a5702SAndreas Gohr public function getRelativeRevision($rev, $direction) 3260c3a5702SAndreas Gohr { 3270c3a5702SAndreas Gohr $rev = max($rev, 0); 3280c3a5702SAndreas Gohr $direction = (int)$direction; 3290c3a5702SAndreas Gohr 3300c3a5702SAndreas Gohr //no direction given or last rev, so no follow-up 3310c3a5702SAndreas Gohr if (!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) { 3320c3a5702SAndreas Gohr return false; 3330c3a5702SAndreas Gohr } 3340c3a5702SAndreas Gohr 3350c3a5702SAndreas Gohr //get lines from changelog 336*d41f5a8fSAndreas Gohr $result = $this->readloglines($rev); 337*d41f5a8fSAndreas Gohr if ($result === false) return false; 338*d41f5a8fSAndreas Gohr [$fp, $lines, $head, $tail, $eof] = $result; 3390c3a5702SAndreas Gohr if (empty($lines)) return false; 3400c3a5702SAndreas Gohr 3415d9428a0SSatoshi Sahara // look for revisions later/earlier than $rev, when founded count till the wanted revision is reached 3420c3a5702SAndreas Gohr // also parse and cache changelog lines for getRevisionInfo(). 343eeda7adaSGerrit Uitslag $revCounter = 0; 344eeda7adaSGerrit Uitslag $relativeRev = false; 345eeda7adaSGerrit Uitslag $checkOtherChunk = true; //always runs once 346eeda7adaSGerrit Uitslag while (!$relativeRev && $checkOtherChunk) { 3470603e565SAndreas Gohr $info = []; 3480c3a5702SAndreas Gohr //parse in normal or reverse order 3490c3a5702SAndreas Gohr $count = count($lines); 3500c3a5702SAndreas Gohr if ($direction > 0) { 3510c3a5702SAndreas Gohr $start = 0; 3520c3a5702SAndreas Gohr $step = 1; 3530c3a5702SAndreas Gohr } else { 3540c3a5702SAndreas Gohr $start = $count - 1; 3550c3a5702SAndreas Gohr $step = -1; 3560c3a5702SAndreas Gohr } 3570603e565SAndreas Gohr for ($i = $start; $i >= 0 && $i < $count; $i += $step) { 358a835c93aSGerrit Uitslag $info = $this->parseAndCacheLogLine($lines[$i]); 359a835c93aSGerrit Uitslag if (is_array($info)) { 3600c3a5702SAndreas Gohr //look for revs older/earlier then reference $rev and select $direction-th one 361bd17ac90SSatoshi Sahara if (($direction > 0 && $info['date'] > $rev) || ($direction < 0 && $info['date'] < $rev)) { 362eeda7adaSGerrit Uitslag $revCounter++; 363eeda7adaSGerrit Uitslag if ($revCounter == abs($direction)) { 364eeda7adaSGerrit Uitslag $relativeRev = $info['date']; 3650c3a5702SAndreas Gohr } 3660c3a5702SAndreas Gohr } 3670c3a5702SAndreas Gohr } 3680c3a5702SAndreas Gohr } 3690c3a5702SAndreas Gohr 3700c3a5702SAndreas Gohr //true when $rev is found, but not the wanted follow-up. 371eeda7adaSGerrit Uitslag $checkOtherChunk = $fp 372eeda7adaSGerrit Uitslag && ($info['date'] == $rev || ($revCounter > 0 && !$relativeRev)) 3730603e565SAndreas Gohr && (!($tail == $eof && $direction > 0) && !($head == 0 && $direction < 0)); 3740c3a5702SAndreas Gohr 375eeda7adaSGerrit Uitslag if ($checkOtherChunk) { 3760603e565SAndreas Gohr [$lines, $head, $tail] = $this->readAdjacentChunk($fp, $head, $tail, $direction); 3770c3a5702SAndreas Gohr 3780c3a5702SAndreas Gohr if (empty($lines)) break; 3790c3a5702SAndreas Gohr } 3800c3a5702SAndreas Gohr } 3810c3a5702SAndreas Gohr if ($fp) { 3820c3a5702SAndreas Gohr fclose($fp); 3830c3a5702SAndreas Gohr } 3840c3a5702SAndreas Gohr 385eeda7adaSGerrit Uitslag return $relativeRev; 3860c3a5702SAndreas Gohr } 3870c3a5702SAndreas Gohr 3880c3a5702SAndreas Gohr /** 3890c3a5702SAndreas Gohr * Returns revisions around rev1 and rev2 3900c3a5702SAndreas Gohr * When available it returns $max entries for each revision 3910c3a5702SAndreas Gohr * 3920c3a5702SAndreas Gohr * @param int $rev1 oldest revision timestamp 3930c3a5702SAndreas Gohr * @param int $rev2 newest revision timestamp (0 looks up last revision) 3940c3a5702SAndreas Gohr * @param int $max maximum number of revisions returned 3950c3a5702SAndreas Gohr * @return array with two arrays with revisions surrounding rev1 respectively rev2 3960c3a5702SAndreas Gohr */ 3970c3a5702SAndreas Gohr public function getRevisionsAround($rev1, $rev2, $max = 50) 3980c3a5702SAndreas Gohr { 3990603e565SAndreas Gohr $max = (int) (abs($max) / 2) * 2 + 1; 4000c3a5702SAndreas Gohr $rev1 = max($rev1, 0); 4010c3a5702SAndreas Gohr $rev2 = max($rev2, 0); 4020c3a5702SAndreas Gohr 4030c3a5702SAndreas Gohr if ($rev2) { 4040c3a5702SAndreas Gohr if ($rev2 < $rev1) { 4050c3a5702SAndreas Gohr $rev = $rev2; 4060c3a5702SAndreas Gohr $rev2 = $rev1; 4070c3a5702SAndreas Gohr $rev1 = $rev; 4080c3a5702SAndreas Gohr } 4090c3a5702SAndreas Gohr } else { 4100c3a5702SAndreas Gohr //empty right side means a removed page. Look up last revision. 411bd17ac90SSatoshi Sahara $rev2 = $this->currentRevision(); 4120c3a5702SAndreas Gohr } 4130c3a5702SAndreas Gohr //collect revisions around rev2 414*d41f5a8fSAndreas Gohr $result2 = $this->retrieveRevisionsAround($rev2, $max); 415*d41f5a8fSAndreas Gohr if ($result2 === false) return [[], []]; 416*d41f5a8fSAndreas Gohr [$revs2, $allRevs, $fp, $lines, $head, $tail] = $result2; 4170c3a5702SAndreas Gohr 4180603e565SAndreas Gohr if (empty($revs2)) return [[], []]; 4190c3a5702SAndreas Gohr 4200c3a5702SAndreas Gohr //collect revisions around rev1 4210f8604a9SAndreas Gohr $index = array_search($rev1, $allRevs); 4220c3a5702SAndreas Gohr if ($index === false) { 4230c3a5702SAndreas Gohr //no overlapping revisions 424*d41f5a8fSAndreas Gohr $result1 = $this->retrieveRevisionsAround($rev1, $max); 425*d41f5a8fSAndreas Gohr if ($result1 === false) { 426*d41f5a8fSAndreas Gohr $revs1 = []; 427*d41f5a8fSAndreas Gohr } else { 428*d41f5a8fSAndreas Gohr [$revs1, , , , , ] = $result1; 4290603e565SAndreas Gohr if (empty($revs1)) $revs1 = []; 430*d41f5a8fSAndreas Gohr } 4310c3a5702SAndreas Gohr } else { 4320c3a5702SAndreas Gohr //revisions overlaps, reuse revisions around rev2 433eeda7adaSGerrit Uitslag $lastRev = array_pop($allRevs); //keep last entry that could be external edit 434eeda7adaSGerrit Uitslag $revs1 = $allRevs; 4350c3a5702SAndreas Gohr while ($head > 0) { 4360c3a5702SAndreas Gohr for ($i = count($lines) - 1; $i >= 0; $i--) { 437a835c93aSGerrit Uitslag $info = $this->parseAndCacheLogLine($lines[$i]); 438a835c93aSGerrit Uitslag if (is_array($info)) { 439bd17ac90SSatoshi Sahara $revs1[] = $info['date']; 4400c3a5702SAndreas Gohr $index++; 4410c3a5702SAndreas Gohr 44285160059SGerrit Uitslag if ($index > (int) ($max / 2)) { 44385160059SGerrit Uitslag break 2; 44485160059SGerrit Uitslag } 4450c3a5702SAndreas Gohr } 4460c3a5702SAndreas Gohr } 4470c3a5702SAndreas Gohr 4480603e565SAndreas Gohr [$lines, $head, $tail] = $this->readAdjacentChunk($fp, $head, $tail, -1); 4490c3a5702SAndreas Gohr } 4500c3a5702SAndreas Gohr sort($revs1); 451eeda7adaSGerrit Uitslag $revs1[] = $lastRev; //push back last entry 4525d9428a0SSatoshi Sahara 4530c3a5702SAndreas Gohr //return wanted selection 4540603e565SAndreas Gohr $revs1 = array_slice($revs1, max($index - (int) ($max / 2), 0), $max); 4550c3a5702SAndreas Gohr } 4560c3a5702SAndreas Gohr 4570603e565SAndreas Gohr return [array_reverse($revs1), array_reverse($revs2)]; 4580c3a5702SAndreas Gohr } 4590c3a5702SAndreas Gohr 4600c3a5702SAndreas Gohr /** 4610c3a5702SAndreas Gohr * Return an existing revision for a specific date which is 4620c3a5702SAndreas Gohr * the current one or younger or equal then the date 4630c3a5702SAndreas Gohr * 4640c3a5702SAndreas Gohr * @param number $date_at timestamp 4650c3a5702SAndreas Gohr * @return string revision ('' for current) 4660c3a5702SAndreas Gohr */ 4670c3a5702SAndreas Gohr public function getLastRevisionAt($date_at) 4680c3a5702SAndreas Gohr { 469d154755dSSatoshi Sahara $fileLastMod = $this->getFilename(); 4700c3a5702SAndreas Gohr //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current 471d154755dSSatoshi Sahara if (file_exists($fileLastMod) && $date_at >= @filemtime($fileLastMod)) { 4720c3a5702SAndreas Gohr return ''; 4730603e565SAndreas Gohr } elseif ($rev = $this->getRelativeRevision($date_at + 1, -1)) { 4740603e565SAndreas Gohr //+1 to get also the requested date revision 4750c3a5702SAndreas Gohr return $rev; 4760c3a5702SAndreas Gohr } else { 4770c3a5702SAndreas Gohr return false; 4780c3a5702SAndreas Gohr } 4790c3a5702SAndreas Gohr } 4800c3a5702SAndreas Gohr 4810c3a5702SAndreas Gohr /** 4820c3a5702SAndreas Gohr * Collect the $max revisions near to the timestamp $rev 4830c3a5702SAndreas Gohr * 484bd17ac90SSatoshi Sahara * Ideally, half of retrieved timestamps are older than $rev, another half are newer. 485eeda7adaSGerrit Uitslag * The returned array $requestedRevs may not contain the reference timestamp $rev 486bd17ac90SSatoshi Sahara * when it does not match any revision value recorded in changelog. 487bd17ac90SSatoshi Sahara * 4880c3a5702SAndreas Gohr * @param int $rev revision timestamp 4890c3a5702SAndreas Gohr * @param int $max maximum number of revisions to be returned 4900c3a5702SAndreas Gohr * @return bool|array 4910c3a5702SAndreas Gohr * return array with entries: 492eeda7adaSGerrit Uitslag * - $requestedRevs: array of with $max revision timestamps 4930c3a5702SAndreas Gohr * - $revs: all parsed revision timestamps 4940c3a5702SAndreas Gohr * - $fp: file pointer only defined for chuck reading, needs closing. 4950c3a5702SAndreas Gohr * - $lines: non-parsed changelog lines before the parsed revisions 496eeda7adaSGerrit Uitslag * - $head: position of first read changelog line 497eeda7adaSGerrit Uitslag * - $lastTail: position of end of last read changelog line 4980c3a5702SAndreas Gohr * otherwise false 4990c3a5702SAndreas Gohr */ 5000c3a5702SAndreas Gohr protected function retrieveRevisionsAround($rev, $max) 5010c3a5702SAndreas Gohr { 5020603e565SAndreas Gohr $revs = []; 5030603e565SAndreas Gohr $afterCount = 0; 5040603e565SAndreas Gohr $beforeCount = 0; 505a3984ddfSSatoshi Sahara 5060c3a5702SAndreas Gohr //get lines from changelog 507*d41f5a8fSAndreas Gohr $result = $this->readloglines($rev); 508*d41f5a8fSAndreas Gohr if ($result === false) return false; 509*d41f5a8fSAndreas Gohr [$fp, $lines, $startHead, $startTail, $eof] = $result; 510bd17ac90SSatoshi Sahara if (empty($lines)) return false; 5110c3a5702SAndreas Gohr 512bd17ac90SSatoshi Sahara //parse changelog lines in chunk, and read forward more chunks until $max/2 is reached 513eeda7adaSGerrit Uitslag $head = $startHead; 514eeda7adaSGerrit Uitslag $tail = $startTail; 5150c3a5702SAndreas Gohr while (count($lines) > 0) { 5160c3a5702SAndreas Gohr foreach ($lines as $line) { 517a835c93aSGerrit Uitslag $info = $this->parseAndCacheLogLine($line); 518a835c93aSGerrit Uitslag if (is_array($info)) { 519bd17ac90SSatoshi Sahara $revs[] = $info['date']; 520bd17ac90SSatoshi Sahara if ($info['date'] >= $rev) { 5210c3a5702SAndreas Gohr //count revs after reference $rev 522eeda7adaSGerrit Uitslag $afterCount++; 52385160059SGerrit Uitslag if ($afterCount == 1) { 52485160059SGerrit Uitslag $beforeCount = count($revs); 52585160059SGerrit Uitslag } 5260c3a5702SAndreas Gohr } 5270c3a5702SAndreas Gohr //enough revs after reference $rev? 52885160059SGerrit Uitslag if ($afterCount > (int) ($max / 2)) { 52985160059SGerrit Uitslag break 2; 53085160059SGerrit Uitslag } 5310c3a5702SAndreas Gohr } 5320c3a5702SAndreas Gohr } 5330c3a5702SAndreas Gohr //retrieve next chunk 5340603e565SAndreas Gohr [$lines, $head, $tail] = $this->readAdjacentChunk($fp, $head, $tail, 1); 5350c3a5702SAndreas Gohr } 536eeda7adaSGerrit Uitslag $lastTail = $tail; 5370c3a5702SAndreas Gohr 538bd17ac90SSatoshi Sahara // add a possible revision of external edit, create or deletion 5397d34963bSAndreas Gohr if ( 5407d34963bSAndreas Gohr $lastTail == $eof && $afterCount <= (int) ($max / 2) && 541df7627d6SSatoshi Sahara count($revs) && !$this->isCurrentRevision($revs[count($revs) - 1]) 542df7627d6SSatoshi Sahara ) { 543bd17ac90SSatoshi Sahara $revs[] = $this->currentRevision; 544eeda7adaSGerrit Uitslag $afterCount++; 545bd17ac90SSatoshi Sahara } 546bd17ac90SSatoshi Sahara 547eeda7adaSGerrit Uitslag if ($afterCount == 0) { 548bd17ac90SSatoshi Sahara //given timestamp $rev is newer than the most recent line in chunk 549bd17ac90SSatoshi Sahara return false; //FIXME: or proceed to collect older revisions? 550bd17ac90SSatoshi Sahara } 551bd17ac90SSatoshi Sahara 552bd17ac90SSatoshi Sahara //read more chunks backward until $max/2 is reached and total number of revs is equal to $max 5530603e565SAndreas Gohr $lines = []; 5540c3a5702SAndreas Gohr $i = 0; 555eeda7adaSGerrit Uitslag $head = $startHead; 556eeda7adaSGerrit Uitslag $tail = $startTail; 5570c3a5702SAndreas Gohr while ($head > 0) { 5580603e565SAndreas Gohr [$lines, $head, $tail] = $this->readAdjacentChunk($fp, $head, $tail, -1); 5590c3a5702SAndreas Gohr 5600c3a5702SAndreas Gohr for ($i = count($lines) - 1; $i >= 0; $i--) { 561a835c93aSGerrit Uitslag $info = $this->parseAndCacheLogLine($lines[$i]); 562a835c93aSGerrit Uitslag if (is_array($info)) { 563bd17ac90SSatoshi Sahara $revs[] = $info['date']; 564eeda7adaSGerrit Uitslag $beforeCount++; 5650c3a5702SAndreas Gohr //enough revs before reference $rev? 56685160059SGerrit Uitslag if ($beforeCount > max((int) ($max / 2), $max - $afterCount)) { 56785160059SGerrit Uitslag break 2; 56885160059SGerrit Uitslag } 5690c3a5702SAndreas Gohr } 5700c3a5702SAndreas Gohr } 5710c3a5702SAndreas Gohr } 5720c3a5702SAndreas Gohr //keep only non-parsed lines 5730c3a5702SAndreas Gohr $lines = array_slice($lines, 0, $i); 574bd17ac90SSatoshi Sahara 575bd17ac90SSatoshi Sahara sort($revs); 576bd17ac90SSatoshi Sahara 5770c3a5702SAndreas Gohr //trunk desired selection 578eeda7adaSGerrit Uitslag $requestedRevs = array_slice($revs, -$max, $max); 5790c3a5702SAndreas Gohr 5800603e565SAndreas Gohr return [$requestedRevs, $revs, $fp, $lines, $head, $lastTail]; 5810c3a5702SAndreas Gohr } 582a3984ddfSSatoshi Sahara 583a3984ddfSSatoshi Sahara /** 584bd17ac90SSatoshi Sahara * Get the current revision information, considering external edit, create or deletion 585bd17ac90SSatoshi Sahara * 586a19054e9SSatoshi Sahara * When the file has not modified since its last revision, the information of the last 58705282e9fSSatoshi Sahara * change that had already recorded in the changelog is returned as current change info. 588a19054e9SSatoshi Sahara * Otherwise, the change information since the last revision caused outside DokuWiki 58905282e9fSSatoshi Sahara * should be returned, which is referred as "external revision". 590bd17ac90SSatoshi Sahara * 59105282e9fSSatoshi Sahara * The change date of the file can be determined by timestamp as far as the file exists, 59205282e9fSSatoshi Sahara * however this is not possible when the file has already deleted outside of DokuWiki. 59366f4cdd4SSatoshi Sahara * In such case we assign 1 sec before current time() for the external deletion. 59405282e9fSSatoshi Sahara * As a result, the value of current revision identifier may change each time because: 59505282e9fSSatoshi Sahara * 1) the file has again modified outside of DokuWiki, or 59605282e9fSSatoshi Sahara * 2) the value is essentially volatile for deleted but once existed files. 597bd17ac90SSatoshi Sahara * 598bd17ac90SSatoshi Sahara * @return bool|array false when page had never existed or array with entries: 599bd17ac90SSatoshi Sahara * - date: revision identifier (timestamp or last revision +1) 600bd17ac90SSatoshi Sahara * - ip: IPv4 address (127.0.0.1) 601bd17ac90SSatoshi Sahara * - type: log line type 602bd17ac90SSatoshi Sahara * - id: id of page or media 603bd17ac90SSatoshi Sahara * - user: user name 604bd17ac90SSatoshi Sahara * - sum: edit summary (or action reason) 605bd17ac90SSatoshi Sahara * - extra: extra data (varies by line type) 606bd17ac90SSatoshi Sahara * - sizechange: change of filesize 607dbf582ddSSatoshi Sahara * - timestamp: unix timestamp or false (key set only for external edit occurred) 608a835c93aSGerrit Uitslag * additional: 609a835c93aSGerrit Uitslag * - mode: page or media 610bd17ac90SSatoshi Sahara * 611bd17ac90SSatoshi Sahara * @author Satoshi Sahara <sahara.satoshi@gmail.com> 612bd17ac90SSatoshi Sahara */ 613bd17ac90SSatoshi Sahara public function getCurrentRevisionInfo() 614bd17ac90SSatoshi Sahara { 615bd17ac90SSatoshi Sahara global $lang; 616bd17ac90SSatoshi Sahara 61785160059SGerrit Uitslag if (isset($this->currentRevision)) { 61885160059SGerrit Uitslag return $this->getRevisionInfo($this->currentRevision); 61985160059SGerrit Uitslag } 620bd17ac90SSatoshi Sahara 621eeda7adaSGerrit Uitslag // get revision id from the item file timestamp and changelog 622dbf582ddSSatoshi Sahara $fileLastMod = $this->getFilename(); 623dbf582ddSSatoshi Sahara $fileRev = @filemtime($fileLastMod); // false when the file not exist 624df7627d6SSatoshi Sahara $lastRev = $this->lastRevision(); // false when no changelog 625bd17ac90SSatoshi Sahara 626df7627d6SSatoshi Sahara if (!$fileRev && !$lastRev) { // has never existed 627df7627d6SSatoshi Sahara $this->currentRevision = false; 628bd17ac90SSatoshi Sahara return false; 629bd17ac90SSatoshi Sahara } elseif ($fileRev === $lastRev) { // not external edit 630bd17ac90SSatoshi Sahara $this->currentRevision = $lastRev; 6315ec96136SSatoshi Sahara return $this->getRevisionInfo($lastRev); 632bd17ac90SSatoshi Sahara } 633bd17ac90SSatoshi Sahara 634bd17ac90SSatoshi Sahara if (!$fileRev && $lastRev) { // item file does not exist 635bd17ac90SSatoshi Sahara // check consistency against changelog 63686216bf0SGerrit Uitslag $revInfo = $this->getRevisionInfo($lastRev, false); 637bd17ac90SSatoshi Sahara if ($revInfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 638bd17ac90SSatoshi Sahara $this->currentRevision = $lastRev; 63954d95e36SGerrit Uitslag return $revInfo; 640bd17ac90SSatoshi Sahara } 641bd17ac90SSatoshi Sahara 64266f4cdd4SSatoshi Sahara // externally deleted, set revision date as late as possible 643bd17ac90SSatoshi Sahara $revInfo = [ 64466f4cdd4SSatoshi Sahara 'date' => max($lastRev + 1, time() - 1), // 1 sec before now or new page save 645bd17ac90SSatoshi Sahara 'ip' => '127.0.0.1', 646bd17ac90SSatoshi Sahara 'type' => DOKU_CHANGE_TYPE_DELETE, 647bd17ac90SSatoshi Sahara 'id' => $this->id, 648bd17ac90SSatoshi Sahara 'user' => '', 649df7627d6SSatoshi Sahara 'sum' => $lang['deleted'] . ' - ' . $lang['external_edit'] . ' (' . $lang['unknowndate'] . ')', 650bd17ac90SSatoshi Sahara 'extra' => '', 6516e695190SAndreas Gohr 'sizechange' => -io_getSizeFile($this->getFilename($lastRev)), 652dbf582ddSSatoshi Sahara 'timestamp' => false, 653a835c93aSGerrit Uitslag 'mode' => $this->getMode() 654bd17ac90SSatoshi Sahara ]; 6550b5bb6b4SGerrit Uitslag } else { // item file exists, with timestamp $fileRev 65654d95e36SGerrit Uitslag // here, file timestamp $fileRev is different with last revision timestamp $lastRev in changelog 657df7627d6SSatoshi Sahara $isJustCreated = $lastRev === false || ( 658e39c2efbSSatoshi Sahara $fileRev > $lastRev && 65986216bf0SGerrit Uitslag $this->getRevisionInfo($lastRev, false)['type'] == DOKU_CHANGE_TYPE_DELETE 660e39c2efbSSatoshi Sahara ); 661bd17ac90SSatoshi Sahara $filesize_new = filesize($this->getFilename()); 6626e695190SAndreas Gohr $filesize_old = $isJustCreated ? 0 : io_getSizeFile($this->getFilename($lastRev)); 663bd17ac90SSatoshi Sahara $sizechange = $filesize_new - $filesize_old; 664bd17ac90SSatoshi Sahara 665dbf582ddSSatoshi Sahara if ($isJustCreated) { 6668ff5c11aSSatoshi Sahara $timestamp = $fileRev; 667bd17ac90SSatoshi Sahara $sum = $lang['created'] . ' - ' . $lang['external_edit']; 668bd17ac90SSatoshi Sahara } elseif ($fileRev > $lastRev) { 6698ff5c11aSSatoshi Sahara $timestamp = $fileRev; 670bd17ac90SSatoshi Sahara $sum = $lang['external_edit']; 671bd17ac90SSatoshi Sahara } else { 672eeda7adaSGerrit Uitslag // $fileRev is older than $lastRev, that is erroneous/incorrect occurrence. 67366f4cdd4SSatoshi Sahara $msg = "Warning: current file modification time is older than last revision date"; 67410f359adSAndreas Gohr $details = 'File revision: ' . $fileRev . ' ' . dformat($fileRev, "%Y-%m-%d %H:%M:%S") . "\n" 67510f359adSAndreas Gohr . 'Last revision: ' . $lastRev . ' ' . dformat($lastRev, "%Y-%m-%d %H:%M:%S"); 67666f4cdd4SSatoshi Sahara Logger::error($msg, $details, $this->getFilename()); 67766f4cdd4SSatoshi Sahara $timestamp = false; 678df7627d6SSatoshi Sahara $sum = $lang['external_edit'] . ' (' . $lang['unknowndate'] . ')'; 679bd17ac90SSatoshi Sahara } 680bd17ac90SSatoshi Sahara 681bd17ac90SSatoshi Sahara // externally created or edited 682bd17ac90SSatoshi Sahara $revInfo = [ 68366f4cdd4SSatoshi Sahara 'date' => $timestamp ?: $lastRev + 1, 684bd17ac90SSatoshi Sahara 'ip' => '127.0.0.1', 685bd17ac90SSatoshi Sahara 'type' => $isJustCreated ? DOKU_CHANGE_TYPE_CREATE : DOKU_CHANGE_TYPE_EDIT, 686bd17ac90SSatoshi Sahara 'id' => $this->id, 687bd17ac90SSatoshi Sahara 'user' => '', 688bd17ac90SSatoshi Sahara 'sum' => $sum, 689bd17ac90SSatoshi Sahara 'extra' => '', 690bd17ac90SSatoshi Sahara 'sizechange' => $sizechange, 691bd17ac90SSatoshi Sahara 'timestamp' => $timestamp, 692a835c93aSGerrit Uitslag 'mode' => $this->getMode() 693bd17ac90SSatoshi Sahara ]; 694bd17ac90SSatoshi Sahara } 695bd17ac90SSatoshi Sahara 696bd17ac90SSatoshi Sahara // cache current revision information of external edition 697bd17ac90SSatoshi Sahara $this->currentRevision = $revInfo['date']; 698bd17ac90SSatoshi Sahara $this->cache[$this->id][$this->currentRevision] = $revInfo; 699bd17ac90SSatoshi Sahara return $this->getRevisionInfo($this->currentRevision); 700bd17ac90SSatoshi Sahara } 701312e7095SSatoshi Sahara 702312e7095SSatoshi Sahara /** 703312e7095SSatoshi Sahara * Mechanism to trace no-actual external current revision 704312e7095SSatoshi Sahara * @param int $rev 705312e7095SSatoshi Sahara */ 706312e7095SSatoshi Sahara public function traceCurrentRevision($rev) 707312e7095SSatoshi Sahara { 708312e7095SSatoshi Sahara if ($rev > $this->lastRevision()) { 709312e7095SSatoshi Sahara $rev = $this->currentRevision(); 710312e7095SSatoshi Sahara } 711312e7095SSatoshi Sahara return $rev; 712312e7095SSatoshi Sahara } 7130c3a5702SAndreas Gohr} 714