summaryrefslogtreecommitdiff
path: root/inc/pageutils.php
diff options
context:
space:
mode:
authorandi <andi@splitbrain.org>2005-04-15 22:47:35 +0200
committerandi <andi@splitbrain.org>2005-04-15 22:47:35 +0200
commitb625487d2258a6f1f875813206adc9a5857dab24 (patch)
treed58871efbfeb198ad4634add0e496b5cb890b655 /inc/pageutils.php
parent4826ab45befb9eb1c664b5d8c8a0f03a7b750b8b (diff)
downloadrpg-b625487d2258a6f1f875813206adc9a5857dab24.tar.gz
rpg-b625487d2258a6f1f875813206adc9a5857dab24.tar.bz2
new parser: more hacking, RSS readded
darcs-hash:20050415204735-9977f-613d9b007452d538dcb8fce4ade5cbec389c4415.gz
Diffstat (limited to 'inc/pageutils.php')
-rw-r--r--inc/pageutils.php200
1 files changed, 200 insertions, 0 deletions
diff --git a/inc/pageutils.php b/inc/pageutils.php
new file mode 100644
index 000000000..367e53625
--- /dev/null
+++ b/inc/pageutils.php
@@ -0,0 +1,200 @@
+<?php
+/**
+ * Utilities for handling pagenames
+ *
+ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+
+
+/**
+ * Remove unwanted chars from ID
+ *
+ * Cleans a given ID to only use allowed characters. Accented characters are
+ * converted to unaccented ones
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function cleanID($id){
+ global $conf;
+ global $lang;
+ $id = trim($id);
+ $id = utf8_strtolower($id);
+
+ //alternative namespace seperator
+ $id = strtr($id,';',':');
+ if($conf['useslash']){
+ $id = strtr($id,'/',':');
+ }else{
+ $id = strtr($id,'/','_');
+ }
+
+ if($conf['deaccent']) $id = utf8_deaccent($id,-1);
+
+ //remove specials
+ $id = utf8_stripspecials($id,'_','_:.-');
+
+ //clean up
+ $id = preg_replace('#__#','_',$id);
+ $id = preg_replace('#:+#',':',$id);
+ $id = trim($id,':._-');
+ $id = preg_replace('#:[:\._\-]+#',':',$id);
+
+ return($id);
+}
+
+/**
+ * Return namespacepart of a wiki ID
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function getNS($id){
+ if(strpos($id,':')!==false){
+ return substr($id,0,strrpos($id,':'));
+ }
+ return false;
+}
+
+/**
+ * Returns the ID without the namespace
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function noNS($id){
+ return preg_replace('/.*:/','',$id);
+}
+
+/**
+ * returns the full path to the datafile specified by ID and
+ * optional revision
+ *
+ * The filename is URL encoded to protect Unicode chars
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function wikiFN($id,$rev=''){
+ global $conf;
+ $id = cleanID($id);
+ $id = str_replace(':','/',$id);
+ if(empty($rev)){
+ $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
+ }else{
+ $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
+ if($conf['usegzip'] && !@file_exists($fn)){
+ //return gzip if enabled and plaintext doesn't exist
+ $fn .= '.gz';
+ }
+ }
+ return $fn;
+}
+
+/**
+ * returns the full path to the mediafile specified by ID
+ *
+ * The filename is URL encoded to protect Unicode chars
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function mediaFN($id){
+ global $conf;
+ $id = cleanID($id);
+ $id = str_replace(':','/',$id);
+ $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
+ return $fn;
+}
+
+/**
+ * Returns the full filepath to a localized textfile if local
+ * version isn't found the english one is returned
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function localeFN($id){
+ global $conf;
+ $file = './lang/'.$conf['lang'].'/'.$id.'.txt';
+ if(!@file_exists($file)){
+ //fall back to english
+ $file = './lang/en/'.$id.'.txt';
+ }
+ return $file;
+}
+
+/**
+ * Returns a full media id
+ *
+ * uses global $ID to resolve relative pages
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function resolve_mediaid(&$page,&$exists){
+ global $ID;
+ global $conf;
+ $ns = getNS($ID);
+ //if links starts with . add current namespace
+ if($page{0} == '.'){
+ $page = $ns.':'.substr($page,1);
+ }
+
+ //if link contains no namespace. add current namespace (if any)
+ if($ns !== false && strpos($page,':') === false){
+ $page = $ns.':'.$page;
+ }
+
+ $page = cleanID($page);
+ $file = mediaFN($page);
+ $exists = @file_exists($file);
+}
+
+/**
+ * Returns a full page id
+ *
+ * uses global $ID to resolve relative pages
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function resolve_pageid(&$page,&$exists){
+ global $ID;
+ global $conf;
+ $ns = getNS($ID);
+
+ //if links starts with . add current namespace
+ if($page{0} == '.'){
+ $page = $ns.':'.substr($page,1);
+ }
+
+ //if link contains no namespace. add current namespace (if any)
+ if($ns !== false && strpos($page,':') === false){
+ $page = $ns.':'.$page;
+ }
+
+ //keep hashlink if exists then clean both parts
+ list($page,$hash) = split('#',$page,2);
+ $page = cleanID($page);
+ $hash = cleanID($hash);
+
+ $file = wikiFN($page);
+
+ $exists = false;
+
+ //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;
+ }
+
+ //add hash if any
+ if(!empty($hash)) $page.'#'.$hash;
+}
+
+//Setup VIM: ex: et ts=2 enc=utf-8 :