1<?php 2/** 3 * Utilities for handling pagenames 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9 10/** 11 * Remove unwanted chars from ID 12 * 13 * Cleans a given ID to only use allowed characters. Accented characters are 14 * converted to unaccented ones 15 * 16 * @author Andreas Gohr <andi@splitbrain.org> 17 */ 18function cleanID($id){ 19 global $conf; 20 global $lang; 21 $id = trim($id); 22 $id = utf8_strtolower($id); 23 24 //alternative namespace seperator 25 $id = strtr($id,';',':'); 26 if($conf['useslash']){ 27 $id = strtr($id,'/',':'); 28 }else{ 29 $id = strtr($id,'/','_'); 30 } 31 32 if($conf['deaccent']) $id = utf8_deaccent($id,-1); 33 34 //remove specials 35 $id = utf8_stripspecials($id,'_','_:.-'); 36 37 //clean up 38 $id = preg_replace('#__#','_',$id); 39 $id = preg_replace('#:+#',':',$id); 40 $id = trim($id,':._-'); 41 $id = preg_replace('#:[:\._\-]+#',':',$id); 42 43 return($id); 44} 45 46/** 47 * Return namespacepart of a wiki ID 48 * 49 * @author Andreas Gohr <andi@splitbrain.org> 50 */ 51function getNS($id){ 52 if(strpos($id,':')!==false){ 53 return substr($id,0,strrpos($id,':')); 54 } 55 return false; 56} 57 58/** 59 * Returns the ID without the namespace 60 * 61 * @author Andreas Gohr <andi@splitbrain.org> 62 */ 63function noNS($id){ 64 return preg_replace('/.*:/','',$id); 65} 66 67/** 68 * returns the full path to the datafile specified by ID and 69 * optional revision 70 * 71 * The filename is URL encoded to protect Unicode chars 72 * 73 * @author Andreas Gohr <andi@splitbrain.org> 74 */ 75function wikiFN($id,$rev=''){ 76 global $conf; 77 $id = cleanID($id); 78 $id = str_replace(':','/',$id); 79 if(empty($rev)){ 80 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 81 }else{ 82 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 83 if($conf['usegzip'] && !@file_exists($fn)){ 84 //return gzip if enabled and plaintext doesn't exist 85 $fn .= '.gz'; 86 } 87 } 88 return $fn; 89} 90 91/** 92 * returns the full path to the mediafile specified by ID 93 * 94 * The filename is URL encoded to protect Unicode chars 95 * 96 * @author Andreas Gohr <andi@splitbrain.org> 97 */ 98function mediaFN($id){ 99 global $conf; 100 $id = cleanID($id); 101 $id = str_replace(':','/',$id); 102 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 103 return $fn; 104} 105 106/** 107 * Returns the full filepath to a localized textfile if local 108 * version isn't found the english one is returned 109 * 110 * @author Andreas Gohr <andi@splitbrain.org> 111 */ 112function localeFN($id){ 113 global $conf; 114 $file = './lang/'.$conf['lang'].'/'.$id.'.txt'; 115 if(!@file_exists($file)){ 116 //fall back to english 117 $file = './lang/en/'.$id.'.txt'; 118 } 119 return $file; 120} 121 122/** 123 * Returns a full media id 124 * 125 * uses global $ID to resolve relative pages 126 * 127 * @author Andreas Gohr <andi@splitbrain.org> 128 */ 129function resolve_mediaid(&$page,&$exists){ 130 global $ID; 131 global $conf; 132 $ns = getNS($ID); 133 //if links starts with . add current namespace 134 if($page{0} == '.'){ 135 $page = $ns.':'.substr($page,1); 136 } 137 138 //if link contains no namespace. add current namespace (if any) 139 if($ns !== false && strpos($page,':') === false){ 140 $page = $ns.':'.$page; 141 } 142 143 $page = cleanID($page); 144 $file = mediaFN($page); 145 $exists = @file_exists($file); 146} 147 148/** 149 * Returns a full page id 150 * 151 * uses global $ID to resolve relative pages 152 * 153 * @author Andreas Gohr <andi@splitbrain.org> 154 */ 155function resolve_pageid(&$page,&$exists){ 156 global $ID; 157 global $conf; 158 $ns = getNS($ID); 159 160 //if links starts with . add current namespace 161 if($page{0} == '.'){ 162 $page = $ns.':'.substr($page,1); 163 } 164 165 //if link contains no namespace. add current namespace (if any) 166 if($ns !== false && strpos($page,':') === false){ 167 $page = $ns.':'.$page; 168 } 169 170 //keep hashlink if exists then clean both parts 171 list($page,$hash) = split('#',$page,2); 172 $page = cleanID($page); 173 $hash = cleanID($hash); 174 175 $file = wikiFN($page); 176 177 $exists = false; 178 179 //check alternative plural/nonplural form 180 if(!@file_exists($file)){ 181 if( $conf['autoplural'] ){ 182 if(substr($page,-1) == 's'){ 183 $try = substr($page,0,-1); 184 }else{ 185 $try = $page.'s'; 186 } 187 if(@file_exists(wikiFN($try))){ 188 $page = $try; 189 $exists = true; 190 } 191 } 192 }else{ 193 $exists = true; 194 } 195 196 //add hash if any 197 if(!empty($hash)) $page.'#'.$hash; 198} 199 200//Setup VIM: ex: et ts=2 enc=utf-8 : 201