Merge pull request #624 from lisps/revisions

date_at support
This commit is contained in:
Andreas Gohr 2014-09-29 18:29:20 +02:00
commit 17553fca19
14 changed files with 286 additions and 31 deletions

View File

@ -0,0 +1,127 @@
<?php
/**
* Tests for requesting revisioninfo of a revision of a page with getRevisionInfo()
*
* This class uses the files:
* - data/pages/mailinglist.txt
* - data/meta/mailinglist.changes
*/
class changelog_getlastrevisionat_test extends DokuWikiTest {
private $pageid = 'mailinglist';
function setup() {
parent::setup();
global $cache_revinfo;
$cache =& $cache_revinfo;
if(isset($cache['nonexist'])) {
unset($cache['nonexist']);
}
if(isset($cache['mailinglist'])) {
unset($cache['mailinglist']);
}
}
/**
* no nonexist.changes meta file available
*/
function test_changemetadatanotexists() {
$rev = 1362525899;
$id = 'nonexist';
$revsexpected = false;
$pagelog = new PageChangeLog($id, $chunk_size = 8192);
$revs = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($revsexpected, $revs);
}
/**
* start at exact current revision of mailinglist page
*
*/
function test_startatexactcurrentrev() {
$rev = 1385051947;
$revsexpected = '';
//set a known timestamp
touch(wikiFN($this->pageid), $rev);
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
$revs = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($revsexpected, $revs);
}
/**
* test a future revision
*
*/
function test_futurerev() {
$rev = 1385051947;
$revsexpected = '';
//set a known timestamp
touch(wikiFN($this->pageid), $rev);
$rev +=1;
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
$revs = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($revsexpected, $revs);
}
/**
* start at exact last revision of mailinglist page
*
*/
function test_exactlastrev() {
$rev = 1360110636;
$revsexpected = 1360110636;
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
$revs = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($revsexpected, $revs);
}
/**
* Request not existing revision
*
*/
function test_olderrev() {
$rev = 1;
$revexpected = false;
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
$revfound = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($revexpected, $revfound);
}
/**
* Start at non existing revision somewhere between existing revisions
*/
function test_notexistingrev() {
$rev = 1362525890;
$revexpected = 1362525359;
$pagelog = new PageChangeLog($this->pageid, $chunk_size = 8192);
$revfound = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($revexpected, $revfound);
}
/**
* request nonexisting page
*
*/
function test_notexistingpage() {
$rev = 1385051947;
$currentexpected = false;
$pagelog = new PageChangeLog('nonexistingpage', $chunk_size = 8192);
$current = $pagelog->getLastRevisionAt($rev);
$this->assertEquals($currentexpected, $current);
}
}

View File

@ -146,4 +146,15 @@ class common_ml_test extends DokuWikiTest {
$this->assertEquals($expect, ml($id, $args));
}
function test_ml_empty_rev() {
global $conf;
$conf['useslash'] = 0;
$conf['userewrite'] = 0;
$args = array('a' => 'b', 'c' => 'd', 'rev' => '');
$expect = DOKU_BASE . $this->script . '?a=b&amp;c=d&amp;media=some:img.jpg';
$this->assertEquals($expect, ml('some:img.jpg', $args));
}
}

View File

@ -142,6 +142,17 @@ class common_wl_test extends DokuWikiTest {
$expect = DOKU_BASE . DOKU_SCRIPT . '/some/one?a=b&c=d';
$this->assertEquals($expect, wl('some:one', 'a=b,c=d', false, '&'));
}
function test_wl_empty_rev() {
global $conf;
$conf['useslash'] = 0;
$conf['userewrite'] = 0;
$args = array('a' => 'b', 'c' => 'd', 'rev' => '');
$expect = DOKU_BASE . DOKU_SCRIPT . '?id=some:&amp;a=b&amp;c=d';
$this->assertEquals($expect, wl('some:', $args));
}

View File

