Skip to content

Commit

Permalink
Merge pull request #624 from lisps/revisions
Browse files Browse the repository at this point in the history
 date_at support
  • Loading branch information
splitbrain committed Sep 29, 2014
2 parents 2f98176 + 80d9f3d commit 17553fc
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 31 deletions.
127 changes: 127 additions & 0 deletions _test/tests/inc/changelog_getlastrevisionat.test.php
@@ -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);
}
}
11 changes: 11 additions & 0 deletions _test/tests/inc/common_ml.test.php
Expand Up @@ -146,4 +146,15 @@ function test_ml_imgresize_array_external() {
$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));
}
}
11 changes: 11 additions & 0 deletions _test/tests/inc/common_wl.test.php
Expand Up @@ -142,6 +142,17 @@ function test_wl_useslash_rewrite2() {
$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));
}



Expand Down
37 changes: 36 additions & 1 deletion doku.php
Expand Up @@ -34,6 +34,7 @@
$ID = getID();

$REV = $INPUT->int('rev');
$DATE_AT = $INPUT->str('at');
$IDX = $INPUT->str('idx');
$DATE = $INPUT->int('date');
$RANGE = $INPUT->str('range');
Expand All @@ -47,7 +48,41 @@
$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
Expand Down
20 changes: 20 additions & 0 deletions inc/changelog.php
Expand Up @@ -845,6 +845,25 @@ protected function getNewlinepointer($fp, $finger) {
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
Expand Down Expand Up @@ -1072,3 +1091,4 @@ function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false) {
}
return $changelog->getRevisions($first, $num);
}

3 changes: 3 additions & 0 deletions inc/common.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions inc/html.php
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions inc/lang/en/lang.php
Expand Up @@ -368,4 +368,6 @@
$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 :
38 changes: 29 additions & 9 deletions inc/pageutils.php
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 17553fc

Please sign in to comment.