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){
25 $id = $INPUT->str($param);
27 //construct page id from request URI
28 if(empty($id) && $conf['userewrite'] == 2){
29 $request = $_SERVER['REQUEST_URI'];
36 $relpath = 'lib/exe/';
38 $script = $conf['basedir'].$relpath.utf8_basename($_SERVER['SCRIPT_FILENAME']);
40 }elseif($_SERVER['PATH_INFO']){
41 $request = $_SERVER['PATH_INFO'];
42 }elseif($_SERVER['SCRIPT_NAME']){
43 $script = $_SERVER['SCRIPT_NAME'];
44 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
45 $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
46 $_SERVER['SCRIPT_FILENAME']);
47 $script = '/'.$script;
50 //clean script and request (fixes a windows problem)
51 $script = preg_replace('/\/\/+/','/',$script);
52 $request = preg_replace('/\/\/+/','/',$request);
54 //remove script URL and Querystring to gain the id
55 if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
56 $id = preg_replace ('/\?.*/','',$match[1]);
59 //strip leading slashes
60 $id = preg_replace('!^/+!','',$id);
63 // Namespace autolinking from URL
64 if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
65 if(page_exists($id.$conf['start'])){
66 // start page inside namespace
67 $id = $id.$conf['start'];
68 }elseif(page_exists($id.noNS(cleanID($id)))){
69 // page named like the NS inside the NS
70 $id = $id.noNS(cleanID($id));
71 }elseif(page_exists($id)){
72 // page like namespace exists
73 $id = substr($id,0,-1);
75 // fall back to default
76 $id = $id.$conf['start'];
78 send_redirect(wl($id,'',true));
81 if($clean) $id = cleanID($id);
82 if(empty($id) && $param=='id') $id = $conf['start'];
88 * Remove unwanted chars from ID
90 * Cleans a given ID to only use allowed characters. Accented characters are
91 * converted to unaccented ones
93 * @author Andreas Gohr <andi@splitbrain.org>
94 * @param string $raw_id The pageid to clean
95 * @param boolean $ascii Force ASCII
96 * @param boolean $media DEPRECATED
98 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 = trim($id,':._-');
137 $id = preg_replace('#:[:\._\-]+#',':',$id);
138 $id = preg_replace('#[:\._\-]+:#',':',$id);
140 $cache[(string)$raw_id] = $id;
145 * Return namespacepart of a wiki ID
147 * @author Andreas Gohr <andi@splitbrain.org>
150 $pos = strrpos((string)$id,':');
152 return substr((string)$id,0,$pos);
158 * Returns the ID without the namespace
160 * @author Andreas Gohr <andi@splitbrain.org>
163 $pos = strrpos($id, ':');
165 return substr($id, $pos+1);
172 * Returns the current namespace
174 * @author Nathan Fritz <fritzn@crown.edu>
176 function curNS($id) {
177 return noNS(getNS($id));
181 * Returns the ID without the namespace or current namespace for 'start' pages
183 * @author Nathan Fritz <fritzn@crown.edu>
185 function noNSorNS($id) {
189 if ($p == $conf['start'] || $p == false) {
192 return $conf['start'];
199 * Creates a XHTML valid linkid from a given headline title
201 * @param string $title The headline title
202 * @param array|bool $check Existing IDs (title => number)
203 * @return string the title
204 * @author Andreas Gohr <andi@splitbrain.org>
206 function sectionID($title,&$check) {
207 $title = str_replace(array(':','.'),'',cleanID($title));
208 $new = ltrim($title,'0123456789_-');
210 $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
215 if(is_array($check)){
216 // make sure tiles are unique
217 if (!array_key_exists ($title,$check)) {
220 $title .= ++ $check[$title];
229 * Wiki page existence check
231 * parameters as for wikiFN
233 * @author Chris Smith <chris@jalakai.co.uk>
235 function page_exists($id,$rev='',$clean=true) {
236 return @file_exists(wikiFN($id,$rev,$clean));
240 * returns the full path to the datafile specified by ID and optional revision
242 * The filename is URL encoded to protect Unicode chars
244 * @param $raw_id string id of wikipage
245 * @param $rev string page revision, empty string for current
246 * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false
247 * when $id is guaranteed to have been cleaned already.
249 * @author Andreas Gohr <andi@splitbrain.org>
251 function wikiFN($raw_id,$rev='',$clean=true){
254 global $cache_wikifn;
255 $cache = & $cache_wikifn;
257 if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
258 return $cache[$raw_id][$rev];
263 if ($clean) $id = cleanID($id);
264 $id = str_replace(':','/',$id);
266 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
268 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
269 if($conf['compression']){
270 //test for extensions here, we want to read both compressions
271 if (@file_exists($fn . '.gz')){
273 }else if(@file_exists($fn . '.bz2')){
276 //file doesnt exist yet, so we take the configured extension
277 $fn .= '.' . $conf['compression'];
282 if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
283 $cache[$raw_id][$rev] = $fn;
288 * Returns the full path to the file for locking the page while editing.
290 * @author Ben Coburn <btcoburn@silicodon.net>
292 function wikiLockFN($id) {
294 return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
299 * returns the full path to the meta file specified by ID and extension
301 * @author Steven Danz <steven-danz@kc.rr.com>
303 function metaFN($id,$ext){
306 $id = str_replace(':','/',$id);
307 $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
312 * returns the full path to the media's meta file specified by ID and extension
314 * @author Kate Arzamastseva <pshns@ukr.net>
316 function mediaMetaFN($id,$ext){
319 $id = str_replace(':','/',$id);
320 $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext;
325 * returns an array of full paths to all metafiles of a given ID
327 * @author Esther Brunner <esther@kaffeehaus.ch>
328 * @author Michael Hamann <michael@content-space.de>
330 function metaFiles($id){
331 $basename = metaFN($id, '');
332 $files = glob($basename.'.*', GLOB_MARK);
333 // filter files like foo.bar.meta when $id == 'foo'
334 return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array();
338 * returns the full path to the mediafile specified by ID
340 * The filename is URL encoded to protect Unicode chars
342 * @author Andreas Gohr <andi@splitbrain.org>
343 * @author Kate Arzamastseva <pshns@ukr.net>
345 function mediaFN($id, $rev=''){
348 $id = str_replace(':','/',$id);
350 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
352 $ext = mimetype($id);
353 $name = substr($id,0, -1*strlen($ext[0])-1);
354 $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]);
360 * Returns the full filepath to a localized file if local
361 * version isn't found the english one is returned
363 * @param string $id The id of the local file
364 * @param string $ext The file extension (usually txt)
365 * @author Andreas Gohr <andi@splitbrain.org>
367 function localeFN($id,$ext='txt'){
369 $file = DOKU_CONF.'/lang/'.$conf['lang'].'/'.$id.'.'.$ext;
370 if(!@file_exists($file)){
371 $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext;
372 if(!@file_exists($file)){
373 //fall back to english
374 $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext;
381 * Resolve relative paths in IDs
383 * Do not call directly use resolve_mediaid or resolve_pageid
386 * Partyly based on a cleanPath function found at
387 * http://www.php.net/manual/en/function.realpath.php#57016
389 * @author <bart at mediawave dot nl>
391 function resolve_id($ns,$id,$clean=true){
394 // some pre cleaning for useslash:
395 if($conf['useslash']) $id = str_replace('/',':',$id);
397 // if the id starts with a dot we need to handle the
400 // normalize initial dots without a colon
401 $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
402 // prepend the current namespace
407 $pathA = explode(':', $id);
408 if (!$pathA[0]) $result[] = '';
409 foreach ($pathA AS $key => $dir) {
411 if (end($result) == '..') {
413 } elseif (!array_pop($result)) {
416 } elseif ($dir && $dir != '.') {
420 if (!end($pathA)) $result[] = '';
421 $id = implode(':', $result);
422 }elseif($ns !== false && strpos($id,':') === false){
423 //if link contains no namespace. add current namespace (if any)
427 if($clean) $id = cleanID($id);
432 * Returns a full media id
434 * @author Andreas Gohr <andi@splitbrain.org>
436 function resolve_mediaid($ns,&$page,&$exists){
437 $page = resolve_id($ns,$page);
438 $file = mediaFN($page);
439 $exists = @file_exists($file);
443 * Returns a full page id
445 * @author Andreas Gohr <andi@splitbrain.org>
447 function resolve_pageid($ns,&$page,&$exists){
452 //empty address should point to current page
457 //keep hashlink if exists then clean both parts
458 if (strpos($page,'#')) {
459 list($page,$hash) = explode('#',$page,2);
463 $hash = cleanID($hash);
464 $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
466 // get filename (calls clean itself)
467 $file = wikiFN($page);
469 // if ends with colon or slash we have a namespace link
470 if(in_array(substr($page,-1), array(':', ';')) ||
471 ($conf['useslash'] && substr($page,-1) == '/')){
472 if(page_exists($page.$conf['start'])){
473 // start page inside namespace
474 $page = $page.$conf['start'];
476 }elseif(page_exists($page.noNS(cleanID($page)))){
477 // page named like the NS inside the NS
478 $page = $page.noNS(cleanID($page));
480 }elseif(page_exists($page)){
481 // page like namespace exists
485 // fall back to default
486 $page = $page.$conf['start'];
489 //check alternative plural/nonplural form
490 if(!@file_exists($file)){
491 if( $conf['autoplural'] ){
492 if(substr($page,-1) == 's'){
493 $try = substr($page,0,-1);
497 if(page_exists($try)){
507 // now make sure we have a clean page
508 $page = cleanID($page);
511 if(!empty($hash)) $page .= '#'.$hash;
515 * Returns the name of a cachefile from given data
517 * The needed directory is created by this function!
519 * @author Andreas Gohr <andi@splitbrain.org>
521 * @param string $data This data is used to create a unique md5 name
522 * @param string $ext This is appended to the filename if given
523 * @return string The filename of the cachefile
525 function getCacheName($data,$ext=''){
528 $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
529 io_makeFileDir($file);
534 * Checks a pageid against $conf['hidepages']
536 * @author Andreas Gohr <gohr@cosmocode.de>
538 function isHiddenPage($id){
541 if(empty($conf['hidepages'])) return false;
542 if($ACT == 'admin') return false;
544 if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
551 * Reverse of isHiddenPage
553 * @author Andreas Gohr <gohr@cosmocode.de>
555 function isVisiblePage($id){
556 return !isHiddenPage($id);
560 * Format an id for output to a user
562 * Namespaces are denoted by a trailing “:*”. The root namespace is
563 * “*”. Output is escaped.
565 * @author Adrian Lang <lang@cosmocode.de>
568 function prettyprint_id($id) {
569 if (!$id || $id === ':') {
572 if ((substr($id, -1, 1) === ':')) {
579 * Encode a UTF-8 filename to use on any filesystem
581 * Uses the 'fnencode' option to determine encoding
583 * When the second parameter is true the string will
584 * be encoded only if non ASCII characters are detected -
585 * This makes it safe to run it multiple times on the
586 * same string (default is true)
588 * @author Andreas Gohr <andi@splitbrain.org>
591 function utf8_encodeFN($file,$safe=true){
593 if($conf['fnencode'] == 'utf-8') return $file;
595 if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
599 if($conf['fnencode'] == 'safe'){
600 return SafeFN::encode($file);
603 $file = urlencode($file);
604 $file = str_replace('%2F','/',$file);
609 * Decode a filename back to UTF-8
611 * Uses the 'fnencode' option to determine encoding
613 * @author Andreas Gohr <andi@splitbrain.org>
616 function utf8_decodeFN($file){
618 if($conf['fnencode'] == 'utf-8') return $file;
620 if($conf['fnencode'] == 'safe'){
621 return SafeFN::decode($file);
624 return urldecode($file);
628 * Find a page in the current namespace (determined from $ID) or any
631 * Used for sidebars, but can be used other stuff as well
633 * @todo add event hook
634 * @param string $page the pagename you're looking for
635 * @return string|false the full page id of the found page, false if any
637 function page_findnearest($page){
643 $pageid = ltrim("$ns:$page",':');
644 if(page_exists($pageid)){