@ -34,6 +34,7 @@ $QUERY = trim($INPUT->str('id'));
$ID = getID();
$REV = $INPUT->int('rev');
$DATE_AT = $INPUT->str('at');
$IDX = $INPUT->str('idx');
$DATE = $INPUT->int('date');
$RANGE = $INPUT->str('range');
@ -47,7 +48,41 @@ $PRE = cleanText(substr($INPUT->post->str('prefix'), 0, -1));
$SUF = cleanText($INPUT->post->str('suffix'));
$SUM = $INPUT->post->str('summary');
//make info about the selected page available
//parse DATE_AT
if($DATE_AT) {
$date_parse = strtotime($DATE_AT);
if($date_parse) {
$DATE_AT = $date_parse;
} else { // check for UNIX Timestamp
$date_parse = @date('Ymd',$DATE_AT);
if(!$date_parse || $date_parse === '19700101') {
msg(sprintf($lang['unable_to_parse_date'], $DATE_AT));
$DATE_AT = null;
}
}
}
//check for existing $REV related to $DATE_AT
if($DATE_AT) {
$pagelog = new PageChangeLog($ID);
$rev_t = $pagelog->getLastRevisionAt($DATE_AT);
if($rev_t === '') { //current revision
$REV = null;
$DATE_AT = null;
} else if ($rev_t === false) { //page did not exist
$rev_n = $pagelog->getRelativeRevision($DATE_AT,+1);
msg(sprintf($lang['page_nonexist_rev'],
strftime($conf['dformat'],$DATE_AT),
wl($ID, array('rev' => $rev_n)),
strftime($conf['dformat'],$rev_n)));
$REV = $DATE_AT; //will result in a page not exists message
} else {
$REV = $rev_t;
}
}
//make infos about the selected page available
$INFO = pageinfo();
//export minimal info to JS, plugins can add more

View File

