From 52eebd6f3d0414b1f93144fd6661bad377369dfd Mon Sep 17 00:00:00 2001 From: lisps Date: Mon, 25 Nov 2013 11:41:22 +0100 Subject: [PATCH 1/5] add revision support --- conf/default.php | 1 + conf/metadata.php | 1 + helper.php | 96 +++++++++++++++++++++++++++++++++-------------- syntax/footer.php | 10 ++--- 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/conf/default.php b/conf/default.php index 5be9e6f..f09ea19 100644 --- a/conf/default.php +++ b/conf/default.php @@ -26,4 +26,5 @@ $conf['order'] = 'id'; // order in which the pages are included in the case of multiple pages $conf['rsort'] = 0; // reverse sort order $conf['depth'] = 1; // maximum depth of namespace includes, 0 for unlimited depth +$conf['revision'] = 01; // show included pages with respect to revision //Setup VIM: ex: et ts=2 : diff --git a/conf/metadata.php b/conf/metadata.php index 65b8ba2..1e911cb 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -29,4 +29,5 @@ $meta['order'] = array('multichoice', '_choices' => array('id', 'title', 'created', 'modified', 'indexmenu', 'custom')); $meta['rsort'] = array('onoff'); $meta['depth'] = array('numeric', '_min' => 0); +$meta['revision'] = array('onoff'); //Setup VIM: ex: et ts=2 : diff --git a/helper.php b/helper.php index ff4003b..d69c496 100644 --- a/helper.php +++ b/helper.php @@ -49,6 +49,7 @@ function helper_plugin_include() { $this->defaults['order'] = $this->getConf('order'); $this->defaults['rsort'] = $this->getConf('rsort'); $this->defaults['depth'] = $this->getConf('depth'); + $this->defaults['revision'] = $this->getConf('revision'); } /** @@ -68,6 +69,8 @@ function getMethods() { * Overrides standard values for showfooter and firstseconly settings */ function get_flags($setflags) { + global $REV; + global $DATE_AT; // load defaults $flags = $this->defaults; foreach ($setflags as $flag) { @@ -219,6 +222,12 @@ function get_flags($setflags) { // the include_content URL parameter overrides flags if (isset($_REQUEST['include_content'])) $flags['linkonly'] = 0; + + //we have to disable some functions + if (($flags['revision'] && $REV) || $DATE_AT) { + $flags['editbtn'] = 0; + } + return $flags; } @@ -237,9 +246,12 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ global $ID; $root_id = $ID; } - + $page_rev = ''; + if(in_array($mode,array('page','section'))) { + $page_rev = $this->_get_revision($page,$flags); + } if ($flags['linkonly']) { - if (page_exists($page) || $flags['pageexists'] == 0) { + if (page_exists($page,$page_rev) || $flags['pageexists'] == 0) { $title = ''; if ($flags['title']) $title = p_get_first_heading($page); @@ -256,17 +268,22 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ $ins = array(); } } else { - if (page_exists($page)) { + if (page_exists($page,$page_rev)) { global $ID; $backupID = $ID; $ID = $page; // Change the global $ID as otherwise plugins like the discussion plugin will save data for the wrong page - $ins = p_cached_instructions(wikiFN($page), false, $page); + if($page_rev){ + $ins = p_get_instructions(io_readWikiPage(wikiFN($page,$page_rev),$page,$page_rev)); + } else { + $ins = p_cached_instructions(wikiFN($page), false, $page); + } + $ID = $backupID; } else { $ins = array(); } - $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages); + $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages, $page_rev); } return $ins; } @@ -284,7 +301,7 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ * * @author Michael Klier */ - function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array()) { + function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array(), $page_rev = '') { global $conf; // filter instructions if needed @@ -512,7 +529,7 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc // add footer if($flags['footer']) { - $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id); + $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id, $page_rev); } // wrap content at the beginning of the include that is not in a section in a section @@ -552,10 +569,10 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc * * @author Michael Klier */ - function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) { + function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id, $page_rev) { $footer = array(); $footer[0] = 'plugin'; - $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl)); + $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl, $page_rev)); return $footer; } @@ -661,6 +678,8 @@ function _get_firstsec(&$ins, $page) { */ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { global $conf; + global $REV; + global $DATE_AT; $pages = array(); switch($mode) { case 'namespace': @@ -691,7 +710,7 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { } break; default: - $page = $this->_apply_macro($page); + $page = $this->_apply_macro($page,$flags); resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID if (auth_quickaclcheck($page) >= AUTH_READ) $pages[] = $page; @@ -743,7 +762,8 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { $result = array(); foreach ($pages as $page) { - $exists = page_exists($page); + $page_rev = $this->_get_revision($page,$flags); + $exists = page_exists($page,$page_rev); $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); } return $result; @@ -784,7 +804,7 @@ function _get_included_pages_from_meta_instructions($instructions) { /** * Makes user or date dependent includes possible */ - function _apply_macro($id) { + function _apply_macro($id,$flags) { global $INFO; global $auth; @@ -794,32 +814,36 @@ function _apply_macro($id) { $user = $_SERVER['REMOTE_USER']; $group = $INFO['userinfo']['grps'][0]; - $time_stamp = time(); + if(($flags['revision'] && $REV) || $DATE_AT) { + $time_stamp = max($REV,$DATE_AT); + } else { + $time_stamp = time(); + } if(preg_match('/@DATE(\w+)@/',$id,$matches)) { switch($matches[1]) { case 'PMONTH': - $time_stamp = strtotime("-1 month"); + $time_stamp = strtotime("-1 month",$time_stamp); break; case 'NMONTH': - $time_stamp = strtotime("+1 month"); + $time_stamp = strtotime("+1 month",$time_stamp); break; case 'NWEEK': - $time_stamp = strtotime("+1 week"); + $time_stamp = strtotime("+1 week",$time_stamp); break; case 'PWEEK': - $time_stamp = strtotime("-1 week"); + $time_stamp = strtotime("-1 week",$time_stamp); break; case 'TOMORROW': - $time_stamp = strtotime("+1 day"); + $time_stamp = strtotime("+1 day",$time_stamp); break; case 'YESTERDAY': - $time_stamp = strtotime("-1 day"); + $time_stamp = strtotime("-1 day",$time_stamp); break; case 'NYEAR': - $time_stamp = strtotime("+1 year"); + $time_stamp = strtotime("+1 year",$time_stamp); break; case 'PYEAR': - $time_stamp = strtotime("-1 year"); + $time_stamp = strtotime("-1 year",$time_stamp); break; } $id = preg_replace('/@DATE(\w+)@/','', $id); @@ -833,16 +857,30 @@ function _apply_macro($id) { '@MONTH@' => date('m',$time_stamp), '@WEEK@' => date('W',$time_stamp), '@DAY@' => date('d',$time_stamp), - '@YEARPMONTH@' => date('Ym',strtotime("-1 month")), - '@PMONTH@' => date('m',strtotime("-1 month")), - '@NMONTH@' => date('m',strtotime("+1 month")), - '@YEARNMONTH@' => date('Ym',strtotime("+1 month")), - '@YEARPWEEK@' => date('YW',strtotime("-1 week")), - '@PWEEK@' => date('W',strtotime("-1 week")), - '@NWEEK@' => date('W',strtotime("+1 week")), - '@YEARNWEEK@' => date('YW',strtotime("+1 week")), + '@YEARPMONTH@' => date('Ym',strtotime("-1 month",$time_stamp)), + '@PMONTH@' => date('m',strtotime("-1 month",$time_stamp)), + '@NMONTH@' => date('m',strtotime("+1 month",$time_stamp)), + '@YEARNMONTH@' => date('Ym',strtotime("+1 month",$time_stamp)), + '@YEARPWEEK@' => date('YW',strtotime("-1 week",$time_stamp)), + '@PWEEK@' => date('W',strtotime("-1 week",$time_stamp)), + '@NWEEK@' => date('W',strtotime("+1 week",$time_stamp)), + '@YEARNWEEK@' => date('YW',strtotime("+1 week",$time_stamp)), ); return str_replace(array_keys($replace), array_values($replace), $id); } + + function _get_revision($page,$flags) { + global $DATE_AT; + global $REV; + $page_rev = ''; + if($DATE_AT) { + $pagelog = new PageChangeLog($page); + $page_rev = $pagelog->getLastRevisionAt($DATE_AT); + } else if($flags['revision'] && $REV) { + $pagelog = new PageChangeLog($page); + $page_rev = $pagelog->getLastRevisionAt($REV); + } + return $page_rev; + } } // vim:ts=4:sw=4:et: diff --git a/syntax/footer.php b/syntax/footer.php index 7e6dc63..112e892 100644 --- a/syntax/footer.php +++ b/syntax/footer.php @@ -34,10 +34,10 @@ function handle($match, $state, $pos, Doku_Handler &$handler) { */ function render($mode, Doku_Renderer &$renderer, $data) { - list($page, $sect, $sect_title, $flags, $redirect_id, $footer_lvl) = $data; + list($page, $sect, $sect_title, $flags, $redirect_id, $footer_lvl, $page_rev) = $data; if ($mode == 'xhtml') { - $renderer->doc .= $this->html_footer($page, $sect, $sect_title, $flags, $footer_lvl, $renderer); + $renderer->doc .= $this->html_footer($page, $sect, $sect_title, $flags, $footer_lvl, $page_rev, $renderer); return true; } return false; @@ -46,7 +46,7 @@ function render($mode, Doku_Renderer &$renderer, $data) { /** * Returns the meta line below the included page */ - function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) { + function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, $page_rev, &$renderer) { global $conf, $ID; if(!$flags['footer']) return ''; @@ -57,7 +57,7 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) // permalink if ($flags['permalink']) { $class = ($exists ? 'wikilink1' : 'wikilink2'); - $url = ($sect) ? wl($page) . '#' . $sect : wl($page); + $url = ($sect) ? wl($page,array('rev'=>$page_rev)) . '#' . $sect : wl($page,array('rev'=>$page_rev)); $name = ($sect) ? $sect_title : $page; $title = ($sect) ? $page . '#' . $sect : $page; if (!$title) $title = str_replace('_', ' ', noNS($page)); @@ -84,7 +84,7 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, &$renderer) // modified date if ($flags['mdate'] && $exists) { - $mdate = $meta['date']['modified']; + $mdate = $page_rev ? $page_rev : $meta['date']['modified']; if ($mdate) { $xhtml[] = '' . strftime($conf['dformat'], $mdate) From 6291ff2fe84505fc772268e90b0130f615a75b7a Mon Sep 17 00:00:00 2001 From: lisps Date: Mon, 25 Nov 2013 11:43:27 +0100 Subject: [PATCH 2/5] set revision default to 0 --- conf/default.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/default.php b/conf/default.php index f09ea19..276f0d3 100644 --- a/conf/default.php +++ b/conf/default.php @@ -26,5 +26,5 @@ $conf['order'] = 'id'; // order in which the pages are included in the case of multiple pages $conf['rsort'] = 0; // reverse sort order $conf['depth'] = 1; // maximum depth of namespace includes, 0 for unlimited depth -$conf['revision'] = 01; // show included pages with respect to revision +$conf['revision'] = 0; // show included pages with respect to revision //Setup VIM: ex: et ts=2 : From ff6ec9c141c9fc2b656cccade41c2982c50450d5 Mon Sep 17 00:00:00 2001 From: lisps Date: Mon, 25 Nov 2013 14:04:25 +0100 Subject: [PATCH 3/5] add fall-back class helper_plugin_include_PageChangelog --- helper.php | 505 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 499 insertions(+), 6 deletions(-) diff --git a/helper.php b/helper.php index d69c496..cd91fde 100644 --- a/helper.php +++ b/helper.php @@ -873,14 +873,507 @@ function _get_revision($page,$flags) { global $DATE_AT; global $REV; $page_rev = ''; - if($DATE_AT) { - $pagelog = new PageChangeLog($page); - $page_rev = $pagelog->getLastRevisionAt($DATE_AT); - } else if($flags['revision'] && $REV) { - $pagelog = new PageChangeLog($page); - $page_rev = $pagelog->getLastRevisionAt($REV); + if(($flags['revision'] && $REV) || $DATE_AT) { + if (method_exists('PageChangeLog', 'getLastRevisionAt')) { + $pagelog = new PageChangeLog($page); + } else { + $pagelog = new helper_plugin_include_PageChangelog($page); + } + $page_rev = $pagelog->getLastRevisionAt($DATE_AT ? $DATE_AT : $REV); } return $page_rev; } } + + +/** + * Class ChangeLog + * methods for handling of changelog of pages or media files + */ +abstract class helper_plugin_include_ChangeLog { + + /** @var string */ + protected $id; + /** @var int */ + protected $chunk_size; + /** @var array */ + protected $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(); + } + + $this->id = $id; + $this->setChunkSize($chunk_size); + + } + + /** + * 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 + */ + public function setChunkSize($chunk_size) { + if(!is_numeric($chunk_size)) $chunk_size = 0; + + $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) + * + * 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 + * @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) { + $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]; + } + + //read lines from changelog + list($fp, $lines) = $this->readloglines($rev); + if($fp) { + fclose($fp); + } + if(empty($lines)) return false; + + // parse and cache changelog lines + foreach($lines as $value) { + $tmp = parseChangelogLine($value); + if($tmp !== false) { + $this->cache[$this->id][$tmp['date']] = $tmp; + } + } + if(!isset($this->cache[$this->id][$rev])) { + return false; + } + return $this->cache[$this->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. + * + * 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 + * @return array with the revision timestamps + * + * @author Ben Coburn + * @author Kate Arzamastseva + */ + public function getRevisions($first, $num) { + $revs = array(); + $lines = array(); + $count = 0; + + $num = max($num, 0); + if($num == 0) { + return $revs; + } + + if($first < 0) { + $first = 0; + } 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 + $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) { + $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); + continue; + } else { + $finger = $nl; + } + + // 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)); //todo why not use chunk_size? + 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); + } + + // 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) { + $this->cache[$this->id][$tmp['date']] = $tmp; + $revs[] = $tmp['date']; + } + } + + 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 + * @return bool|int + * timestamp of the requested revision + * otherwise false + */ + public function getRelativeRevision($rev, $direction) { + $rev = max($rev, 0); + $direction = (int) $direction; + + //no direction given or last rev, so no follow-up + if(!$direction || ($direction > 0 && $this->isCurrentRevision($rev))) { + return false; + } + + //get lines from changelog + 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 + // 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) { + $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']; + } + } + } + } + + //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; + $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; + } + } + } + + //load next chunck + $lines = $this->readChunk($fp, $head, $tail); + if(empty($lines)) break; + } + } + if($fp) { + fclose($fp); + } + + return $relativerev; + } + + /** + * Returns lines from changelog. + * If file larger than $chuncksize, only chunck is read that could contain $rev. + * + * @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($rev) { + $file = $this->getChangelogFilename(); + + if(!@file_exists($file)) { + return false; + } + + $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; + } + } 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); + if($finger == $head || $finger == $tail) { + break; + } + $tmp = parseChangelogLine($tmp); + $finger_rev = $tmp['date']; + + 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 + ); + } + + /** + * 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(min($this->chunk_size, $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; + } + + /** + * 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); + $nl = $finger; + if($finger > 0) { + fgets($fp); // slip the finger forward to a new line + $nl = ftell($fp); + } + return $nl; + } + + /** + * 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 + */ + public function isCurrentRevision($rev) { + return $rev == @filemtime($this->getFilename()); + } + + /** + * Return an existing revision for a specific date which is + * the current one or younger or equal then the date + * + * @param string $id + * @param number $date_at timestamp + * @return string revision ('' for current) + */ + function getLastRevisionAt($date_at){ + //requested date_at(timestamp) younger or equal then modified_time($this->id) => load current + if($date_at >= @filemtime($this->getFilename())) { + return ''; + } else if ($rev = $this->getRelativeRevision($date_at+1, -1)) { //+1 to get also the requested date revision + return $rev; + } else { + return false; + } + } +} + +class helper_plugin_include_PageChangelog extends helper_plugin_include_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); + } +} // vim:ts=4:sw=4:et: From 963ad3068328440626cd2b3f85db8e03c7402fd0 Mon Sep 17 00:00:00 2001 From: lisps Date: Mon, 25 Nov 2013 14:46:37 +0100 Subject: [PATCH 4/5] add comment only add rev param when needed --- helper.php | 12 ++++++++++++ syntax/footer.php | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/helper.php b/helper.php index cd91fde..46286aa 100644 --- a/helper.php +++ b/helper.php @@ -869,6 +869,15 @@ function _apply_macro($id,$flags) { return str_replace(array_keys($replace), array_values($replace), $id); } + + /** + * returns the revsision of a page + * based on configuration($flags), $REV and $DATE_AT + * + * @param string $page page id + * @param array $flags configuration array see get_flags() + * @return string revision ('' if current) + **/ function _get_revision($page,$flags) { global $DATE_AT; global $REV; @@ -885,6 +894,9 @@ function _get_revision($page,$flags) { } } +/****************************************************************************** + * Following code is copied from inc/changelog.php from diff_navigation branch + ******************************************************************************/ /** * Class ChangeLog diff --git a/syntax/footer.php b/syntax/footer.php index 112e892..20e8a5f 100644 --- a/syntax/footer.php +++ b/syntax/footer.php @@ -57,7 +57,11 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, $page_rev, // permalink if ($flags['permalink']) { $class = ($exists ? 'wikilink1' : 'wikilink2'); - $url = ($sect) ? wl($page,array('rev'=>$page_rev)) . '#' . $sect : wl($page,array('rev'=>$page_rev)); + $url_params = ''; + if($page_rev) { + $url_params = array('rev'=>$page_rev); + } + $url = ($sect) ? wl($page,$url_params) . '#' . $sect : wl($page,$url_params); $name = ($sect) ? $sect_title : $page; $title = ($sect) ? $page . '#' . $sect : $page; if (!$title) $title = str_replace('_', ' ', noNS($page)); From f4b7a37089d2d321eebe89681ba8c4b0fe46833c Mon Sep 17 00:00:00 2001 From: lisps Date: Mon, 25 Nov 2013 15:40:30 +0100 Subject: [PATCH 5/5] delete unneeded code --- helper.php | 201 ++-------------------------------------------- syntax/footer.php | 6 +- 2 files changed, 10 insertions(+), 197 deletions(-) diff --git a/helper.php b/helper.php index 46286aa..dd84366 100644 --- a/helper.php +++ b/helper.php @@ -900,9 +900,9 @@ function _get_revision($page,$flags) { /** * Class ChangeLog - * methods for handling of changelog of pages or media files + * methods for handling of changelog of pages */ -abstract class helper_plugin_include_ChangeLog { +class helper_plugin_include_PageChangelog { /** @var string */ protected $id; @@ -942,189 +942,24 @@ public function setChunkSize($chunk_size) { $this->chunk_size = (int) max($chunk_size, 0); } - /** + /** * Returns path to changelog * * @return string path to file */ - abstract protected function getChangelogFilename(); + protected function getChangelogFilename() { + return metaFN($this->id, '.changes'); + } /** * 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) - * - * 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 - * @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) { - $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]; - } - - //read lines from changelog - list($fp, $lines) = $this->readloglines($rev); - if($fp) { - fclose($fp); - } - if(empty($lines)) return false; - - // parse and cache changelog lines - foreach($lines as $value) { - $tmp = parseChangelogLine($value); - if($tmp !== false) { - $this->cache[$this->id][$tmp['date']] = $tmp; - } - } - if(!isset($this->cache[$this->id][$rev])) { - return false; - } - return $this->cache[$this->id][$rev]; + protected function getFilename() { + return wikiFN($this->id); } - /** - * 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 - * @return array with the revision timestamps - * - * @author Ben Coburn - * @author Kate Arzamastseva - */ - public function getRevisions($first, $num) { - $revs = array(); - $lines = array(); - $count = 0; - - $num = max($num, 0); - if($num == 0) { - return $revs; - } - - if($first < 0) { - $first = 0; - } 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 - $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) { - $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); - continue; - } else { - $finger = $nl; - } - - // 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)); //todo why not use chunk_size? - 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); - } - - // 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) { - $this->cache[$this->id][$tmp['date']] = $tmp; - $revs[] = $tmp['date']; - } - } - - return $revs; - } /** * Get the nth revision left or right handside for a specific page id and revision (timestamp) @@ -1368,24 +1203,4 @@ function getLastRevisionAt($date_at){ } } -class helper_plugin_include_PageChangelog extends helper_plugin_include_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); - } -} // vim:ts=4:sw=4:et: diff --git a/syntax/footer.php b/syntax/footer.php index 20e8a5f..e93a8fb 100644 --- a/syntax/footer.php +++ b/syntax/footer.php @@ -57,10 +57,8 @@ function html_footer($page, $sect, $sect_title, $flags, $footer_lvl, $page_rev, // permalink if ($flags['permalink']) { $class = ($exists ? 'wikilink1' : 'wikilink2'); - $url_params = ''; - if($page_rev) { - $url_params = array('rev'=>$page_rev); - } + + $url_params = $page_rev ? array('rev'=>$page_rev) : ''; $url = ($sect) ? wl($page,$url_params) . '#' . $sect : wl($page,$url_params); $name = ($sect) ? $sect_title : $page; $title = ($sect) ? $page . '#' . $sect : $page;