From f9528c0c7accd2da60f16864555b4982b940a1a0 Mon Sep 17 00:00:00 2001 From: Klap-in Date: Sun, 27 Jan 2013 22:02:47 +0100 Subject: Added diff navigation Navigation links to move to previous edit before revision in left column or next/last edit after revision in right column. --- inc/changelog.php | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index 9768fea51..e25c3144e 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -543,4 +543,217 @@ function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { return $revs; } +/** + * Get the nth revision left or right handside for a specific page id + * and revision (timestamp). For large changelog files, only the chunk containing the + * reference revision $rev is read and sometimes a next chunck. + * + * Adjacent changelog lines are optimistically parsed and cached to speed up + * consecutive calls to getRevisionInfo. + * + * @author Gerrit Uitslag + * + * based on getRevisionInfo by + * @author Ben Coburn + * @author Kate Arzamastseva + * + * @param string $id pageid + * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) + * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev + * @param int $chunk_size maximum block size + * @param bool $media + * @return bool|string + */ +function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = false) { + global $cache_revinfo; + global $INFO; + $cache =& $cache_revinfo; + if(!isset($cache[$id])) { + $cache[$id] = array(); + } + $rev = max($rev, 0); + + //no direction given or last rev, so no follow-up + if(!$direction || ($direction > 0 && $rev == $INFO['meta']['last_change']['date'])) { + return false; + } + + if($media) { + $file = mediaMetaFN($id, '.changes'); + } else { + $file = metaFN($id, '.changes'); + } + if(!@file_exists($file)) { + return false; + } + + //get $lines from changelog + $lines = array(); + $fp = null; + $tail = 0; + $head = 0; + $eof = 0; + if(filesize($file) < $chunk_size || $chunk_size == 0) { + // read whole file + $uses_chuncks = false; + $lines = file($file); + if($lines === false) { + return false; + } + } else { + // read by chunk + $uses_chuncks = true; + $fp = fopen($file, 'rb'); // "file pointer" + if($fp === false) { + return false; + } //error + $head = 0; + fseek($fp, 0, SEEK_END); //set file position indicator 0 byte from end. + $tail = ftell($fp); //return current position of pointer as integer + $eof = $tail; + $finger = 0; + $finger_rev = 0; + + // find chunk + while($tail - $head > $chunk_size) { + $finger = $head + floor(($tail - $head) / 2.0); + $finger = getNewlinepointer($fp, $finger); + $tmp = fgets($fp); // then read at that location + $tmp = parseChangelogLine($tmp); + $finger_rev = $tmp['date']; + if($finger == $head || $finger == $tail) { + break; + } + if($finger_rev > $rev) { + $tail = $finger; + } else { + $head = $finger; + } + } + + if($tail - $head < 1) { + // cound not find chunk, assume requested rev is missing + fclose($fp); + return false; + } + + $lines = readChunk($fp, $head, $tail); + } + + // look for revisions later then $rev, when founded count till the wanted revision is reached + // also parse and cache changelog lines that pass + $revcounter = 0; + $relrev = false; + $tmp = array(); + $checkotherchunck = true; //always runs once + while(!$relrev && $checkotherchunck) { + + if($direction > 0) { + foreach($lines as $value) { + $tmp = parseChangelogLine($value); + if($tmp !== false) { + $cache[$id][$tmp['date']] = $tmp; + //look for revs older then reference $rev and select $direction-th one + if($tmp['date'] > $rev) { + $revcounter++; + if($revcounter == $direction) { + $relrev = $tmp['date']; + } + } + } + } + } else { + //parse in reverse order + for($i = count($lines) - 1; $i >= 0; $i--) { + $tmp = parseChangelogLine($lines[$i]); + if($tmp !== false) { + $cache[$id][$tmp['date']] = $tmp; + //look for revs older then reference $rev and select $direction-th one + if($tmp['date'] < $rev) { + $revcounter++; + if($revcounter == abs($direction)) { + $relrev = $tmp['date']; + } + } + } + } + } + + //true when $rev is found, but not the wanted follow-up. + $checkotherchunck = $uses_chuncks + && ($tmp['date'] == $rev || ($revcounter > 0 && !$relrev)) + && !feof($fp); + + if($checkotherchunck) { + if($direction > 0) { + //get interval of next chunck, smaller than $chunck_size + $head = $tail; + $lookpointer = true; + $tail = $head + floor($chunk_size * (2 / 3)); + while($lookpointer) { + $tail = min($tail, $eof); + $tail = getNewlinepointer($fp, $tail); + $lookpointer = $tail - $head > $chunk_size; + if($lookpointer) { + $tail = $head + floor(($tail - $head) / 2); + } + } + } else { + $tail = $head; + $head = max($tail - $chunk_size, 0); + $head = getNewlinepointer($fp, $head); + } + + //load next chunck + $lines = readChunk($fp, $head, $tail); + } + } + if($uses_chuncks) { + fclose($fp); + } + if($relrev == $INFO['meta']['last_change']['date']) { + return 'current'; + } + return $relrev; +} + +/** + * Read chunk and return array with lines of given chunck. + * Has no check if $head and $tail are really at a new line + * + * @param $fp resource filepointer + * @param $head int start point chunck + * @param $tail int end point chunck + * @return array lines read from chunck + */ +function readChunk($fp, $head, $tail) { + $chunk = ''; + $chunk_size = max($tail - $head, 0); // found chunk size + $got = 0; + fseek($fp, $head); + while($got < $chunk_size && !feof($fp)) { + $tmp = @fread($fp, max($chunk_size - $got, 0)); + if($tmp === false) { + break; + } //error state + $got += strlen($tmp); + $chunk .= $tmp; + } + $lines = explode("\n", $chunk); + array_pop($lines); // remove trailing newline + return $lines; +} + +/** + * Set pointer to first new line after $finger and return its position + * + * @param $fp resource filepointer + * @param $finger int a pointer + * @return int pointer + */ +function getNewlinepointer($fp, $finger) { + fseek($fp, $finger); + fgets($fp); // slip the finger forward to a new line + return ftell($fp); +} -- cgit v1.2.3 From ce2f604739dc66e32e05f7078ada1711fe387979 Mon Sep 17 00:00:00 2001 From: Klap-in Date: Mon, 28 Jan 2013 01:17:40 +0100 Subject: Add working EoF and BoF checks when chunck reading. Also isset on meta-last_change-date entry. --- inc/changelog.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index e25c3144e..d2be5b2db 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -574,7 +574,7 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = $rev = max($rev, 0); //no direction given or last rev, so no follow-up - if(!$direction || ($direction > 0 && $rev == $INFO['meta']['last_change']['date'])) { + if(!$direction || ($direction > 0 && isset($INFO['meta']['last_change']['date']) && $rev == $INFO['meta']['last_change']['date'])) { return false; } @@ -682,7 +682,7 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = //true when $rev is found, but not the wanted follow-up. $checkotherchunck = $uses_chuncks && ($tmp['date'] == $rev || ($revcounter > 0 && !$relrev)) - && !feof($fp); + && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); if($checkotherchunck) { if($direction > 0) { @@ -712,7 +712,7 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = fclose($fp); } - if($relrev == $INFO['meta']['last_change']['date']) { + if(isset($INFO['meta']['last_change']) && $relrev == $INFO['meta']['last_change']['date']) { return 'current'; } return $relrev; -- cgit v1.2.3 From 9a01fb1de9ece0c4d37cb331e51e3b0b7dcf75a8 Mon Sep 17 00:00:00 2001 From: Klap-in Date: Mon, 28 Jan 2013 01:39:57 +0100 Subject: Add some breaks to while loops. --- inc/changelog.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index d2be5b2db..c5c1c8246 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -697,6 +697,7 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = if($lookpointer) { $tail = $head + floor(($tail - $head) / 2); } + if($tail == $head) break; } } else { $tail = $head; @@ -706,6 +707,7 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = //load next chunck $lines = readChunk($fp, $head, $tail); + if(empty($lines)) break; } } if($uses_chuncks) { -- cgit v1.2.3 From 040f0e135c37c5b544f16277ff69205369df5f1f Mon Sep 17 00:00:00 2001 From: Klap-in Date: Mon, 4 Feb 2013 15:45:59 +0100 Subject: some refactoring --- inc/changelog.php | 265 ++++++++++++++++++++++++------------------------------ 1 file changed, 117 insertions(+), 148 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index c5c1c8246..fc9dc9fd1 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -360,59 +360,13 @@ function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { } else { $file = metaFN($id, '.changes'); } - if (!@file_exists($file)) { return false; } - if (filesize($file)<$chunk_size || $chunk_size==0) { - // read whole file - $lines = file($file); - if ($lines===false) { return false; } - } else { - // read by chunk - $fp = fopen($file, 'rb'); // "file pointer" - if ($fp===false) { return false; } - $head = 0; - fseek($fp, 0, SEEK_END); - $tail = ftell($fp); - $finger = 0; - $finger_rev = 0; - // find chunk - while ($tail-$head>$chunk_size) { - $finger = $head+floor(($tail-$head)/2.0); - fseek($fp, $finger); - fgets($fp); // slip the finger forward to a new line - $finger = ftell($fp); - $tmp = fgets($fp); // then read at that location - $tmp = parseChangelogLine($tmp); - $finger_rev = $tmp['date']; - if ($finger==$head || $finger==$tail) { break; } - if ($finger_rev>$rev) { - $tail = $finger; - } else { - $head = $finger; - } - } - - if ($tail-$head<1) { - // cound not find chunk, assume requested rev is missing - fclose($fp); - return false; - } - - // read chunk - $chunk = ''; - $chunk_size = max($tail-$head, 0); // found chunk size - $got = 0; - fseek($fp, $head); - while ($got<$chunk_size && !feof($fp)) { - $tmp = @fread($fp, max($chunk_size-$got, 0)); - if ($tmp===false) { break; } //error state - $got += strlen($tmp); - $chunk .= $tmp; - } - $lines = explode("\n", $chunk); - array_pop($lines); // remove trailing newline + //read lines from changelog + list($fp, $lines) = _readloglines($file, $rev, $chunk_size); + if($fp) { fclose($fp); } + if(empty($lines)) return false; // parse and cache changelog lines foreach ($lines as $value) { @@ -572,9 +526,13 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = $cache[$id] = array(); } $rev = max($rev, 0); + $direction = (int) $direction; //no direction given or last rev, so no follow-up - if(!$direction || ($direction > 0 && isset($INFO['meta']['last_change']['date']) && $rev == $INFO['meta']['last_change']['date'])) { + if(!$direction || + ($direction > 0 + && isset($INFO['meta']['last_change']['date']) + && $rev == $INFO['meta']['last_change']['date'])) { return false; } @@ -583,42 +541,125 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = } else { $file = metaFN($id, '.changes'); } + + //get lines from changelog + list($fp, $lines, $head, $tail, $eof) = _readloglines($file, $rev, $chunk_size); + if(empty($lines)) return false; + + // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached + // also parse and cache changelog lines for getRevisionInfo(). + $revcounter = 0; + $relativerev = false; + $checkotherchunck = true; //always runs once + while(!$relativerev && $checkotherchunck) { + $tmp = array(); + //parse in normal or reverse order + $count = count($lines); + if($direction > 0) { + $start = 0; + $step = 1; + } else { + $start = $count - 1; + $step = -1; + } + for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { + $tmp = parseChangelogLine($lines[$i]); + if($tmp !== false) { + $cache[$id][$tmp['date']] = $tmp; + //look for revs older/earlier then reference $rev and select $direction-th one + if(($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) { + $revcounter++; + if($revcounter == abs($direction)) { + $relativerev = $tmp['date']; + } + } + } + } + + //true when $rev is found, but not the wanted follow-up. + $checkotherchunck = $fp + && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev)) + && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); + + if($checkotherchunck) { + //search bounds of chunck, rounded on new line, but smaller than $chunck_size + if($direction > 0) { + $head = $tail; + $lookpointer = true; + $tail = $head + floor($chunk_size * (2 / 3)); + while($lookpointer) { + $tail = min($tail, $eof); + $tail = _getNewlinepointer($fp, $tail); + $lookpointer = $tail - $head > $chunk_size; + if($lookpointer) { + $tail = $head + floor(($tail - $head) / 2); + } + if($tail == $head) break; + } + } else { + $tail = $head; + $head = max($tail - $chunk_size, 0); + $head = _getNewlinepointer($fp, $head); + } + + //load next chunck + $lines = _readChunk($fp, $head, $tail); + if(empty($lines)) break; + } + } + if($fp) { + fclose($fp); + } + + if(isset($INFO['meta']['last_change']) && $relativerev == $INFO['meta']['last_change']['date']) { + return 'current'; + } + return $relativerev; +} + +/** + * get lines from changelog. + * If file larger than $chuncksize, only chunck is read that could contain $rev. + * + * @param int $file path to changelog file + * @param int $rev revision timestamp + * @param int $chunk_size maximum block size read from file + * @return array(fp, array(changeloglines), $head, $tail, $eof)|bool + * returns false when not succeed. fp only defined for chuck reading, needs closing. + */ +function _readloglines($file, $rev, $chunk_size) { if(!@file_exists($file)) { return false; } - //get $lines from changelog - $lines = array(); $fp = null; - $tail = 0; $head = 0; + $tail = 0; $eof = 0; if(filesize($file) < $chunk_size || $chunk_size == 0) { // read whole file - $uses_chuncks = false; - $lines = file($file); + $lines = file($file); if($lines === false) { return false; } } else { // read by chunk - $uses_chuncks = true; - $fp = fopen($file, 'rb'); // "file pointer" + $fp = fopen($file, 'rb'); // "file pointer" if($fp === false) { return false; - } //error + } $head = 0; - fseek($fp, 0, SEEK_END); //set file position indicator 0 byte from end. - $tail = ftell($fp); //return current position of pointer as integer - $eof = $tail; + fseek($fp, 0, SEEK_END); + $eof = ftell($fp); + $tail = $eof; $finger = 0; $finger_rev = 0; // find chunk while($tail - $head > $chunk_size) { $finger = $head + floor(($tail - $head) / 2.0); - $finger = getNewlinepointer($fp, $finger); - $tmp = fgets($fp); // then read at that location + $finger = _getNewlinepointer($fp, $finger); + $tmp = fgets($fp); $tmp = parseChangelogLine($tmp); $finger_rev = $tmp['date']; if($finger == $head || $finger == $tail) { @@ -637,87 +678,15 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = return false; } - $lines = readChunk($fp, $head, $tail); - } - - // look for revisions later then $rev, when founded count till the wanted revision is reached - // also parse and cache changelog lines that pass - $revcounter = 0; - $relrev = false; - $tmp = array(); - $checkotherchunck = true; //always runs once - while(!$relrev && $checkotherchunck) { - - if($direction > 0) { - foreach($lines as $value) { - $tmp = parseChangelogLine($value); - if($tmp !== false) { - $cache[$id][$tmp['date']] = $tmp; - //look for revs older then reference $rev and select $direction-th one - if($tmp['date'] > $rev) { - $revcounter++; - if($revcounter == $direction) { - $relrev = $tmp['date']; - } - } - } - } - } else { - //parse in reverse order - for($i = count($lines) - 1; $i >= 0; $i--) { - $tmp = parseChangelogLine($lines[$i]); - if($tmp !== false) { - $cache[$id][$tmp['date']] = $tmp; - //look for revs older then reference $rev and select $direction-th one - if($tmp['date'] < $rev) { - $revcounter++; - if($revcounter == abs($direction)) { - $relrev = $tmp['date']; - } - } - } - } - } - - //true when $rev is found, but not the wanted follow-up. - $checkotherchunck = $uses_chuncks - && ($tmp['date'] == $rev || ($revcounter > 0 && !$relrev)) - && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); - - if($checkotherchunck) { - if($direction > 0) { - //get interval of next chunck, smaller than $chunck_size - $head = $tail; - $lookpointer = true; - $tail = $head + floor($chunk_size * (2 / 3)); - while($lookpointer) { - $tail = min($tail, $eof); - $tail = getNewlinepointer($fp, $tail); - $lookpointer = $tail - $head > $chunk_size; - if($lookpointer) { - $tail = $head + floor(($tail - $head) / 2); - } - if($tail == $head) break; - } - } else { - $tail = $head; - $head = max($tail - $chunk_size, 0); - $head = getNewlinepointer($fp, $head); - } - - //load next chunck - $lines = readChunk($fp, $head, $tail); - if(empty($lines)) break; - } - } - if($uses_chuncks) { - fclose($fp); + $lines = _readChunk($fp, $head, $tail); } - - if(isset($INFO['meta']['last_change']) && $relrev == $INFO['meta']['last_change']['date']) { - return 'current'; - } - return $relrev; + return array( + $fp, + $lines, + $head, + $tail, + $eof + ); } /** @@ -729,16 +698,16 @@ function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = * @param $tail int end point chunck * @return array lines read from chunck */ -function readChunk($fp, $head, $tail) { +function _readChunk($fp, $head, $tail) { $chunk = ''; $chunk_size = max($tail - $head, 0); // found chunk size $got = 0; fseek($fp, $head); while($got < $chunk_size && !feof($fp)) { $tmp = @fread($fp, max($chunk_size - $got, 0)); - if($tmp === false) { + if($tmp === false) { //error state break; - } //error state + } $got += strlen($tmp); $chunk .= $tmp; } @@ -754,7 +723,7 @@ function readChunk($fp, $head, $tail) { * @param $finger int a pointer * @return int pointer */ -function getNewlinepointer($fp, $finger) { +function _getNewlinepointer($fp, $finger) { fseek($fp, $finger); fgets($fp); // slip the finger forward to a new line return ftell($fp); -- cgit v1.2.3 From 8eccf945d3e3d5f072979c015b21624810f7d0a8 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Wed, 20 Nov 2013 13:18:44 +0100 Subject: page related changelog reading refactored to class --- inc/changelog.php | 771 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 430 insertions(+), 341 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index f7b07ae57..36be9dc79 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -334,399 +334,488 @@ function _handleRecent($line,$ns,$flags,&$seen){ } /** - * Get the changelog information for a specific page id - * and revision (timestamp). Adjacent changelog lines - * are optimistically parsed and cached to speed up - * consecutive calls to getRevisionInfo. For large - * changelog files, only the chunk containing the - * requested changelog line is read. - * - * @author Ben Coburn - * @author Kate Arzamastseva + * Class PageRevisionLog */ -function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { - global $cache_revinfo; - $cache =& $cache_revinfo; - if (!isset($cache[$id])) { $cache[$id] = array(); } - $rev = max($rev, 0); - - // check if it's already in the memory cache - if (isset($cache[$id]) && isset($cache[$id][$rev])) { - return $cache[$id][$rev]; - } - - if ($media) { - $file = mediaMetaFN($id, '.changes'); - } else { - $file = metaFN($id, '.changes'); - } +class PageRevisionLog { + + /** @var string */ + private $id; + /** @var int */ + private $chunk_size; + /** @var array */ + private $cache; + + /** + * Constructor + * + * @param string $id page id + * @param int $chunk_size maximum block size read from file + */ + public function __construct($id, $chunk_size = 8192) { + global $cache_revinfo; + + $this->cache =& $cache_revinfo; + if(!isset($this->cache[$id])) { + $this->cache[$id] = array(); + } - //read lines from changelog - list($fp, $lines) = _readloglines($file, $rev, $chunk_size); - if($fp) { - fclose($fp); - } - if(empty($lines)) return false; + $this->id = $id; + $this->setChunkSize($chunk_size); - // parse and cache changelog lines - foreach ($lines as $value) { - $tmp = parseChangelogLine($value); - if ($tmp!==false) { - $cache[$id][$tmp['date']] = $tmp; - } } - if (!isset($cache[$id][$rev])) { return false; } - return $cache[$id][$rev]; -} -/** - * Return a list of page revisions numbers - * Does not guarantee that the revision exists in the attic, - * only that a line with the date exists in the changelog. - * By default the current revision is skipped. - * - * id: the page of interest - * first: skip the first n changelog lines - * num: number of revisions to return - * - * The current revision is automatically skipped when the page exists. - * See $INFO['meta']['last_change'] for the current revision. - * - * For efficiency, the log lines are parsed and cached for later - * calls to getRevisionInfo. Large changelog files are read - * backwards in chunks until the requested number of changelog - * lines are recieved. - * - * @author Ben Coburn - * @author Kate Arzamastseva - */ -function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { - global $cache_revinfo; - $cache =& $cache_revinfo; - if (!isset($cache[$id])) { $cache[$id] = array(); } + /** + * Set chunk size for file reading + * + * @param int $chunk_size maximum block size read from file + */ + public function setChunkSize($chunk_size) { + if(!is_numeric($chunk_size)) $chunk_size = 0; - $revs = array(); - $lines = array(); - $count = 0; - if ($media) { - $file = mediaMetaFN($id, '.changes'); - } else { - $file = metaFN($id, '.changes'); - } - $num = max($num, 0); - if ($num == 0) { return $revs; } - - $chunk_size = max($chunk_size, 0); - if ($first<0) { - $first = 0; - } else if (!$media && @file_exists(wikiFN($id)) || $media && @file_exists(mediaFN($id))) { - // skip current revision if the page exists - $first = max($first+1, 0); + $this->chunk_size = (int) max($chunk_size, 0); } - if (!@file_exists($file)) { return $revs; } - if (filesize($file)<$chunk_size || $chunk_size==0) { - // read whole file - $lines = file($file); - if ($lines===false) { return $revs; } - } else { - // read chunks backwards - $fp = fopen($file, 'rb'); // "file pointer" - if ($fp===false) { return $revs; } - fseek($fp, 0, SEEK_END); - $tail = ftell($fp); - - // chunk backwards - $finger = max($tail-$chunk_size, 0); - while ($count<$num+$first) { - fseek($fp, $finger); - $nl = $finger; - if ($finger>0) { - fgets($fp); // slip the finger forward to a new line - $nl = ftell($fp); - } + /** + * Get the changelog information for a specific page id and revision (timestamp) + * + * Adjacent changelog lines are optimistically parsed and cached to speed up + * consecutive calls to getRevisionInfo. For large changelog files, only the chunk + * containing the requested changelog line is read. + * + * @param int $rev revision timestamp + * @param bool $media look into media log? + * @return bool|array false or array with entries: + * - date: unix timestamp + * - ip: IPv4 address (127.0.0.1) + * - type: log line type + * - id: page id + * - user: user name + * - sum: edit summary (or action reason) + * - extra: extra data (varies by line type) + * + * @author Ben Coburn + * @author Kate Arzamastseva + */ + public function getRevisionInfo($rev, $media = false) { + $rev = max($rev, 0); + + // check if it's already in the memory cache + if(isset($this->cache[$this->id]) && isset($this->cache[$this->id][$rev])) { + return $this->cache[$this->id][$rev]; + } - // was the chunk big enough? if not, take another bite - if($nl > 0 && $tail <= $nl){ - $finger = max($finger-$chunk_size, 0); - continue; - }else{ - $finger = $nl; - } + //read lines from changelog + list($fp, $lines) = $this->readloglines($media, $rev); + if($fp) { + fclose($fp); + } + if(empty($lines)) return false; - // read chunk - $chunk = ''; - $read_size = max($tail-$finger, 0); // found chunk size - $got = 0; - while ($got<$read_size && !feof($fp)) { - $tmp = @fread($fp, max($read_size-$got, 0)); - if ($tmp===false) { break; } //error state - $got += strlen($tmp); - $chunk .= $tmp; - } - $tmp = explode("\n", $chunk); - array_pop($tmp); // remove trailing newline - - // combine with previous chunk - $count += count($tmp); - $lines = array_merge($tmp, $lines); - - // next chunk - if ($finger==0) { break; } // already read all the lines - else { - $tail = $finger; - $finger = max($tail-$chunk_size, 0); + // parse and cache changelog lines + foreach($lines as $value) { + $tmp = parseChangelogLine($value); + if($tmp !== false) { + $this->cache[$this->id][$tmp['date']] = $tmp; } } - fclose($fp); - } - - // skip parsing extra lines - $num = max(min(count($lines)-$first, $num), 0); - if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } - else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } - else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } - - // handle lines in reverse order - for ($i = count($lines)-1; $i >= 0; $i--) { - $tmp = parseChangelogLine($lines[$i]); - if ($tmp!==false) { - $cache[$id][$tmp['date']] = $tmp; - $revs[] = $tmp['date']; + if(!isset($this->cache[$this->id][$rev])) { + return false; } + return $this->cache[$this->id][$rev]; } - return $revs; -} + /** + * Return a list of page revisions numbers + * + * Does not guarantee that the revision exists in the attic, + * only that a line with the date exists in the changelog. + * By default the current revision is skipped. + * + * The current revision is automatically skipped when the page exists. + * See $INFO['meta']['last_change'] for the current revision. + * A negative $first let read the current revision too. + * + * For efficiency, the log lines are parsed and cached for later + * calls to getRevisionInfo. Large changelog files are read + * backwards in chunks until the requested number of changelog + * lines are recieved. + * + * @param int $first skip the first n changelog lines + * @param int $num number of revisions to return + * @param bool $media look into media log? + * @return array with the revision timestamps + * + * @author Ben Coburn + * @author Kate Arzamastseva + */ + public function getRevisions($first, $num, $media = false) { + $revs = array(); + $lines = array(); + $count = 0; + if ($media) { + $file = mediaMetaFN($this->id, '.changes'); + } else { + $file = metaFN($this->id, '.changes'); + } + $num = max($num, 0); + if ($num == 0) { return $revs; } + + $this->chunk_size = max($this->chunk_size, 0); + if ($first<0) { + $first = 0; + } else if (!$media && @file_exists(wikiFN($this->id)) || $media && @file_exists(mediaFN($this->id))) { + // skip current revision if the page exists + $first = max($first+1, 0); + } -/** - * Get the nth revision left or right handside for a specific page id - * and revision (timestamp). For large changelog files, only the chunk containing the - * reference revision $rev is read and sometimes a next chunck. - * - * Adjacent changelog lines are optimistically parsed and cached to speed up - * consecutive calls to getRevisionInfo. - * - * @author Gerrit Uitslag - * - * based on getRevisionInfo by - * @author Ben Coburn - * @author Kate Arzamastseva - * - * @param string $id pageid - * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) - * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev - * @param int $chunk_size maximum block size - * @param bool $media - * @return bool|string - */ -function getRelativeRevision($id, $rev, $direction, $chunk_size = 8192, $media = false) { - global $cache_revinfo; - global $INFO; - $cache =& $cache_revinfo; - if(!isset($cache[$id])) { - $cache[$id] = array(); - } - $rev = max($rev, 0); - $direction = (int) $direction; - - //no direction given or last rev, so no follow-up - if(!$direction || - ($direction > 0 - && isset($INFO['meta']['last_change']['date']) - && $rev == $INFO['meta']['last_change']['date'])) { - return false; - } + if (!@file_exists($file)) { return $revs; } + if (filesize($file)<$this->chunk_size || $this->chunk_size==0) { + // read whole file + $lines = file($file); + if ($lines===false) { return $revs; } + } else { + // read chunks backwards + $fp = fopen($file, 'rb'); // "file pointer" + if ($fp===false) { return $revs; } + fseek($fp, 0, SEEK_END); + $tail = ftell($fp); + + // chunk backwards + $finger = max($tail-$this->chunk_size, 0); + while ($count<$num+$first) { + fseek($fp, $finger); + $nl = $finger; + if ($finger>0) { + fgets($fp); // slip the finger forward to a new line + $nl = ftell($fp); + } - if($media) { - $file = mediaMetaFN($id, '.changes'); - } else { - $file = metaFN($id, '.changes'); - } + // was the chunk big enough? if not, take another bite + if($nl > 0 && $tail <= $nl){ + $finger = max($finger-$this->chunk_size, 0); + continue; + }else{ + $finger = $nl; + } - //get lines from changelog - list($fp, $lines, $head, $tail, $eof) = _readloglines($file, $rev, $chunk_size); - if(empty($lines)) return false; - - // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached - // also parse and cache changelog lines for getRevisionInfo(). - $revcounter = 0; - $relativerev = false; - $checkotherchunck = true; //always runs once - while(!$relativerev && $checkotherchunck) { - $tmp = array(); - //parse in normal or reverse order - $count = count($lines); - if($direction > 0) { - $start = 0; - $step = 1; - } else { - $start = $count - 1; - $step = -1; + // read chunk + $chunk = ''; + $read_size = max($tail-$finger, 0); // found chunk size + $got = 0; + while ($got<$read_size && !feof($fp)) { + $tmp = @fread($fp, max($read_size-$got, 0)); + if ($tmp===false) { break; } //error state + $got += strlen($tmp); + $chunk .= $tmp; + } + $tmp = explode("\n", $chunk); + array_pop($tmp); // remove trailing newline + + // combine with previous chunk + $count += count($tmp); + $lines = array_merge($tmp, $lines); + + // next chunk + if ($finger==0) { break; } // already read all the lines + else { + $tail = $finger; + $finger = max($tail-$this->chunk_size, 0); + } + } + fclose($fp); } - for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { + + // skip parsing extra lines + $num = max(min(count($lines)-$first, $num), 0); + if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } + else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } + else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } + + // handle lines in reverse order + for ($i = count($lines)-1; $i >= 0; $i--) { $tmp = parseChangelogLine($lines[$i]); - if($tmp !== false) { - $cache[$id][$tmp['date']] = $tmp; - //look for revs older/earlier then reference $rev and select $direction-th one - if(($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) { - $revcounter++; - if($revcounter == abs($direction)) { - $relativerev = $tmp['date']; - } - } + if ($tmp!==false) { + $this->cache[$this->id][$tmp['date']] = $tmp; + $revs[] = $tmp['date']; } } - //true when $rev is found, but not the wanted follow-up. - $checkotherchunck = $fp - && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev)) - && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); + return $revs; + } + + /** + * Get the nth revision left or right handside for a specific page id and revision (timestamp) + * + * For large changelog files, only the chunk containing the + * reference revision $rev is read and sometimes a next chunck. + * + * Adjacent changelog lines are optimistically parsed and cached to speed up + * consecutive calls to getRevisionInfo. + * + * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) + * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev + * @param bool $media look into media log? + * @return bool|int + * timestamp of the requested revision + * otherwise false + */ + public function getRelativeRevision($rev, $direction, $media = false) { + global $INFO; + + $rev = max($rev, 0); + $direction = (int) $direction; + + //no direction given or last rev, so no follow-up + if(!$direction || + ($direction > 0 + && isset($INFO['meta']['last_change']['date']) + && $rev == $INFO['meta']['last_change']['date'])) { + return false; + } - if($checkotherchunck) { - //search bounds of chunck, rounded on new line, but smaller than $chunck_size + //get lines from changelog + list($fp, $lines, $head, $tail, $eof) = $this->readloglines($media, $rev); + if(empty($lines)) return false; + + // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached + // also parse and cache changelog lines for getRevisionInfo(). + $revcounter = 0; + $relativerev = false; + $checkotherchunck = true; //always runs once + while(!$relativerev && $checkotherchunck) { + $tmp = array(); + //parse in normal or reverse order + $count = count($lines); if($direction > 0) { - $head = $tail; - $lookpointer = true; - $tail = $head + floor($chunk_size * (2 / 3)); - while($lookpointer) { - $tail = min($tail, $eof); - $tail = _getNewlinepointer($fp, $tail); - $lookpointer = $tail - $head > $chunk_size; - if($lookpointer) { - $tail = $head + floor(($tail - $head) / 2); + $start = 0; + $step = 1; + } else { + $start = $count - 1; + $step = -1; + } + for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { + $tmp = parseChangelogLine($lines[$i]); + if($tmp !== false) { + $this->cache[$this->id][$tmp['date']] = $tmp; + //look for revs older/earlier then reference $rev and select $direction-th one + if(($direction > 0 && $tmp['date'] > $rev) || ($direction < 0 && $tmp['date'] < $rev)) { + $revcounter++; + if($revcounter == abs($direction)) { + $relativerev = $tmp['date']; + } } - if($tail == $head) break; } - } else { - $tail = $head; - $head = max($tail - $chunk_size, 0); - $head = _getNewlinepointer($fp, $head); } - //load next chunck - $lines = _readChunk($fp, $head, $tail); - if(empty($lines)) break; + //true when $rev is found, but not the wanted follow-up. + $checkotherchunck = $fp + && ($tmp['date'] == $rev || ($revcounter > 0 && !$relativerev)) + && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); + + if($checkotherchunck) { + //search bounds of chunck, rounded on new line, but smaller than $chunck_size + if($direction > 0) { + $head = $tail; + $lookpointer = true; + $tail = $head + floor($this->chunk_size * (2 / 3)); + while($lookpointer) { + $tail = min($tail, $eof); + $tail = $this->getNewlinepointer($fp, $tail); + $lookpointer = $tail - $head > $this->chunk_size; + if($lookpointer) { + $tail = $head + floor(($tail - $head) / 2); + } + if($tail == $head) break; + } + } else { + $tail = $head; + $head = max($tail - $this->chunk_size, 0); + $head = $this->getNewlinepointer($fp, $head); + } + + //load next chunck + $lines = $this->readChunk($fp, $head, $tail); + if(empty($lines)) break; + } + } + if($fp) { + fclose($fp); } - } - if($fp) { - fclose($fp); - } - if(isset($INFO['meta']['last_change']) && $relativerev == $INFO['meta']['last_change']['date']) { - return 'current'; + return $relativerev; } - return $relativerev; -} -/** - * get lines from changelog. - * If file larger than $chuncksize, only chunck is read that could contain $rev. - * - * @param int $file path to changelog file - * @param int $rev revision timestamp - * @param int $chunk_size maximum block size read from file - * @return array(fp, array(changeloglines), $head, $tail, $eof)|bool - * returns false when not succeed. fp only defined for chuck reading, needs closing. - */ -function _readloglines($file, $rev, $chunk_size) { - if(!@file_exists($file)) { - return false; - } - $fp = null; - $head = 0; - $tail = 0; - $eof = 0; - if(filesize($file) < $chunk_size || $chunk_size == 0) { - // read whole file - $lines = file($file); - if($lines === false) { - return false; + /** + * Returns lines from changelog. + * If file larger than $chuncksize, only chunck is read that could contain $rev. + * + * @param bool $media look into media log? + * @param int $rev revision timestamp + * @return array(fp, array(changeloglines), $head, $tail, $eof)|bool + * returns false when not succeed. fp only defined for chuck reading, needs closing. + */ + protected function readloglines($media, $rev) { + if($media) { + $file = mediaMetaFN($this->id, '.changes'); + } else { + $file = metaFN($this->id, '.changes'); } - } else { - // read by chunk - $fp = fopen($file, 'rb'); // "file pointer" - if($fp === false) { + + if(!@file_exists($file)) { return false; } - $head = 0; - fseek($fp, 0, SEEK_END); - $eof = ftell($fp); - $tail = $eof; - $finger = 0; - $finger_rev = 0; - - // find chunk - while($tail - $head > $chunk_size) { - $finger = $head + floor(($tail - $head) / 2.0); - $finger = _getNewlinepointer($fp, $finger); - $tmp = fgets($fp); - $tmp = parseChangelogLine($tmp); - $finger_rev = $tmp['date']; - if($finger == $head || $finger == $tail) { - break; + + $fp = null; + $head = 0; + $tail = 0; + $eof = 0; + + if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { + // read whole file + $lines = file($file); + if($lines === false) { + return false; } - if($finger_rev > $rev) { - $tail = $finger; - } else { - $head = $finger; + } else { + // read by chunk + $fp = fopen($file, 'rb'); // "file pointer" + if($fp === false) { + return false; + } + $head = 0; + fseek($fp, 0, SEEK_END); + $eof = ftell($fp); + $tail = $eof; + + // find chunk + while($tail - $head > $this->chunk_size) { + $finger = $head + floor(($tail - $head) / 2.0); + $finger = $this->getNewlinepointer($fp, $finger); + $tmp = fgets($fp); + $tmp = parseChangelogLine($tmp); + $finger_rev = $tmp['date']; + if($finger == $head || $finger == $tail) { + break; + } + if($finger_rev > $rev) { + $tail = $finger; + } else { + $head = $finger; + } + } + + if($tail - $head < 1) { + // cound not find chunk, assume requested rev is missing + fclose($fp); + return false; } + + $lines = $this->readChunk($fp, $head, $tail); } + return array( + $fp, + $lines, + $head, + $tail, + $eof + ); + } - if($tail - $head < 1) { - // cound not find chunk, assume requested rev is missing - fclose($fp); - return false; + /** + * Read chunk and return array with lines of given chunck. + * Has no check if $head and $tail are really at a new line + * + * @param $fp resource filepointer + * @param $head int start point chunck + * @param $tail int end point chunck + * @return array lines read from chunck + */ + protected function readChunk($fp, $head, $tail) { + $chunk = ''; + $chunk_size = max($tail - $head, 0); // found chunk size + $got = 0; + fseek($fp, $head); + while($got < $chunk_size && !feof($fp)) { + $tmp = @fread($fp, max($chunk_size - $got, 0)); + if($tmp === false) { //error state + break; + } + $got += strlen($tmp); + $chunk .= $tmp; } + $lines = explode("\n", $chunk); + array_pop($lines); // remove trailing newline + return $lines; + } - $lines = _readChunk($fp, $head, $tail); + /** + * Set pointer to first new line after $finger and return its position + * + * @param $fp resource filepointer + * @param $finger int a pointer + * @return int pointer + */ + protected function getNewlinepointer($fp, $finger) { + fseek($fp, $finger); + fgets($fp); // slip the finger forward to a new line + return ftell($fp); + } + + /** + * Check whether given revision is the current page + * + * @param int $rev timestamp of current page + * @return bool true if $rev is current revision, otherwise false + */ + static public function isCurrentRevision($rev){ + return isset($INFO['meta']['last_change']) && $rev == $INFO['meta']['last_change']['date']; } - return array( - $fp, - $lines, - $head, - $tail, - $eof - ); } /** - * Read chunk and return array with lines of given chunck. - * Has no check if $head and $tail are really at a new line + * Get the changelog information for a specific page id + * and revision (timestamp). Adjacent changelog lines + * are optimistically parsed and cached to speed up + * consecutive calls to getRevisionInfo. For large + * changelog files, only the chunk containing the + * requested changelog line is read. * - * @param $fp resource filepointer - * @param $head int start point chunck - * @param $tail int end point chunck - * @return array lines read from chunck + * @deprecated 20-11-2013 + * + * @author Ben Coburn + * @author Kate Arzamastseva */ -function _readChunk($fp, $head, $tail) { - $chunk = ''; - $chunk_size = max($tail - $head, 0); // found chunk size - $got = 0; - fseek($fp, $head); - while($got < $chunk_size && !feof($fp)) { - $tmp = @fread($fp, max($chunk_size - $got, 0)); - if($tmp === false) { //error state - break; - } - $got += strlen($tmp); - $chunk .= $tmp; - } - $lines = explode("\n", $chunk); - array_pop($lines); // remove trailing newline - return $lines; +function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { + + $log = new PageRevisionLog($id, $chunk_size); + return $log->getRevisionInfo($rev, $media); } /** - * Set pointer to first new line after $finger and return its position + * Return a list of page revisions numbers + * Does not guarantee that the revision exists in the attic, + * only that a line with the date exists in the changelog. + * By default the current revision is skipped. + * + * id: the page of interest + * first: skip the first n changelog lines + * num: number of revisions to return + * + * The current revision is automatically skipped when the page exists. + * See $INFO['meta']['last_change'] for the current revision. + * + * For efficiency, the log lines are parsed and cached for later + * calls to getRevisionInfo. Large changelog files are read + * backwards in chunks until the requested number of changelog + * lines are recieved. + * + * @deprecated 20-11-2013 * - * @param $fp resource filepointer - * @param $finger int a pointer - * @return int pointer + * @author Ben Coburn + * @author Kate Arzamastseva */ -function _getNewlinepointer($fp, $finger) { - fseek($fp, $finger); - fgets($fp); // slip the finger forward to a new line - return ftell($fp); +function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { + $log = new PageRevisionLog($id, $chunk_size); + return $log->getRevisions($first, $num, $media); } -- cgit v1.2.3 From ee33e0c5628d9cab52676a5aa2f55356c563c209 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Wed, 20 Nov 2013 13:47:13 +0100 Subject: isCurrentPage is page dependent --- inc/changelog.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index 36be9dc79..de26fdf6a 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -560,16 +560,11 @@ class PageRevisionLog { * otherwise false */ public function getRelativeRevision($rev, $direction, $media = false) { - global $INFO; - $rev = max($rev, 0); $direction = (int) $direction; //no direction given or last rev, so no follow-up - if(!$direction || - ($direction > 0 - && isset($INFO['meta']['last_change']['date']) - && $rev == $INFO['meta']['last_change']['date'])) { + if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev)) ) { return false; } @@ -768,7 +763,7 @@ class PageRevisionLog { * @param int $rev timestamp of current page * @return bool true if $rev is current revision, otherwise false */ - static public function isCurrentRevision($rev){ + public function isCurrentRevision($rev){ return isset($INFO['meta']['last_change']) && $rev == $INFO['meta']['last_change']['date']; } } -- cgit v1.2.3 From 0f13c836f0ee71a3188a775b9ea64025e10d38f5 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Thu, 21 Nov 2013 18:02:12 +0100 Subject: Improved isCurrentRevision(), tests included --- inc/changelog.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index de26fdf6a..722365853 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -760,11 +760,12 @@ class PageRevisionLog { /** * Check whether given revision is the current page * - * @param int $rev timestamp of current page + * @param int $rev timestamp of current page + * @param bool $media look for media? * @return bool true if $rev is current revision, otherwise false */ - public function isCurrentRevision($rev){ - return isset($INFO['meta']['last_change']) && $rev == $INFO['meta']['last_change']['date']; + public function isCurrentRevision($rev, $media = false) { + return $rev == @filemtime($media ? mediaFN($this->id) : wikiFN($this->id)); } } -- cgit v1.2.3 From 047bad06fab8157452aa0dd04379a7c507b1f39f Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Thu, 21 Nov 2013 21:07:08 +0100 Subject: refactor PageRevisionLog into Media- and PageChangelog extending Changelog --- inc/changelog.php | 124 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 35 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index 722365853..f70f20ff9 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -334,16 +334,17 @@ function _handleRecent($line,$ns,$flags,&$seen){ } /** - * Class PageRevisionLog + * Class ChangeLog + * methods for handling of changelog of pages or media files */ -class PageRevisionLog { +abstract class ChangeLog { /** @var string */ - private $id; + protected $id; /** @var int */ - private $chunk_size; + protected $chunk_size; /** @var array */ - private $cache; + protected $cache; /** * Constructor @@ -366,6 +367,7 @@ class PageRevisionLog { /** * Set chunk size for file reading + * Chunk size zero let read whole file at once * * @param int $chunk_size maximum block size read from file */ @@ -375,6 +377,20 @@ class PageRevisionLog { $this->chunk_size = (int) max($chunk_size, 0); } + /** + * Returns path to changelog + * + * @return string path to file + */ + abstract protected function getChangelogFilename(); + + /** + * Returns path to current page/media + * + * @return string path to file + */ + abstract protected function getFilename(); + /** * Get the changelog information for a specific page id and revision (timestamp) * @@ -383,7 +399,6 @@ class PageRevisionLog { * containing the requested changelog line is read. * * @param int $rev revision timestamp - * @param bool $media look into media log? * @return bool|array false or array with entries: * - date: unix timestamp * - ip: IPv4 address (127.0.0.1) @@ -396,7 +411,7 @@ class PageRevisionLog { * @author Ben Coburn * @author Kate Arzamastseva */ - public function getRevisionInfo($rev, $media = false) { + public function getRevisionInfo($rev) { $rev = max($rev, 0); // check if it's already in the memory cache @@ -405,7 +420,7 @@ class PageRevisionLog { } //read lines from changelog - list($fp, $lines) = $this->readloglines($media, $rev); + list($fp, $lines) = $this->readloglines($rev); if($fp) { fclose($fp); } @@ -442,32 +457,28 @@ class PageRevisionLog { * * @param int $first skip the first n changelog lines * @param int $num number of revisions to return - * @param bool $media look into media log? * @return array with the revision timestamps * * @author Ben Coburn * @author Kate Arzamastseva */ - public function getRevisions($first, $num, $media = false) { + public function getRevisions($first, $num) { $revs = array(); $lines = array(); $count = 0; - if ($media) { - $file = mediaMetaFN($this->id, '.changes'); - } else { - $file = metaFN($this->id, '.changes'); - } + $num = max($num, 0); if ($num == 0) { return $revs; } - $this->chunk_size = max($this->chunk_size, 0); if ($first<0) { $first = 0; - } else if (!$media && @file_exists(wikiFN($this->id)) || $media && @file_exists(mediaFN($this->id))) { + } else if (@file_exists($this->getFilename())) { // skip current revision if the page exists $first = max($first+1, 0); } + $file = $this->getChangelogFilename(); + if (!@file_exists($file)) { return $revs; } if (filesize($file)<$this->chunk_size || $this->chunk_size==0) { // read whole file @@ -554,12 +565,11 @@ class PageRevisionLog { * * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev - * @param bool $media look into media log? * @return bool|int * timestamp of the requested revision * otherwise false */ - public function getRelativeRevision($rev, $direction, $media = false) { + public function getRelativeRevision($rev, $direction) { $rev = max($rev, 0); $direction = (int) $direction; @@ -569,7 +579,7 @@ class PageRevisionLog { } //get lines from changelog - list($fp, $lines, $head, $tail, $eof) = $this->readloglines($media, $rev); + list($fp, $lines, $head, $tail, $eof) = $this->readloglines($rev); if(empty($lines)) return false; // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached @@ -645,17 +655,12 @@ class PageRevisionLog { * Returns lines from changelog. * If file larger than $chuncksize, only chunck is read that could contain $rev. * - * @param bool $media look into media log? * @param int $rev revision timestamp * @return array(fp, array(changeloglines), $head, $tail, $eof)|bool * returns false when not succeed. fp only defined for chuck reading, needs closing. */ - protected function readloglines($media, $rev) { - if($media) { - $file = mediaMetaFN($this->id, '.changes'); - } else { - $file = metaFN($this->id, '.changes'); - } + protected function readloglines($rev) { + $file = $this->getChangelogFilename(); if(!@file_exists($file)) { return false; @@ -761,14 +766,56 @@ class PageRevisionLog { * Check whether given revision is the current page * * @param int $rev timestamp of current page - * @param bool $media look for media? * @return bool true if $rev is current revision, otherwise false */ - public function isCurrentRevision($rev, $media = false) { - return $rev == @filemtime($media ? mediaFN($this->id) : wikiFN($this->id)); + public function isCurrentRevision($rev) { + return $rev == @filemtime($this->getFilename()); + } +} + +class PageChangelog extends ChangeLog { + + /** + * Returns path to changelog + * + * @return string path to file + */ + protected function getChangelogFilename() { + return metaFN($this->id, '.changes'); + } + + /** + * Returns path to current page/media + * + * @return string path to file + */ + protected function getFilename() { + return wikiFN($this->id); + } +} + +class MediaChangelog extends ChangeLog { + + /** + * Returns path to changelog + * + * @return string path to file + */ + protected function getChangelogFilename() { + return mediaMetaFN($this->id, '.changes'); + } + + /** + * Returns path to current page/media + * + * @return string path to file + */ + protected function getFilename() { + return mediaFN($this->id); } } + /** * Get the changelog information for a specific page id * and revision (timestamp). Adjacent changelog lines @@ -783,9 +830,12 @@ class PageRevisionLog { * @author Kate Arzamastseva */ function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { - - $log = new PageRevisionLog($id, $chunk_size); - return $log->getRevisionInfo($rev, $media); + if($media) { + $changelog = new MediaChangeLog($id, $chunk_size); + } else { + $changelog = new PageChangeLog($id, $chunk_size); + } + return $changelog->getRevisionInfo($rev); } /** @@ -812,6 +862,10 @@ function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { * @author Kate Arzamastseva */ function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { - $log = new PageRevisionLog($id, $chunk_size); - return $log->getRevisions($first, $num, $media); + if($media) { + $changelog = new MediaChangeLog($id, $chunk_size); + } else { + $changelog = new PageChangeLog($id, $chunk_size); + } + return $changelog->getRevisions($first, $num); } -- cgit v1.2.3 From 59cce2d943ee9a18fafc9a0594ede031f7bf7190 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Fri, 22 Nov 2013 15:30:48 +0100 Subject: Improve changelog reading. Inclusive unittests for chunks smaller than changelog lines. --- inc/changelog.php | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index f70f20ff9..f47042066 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -494,12 +494,7 @@ abstract class ChangeLog { // chunk backwards $finger = max($tail-$this->chunk_size, 0); while ($count<$num+$first) { - fseek($fp, $finger); - $nl = $finger; - if ($finger>0) { - fgets($fp); // slip the finger forward to a new line - $nl = ftell($fp); - } + $nl = getNewlinepointer($fp, $finger); // was the chunk big enough? if not, take another bite if($nl > 0 && $tail <= $nl){ @@ -514,7 +509,7 @@ abstract class ChangeLog { $read_size = max($tail-$finger, 0); // found chunk size $got = 0; while ($got<$read_size && !feof($fp)) { - $tmp = @fread($fp, max($read_size-$got, 0)); + $tmp = @fread($fp, max($read_size-$got, 0)); //todo why not use chunk_size? if ($tmp===false) { break; } //error state $got += strlen($tmp); $chunk .= $tmp; @@ -620,22 +615,22 @@ abstract class ChangeLog { if($checkotherchunck) { //search bounds of chunck, rounded on new line, but smaller than $chunck_size if($direction > 0) { - $head = $tail; - $lookpointer = true; - $tail = $head + floor($this->chunk_size * (2 / 3)); - while($lookpointer) { - $tail = min($tail, $eof); - $tail = $this->getNewlinepointer($fp, $tail); - $lookpointer = $tail - $head > $this->chunk_size; - if($lookpointer) { - $tail = $head + floor(($tail - $head) / 2); - } - if($tail == $head) break; - } + $head = $tail; + $tail = $head + floor($this->chunk_size * (2 / 3)); + $tail = $this->getNewlinepointer($fp, $tail); } else { $tail = $head; $head = max($tail - $this->chunk_size, 0); - $head = $this->getNewlinepointer($fp, $head); + while(true) { + $nl = $this->getNewlinepointer($fp, $head); + // was the chunk big enough? if not, take another bite + if($nl > 0 && $tail <= $nl) { + $head = max($head - $this->chunk_size, 0); + } else { + $head = $nl; + break; + } + } } //load next chunck @@ -693,11 +688,12 @@ abstract class ChangeLog { $finger = $head + floor(($tail - $head) / 2.0); $finger = $this->getNewlinepointer($fp, $finger); $tmp = fgets($fp); - $tmp = parseChangelogLine($tmp); - $finger_rev = $tmp['date']; if($finger == $head || $finger == $tail) { break; } + $tmp = parseChangelogLine($tmp); + $finger_rev = $tmp['date']; + if($finger_rev > $rev) { $tail = $finger; } else { @@ -737,7 +733,7 @@ abstract class ChangeLog { $got = 0; fseek($fp, $head); while($got < $chunk_size && !feof($fp)) { - $tmp = @fread($fp, max($chunk_size - $got, 0)); + $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0)); if($tmp === false) { //error state break; } @@ -758,8 +754,12 @@ abstract class ChangeLog { */ protected function getNewlinepointer($fp, $finger) { fseek($fp, $finger); - fgets($fp); // slip the finger forward to a new line - return ftell($fp); + $nl = $finger; + if($finger > 0) { + fgets($fp); // slip the finger forward to a new line + $nl = ftell($fp); + } + return $nl; } /** -- cgit v1.2.3 From 7d1e323e214bc52984e7df38732878be392adc5f Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Fri, 22 Nov 2013 15:50:04 +0100 Subject: bugfix and reformatting changelog. small chunck unittests --- inc/changelog.php | 122 +++++++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 56 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index f47042066..26480ad23 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -350,7 +350,7 @@ abstract class ChangeLog { * Constructor * * @param string $id page id - * @param int $chunk_size maximum block size read from file + * @param int $chunk_size maximum block size read from file */ public function __construct($id, $chunk_size = 8192) { global $cache_revinfo; @@ -398,7 +398,7 @@ abstract class ChangeLog { * consecutive calls to getRevisionInfo. For large changelog files, only the chunk * containing the requested changelog line is read. * - * @param int $rev revision timestamp + * @param int $rev revision timestamp * @return bool|array false or array with entries: * - date: unix timestamp * - ip: IPv4 address (127.0.0.1) @@ -455,8 +455,8 @@ abstract class ChangeLog { * backwards in chunks until the requested number of changelog * lines are recieved. * - * @param int $first skip the first n changelog lines - * @param int $num number of revisions to return + * @param int $first skip the first n changelog lines + * @param int $num number of revisions to return * @return array with the revision timestamps * * @author Ben Coburn @@ -465,52 +465,62 @@ abstract class ChangeLog { public function getRevisions($first, $num) { $revs = array(); $lines = array(); - $count = 0; + $count = 0; $num = max($num, 0); - if ($num == 0) { return $revs; } + if($num == 0) { + return $revs; + } - if ($first<0) { + if($first < 0) { $first = 0; - } else if (@file_exists($this->getFilename())) { + } else if(@file_exists($this->getFilename())) { // skip current revision if the page exists - $first = max($first+1, 0); + $first = max($first + 1, 0); } $file = $this->getChangelogFilename(); - if (!@file_exists($file)) { return $revs; } - if (filesize($file)<$this->chunk_size || $this->chunk_size==0) { + if(!@file_exists($file)) { + return $revs; + } + if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { // read whole file $lines = file($file); - if ($lines===false) { return $revs; } + if($lines === false) { + return $revs; + } } else { // read chunks backwards $fp = fopen($file, 'rb'); // "file pointer" - if ($fp===false) { return $revs; } + if($fp === false) { + return $revs; + } fseek($fp, 0, SEEK_END); $tail = ftell($fp); // chunk backwards - $finger = max($tail-$this->chunk_size, 0); - while ($count<$num+$first) { - $nl = getNewlinepointer($fp, $finger); + $finger = max($tail - $this->chunk_size, 0); + while($count < $num + $first) { + $nl = $this->getNewlinepointer($fp, $finger); // was the chunk big enough? if not, take another bite - if($nl > 0 && $tail <= $nl){ - $finger = max($finger-$this->chunk_size, 0); + if($nl > 0 && $tail <= $nl) { + $finger = max($finger - $this->chunk_size, 0); continue; - }else{ + } else { $finger = $nl; } // read chunk $chunk = ''; - $read_size = max($tail-$finger, 0); // found chunk size + $read_size = max($tail - $finger, 0); // found chunk size $got = 0; - while ($got<$read_size && !feof($fp)) { - $tmp = @fread($fp, max($read_size-$got, 0)); //todo why not use chunk_size? - if ($tmp===false) { break; } //error state + while($got < $read_size && !feof($fp)) { + $tmp = @fread($fp, max($read_size - $got, 0)); //todo why not use chunk_size? + if($tmp === false) { + break; + } //error state $got += strlen($tmp); $chunk .= $tmp; } @@ -522,25 +532,27 @@ abstract class ChangeLog { $lines = array_merge($tmp, $lines); // next chunk - if ($finger==0) { break; } // already read all the lines + if($finger == 0) { + break; + } // already read all the lines else { $tail = $finger; - $finger = max($tail-$this->chunk_size, 0); + $finger = max($tail - $this->chunk_size, 0); } } fclose($fp); } // skip parsing extra lines - $num = max(min(count($lines)-$first, $num), 0); - if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } - else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } - else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } + $num = max(min(count($lines) - $first, $num), 0); + if ($first > 0 && $num > 0) { $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num); } + else if($first > 0 && $num == 0) { $lines = array_slice($lines, 0, max(count($lines) - $first, 0)); } + else if($first == 0 && $num > 0) { $lines = array_slice($lines, max(count($lines) - $num, 0)); } // handle lines in reverse order - for ($i = count($lines)-1; $i >= 0; $i--) { + for($i = count($lines) - 1; $i >= 0; $i--) { $tmp = parseChangelogLine($lines[$i]); - if ($tmp!==false) { + if($tmp !== false) { $this->cache[$this->id][$tmp['date']] = $tmp; $revs[] = $tmp['date']; } @@ -558,8 +570,8 @@ abstract class ChangeLog { * Adjacent changelog lines are optimistically parsed and cached to speed up * consecutive calls to getRevisionInfo. * - * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) - * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev + * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) + * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev * @return bool|int * timestamp of the requested revision * otherwise false @@ -569,7 +581,7 @@ abstract class ChangeLog { $direction = (int) $direction; //no direction given or last rev, so no follow-up - if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev)) ) { + if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) { return false; } @@ -579,8 +591,8 @@ abstract class ChangeLog { // look for revisions later/earlier then $rev, when founded count till the wanted revision is reached // also parse and cache changelog lines for getRevisionInfo(). - $revcounter = 0; - $relativerev = false; + $revcounter = 0; + $relativerev = false; $checkotherchunck = true; //always runs once while(!$relativerev && $checkotherchunck) { $tmp = array(); @@ -588,10 +600,10 @@ abstract class ChangeLog { $count = count($lines); if($direction > 0) { $start = 0; - $step = 1; + $step = 1; } else { $start = $count - 1; - $step = -1; + $step = -1; } for($i = $start; $i >= 0 && $i < $count; $i = $i + $step) { $tmp = parseChangelogLine($lines[$i]); @@ -645,12 +657,11 @@ abstract class ChangeLog { return $relativerev; } - /** * Returns lines from changelog. * If file larger than $chuncksize, only chunck is read that could contain $rev. * - * @param int $rev revision timestamp + * @param int $rev revision timestamp * @return array(fp, array(changeloglines), $head, $tail, $eof)|bool * returns false when not succeed. fp only defined for chuck reading, needs closing. */ @@ -661,10 +672,10 @@ abstract class ChangeLog { return false; } - $fp = null; - $head = 0; - $tail = 0; - $eof = 0; + $fp = null; + $head = 0; + $tail = 0; + $eof = 0; if(filesize($file) < $this->chunk_size || $this->chunk_size == 0) { // read whole file @@ -680,18 +691,18 @@ abstract class ChangeLog { } $head = 0; fseek($fp, 0, SEEK_END); - $eof = ftell($fp); - $tail = $eof; + $eof = ftell($fp); + $tail = $eof; // find chunk while($tail - $head > $this->chunk_size) { - $finger = $head + floor(($tail - $head) / 2.0); - $finger = $this->getNewlinepointer($fp, $finger); - $tmp = fgets($fp); + $finger = $head + floor(($tail - $head) / 2.0); + $finger = $this->getNewlinepointer($fp, $finger); + $tmp = fgets($fp); if($finger == $head || $finger == $tail) { break; } - $tmp = parseChangelogLine($tmp); + $tmp = parseChangelogLine($tmp); $finger_rev = $tmp['date']; if($finger_rev > $rev) { @@ -728,9 +739,9 @@ abstract class ChangeLog { * @return array lines read from chunck */ protected function readChunk($fp, $head, $tail) { - $chunk = ''; + $chunk = ''; $chunk_size = max($tail - $head, 0); // found chunk size - $got = 0; + $got = 0; fseek($fp, $head); while($got < $chunk_size && !feof($fp)) { $tmp = @fread($fp, max(min($this->chunk_size, $chunk_size - $got), 0)); @@ -765,7 +776,7 @@ abstract class ChangeLog { /** * Check whether given revision is the current page * - * @param int $rev timestamp of current page + * @param int $rev timestamp of current page * @return bool true if $rev is current revision, otherwise false */ public function isCurrentRevision($rev) { @@ -815,7 +826,6 @@ class MediaChangelog extends ChangeLog { } } - /** * Get the changelog information for a specific page id * and revision (timestamp). Adjacent changelog lines @@ -829,7 +839,7 @@ class MediaChangelog extends ChangeLog { * @author Ben Coburn * @author Kate Arzamastseva */ -function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { +function getRevisionInfo($id, $rev, $chunk_size = 8192, $media = false) { if($media) { $changelog = new MediaChangeLog($id, $chunk_size); } else { @@ -861,7 +871,7 @@ function getRevisionInfo($id, $rev, $chunk_size=8192, $media=false) { * @author Ben Coburn * @author Kate Arzamastseva */ -function getRevisions($id, $first, $num, $chunk_size=8192, $media=false) { +function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false) { if($media) { $changelog = new MediaChangeLog($id, $chunk_size); } else { -- cgit v1.2.3 From 80e97297edd90144da2bafba9158bd9295bdda6e Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Wed, 27 Nov 2013 00:39:35 +0100 Subject: read changelog with chunks of chunksize size in getRevisions() --- inc/changelog.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index 26480ad23..33cdaf533 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -517,7 +517,7 @@ abstract class ChangeLog { $read_size = max($tail - $finger, 0); // found chunk size $got = 0; while($got < $read_size && !feof($fp)) { - $tmp = @fread($fp, max($read_size - $got, 0)); //todo why not use chunk_size? + $tmp = @fread($fp, max(min($this->chunk_size, $read_size - $got), 0)); if($tmp === false) { break; } //error state -- cgit v1.2.3 From 1da8dc976a4e9184fe550789a77d8e5cb866926f Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Wed, 27 Nov 2013 00:56:02 +0100 Subject: retrieve revisions around some given revisions With unit tests One case is not yet fixed: when rev1 is first rev from changelog, only $max/2 revisions are retrieved, but it should retrieve $max revisions. --- inc/changelog.php | 204 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 182 insertions(+), 22 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index 33cdaf533..dfcd1f241 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -625,28 +625,8 @@ abstract class ChangeLog { && !(($tail == $eof && $direction > 0) || ($head == 0 && $direction < 0)); if($checkotherchunck) { - //search bounds of chunck, rounded on new line, but smaller than $chunck_size - if($direction > 0) { - $head = $tail; - $tail = $head + floor($this->chunk_size * (2 / 3)); - $tail = $this->getNewlinepointer($fp, $tail); - } else { - $tail = $head; - $head = max($tail - $this->chunk_size, 0); - while(true) { - $nl = $this->getNewlinepointer($fp, $head); - // was the chunk big enough? if not, take another bite - if($nl > 0 && $tail <= $nl) { - $head = max($head - $this->chunk_size, 0); - } else { - $head = $nl; - break; - } - } - } + list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, $direction); - //load next chunck - $lines = $this->readChunk($fp, $head, $tail); if(empty($lines)) break; } } @@ -657,6 +637,61 @@ abstract class ChangeLog { return $relativerev; } + /** + * Returns revisions around rev1 and rev2 + * When available it returns $max entries for each revision + * + * @param int $rev1 oldest revision timestamp + * @param int $rev2 newest revision timestamp + * @param int $max maximum number of revisions returned + * @return array with two arrays with revisions surrounding rev1 respectively rev2 + */ + public function getRevisionsAround($rev1, $rev2, $max = 50) { + $max = floor(abs($max) / 2)*2 + 1; + $rev1 = max($rev1, 0); + $rev2 = max($rev2, 0); + + if($rev2 < $rev1) { + $rev = $rev2; + $rev2 = $rev1; + $rev1 = $rev; + } + //collect revisions around rev2 + list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max); + + if(empty($revs2)) return array(array(), array()); + + //collect revisions around rev1 + $index = array_search($rev1, $allrevs); + if($index === false) { + //no overlapping revisions + list($revs1,,,,,) = $this->retrieveRevisionsAround($rev1, $max); + if(empty($revs1)) $revs1 = array(); + } else { + //revisions overlaps, reuse revisions around rev2 + $revs1 = $allrevs; + while($head > 0) { + for($i = count($lines) - 1; $i >= 0; $i--) { + $tmp = parseChangelogLine($lines[$i]); + if($tmp !== false) { + $this->cache[$this->id][$tmp['date']] = $tmp; + $revs1[] = $tmp['date']; + $index++; + + if($index > floor($max / 2)) break 2; + } + } + + list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); + } + sort($revs1); + //return wanted selection + $revs1 = array_slice($revs1, max($index - floor($max/2), 0), $max); + } + + return array($revs1, $revs2); + } + /** * Returns lines from changelog. * If file larger than $chuncksize, only chunck is read that could contain $rev. @@ -759,7 +794,7 @@ abstract class ChangeLog { /** * Set pointer to first new line after $finger and return its position * - * @param $fp resource filepointer + * @param resource $fp filepointer * @param $finger int a pointer * @return int pointer */ @@ -782,8 +817,130 @@ abstract class ChangeLog { public function isCurrentRevision($rev) { return $rev == @filemtime($this->getFilename()); } + + /** + * Returns the next lines of the changelog of the chunck before head or after tail + * + * @param resource $fp filepointer + * @param int $head position head of last chunk + * @param int $tail position tail of last chunk + * @param int $direction positive forward, negative backward + * @return array with entries: + * - $lines: changelog lines of readed chunk + * - $head: head of chunk + * - $tail: tail of chunk + */ + protected function readAdjacentChunk($fp, $head, $tail, $direction) { + if(!$fp) return array(array(), $head, $tail); + + if($direction > 0) { + //read forward + $head = $tail; + $tail = $head + floor($this->chunk_size * (2 / 3)); + $tail = $this->getNewlinepointer($fp, $tail); + } else { + //read backward + $tail = $head; + $head = max($tail - $this->chunk_size, 0); + while(true) { + $nl = $this->getNewlinepointer($fp, $head); + // was the chunk big enough? if not, take another bite + if($nl > 0 && $tail <= $nl) { + $head = max($head - $this->chunk_size, 0); + } else { + $head = $nl; + break; + } + } + } + + //load next chunck + $lines = $this->readChunk($fp, $head, $tail); + return array($lines, $head, $tail); + } + + /** + * Collect the $max revisions near to the timestamp $rev + * + * @param int $rev revision timestamp + * @param int $max maximum number of revisions to be returned + * @return bool|array + * return array with entries: + * - $requestedrevs: array of with $max revision timestamps + * - $revs: all parsed revision timestamps + * - $fp: filepointer only defined for chuck reading, needs closing. + * - $lines: non-parsed changelog lines before the parsed revisions + * - $head: position of first readed changelogline + * - $lasttail: position of end of last readed changelogline + * otherwise false + */ + protected function retrieveRevisionsAround($rev, $max) { + //get lines from changelog + list($fp, $lines, $starthead, $starttail, $eof) = $this->readloglines($rev); + if(empty($lines)) return false; + + //parse chunk containing $rev, and read forward more chunks until $max/2 is reached + $head = $starthead; + $tail = $starttail; + $revs = array(); + $aftercount = $beforecount = 0; + while(count($lines) > 0) { + foreach($lines as $line) { + $tmp = parseChangelogLine($line); + if($tmp !== false) { + $this->cache[$this->id][$tmp['date']] = $tmp; + $revs[] = $tmp['date']; + if($tmp['date'] >= $rev) { + //count revs after reference $rev + $aftercount++; + if($aftercount == 1) $beforecount = count($revs); + } + //enough revs after reference $rev? + if($aftercount > floor($max / 2)) break 2; + } + } + //retrieve next chunk + list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, 1); + } + if($aftercount == 0) return false; + + $lasttail = $tail; + + //read additional chuncks backward until $max/2 is reached and total number of revs is equal to $max + $lines = array(); + $i = 0; + if($aftercount > 0) { + $head = $starthead; + $tail = $starttail; + while($head > 0) { + list($lines, $head, $tail) = $this->readAdjacentChunk($fp, $head, $tail, -1); + + for($i = count($lines) - 1; $i >= 0; $i--) { + $tmp = parseChangelogLine($lines[$i]); + if($tmp !== false) { + $this->cache[$this->id][$tmp['date']] = $tmp; + $revs[] = $tmp['date']; + $beforecount++; + //enough revs before reference $rev? + if($beforecount > max(floor($max / 2), $max - $aftercount)) break 2; + } + } + } + } + sort($revs); + + //keep only non-parsed lines + $lines = array_slice($lines, 0, $i); + //trunk desired selection + $requestedrevs = array_slice($revs, -$max, $max); + + return array($requestedrevs, $revs, $fp, $lines, $head, $lasttail); + } } +/** + * Class PageChangelog handles changelog of a wiki page + */ class PageChangelog extends ChangeLog { /** @@ -805,6 +962,9 @@ class PageChangelog extends ChangeLog { } } +/** + * Class MediaChangelog handles changelog of a media file + */ class MediaChangelog extends ChangeLog { /** -- cgit v1.2.3 From 2e608c527514d5fb03eb88c8da1c101f231d5736 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Sat, 15 Feb 2014 20:41:09 +0100 Subject: reverse revisions display order --- inc/changelog.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index dfcd1f241..d2ad23c08 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -689,7 +689,7 @@ abstract class ChangeLog { $revs1 = array_slice($revs1, max($index - floor($max/2), 0), $max); } - return array($revs1, $revs2); + return array(array_reverse($revs1), array_reverse($revs2)); } /** -- cgit v1.2.3 From 621bbd2a24f6ceac0310c04b27e11a2c7c325294 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Wed, 19 Feb 2014 17:58:36 +0100 Subject: diff of removed page, require handling right rev=0 When page is removed, and diff can be requested between a revision and current situation. This results in right revision is 0. Similar case just after creating a page. A diff between the first version and nothing before, result in left revision is 0. In these cases a empty dummy revision is placed as selected value in dropdown. Otherwise user got distracted by the revisions details shown in select field, which are not related to the diff below. --- inc/changelog.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index d2ad23c08..28e53e77a 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -642,7 +642,7 @@ abstract class ChangeLog { * When available it returns $max entries for each revision * * @param int $rev1 oldest revision timestamp - * @param int $rev2 newest revision timestamp + * @param int $rev2 newest revision timestamp (0 looks up last revision) * @param int $max maximum number of revisions returned * @return array with two arrays with revisions surrounding rev1 respectively rev2 */ @@ -651,10 +651,16 @@ abstract class ChangeLog { $rev1 = max($rev1, 0); $rev2 = max($rev2, 0); - if($rev2 < $rev1) { - $rev = $rev2; - $rev2 = $rev1; - $rev1 = $rev; + if($rev2) { + if($rev2 < $rev1) { + $rev = $rev2; + $rev2 = $rev1; + $rev1 = $rev; + } + } else { + //empty right side means a removed page. Look up last revision. + $revs = $this->getRevisions(-1, 1); + $rev2 = $revs[0]; } //collect revisions around rev2 list($revs2, $allrevs, $fp, $lines, $head, $tail) = $this->retrieveRevisionsAround($rev2, $max); -- cgit v1.2.3 From 585bf44e2b756eac2e1cfce7035ef237bc02a788 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Thu, 6 Mar 2014 19:55:56 +0000 Subject: amend $_SERVER to $INPUT->server --- inc/changelog.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'inc/changelog.php') diff --git a/inc/changelog.php b/inc/changelog.php index 6ff1e0eca..cd46b1ec0 100644 --- a/inc/changelog.php +++ b/inc/changelog.php @@ -52,6 +52,8 @@ function parseChangelogLine($line) { */ function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){ global $conf, $INFO; + /** @var Input $INPUT */ + global $INPUT; // check for special flags as keys if (!is_array($flags)) { $flags = array(); } @@ -65,7 +67,7 @@ function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extr if(!$date) $date = time(); //use current time if none supplied $remote = (!$flagExternalEdit)?clientIP(true):'127.0.0.1'; - $user = (!$flagExternalEdit)?$_SERVER['REMOTE_USER']:''; + $user = (!$flagExternalEdit)?$INPUT->server->str('REMOTE_USER'):''; $strip = array("\t", "\n"); $logline = array( @@ -117,12 +119,14 @@ function addLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extr */ function addMediaLogEntry($date, $id, $type=DOKU_CHANGE_TYPE_EDIT, $summary='', $extra='', $flags=null){ global $conf; + /** @var Input $INPUT */ + global $INPUT; $id = cleanid($id); if(!$date) $date = time(); //use current time if none supplied $remote = clientIP(true); - $user = $_SERVER['REMOTE_USER']; + $user = $INPUT->server->str('REMOTE_USER'); $strip = array("\t", "\n"); $logline = array( -- cgit v1.2.3