@ -845,6 +845,25 @@ abstract class ChangeLog {
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;
}
}
/**
* Returns the next lines of the changelog of the chunck before head or after tail
@ -1072,3 +1091,4 @@ function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false) {
}
return $changelog->getRevisions($first, $num);
}

View File

@ -438,6 +438,8 @@ function idfilter($id, $ue = true) {
function wl($id = '', $urlParameters = '', $absolute = false, $separator = '&amp;') {
global $conf;
if(is_array($urlParameters)) {
if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']);
if(isset($urlParameters['at']) && $conf['date_at_format']) $urlParameters['at'] = date($conf['date_at_format'],$urlParameters['at']);
$urlParameters = buildURLparams($urlParameters, $separator);
} else {
$urlParameters = str_replace(',', $separator, $urlParameters);
@ -544,6 +546,7 @@ function ml($id = '', $more = '', $direct = true, $sep = '&amp;', $abs = false)
if(empty($more['w'])) unset($more['w']);
if(empty($more['h'])) unset($more['h']);
if(isset($more['id']) && $direct) unset($more['id']);
if(isset($more['rev']) && !$more['rev']) unset($more['rev']);
$more = buildURLparams($more, $sep);
} else {
$matches = array();

View File

@ -222,6 +222,7 @@ function html_show($txt=null){
global $REV;
global $HIGH;
global $INFO;
global $DATE_AT;
//disable section editing for old revisions or in preview
if($txt || $REV){
$secedit = false;
@ -241,8 +242,8 @@ function html_show($txt=null){
echo '</div></div>';
}else{
if ($REV) print p_locale_xhtml('showrev');
$html = p_wiki_xhtml($ID,$REV,true);
if ($REV||$DATE_AT) print p_locale_xhtml('showrev');
$html = p_wiki_xhtml($ID,$REV,true,$DATE_AT);
$html = html_secedit($html,$secedit);
if($INFO['prependTOC']) $html = tpl_toc(true).$html;
$html = html_hilight($html,$HIGH);

View File

@ -368,4 +368,6 @@ $lang['currentns'] = 'Current namespace';
$lang['searchresult'] = 'Search Result';
$lang['plainhtml'] = 'Plain HTML';
$lang['wikimarkup'] = 'Wiki Markup';
$lang['page_nonexist_rev'] = 'Page did not exist at %s. It was subsequently created at <a href="%s">%s</a>.';
$lang['unable_to_parse_date'] = 'Unable to parse at parameter "%s".';
//Setup VIM: ex: et ts=2 :

View File

@ -255,7 +255,13 @@ function sectionID($title,&$check) {
* @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well)
* @return bool exists?
*/
function page_exists($id,$rev='',$clean=true) {
function page_exists($id,$rev='',$clean=true, $date_at=false) {
if($rev !== '' && $date_at) {
$pagelog = new PageChangeLog($id);
$pagelog_rev = $pagelog->getLastRevisionAt($rev);
if($pagelog_rev !== false)
$rev = $pagelog_rev;
}
return @file_exists(wikiFN($id,$rev,$clean));
}
@ -486,9 +492,17 @@ function resolve_id($ns,$id,$clean=true){
* @param string &$page (reference) relative media id, updated to resolved id
* @param bool &$exists (reference) updated with existance of media
*/
function resolve_mediaid($ns,&$page,&$exists){
function resolve_mediaid($ns,&$page,&$exists,$rev='',$date_at=false){
$page = resolve_id($ns,$page);
$file = mediaFN($page);
if($rev !== '' && $date_at){
$medialog = new MediaChangeLog($page);
$medialog_rev = $medialog->getLastRevisionAt($rev);
if($medialog_rev !== false) {
$rev = $medialog_rev;
}
}
$file = mediaFN($page,$rev);
$exists = @file_exists($file);
}
@ -501,7 +515,7 @@ function resolve_mediaid($ns,&$page,&$exists){
* @param string &$page (reference) relative page id, updated to resolved id
* @param bool &$exists (reference) updated with existance of media
*/
function resolve_pageid($ns,&$page,&$exists){
function resolve_pageid($ns,&$page,&$exists,$rev='',$date_at=false ){
global $conf;
global $ID;
$exists = false;
@ -521,20 +535,26 @@ function resolve_pageid($ns,&$page,&$exists){
$page = resolve_id($ns,$page,false); // resolve but don't clean, yet
// get filename (calls clean itself)
$file = wikiFN($page);
if($rev !== '' && $date_at) {
$pagelog = new PageChangeLog($page);
$pagelog_rev = $pagelog->getLastRevisionAt($rev);
if($pagelog_rev !== false)//something found
$rev = $pagelog_rev;
}
$file = wikiFN($page,$rev);
// if ends with colon or slash we have a namespace link
if(in_array(substr($page,-1), array(':', ';')) ||
($conf['useslash'] && substr($page,-1) == '/')){
if(page_exists($page.$conf['start'])){
if(page_exists($page.$conf['start'],$rev,true,$date_at)){
// start page inside namespace
$page = $page.$conf['start'];
$exists = true;
}elseif(page_exists($page.noNS(cleanID($page)))){
}elseif(page_exists($page.noNS(cleanID($page)),$rev,true,$date_at)){
// page named like the NS inside the NS
$page = $page.noNS(cleanID($page));
$exists = true;
}elseif(page_exists($page)){
}elseif(page_exists($page,$rev,true,$date_at)){
// page like namespace exists
$page = $page;
$exists = true;
@ -551,7 +571,7 @@ function resolve_pageid($ns,&$page,&$exists){
}else{
$try = $page.'s';
}
if(page_exists($try)){
if(page_exists($try,$rev,true,$date_at)){
$page = $try;
$exists = true;
}

View File

@ -28,6 +28,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
/** @var array A stack of section edit data */
protected $sectionedits = array();
var $date_at = ''; // link pages and media against this revision
/** @var int last section edit id, used by startSectionEdit */
protected $lastsecid = 0;
@ -818,7 +819,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$default = $this->_simpleTitle($id);
// now first resolve and clean up the $id
resolve_pageid(getNS($ID), $id, $exists);
resolve_pageid(getNS($ID), $id, $exists, $this->date_at, true);
$name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
if(!$isImage) {
@ -846,11 +847,14 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$link['pre'] = '<span class="curid">';
$link['suf'] = '</span>';
}
$link['more'] = '';
$link['class'] = $class;
$link['url'] = wl($id, $params);
$link['name'] = $name;
$link['title'] = $id;
$link['more'] = '';
$link['class'] = $class;
if($this->date_at) {
$params['at'] = $this->date_at;
}
$link['url'] = wl($id, $params);
$link['name'] = $name;
$link['title'] = $id;
//add search string
if($search) {
($conf['userewrite']) ? $link['url'] .= '?' : $link['url'] .= '&amp;';
@ -1062,7 +1066,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$height = null, $cache = null, $linking = null, $return = false) {
global $ID;
list($src, $hash) = explode('#', $src, 2);
resolve_mediaid(getNS($ID), $src, $exists);
resolve_mediaid(getNS($ID), $src, $exists, $this->date_at, true);
$noLink = false;
$render = ($linking == 'linkonly') ? false : true;
@ -1070,7 +1074,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
list($ext, $mime) = mimetype($src, false);
if(substr($mime, 0, 5) == 'image' && $render) {
$link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), ($linking == 'direct'));
$link['url'] = ml($src, array('id' => $ID, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src)), ($linking == 'direct'));
} elseif(($mime == 'application/x-shockwave-flash' || media_supportedav($mime)) && $render) {
// don't link movies
$noLink = true;
@ -1078,7 +1082,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
// add file icons
$class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
$link['class'] .= ' mediafile mf_'.$class;
$link['url'] = ml($src, array('id' => $ID, 'cache' => $cache), true);
$link['url'] = ml($src, array('id' => $ID, 'cache' => $cache , 'rev'=>$this->_getLastMediaRevisionAt($src)), true);
if($exists) $link['title'] .= ' ('.filesize_h(filesize(mediaFN($src))).')';
}
@ -1436,7 +1440,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
return $title;
}
//add image tag
$ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache)).'"';
$ret .= '<img src="'.ml($src, array('w' => $width, 'h' => $height, 'cache' => $cache, 'rev'=>$this->_getLastMediaRevisionAt($src))).'"';
$ret .= ' class="media'.$align.'"';
if($title) {
@ -1581,7 +1585,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
// see internalmedia() and externalmedia()
list($img['src']) = explode('#', $img['src'], 2);
if($img['type'] == 'internalmedia') {
resolve_mediaid(getNS($ID), $img['src'], $exists);
resolve_mediaid(getNS($ID), $img['src'], $exists ,$this->date_at, true);
}
return $this->_media(
@ -1740,6 +1744,21 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
$out .= '</audio>'.NL;
return $out;
}
/**
* _getLastMediaRevisionAt is a helperfunction to internalmedia() and _media()
* which returns an existing media revision less or equal to rev or date_at
*
* @author lisps
* @param string $media_id
* @access protected
* @return string revision ('' for current)
*/
function _getLastMediaRevisionAt($media_id){
if(!$this->date_at || media_isexternal($media_id)) return '';
$pagelog = new MediaChangeLog($media_id);
return $pagelog->getLastRevisionAt($this->date_at);
}
#endregion
}

View File

@ -56,7 +56,7 @@ define('METADATA_RENDER_UNLIMITED', 4);
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function p_wiki_xhtml($id, $rev='', $excuse=true){
function p_wiki_xhtml($id, $rev='', $excuse=true,$date_at=''){
$file = wikiFN($id,$rev);
$ret = '';
@ -65,9 +65,9 @@ function p_wiki_xhtml($id, $rev='', $excuse=true){
$keep = $ID;
$ID = $id;
if($rev){
if($rev || $date_at){
if(@file_exists($file)){
$ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions
$ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info,$date_at); //no caching on old revisions
}elseif($excuse){
$ret = p_locale_xhtml('norev');
}
@ -583,7 +583,7 @@ function p_sort_modes($a, $b){
* @author Harry Fuecks <hfuecks@gmail.com>
* @author Andreas Gohr <andi@splitbrain.org>
*/
function p_render($mode,$instructions,&$info){
function p_render($mode,$instructions,&$info,$date_at=''){
if(is_null($instructions)) return '';
$Renderer = p_get_renderer($mode);
@ -591,6 +591,10 @@ function p_render($mode,$instructions,&$info){
$Renderer->reset();
if($date_at) {
$Renderer->date_at = $date_at;
}
$Renderer->smileys = getSmileys();
$Renderer->entities = getEntities();
$Renderer->acronyms = getAcronyms();

View File

@ -1123,6 +1123,7 @@ function tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null) {
global $IMG;
/** @var Input $INPUT */
global $INPUT;
global $REV;
$w = tpl_img_getTag('File.Width');
$h = tpl_img_getTag('File.Height');
@ -1147,8 +1148,8 @@ function tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null) {
}
//prepare URLs
$url = ml($IMG, array('cache'=> $INPUT->str('cache')), true, '&');
$src = ml($IMG, array('cache'=> $INPUT->str('cache'), 'w'=> $w, 'h'=> $h), true, '&');
$url = ml($IMG, array('cache'=> $INPUT->str('cache'),'rev'=>$REV), true, '&');
$src = ml($IMG, array('cache'=> $INPUT->str('cache'),'rev'=>$REV, 'w'=> $w, 'h'=> $h), true, '&');
//prepare attributes
$alt = tpl_img_getTag('Simple.Title');

View File

@ -5,6 +5,7 @@ require_once(DOKU_INC.'inc/init.php');
$IMG = getID('media');
$ID = cleanID($INPUT->str('id'));
$REV = $INPUT->int('rev');
// this makes some general info available as well as the info about the
// "parent" page
@ -35,7 +36,7 @@ $ERROR = false;
$AUTH = auth_quickaclcheck($IMG);
if($AUTH >= AUTH_READ){
// check if image exists
$SRC = mediaFN($IMG);
$SRC = mediaFN($IMG,$REV);
if(!@file_exists($SRC)){
//doesn't exist!
http_status(404);

View File

@ -49,7 +49,7 @@ header('X-UA-Compatible: IE=edge,chrome=1');
if($ERROR):
echo '<h1>'.$ERROR.'</h1>';
else: ?>
<?php if($REV) echo p_locale_xhtml('showrev');?>
<h1><?php echo nl2br(hsc(tpl_img_getTag('simple.title'))); ?></h1>
<?php tpl_img(900,700); /* parameters: maximum width, maximum height (and more) */ ?>