1c112d578Sandi<?php 2c112d578Sandi/** 3c112d578Sandi * Utilities for collecting data from config files 4c112d578Sandi * 5c112d578Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 7c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 8c112d578Sandi */ 9c112d578Sandi 10c112d578Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 11c112d578Sandi 12c112d578Sandi require_once(DOKU_INC.'inc/confutils.php'); 13c112d578Sandi require_once(DOKU_INC.'inc/pageutils.php'); 14ee20e7d1Sandi require_once(DOKU_INC.'inc/pluginutils.php'); 15c112d578Sandi 16c112d578Sandi/** 17c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision. 18c112d578Sandi * 19c112d578Sandi * If $excuse is true an explanation is returned if the file 20c112d578Sandi * wasn't found 21c112d578Sandi * 22c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 23c112d578Sandi */ 24c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){ 25c112d578Sandi $file = wikiFN($id,$rev); 26c112d578Sandi $ret = ''; 27c112d578Sandi 28c112d578Sandi //ensure $id is in global $ID (needed for parsing) 291e76272cSandi global $ID; 301e76272cSandi $ID = $id; 31c112d578Sandi 32c112d578Sandi if($rev){ 33c112d578Sandi if(@file_exists($file)){ 349dc2c2afSandi $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions 35c112d578Sandi }elseif($excuse){ 36c112d578Sandi $ret = p_locale_xhtml('norev'); 37c112d578Sandi } 38c112d578Sandi }else{ 39c112d578Sandi if(@file_exists($file)){ 40c112d578Sandi $ret = p_cached_xhtml($file); 41c112d578Sandi }elseif($excuse){ 42c112d578Sandi $ret = p_locale_xhtml('newpage'); 43c112d578Sandi } 44c112d578Sandi } 45c112d578Sandi 46c112d578Sandi return $ret; 47c112d578Sandi} 48c112d578Sandi 49c112d578Sandi/** 50c112d578Sandi * Returns the specified local text in parsed format 51c112d578Sandi * 52c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 53c112d578Sandi */ 54c112d578Sandifunction p_locale_xhtml($id){ 55c112d578Sandi //fetch parsed locale 56c112d578Sandi $html = p_cached_xhtml(localeFN($id)); 57c112d578Sandi return $html; 58c112d578Sandi} 59c112d578Sandi 60c112d578Sandi/** 61c112d578Sandi * Returns the given file parsed to XHTML 62c112d578Sandi * 63c112d578Sandi * Uses and creates a cachefile 64c112d578Sandi * 65c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 669dc2c2afSandi * @todo rewrite to use mode instead of hardcoded XHTML 67c112d578Sandi */ 68c112d578Sandifunction p_cached_xhtml($file){ 69c112d578Sandi global $conf; 70c112d578Sandi $cache = $conf['datadir'].'/_cache/xhtml/'; 71c112d578Sandi $cache .= md5($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT']); 721094c798Sandi $purge = $conf['datadir'].'/_cache/purgefile'; 73c112d578Sandi 74c112d578Sandi // check if cache can be used 75c112d578Sandi $cachetime = @filemtime($cache); // 0 if not exists 76c112d578Sandi 77c112d578Sandi if( @file_exists($file) // does the source exist 78c112d578Sandi && $cachetime > @filemtime($file) // cache is fresh 79c112d578Sandi && ((time() - $cachetime) < $conf['cachetime']) // and is cachefile young enough 80c112d578Sandi && !isset($_REQUEST['purge']) // no purge param was set 811094c798Sandi && ($cachetime > @filemtime($purge)) // and newer than the purgefile 82c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'conf/dokuwiki.php')) // newer than the config file 83c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'conf/local.php')) // newer than the local config file 84c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php')) // newer than the renderer 85c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php')) // newer than the parser 86c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler 87c112d578Sandi { 88c112d578Sandi //well then use the cache 89c112d578Sandi $parsed = io_readfile($cache); 90c112d578Sandi $parsed .= "\n<!-- cachefile $cache used -->\n"; 91c112d578Sandi }else{ 929dc2c2afSandi $parsed = p_render('xhtml', p_cached_instructions($file),$info); //try to use cached instructions 93c112d578Sandi 949dc2c2afSandi if($info['cache']){ 95c112d578Sandi io_saveFile($cache,$parsed); //save cachefile 96c112d578Sandi $parsed .= "\n<!-- no cachefile used, but created -->\n"; 97c112d578Sandi }else{ 98c112d578Sandi @unlink($cache); //try to delete cachefile 99c112d578Sandi $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 100c112d578Sandi } 101c112d578Sandi } 102c112d578Sandi 103c112d578Sandi return $parsed; 104c112d578Sandi} 105c112d578Sandi 106c112d578Sandi/** 107c112d578Sandi * Returns the render instructions for a file 108c112d578Sandi * 109c112d578Sandi * Uses and creates a serialized cache file 110c112d578Sandi * 111c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 112c112d578Sandi */ 11337e34a5eSandifunction p_cached_instructions($file,$cacheonly=false){ 114c112d578Sandi global $conf; 115c112d578Sandi $cache = $conf['datadir'].'/_cache/instructions/'; 116c112d578Sandi $cache .= md5($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT']); 117c112d578Sandi 118c112d578Sandi // check if cache can be used 119c112d578Sandi $cachetime = @filemtime($cache); // 0 if not exists 120c112d578Sandi 12137e34a5eSandi // cache forced? 12237e34a5eSandi if($cacheonly){ 12337e34a5eSandi if($cachetime){ 12437e34a5eSandi return unserialize(io_readfile($cache)); 12537e34a5eSandi }else{ 126fd198316Sandi return array(); 12737e34a5eSandi } 12837e34a5eSandi } 12937e34a5eSandi 130c112d578Sandi if( @file_exists($file) // does the source exist 131c112d578Sandi && $cachetime > @filemtime($file) // cache is fresh 132c112d578Sandi && !isset($_REQUEST['purge']) // no purge param was set 133c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'conf/dokuwiki.php')) // newer than the config file 134c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'conf/local.php')) // newer than the local config file 135c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php')) // newer than the parser 136c112d578Sandi && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler 137c112d578Sandi { 138c112d578Sandi //well then use the cache 139c112d578Sandi return unserialize(io_readfile($cache)); 140c112d578Sandi }elseif(@file_exists($file)){ 141c112d578Sandi // no cache - do some work 1426bbae538Sandi $ins = p_get_instructions(io_readfile($file)); 143c112d578Sandi io_savefile($cache,serialize($ins)); 144c112d578Sandi return $ins; 145c112d578Sandi } 146c112d578Sandi 147c112d578Sandi return NULL; 148c112d578Sandi} 149c112d578Sandi 150c112d578Sandi/** 151c112d578Sandi * turns a page into a list of instructions 152c112d578Sandi * 153c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 154c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 155c112d578Sandi */ 1566bbae538Sandifunction p_get_instructions($text){ 157c112d578Sandi 158*107b01d6Sandi $modes = p_get_parsermodes(); 159ee20e7d1Sandi 160c112d578Sandi // Create the parser 161c112d578Sandi $Parser = & new Doku_Parser(); 162c112d578Sandi 163c112d578Sandi // Add the Handler 164c112d578Sandi $Parser->Handler = & new Doku_Handler(); 165c112d578Sandi 166*107b01d6Sandi //add modes to parser 167*107b01d6Sandi foreach($modes as $mode){ 168*107b01d6Sandi $Parser->addMode($mode['mode'],$mode['obj']); 169c112d578Sandi } 170c112d578Sandi 171c112d578Sandi // Do the parsing 172a2d649c4Sandi $p = $Parser->parse($text); 173ee20e7d1Sandi// dbg($p); 174a2d649c4Sandi return $p; 175c112d578Sandi} 176c112d578Sandi 177c112d578Sandi/** 178*107b01d6Sandi * returns all available parser syntax modes in correct order 179*107b01d6Sandi * 180*107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 181*107b01d6Sandi */ 182*107b01d6Sandifunction p_get_parsermodes(){ 183*107b01d6Sandi global $conf; 184*107b01d6Sandi 185*107b01d6Sandi //reuse old data 186*107b01d6Sandi static $modes = null; 187*107b01d6Sandi if($modes != null){ 188*107b01d6Sandi return $modes; 189*107b01d6Sandi } 190*107b01d6Sandi 191*107b01d6Sandi //import parser classes and mode definitions 192*107b01d6Sandi require_once DOKU_INC . 'inc/parser/parser.php'; 193*107b01d6Sandi 194*107b01d6Sandi // we now collect all syntax modes and their objects, then they will 195*107b01d6Sandi // be sorted and added to the parser in correct order 196*107b01d6Sandi $modes = array(); 197*107b01d6Sandi 198*107b01d6Sandi // add syntax plugins 199*107b01d6Sandi $pluginlist = plugin_list('syntax'); 200*107b01d6Sandi if(count($pluginlist)){ 201*107b01d6Sandi global $PARSER_MODES; 202*107b01d6Sandi $obj = null; 203*107b01d6Sandi foreach($pluginlist as $p){ 204*107b01d6Sandi plugin_load('syntax',$p,$obj); //load plugin into $obj 205*107b01d6Sandi $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 206*107b01d6Sandi //add to modes 207*107b01d6Sandi $modes[] = array( 208*107b01d6Sandi 'sort' => $obj->getSort(), 209*107b01d6Sandi 'mode' => "plugin_$p", 210*107b01d6Sandi 'obj' => $obj, 211*107b01d6Sandi ); 212*107b01d6Sandi } 213*107b01d6Sandi } 214*107b01d6Sandi 215*107b01d6Sandi // add default modes 216*107b01d6Sandi $std_modes = array('listblock','preformatted','notoc','nocache', 217*107b01d6Sandi 'header','table','linebreak','footnote','hr', 218*107b01d6Sandi 'unformatted','php','html','code','file','quote', 219*107b01d6Sandi 'multiplyentity','quotes','internallink','rss', 220*107b01d6Sandi 'media','externallink','emaillink','windowssharelink', 221*107b01d6Sandi 'eol'); 222*107b01d6Sandi foreach($std_modes as $m){ 223*107b01d6Sandi $class = "Doku_Parser_Mode_$m"; 224*107b01d6Sandi $obj = new $class(); 225*107b01d6Sandi $modes[] = array( 226*107b01d6Sandi 'sort' => $obj->getSort(), 227*107b01d6Sandi 'mode' => $m, 228*107b01d6Sandi 'obj' => $obj 229*107b01d6Sandi ); 230*107b01d6Sandi } 231*107b01d6Sandi 232*107b01d6Sandi // add formatting modes 233*107b01d6Sandi $fmt_modes = array('strong','emphasis','underline','monospace', 234*107b01d6Sandi 'subscript','superscript','deleted'); 235*107b01d6Sandi foreach($fmt_modes as $m){ 236*107b01d6Sandi $obj = new Doku_Parser_Mode_formatting($m); 237*107b01d6Sandi $modes[] = array( 238*107b01d6Sandi 'sort' => $obj->getSort(), 239*107b01d6Sandi 'mode' => $m, 240*107b01d6Sandi 'obj' => $obj 241*107b01d6Sandi ); 242*107b01d6Sandi } 243*107b01d6Sandi 244*107b01d6Sandi // add modes which need files 245*107b01d6Sandi $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys())); 246*107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 247*107b01d6Sandi $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms())); 248*107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 249*107b01d6Sandi $obj = new Doku_Parser_Mode_entity(array_keys(getEntities())); 250*107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 251*107b01d6Sandi 252*107b01d6Sandi 253*107b01d6Sandi // add optional camelcase mode 254*107b01d6Sandi if($conf['camelcase']){ 255*107b01d6Sandi $obj = new Doku_Parser_Mode_camelcaselink(); 256*107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 257*107b01d6Sandi } 258*107b01d6Sandi 259*107b01d6Sandi //sort modes 260*107b01d6Sandi usort($modes,'p_sort_modes'); 261*107b01d6Sandi 262*107b01d6Sandi return $modes; 263*107b01d6Sandi} 264*107b01d6Sandi 265*107b01d6Sandi/** 266*107b01d6Sandi * Callback function for usort 267*107b01d6Sandi * 268*107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 269*107b01d6Sandi */ 270*107b01d6Sandifunction p_sort_modes($a, $b){ 271*107b01d6Sandi if($a['sort'] == $b['sort']) return 0; 272*107b01d6Sandi return ($a['sort'] < $b['sort']) ? -1 : 1; 273*107b01d6Sandi} 274*107b01d6Sandi 275*107b01d6Sandi/** 276ac83b9d8Sandi * Renders a list of instruction to the specified output mode 277c112d578Sandi * 2789dc2c2afSandi * In the $info array are informations from the renderer returned 2799dc2c2afSandi * 280c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 281c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 282c112d578Sandi */ 2839dc2c2afSandifunction p_render($mode,$instructions,& $info){ 284c112d578Sandi if(is_null($instructions)) return ''; 285c112d578Sandi 286c112d578Sandi // Create the renderer 287ac83b9d8Sandi if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){ 288ac83b9d8Sandi msg("No renderer for $mode found",-1); 289ac83b9d8Sandi return null; 290ac83b9d8Sandi } 291ac83b9d8Sandi 292ac83b9d8Sandi require_once DOKU_INC."inc/parser/$mode.php"; 293ac83b9d8Sandi $rclass = "Doku_Renderer_$mode"; 294ac83b9d8Sandi $Renderer = & new $rclass(); #FIXME any way to check for class existance? 295c112d578Sandi 296c112d578Sandi $Renderer->smileys = getSmileys(); 297c112d578Sandi $Renderer->entities = getEntities(); 298c112d578Sandi $Renderer->acronyms = getAcronyms(); 299c112d578Sandi $Renderer->interwiki = getInterwiki(); 300c112d578Sandi #$Renderer->badwords = getBadWords(); 301c112d578Sandi 302c112d578Sandi // Loop through the instructions 303c112d578Sandi foreach ( $instructions as $instruction ) { 304c112d578Sandi // Execute the callback against the Renderer 305c112d578Sandi call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]); 306c112d578Sandi } 3079dc2c2afSandi 3089dc2c2afSandi //set info array 3099dc2c2afSandi $info = $Renderer->info; 3109dc2c2afSandi 311c112d578Sandi // Return the output 312c112d578Sandi return $Renderer->doc; 313c112d578Sandi} 314c112d578Sandi 315bb0a59d4Sjan/** 316bb0a59d4Sjan * Gets the first heading from a file 317bb0a59d4Sjan * 318bb0a59d4Sjan * @author Jan Decaluwe <jan@jandecaluwe.com> 319bb0a59d4Sjan */ 320bb0a59d4Sjanfunction p_get_first_heading($id){ 321bb0a59d4Sjan $file = wikiFN($id); 322bb0a59d4Sjan if (@file_exists($file)) { 3236e38d921Sandi $instructions = p_cached_instructions($file,true); 324bb0a59d4Sjan foreach ( $instructions as $instruction ) { 325bb0a59d4Sjan if ($instruction[0] == 'header') { 326bb0a59d4Sjan return $instruction[1][0]; 327bb0a59d4Sjan } 328bb0a59d4Sjan } 329bb0a59d4Sjan } 330bb0a59d4Sjan return NULL; 331bb0a59d4Sjan} 332bb0a59d4Sjan 333c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 334