diff options
author | Gerrit Uitslag <klapinklapin@gmail.com> | 2013-11-21 21:07:08 +0100 |
---|---|---|
committer | Gerrit Uitslag <klapinklapin@gmail.com> | 2013-11-21 21:07:08 +0100 |
commit | 047bad06fab8157452aa0dd04379a7c507b1f39f (patch) | |
tree | c8e4b0b344e648476d521a005ac1467de0ad2ec7 /inc | |
parent | 0f13c836f0ee71a3188a775b9ea64025e10d38f5 (diff) | |
download | rpg-047bad06fab8157452aa0dd04379a7c507b1f39f.tar.gz rpg-047bad06fab8157452aa0dd04379a7c507b1f39f.tar.bz2 |
refactor PageRevisionLog into Media- and PageChangelog extending Changelog
Diffstat (limited to 'inc')
-rw-r--r-- | inc/RemoteAPICore.php | 4 | ||||
-rw-r--r-- | inc/changelog.php | 124 | ||||
-rw-r--r-- | inc/common.php | 4 | ||||
-rw-r--r-- | inc/html.php | 55 | ||||
-rw-r--r-- | inc/media.php | 8 | ||||
-rw-r--r-- | inc/subscription.php | 6 |
6 files changed, 132 insertions, 69 deletions
diff --git a/inc/RemoteAPICore.php b/inc/RemoteAPICore.php index 311ff8c8c..aa1e06f57 100644 --- a/inc/RemoteAPICore.php +++ b/inc/RemoteAPICore.php @@ -374,7 +374,7 @@ class RemoteAPICore { throw new RemoteException('The requested page does not exist', 121); } - $pagelog = new PageRevisionLog($id, 1024); + $pagelog = new PageChangeLog($id, 1024); $info = $pagelog->getRevisionInfo($time); $data = array( @@ -647,7 +647,7 @@ class RemoteAPICore { throw new RemoteException('Empty page ID', 131); } - $pagelog = new PageRevisionLog($id); + $pagelog = new PageChangeLog($id); $revisions = $pagelog->getRevisions($first, $conf['recent']+1); if(count($revisions)==0 && $first!=0) { 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 */ @@ -376,6 +378,20 @@ class PageRevisionLog { } /** + * 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 @@ -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 <btcoburn@silicodon.net> * @author Kate Arzamastseva <pshns@ukr.net> */ - 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 <btcoburn@silicodon.net> * @author Kate Arzamastseva <pshns@ukr.net> */ - 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 <pshns@ukr.net> */ 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 <pshns@ukr.net> */ 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); } diff --git a/inc/common.php b/inc/common.php index dd30b53ab..7821cb3de 100644 --- a/inc/common.php +++ b/inc/common.php @@ -187,7 +187,7 @@ function pageinfo() { $info['meta'] = p_get_metadata($ID); //who's the editor - $pagelog = new PageRevisionLog($ID, 1024); + $pagelog = new PageChangeLog($ID, 1024); if($REV) { $revinfo = $pagelog->getRevisionInfo($REV); } else { @@ -1060,7 +1060,7 @@ function saveWikiText($id, $text, $summary, $minor = false) { $wasRemoved = (trim($text) == ''); // check for empty or whitespace only $wasCreated = !@file_exists($file); $wasReverted = ($REV == true); - $pagelog = new PageRevisionLog($id, 1024); + $pagelog = new PageChangeLog($id, 1024); $newRev = false; $oldRev = $pagelog->getRevisions(-1, 1); // from changelog $oldRev = (int) (empty($oldRev) ? 0 : $oldRev[0]); diff --git a/inc/html.php b/inc/html.php index b48a17fc5..442126f71 100644 --- a/inc/html.php +++ b/inc/html.php @@ -414,18 +414,23 @@ function html_revisions($first=0, $media_id = false){ global $conf; global $lang; $id = $ID; - if ($media_id) $id = $media_id; + if ($media_id) { + $id = $media_id; + $changelog = new MediaChangeLog($id); + } else { + $changelog = new PageChangeLog($id); + } /* we need to get one additional log entry to be able to * decide if this is the last page or is there another one. * see html_recent() */ - $pagelog = new PageRevisionLog($id); - $revisions = $pagelog->getRevisions($first, $conf['recent']+1, (boolean)$media_id); + + $revisions = $changelog->getRevisions($first, $conf['recent']+1); if(count($revisions)==0 && $first!=0){ $first=0; - $revisions = $pagelog->getRevisions($first, $conf['recent']+1, (boolean)$media_id); + $revisions = $changelog->getRevisions($first, $conf['recent']+1); } $hasNext = false; if (count($revisions)>$conf['recent']) { @@ -484,17 +489,18 @@ function html_revisions($first=0, $media_id = false){ $form->addElement(form_makeCloseTag('span')); } - $pagelog->setChunkSize(1024); + $changelog->setChunkSize(1024); $form->addElement(form_makeOpenTag('span', array('class' => 'user'))); - if (!$media_id) $editor = $INFO['editor']; - else { - $revinfo = $pagelog->getRevisionInfo(@filemtime(fullpath(mediaFN($id))), true); - if($revinfo['user']){ + if($media_id) { + $revinfo = $changelog->getRevisionInfo(@filemtime(fullpath(mediaFN($id)))); + if($revinfo['user']) { $editor = $revinfo['user']; - }else{ + } else { $editor = $revinfo['ip']; } + } else { + $editor = $INFO['editor']; } $form->addElement((empty($editor))?('('.$lang['external_edit'].')'):editorinfo($editor)); $form->addElement(form_makeCloseTag('span')); @@ -509,12 +515,11 @@ function html_revisions($first=0, $media_id = false){ foreach($revisions as $rev){ $date = dformat($rev); - if (!$media_id) { - $info = $pagelog->getRevisionInfo($rev); - $exists = page_exists($id,$rev); - } else { - $info = $pagelog->getRevisionInfo($rev,true); - $exists = @file_exists(mediaFN($id,$rev)); + $info = $changelog->getRevisionInfo($rev); + if($media_id) { + $exists = @file_exists(mediaFN($id, $rev)); + } else { + $exists = page_exists($id, $rev); } if ($info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) @@ -706,8 +711,8 @@ function html_recent($first=0, $show_changes='both'){ $href = ''; if ($recent['media']) { - $pagelog = new PageRevisionLog($recent['id']); - $diff = (count($pagelog->getRevisions(0, 1, true)) && @file_exists(mediaFN($recent['id']))); + $medialog = new MediaChangeLog($recent['id']); + $diff = (count($medialog->getRevisions(0, 1)) && @file_exists(mediaFN($recent['id']))); if ($diff) { $href = media_managerURL(array('tab_details' => 'history', 'mediado' => 'diff', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&'); @@ -1009,11 +1014,15 @@ function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = fa $ml_or_wl = $media ? 'ml' : 'wl'; $l_minor = $r_minor = ''; - $pagelog = new PageRevisionLog($id); + if($media) { + $log = new MediaChangeLog($id); + } else { + $log = new PageChangeLog($id); + } if(!$l_rev){ $l_head = '—'; }else{ - $l_info = $pagelog->getRevisionInfo($l_rev, $media); + $l_info = $log->getRevisionInfo($l_rev); if($l_info['user']){ $l_user = '<bdi>'.editorinfo($l_info['user']).'</bdi>'; if(auth_ismanager()) $l_user .= ' <bdo dir="ltr">('.$l_info['ip'].')</bdo>'; @@ -1031,7 +1040,7 @@ function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = fa } if($r_rev){ - $r_info = $pagelog->getRevisionInfo($r_rev, $media); + $r_info = $log->getRevisionInfo($r_rev); if($r_info['user']){ $r_user = '<bdi>'.editorinfo($r_info['user']).'</bdi>'; if(auth_ismanager()) $r_user .= ' <bdo dir="ltr">('.$r_info['ip'].')</bdo>'; @@ -1047,7 +1056,7 @@ function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = fa $r_head_title.'</a></bdi>'. $head_separator.$r_user.' '.$r_sum; }elseif($_rev = @filemtime($media_or_wikiFN($id))){ - $_info = $pagelog->getRevisionInfo($_rev, $media); + $_info = $log->getRevisionInfo($_rev); if($_info['user']){ $_user = '<bdi>'.editorinfo($_info['user']).'</bdi>'; if(auth_ismanager()) $_user .= ' <bdo dir="ltr">('.$_info['ip'].')</bdo>'; @@ -1084,7 +1093,7 @@ function html_diff($text='',$intro=true,$type=null){ global $lang; global $INPUT; global $INFO; - $pagelog = new PageRevisionLog($ID); + $pagelog = new PageChangeLog($ID); if(!$type) { $type = $INPUT->str('difftype'); diff --git a/inc/media.php b/inc/media.php index 9f0c2cb8c..3bdaa8ef7 100644 --- a/inc/media.php +++ b/inc/media.php @@ -492,8 +492,8 @@ function media_saveOldRevision($id){ $date = filemtime($oldf); if (!$conf['mediarevisions']) return $date; - $pagelog = new PageRevisionLog($id); - if (!$pagelog->getRevisionInfo($date, true)) { + $medialog = new MediaChangeLog($id); + if (!$medialog->getRevisionInfo($date)) { // there was an external edit, // there is no log entry for current version of file if (!@file_exists(mediaMetaFN($id,'.changes'))) { @@ -1072,8 +1072,8 @@ function media_diff($image, $ns, $auth, $fromajax = false) { $l_rev = $rev1; }else{ // no revision was given, compare previous to current $r_rev = ''; - $pagelog = new PageRevisionLog($image); - $revs = $pagelog->getRevisions(0, 1, true); + $medialog = new MediaChangeLog($image); + $revs = $medialog->getRevisions(0, 1); if (file_exists(mediaFN($image, $revs[0]))) { $l_rev = $revs[0]; } else { diff --git a/inc/subscription.php b/inc/subscription.php index 87db3c621..e6fb23f63 100644 --- a/inc/subscription.php +++ b/inc/subscription.php @@ -288,7 +288,7 @@ class Subscription { public function send_bulk($page) { if(!$this->isenabled()) return 0; - /** @var auth_basic $auth */ + /** @var DokuWiki_Auth_Plugin $auth */ global $auth; global $conf; global $USERINFO; @@ -336,7 +336,7 @@ class Subscription { while(!is_null($rev) && $rev['date'] >= $lastupdate && ($_SERVER['REMOTE_USER'] === $rev['user'] || $rev['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT)) { - $pagelog = new PageRevisionLog($rev['id']); + $pagelog = new PageChangeLog($rev['id']); $rev = $pagelog->getRevisions($n++, 1); $rev = (count($rev) > 0) ? $rev[0] : null; } @@ -516,7 +516,7 @@ class Subscription { * @return bool */ protected function send_digest($subscriber_mail, $id, $lastupdate) { - $pagelog = new PageRevisionLog($id); + $pagelog = new PageChangeLog($id); $n = 0; do { $rev = $pagelog->getRevisions($n++, 1); |