diff options
author | Andreas Gohr <andi@splitbrain.org> | 2006-06-11 20:44:53 +0200 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2006-06-11 20:44:53 +0200 |
commit | a6ef4796e22156364843a3b42bdd8f2dc78c0db5 (patch) | |
tree | 6e0677e925e8538b5a903d5456763ecd008e4395 | |
parent | 54662a044eb6fd07b3246aed0d598d02bf7a1e4a (diff) | |
download | rpg-a6ef4796e22156364843a3b42bdd8f2dc78c0db5.tar.gz rpg-a6ef4796e22156364843a3b42bdd8f2dc78c0db5.tar.bz2 |
namespace linking first part
This patch adds namespace linking - formerly known as globalstart patch.
It differs somewhat from the original patch. It is not implemented in getID()
but in resolve_pageid().
It is now possible to link to a "default" file of a namespace ending the linkid
with a colon: [[foo:bar:]]
To which page the link links is dependent on the xistance of certain named
files. For the above mentioned link [[foo:bar:]] the following pages are checked:
foo:bar:start
foo:bar:bar
foo:bar
The pages are checked in the order above whatever page is found first will be
linked to. If no page is found foo:bar:start will be chosen. BTW: 'start' is
the value configured in $conf['start']
Note: autoplural linking is not done for those links
This is just the first patch. Several other locations of the code need to be
adjusted to reflect this change and some testing needs to be done (first test
cases are supplied within the patch bundle)
Things that maybe need adjustment:
- tpl_youarehere (hierarchical breadcrumbs)
- tpl_button back
- maybe others (search?)
Patches would be welcome. The best aproach to fix things that don't work is
probably making calls to resolve_pageid() instead of simple cleanID() calls.
darcs-hash:20060611184453-7ad00-ba70b0fcf2cb64d4d4f0ce6bd6e437595cd947d3.gz
-rw-r--r-- | _test/cases/inc/pageutils_resolve_pageid.test.php | 60 | ||||
-rw-r--r-- | inc/pageutils.php | 58 |
2 files changed, 102 insertions, 16 deletions
diff --git a/_test/cases/inc/pageutils_resolve_pageid.test.php b/_test/cases/inc/pageutils_resolve_pageid.test.php new file mode 100644 index 000000000..2546bd2af --- /dev/null +++ b/_test/cases/inc/pageutils_resolve_pageid.test.php @@ -0,0 +1,60 @@ +<?php +require_once DOKU_INC.'inc/utf8.php'; +require_once DOKU_INC.'inc/pageutils.php'; + +class init_resolve_pageid_test extends UnitTestCase { + + + function test1(){ + global $conf; + + // we test multiple cases here + // format: $ns, $page, $output + $tests = array(); + + // relative current in root + $tests[] = array('','page','page'); + $tests[] = array('','.page','page'); + $tests[] = array('','.:page','page'); + + // relative current in namespace + $tests[] = array('lev1:lev2','page','lev1:lev2:page'); + $tests[] = array('lev1:lev2','.page','lev1:lev2:page'); + $tests[] = array('lev1:lev2','.:page','lev1:lev2:page'); + + // relative upper in root + $tests[] = array('','..page','page'); + $tests[] = array('','..:page','page'); + + // relative upper in namespace + $tests[] = array('lev1:lev2','..page','lev1:page'); + $tests[] = array('lev1:lev2','..:page','lev1:page'); + $tests[] = array('lev1:lev2','..:..:page','page'); + $tests[] = array('lev1:lev2','..:..:..:page','page'); + + // strange and broken ones + $tests[] = array('lev1:lev2','....:....:page','lev1:lev2:page'); + $tests[] = array('lev1:lev2','..:..:lev3:page','lev3:page'); + $tests[] = array('lev1:lev2','..:..:lev3:..:page','page'); + $tests[] = array('lev1:lev2','..:..:lev3:..:page:....:...','page'); + + // now some tests with existing and none existing files + $conf['start'] = 'start'; + + $tests[] = array('','.:','start'); + $tests[] = array('foo','.:','foo:start'); + $tests[] = array('','foo:','foo:start'); + $tests[] = array('foo','foo:','foo:start'); + $tests[] = array('foo','playground:','playground:playground'); + + + foreach($tests as $test){ + $page = $test[1]; + resolve_pageid($test[0],$page,$foo); + + $this->assertEqual($page,$test[2]); + } + } + +} +//Setup VIM: ex: et ts=4 enc=utf-8 : diff --git a/inc/pageutils.php b/inc/pageutils.php index 6adeb85b2..29cb0379c 100644 --- a/inc/pageutils.php +++ b/inc/pageutils.php @@ -229,7 +229,7 @@ function localeFN($id){ * * @author <bart at mediawave dot nl> */ -function resolve_id($ns,$id){ +function resolve_id($ns,$id,$clean=true){ // if the id starts with a dot we need to handle the // relative stuff if($id{0} == '.'){ @@ -260,7 +260,8 @@ function resolve_id($ns,$id){ $id = $ns.':'.$id; } - return cleanID($id); + if($clean) $id = cleanID($id); + return $id; } /** @@ -285,28 +286,53 @@ function resolve_pageid($ns,&$page,&$exists){ //keep hashlink if exists then clean both parts list($page,$hash) = split('#',$page,2); - $page = resolve_id($ns,$page); $hash = cleanID($hash); + $page = resolve_id($ns,$page,false); // resolve but don't clean, yet + // get filename (calls clean itself) $file = wikiFN($page); - //check alternative plural/nonplural form - if(!@file_exists($file)){ - if( $conf['autoplural'] ){ - if(substr($page,-1) == 's'){ - $try = substr($page,0,-1); - }else{ - $try = $page.'s'; - } - if(@file_exists(wikiFN($try))){ - $page = $try; - $exists = true; - } + // if ends with colon we have a namespace link + if(substr($page,-1) == ':'){ + if(@file_exists(wikiFN($page.$conf['start']))){ + // start page inside namespace + $page = $page.$conf['start']; + $exists = true; + }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){ + // page named like the NS inside the NS + $page = $page.noNS(cleanID($page)); + $exists = true; + }elseif(@file_exists(wikiFN($page))){ + // page like namespace exists + $page = $page; + $exists = true; + }else{ + // fall back to default + $page = $page.$conf['start']; + $exists = false; } }else{ - $exists = true; + //check alternative plural/nonplural form + if(!@file_exists($file)){ + if( $conf['autoplural'] ){ + if(substr($page,-1) == 's'){ + $try = substr($page,0,-1); + }else{ + $try = $page.'s'; + } + if(@file_exists(wikiFN($try))){ + $page = $try; + $exists = true; + } + } + }else{ + $exists = true; + } } + // now make sure we have a clean page + $page = cleanID($page); + //add hash if any if(!empty($hash)) $page .= '#'.$hash; } |