3 * Utilities for handling pagenames
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 * @todo Combine similar functions like {wiki,media,meta}FN()
11 * Fetch the an ID from request
13 * Uses either standard $_REQUEST variable or extracts it from
14 * the full request URI when userewrite is set to 2
16 * For $param='id' $conf['start'] is returned if no id was found.
17 * If the second parameter is true (default) the ID is cleaned.
19 * @author Andreas Gohr <andi@splitbrain.org>
21 function getID($param='id',$clean=true){
24 $id = isset($_REQUEST[$param]) ?
$_REQUEST[$param] : null;
26 $request = $_SERVER['REQUEST_URI'];
28 //construct page id from request URI
29 if(empty($id) && $conf['userewrite'] == 2){
34 $relpath = 'lib/exe/';
36 $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
38 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['PATH_TRANSLATED']){
39 $request = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
40 $_SERVER['PATH_TRANSLATED']);
41 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
42 $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
43 $_SERVER['SCRIPT_FILENAME']);
44 $script = '/'.$script;
46 $script = $_SERVER['SCRIPT_NAME'];
49 //clean script and request (fixes a windows problem)
50 $script = preg_replace('/\/\/+/','/',$script);
51 $request = preg_replace('/\/\/+/','/',$request);
53 //remove script URL and Querystring to gain the id
54 if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
55 $id = preg_replace ('/\?.*/','',$match[1]);
58 //strip leading slashes
59 $id = preg_replace('!^/+!','',$id);
62 // Namespace autolinking from URL
63 if(substr($id,-1) == ':' ||
($conf['useslash'] && substr($id,-1) == '/')){
64 if(page_exists($id.$conf['start'])){
65 // start page inside namespace
66 $id = $id.$conf['start'];
67 }elseif(page_exists($id.noNS(cleanID($id)))){
68 // page named like the NS inside the NS
69 $id = $id.noNS(cleanID($id));
70 }elseif(page_exists($id)){
71 // page like namespace exists
72 $id = substr($id,0,-1);
74 // fall back to default
75 $id = $id.$conf['start'];
77 send_redirect(wl($id,'',true));
80 if($clean) $id = cleanID($id);
81 if(empty($id) && $param=='id') $id = $conf['start'];
87 * Remove unwanted chars from ID
89 * Cleans a given ID to only use allowed characters. Accented characters are
90 * converted to unaccented ones
92 * @author Andreas Gohr <andi@splitbrain.org>
93 * @param string $raw_id The pageid to clean
94 * @param boolean $ascii Force ASCII
95 * @param boolean $media Allow leading or trailing _ for media files
97 function cleanID($raw_id,$ascii=false,$media=false){
100 static $sepcharpat = null;
102 global $cache_cleanid;
103 $cache = & $cache_cleanid;
105 // check if it's already in the memory cache
106 if (isset($cache[(string)$raw_id])) {
107 return $cache[(string)$raw_id];
110 $sepchar = $conf['sepchar'];
111 if($sepcharpat == null) // build string only once to save clock cycles
112 $sepcharpat = '#\\'.$sepchar.'+#';
114 $id = trim((string)$raw_id);
115 $id = utf8_strtolower($id);
117 //alternative namespace seperator
118 $id = strtr($id,';',':');
119 if($conf['useslash']){
120 $id = strtr($id,'/',':');
122 $id = strtr($id,'/',$sepchar);
125 if($conf['deaccent'] == 2 ||
$ascii) $id = utf8_romanize($id);
126 if($conf['deaccent'] ||
$ascii) $id = utf8_deaccent($id,-1);
129 $id = utf8_stripspecials($id,$sepchar,'\*');
131 if($ascii) $id = utf8_strip($id);
134 $id = preg_replace($sepcharpat,$sepchar,$id);
135 $id = preg_replace('#:+#',':',$id);
136 $id = ($media ?
trim($id,':.-') : trim($id,':._-'));
137 $id = preg_replace('#:[:\._\-]+#',':',$id);
139 $cache[(string)$raw_id] = $id;
144 * Return namespacepart of a wiki ID
146 * @author Andreas Gohr <andi@splitbrain.org>
149 $pos = strrpos((string)$id,':');
151 return substr((string)$id,0,$pos);
157 * Returns the ID without the namespace
159 * @author Andreas Gohr <andi@splitbrain.org>
162 $pos = strrpos($id, ':');
164 return substr($id, $pos+
1);
171 * Returns the current namespace
173 * @author Nathan Fritz <fritzn@crown.edu>
175 function curNS($id) {
176 return noNS(getNS($id));
180 * Returns the ID without the namespace or current namespace for 'start' pages
182 * @author Nathan Fritz <fritzn@crown.edu>
184 function noNSorNS($id) {
188 if ($p == $conf['start']) {
198 * Creates a XHTML valid linkid from a given headline title
200 * @param string $title The headline title
201 * @param array $check List of existing IDs
202 * @author Andreas Gohr <andi@splitbrain.org>
204 function sectionID($title,&$check) {
205 $title = str_replace(array(':','.'),'',cleanID($title));
206 $new = ltrim($title,'0123456789_-');
208 $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
213 if(is_array($check)){
214 // make sure tiles are unique
216 while(in_array($title.$num,$check)){
217 ($num) ?
$num++
: $num = 1;
219 $title = $title.$num;
228 * Wiki page existence check
230 * parameters as for wikiFN
232 * @author Chris Smith <chris@jalakai.co.uk>
234 function page_exists($id,$rev='',$clean=true) {
235 return @file_exists
(wikiFN($id,$rev,$clean));
239 * returns the full path to the datafile specified by ID and optional revision
241 * The filename is URL encoded to protect Unicode chars
243 * @param $raw_id string id of wikipage
244 * @param $rev string page revision, empty string for current
245 * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false
246 * when $id is guaranteed to have been cleaned already.
248 * @author Andreas Gohr <andi@splitbrain.org>
250 function wikiFN($raw_id,$rev='',$clean=true){
253 global $cache_wikifn;
254 $cache = & $cache_wikifn;
256 if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
257 return $cache[$raw_id][$rev];
262 if ($clean) $id = cleanID($id);
263 $id = str_replace(':','/',$id);
265 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
267 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
268 if($conf['compression']){
269 //test for extensions here, we want to read both compressions
270 if (@file_exists
($fn . '.gz')){
272 }else if(@file_exists
($fn . '.bz2')){
275 //file doesnt exist yet, so we take the configured extension
276 $fn .= '.' . $conf['compression'];
281 if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
282 $cache[$raw_id][$rev] = $fn;
287 * Returns the full path to the file for locking the page while editing.
289 * @author Ben Coburn <btcoburn@silicodon.net>
291 function wikiLockFN($id) {
293 return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
298 * returns the full path to the meta file specified by ID and extension
300 * The filename is URL encoded to protect Unicode chars
302 * @author Steven Danz <steven-danz@kc.rr.com>
304 function metaFN($id,$ext){
307 $id = str_replace(':','/',$id);
308 $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
313 * returns an array of full paths to all metafiles of a given ID
315 * @author Esther Brunner <esther@kaffeehaus.ch>
317 function metaFiles($id){
320 $dir = ($ns) ?
metaFN($ns,'').'/' : metaFN($ns,'');
323 $dh = @opendir
($dir);
324 if(!$dh) return $files;
325 while(($file = readdir($dh)) !== false){
326 if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
327 $files[] = $dir.$file;
335 * returns the full path to the mediafile specified by ID
337 * The filename is URL encoded to protect Unicode chars
339 * @author Andreas Gohr <andi@splitbrain.org>
341 function mediaFN($id){
344 $id = str_replace(':','/',$id);
345 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
350 * Returns the full filepath to a localized textfile if local
351 * version isn't found the english one is returned
353 * @author Andreas Gohr <andi@splitbrain.org>
355 function localeFN($id){
357 $file = DOKU_INC
.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
358 if(!@file_exists
($file)){
359 //fall back to english
360 $file = DOKU_INC
.'inc/lang/en/'.$id.'.txt';
366 * Resolve relative paths in IDs
368 * Do not call directly use resolve_mediaid or resolve_pageid
371 * Partyly based on a cleanPath function found at
372 * http://www.php.net/manual/en/function.realpath.php#57016
374 * @author <bart at mediawave dot nl>
376 function resolve_id($ns,$id,$clean=true){
379 // some pre cleaning for useslash:
380 if($conf['useslash']) $id = str_replace('/',':',$id);
382 // if the id starts with a dot we need to handle the
385 // normalize initial dots without a colon
386 $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
387 // prepend the current namespace
392 $pathA = explode(':', $id);
393 if (!$pathA[0]) $result[] = '';
394 foreach ($pathA AS $key => $dir) {
396 if (end($result) == '..') {
398 } elseif (!array_pop($result)) {
401 } elseif ($dir && $dir != '.') {
405 if (!end($pathA)) $result[] = '';
406 $id = implode(':', $result);
407 }elseif($ns !== false && strpos($id,':') === false){
408 //if link contains no namespace. add current namespace (if any)
412 if($clean) $id = cleanID($id);
417 * Returns a full media id
419 * @author Andreas Gohr <andi@splitbrain.org>
421 function resolve_mediaid($ns,&$page,&$exists){
422 $page = resolve_id($ns,$page);
423 $file = mediaFN($page);
424 $exists = @file_exists
($file);
428 * Returns a full page id
430 * @author Andreas Gohr <andi@splitbrain.org>
432 function resolve_pageid($ns,&$page,&$exists){
436 //keep hashlink if exists then clean both parts
437 if (strpos($page,'#')) {
438 list($page,$hash) = explode('#',$page,2);
442 $hash = cleanID($hash);
443 $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
445 // get filename (calls clean itself)
446 $file = wikiFN($page);
448 // if ends with colon or slash we have a namespace link
449 if(substr($page,-1) == ':' ||
($conf['useslash'] && substr($page,-1) == '/')){
450 if(page_exists($page.$conf['start'])){
451 // start page inside namespace
452 $page = $page.$conf['start'];
454 }elseif(page_exists($page.noNS(cleanID($page)))){
455 // page named like the NS inside the NS
456 $page = $page.noNS(cleanID($page));
458 }elseif(page_exists($page)){
459 // page like namespace exists
463 // fall back to default
464 $page = $page.$conf['start'];
467 //check alternative plural/nonplural form
468 if(!@file_exists
($file)){
469 if( $conf['autoplural'] ){
470 if(substr($page,-1) == 's'){
471 $try = substr($page,0,-1);
475 if(page_exists($try)){
485 // now make sure we have a clean page
486 $page = cleanID($page);
489 if(!empty($hash)) $page .= '#'.$hash;
493 * Returns the name of a cachefile from given data
495 * The needed directory is created by this function!
497 * @author Andreas Gohr <andi@splitbrain.org>
499 * @param string $data This data is used to create a unique md5 name
500 * @param string $ext This is appended to the filename if given
501 * @return string The filename of the cachefile
503 function getCacheName($data,$ext=''){
506 $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
507 io_makeFileDir($file);
512 * Checks a pageid against $conf['hidepages']
514 * @author Andreas Gohr <gohr@cosmocode.de>
516 function isHiddenPage($id){
519 if(empty($conf['hidepages'])) return false;
520 if($ACT == 'admin') return false;
522 if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
529 * Reverse of isHiddenPage
531 * @author Andreas Gohr <gohr@cosmocode.de>
533 function isVisiblePage($id){
534 return !isHiddenPage($id);
538 * Format an id for output to a user
540 * Namespaces are denoted by a trailing “:*”. The root namespace is
541 * “*”. Output is escaped.
543 * @author Adrian Lang <lang@cosmocode.de>
546 function prettyprint_id($id) {
547 if (!$id ||
$id === ':') {
550 if ((substr($id, -1, 1) === ':')) {