diff options
-rw-r--r-- | _test/tests/inc/changelog_getrelativerevision.test.php | 82 | ||||
-rw-r--r-- | _test/tests/inc/changelog_getrevisioninfo.test.php | 12 | ||||
-rw-r--r-- | inc/changelog.php | 50 |
3 files changed, 116 insertions, 28 deletions
diff --git a/_test/tests/inc/changelog_getrelativerevision.test.php b/_test/tests/inc/changelog_getrelativerevision.test.php index c636b3c04..f9962066a 100644 --- a/_test/tests/inc/changelog_getrelativerevision.test.php +++ b/_test/tests/inc/changelog_getrelativerevision.test.php @@ -109,9 +109,9 @@ class changelog_getrelativerevision_test extends DokuWikiTest { } /** - * request existing rev + * request existing rev and check cache */ - function test_requestrev() { + function test_requestrev_checkcache() { $rev = 1362525359; $dir = 1; $revexpected = 1362525899; @@ -120,20 +120,96 @@ class changelog_getrelativerevision_test extends DokuWikiTest { $pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192); $revfound = $pagelog->getRelativeRevision($rev, $dir); $this->assertEquals($revexpected, $revfound); + //checked info returned from cache $info = $pagelog->getRevisionInfo($revfound); $this->assertEquals($infoexpected, $info); } /** + * request existing rev + */ + function test_requestnextrev() { + $rev = 1362525899; + + $pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192); + + $dir = 1; + $revexpected = 1362525926; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = 2; + $revexpected = 1362526039; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = -1; + $revexpected = 1362525359; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = -2; + $revexpected = 1362525145; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + } + + /** * request existing rev with chucked reading */ function test_requestnextrev_chuncked() { $rev = 1362525899; + + $pagelog = new PageChangeLog($this->pageid, $chunk_size = 512); + $dir = 1; $revexpected = 1362525926; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); - $pagelog = new PageChangeLog($this->pageid, $chunk_size = 512); + $dir = 2; + $revexpected = 1362526039; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = -1; + $revexpected = 1362525359; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = -2; + $revexpected = 1362525145; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + } + + + /** + * request existing rev with chucked reading, chunk size smaller than line length + */ + function test_requestnextrev_chunkshorterthanlines() { + $rev = 1362525899; + + $pagelog = new PageChangeLog($this->pageid, $chunk_size = 20); + + $dir = 1; + $revexpected = 1362525926; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = 2; + $revexpected = 1362526039; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = -1; + $revexpected = 1362525359; + $revfound = $pagelog->getRelativeRevision($rev, $dir); + $this->assertEquals($revexpected, $revfound); + + $dir = -2; + $revexpected = 1362525145; $revfound = $pagelog->getRelativeRevision($rev, $dir); $this->assertEquals($revexpected, $revfound); } diff --git a/_test/tests/inc/changelog_getrevisioninfo.test.php b/_test/tests/inc/changelog_getrevisioninfo.test.php index 07e229824..79b31d68e 100644 --- a/_test/tests/inc/changelog_getrevisioninfo.test.php +++ b/_test/tests/inc/changelog_getrevisioninfo.test.php @@ -66,6 +66,18 @@ class changelog_getrevisionsinfo_test extends DokuWikiTest { } /** + * request existing rev with chucked reading + */ + function test_requestrev_chunckedsmallerthanlinelength() { + $rev = 1362525899; + $infoexpected = parseChangelogLine($this->logline); + + $pagelog = new PageChangeLog($this->pageid, $chunk_size = 20); + $info = $pagelog->getRevisionInfo($rev); + $this->assertEquals($infoexpected, $info); + } + + /** * request current version */ function test_requestrecentestlogline() { 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; } /** |