1c112d578Sandi<?php 2c112d578Sandi/** 3b8595a66SAndreas Gohr * Utilities for accessing the parser 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 1000976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(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'); 154b5f4f4eSchris require_once(DOKU_INC.'inc/cache.php'); 16c112d578Sandi 17c112d578Sandi/** 18c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision. 19c112d578Sandi * 20c112d578Sandi * If $excuse is true an explanation is returned if the file 21c112d578Sandi * wasn't found 22c112d578Sandi * 23c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 24c112d578Sandi */ 25c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){ 26c112d578Sandi $file = wikiFN($id,$rev); 27c112d578Sandi $ret = ''; 28c112d578Sandi 29c112d578Sandi //ensure $id is in global $ID (needed for parsing) 301e76272cSandi global $ID; 313ff8773bSAndreas Gohr $keep = $ID; 321e76272cSandi $ID = $id; 33c112d578Sandi 34c112d578Sandi if($rev){ 35c112d578Sandi if(@file_exists($file)){ 369dc2c2afSandi $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions 37c112d578Sandi }elseif($excuse){ 38c112d578Sandi $ret = p_locale_xhtml('norev'); 39c112d578Sandi } 40c112d578Sandi }else{ 41c112d578Sandi if(@file_exists($file)){ 424b5f4f4eSchris $ret = p_cached_output($file,'xhtml',$id); 43c112d578Sandi }elseif($excuse){ 44c112d578Sandi $ret = p_locale_xhtml('newpage'); 45c112d578Sandi } 46c112d578Sandi } 47c112d578Sandi 483ff8773bSAndreas Gohr //restore ID (just in case) 493ff8773bSAndreas Gohr $ID = $keep; 503ff8773bSAndreas Gohr 51c112d578Sandi return $ret; 52c112d578Sandi} 53c112d578Sandi 54c112d578Sandi/** 556b7b33dcShfuecks * Returns starting summary for a page (e.g. the first few 566b7b33dcShfuecks * paragraphs), marked up in XHTML. 576b7b33dcShfuecks * 586b7b33dcShfuecks * If $excuse is true an explanation is returned if the file 596b7b33dcShfuecks * wasn't found 606b7b33dcShfuecks * 616b7b33dcShfuecks * @param string wiki page id 626b7b33dcShfuecks * @param reference populated with page title from heading or page id 638716966dSAndreas Gohr * @deprecated 648716966dSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com> 656b7b33dcShfuecks */ 666b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){ 676b7b33dcShfuecks $file = wikiFN($id,$rev); 686b7b33dcShfuecks $ret = ''; 696b7b33dcShfuecks 706b7b33dcShfuecks //ensure $id is in global $ID (needed for parsing) 716b7b33dcShfuecks global $ID; 726b7b33dcShfuecks $keep = $ID; 736b7b33dcShfuecks $ID = $id; 746b7b33dcShfuecks 756b7b33dcShfuecks if($rev){ 766b7b33dcShfuecks if(@file_exists($file)){ 776b7b33dcShfuecks //no caching on old revisions 786b7b33dcShfuecks $ins = p_get_instructions(io_readfile($file)); 796b7b33dcShfuecks }elseif($excuse){ 806b7b33dcShfuecks $ret = p_locale_xhtml('norev'); 816b7b33dcShfuecks //restore ID (just in case) 826b7b33dcShfuecks $ID = $keep; 836b7b33dcShfuecks return $ret; 846b7b33dcShfuecks } 856b7b33dcShfuecks 866b7b33dcShfuecks }else{ 876b7b33dcShfuecks 886b7b33dcShfuecks if(@file_exists($file)){ 896b7b33dcShfuecks // The XHTML for a summary is not cached so use the instruction cache 906b7b33dcShfuecks $ins = p_cached_instructions($file); 916b7b33dcShfuecks }elseif($excuse){ 926b7b33dcShfuecks $ret = p_locale_xhtml('newpage'); 936b7b33dcShfuecks //restore ID (just in case) 946b7b33dcShfuecks $ID = $keep; 956b7b33dcShfuecks return $ret; 966b7b33dcShfuecks } 976b7b33dcShfuecks } 986b7b33dcShfuecks 996b7b33dcShfuecks $ret = p_render('xhtmlsummary',$ins,$info); 1006b7b33dcShfuecks 1016b7b33dcShfuecks if ( $info['sum_pagetitle'] ) { 1026b7b33dcShfuecks $title = $info['sum_pagetitle']; 1036b7b33dcShfuecks } else { 1046b7b33dcShfuecks $title = $id; 1056b7b33dcShfuecks } 1066b7b33dcShfuecks 1076b7b33dcShfuecks $ID = $keep; 1086b7b33dcShfuecks return $ret; 1096b7b33dcShfuecks} 1106b7b33dcShfuecks 1116b7b33dcShfuecks/** 112c112d578Sandi * Returns the specified local text in parsed format 113c112d578Sandi * 114c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 115c112d578Sandi */ 116c112d578Sandifunction p_locale_xhtml($id){ 117c112d578Sandi //fetch parsed locale 1184b5f4f4eSchris $html = p_cached_output(localeFN($id)); 119c112d578Sandi return $html; 120c112d578Sandi} 121c112d578Sandi 122c112d578Sandi/** 1234b5f4f4eSchris * *** DEPRECATED *** 1244b5f4f4eSchris * 1254b5f4f4eSchris * use p_cached_output() 1264b5f4f4eSchris * 127c112d578Sandi * Returns the given file parsed to XHTML 128c112d578Sandi * 129c112d578Sandi * Uses and creates a cachefile 130c112d578Sandi * 1314b5f4f4eSchris * @deprecated 132c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 1339dc2c2afSandi * @todo rewrite to use mode instead of hardcoded XHTML 134c112d578Sandi */ 135c112d578Sandifunction p_cached_xhtml($file){ 1364b5f4f4eSchris return p_cached_output($file); 1374b5f4f4eSchris} 1384b5f4f4eSchris 1394b5f4f4eSchris/** 1404b5f4f4eSchris * Returns the given file parsed into the requested output format 1414b5f4f4eSchris * 1424b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org> 1434b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk> 1444b5f4f4eSchris */ 1454b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') { 146c112d578Sandi global $conf; 147c112d578Sandi 1484b5f4f4eSchris $cache = new cache_renderer($id, $file, $format); 1494b5f4f4eSchris if ($cache->useCache()) { 15085767031SAndreas Gohr $parsed = $cache->retrieveCache(false); 151ea2a4271SAndreas Gohr if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 152c112d578Sandi } else { 1534b5f4f4eSchris $parsed = p_render($format, p_cached_instructions($file,false,$id), $info); 154c112d578Sandi 1559dc2c2afSandi if ($info['cache']) { 1564b5f4f4eSchris $cache->storeCache($parsed); //save cachefile 157ea2a4271SAndreas Gohr if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 158c112d578Sandi }else{ 1594b5f4f4eSchris $cache->removeCache(); //try to delete cachefile 160ea2a4271SAndreas Gohr if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 161c112d578Sandi } 162c112d578Sandi } 163c112d578Sandi 164c112d578Sandi return $parsed; 165c112d578Sandi} 166c112d578Sandi 167c112d578Sandi/** 168c112d578Sandi * Returns the render instructions for a file 169c112d578Sandi * 170c112d578Sandi * Uses and creates a serialized cache file 171c112d578Sandi * 172c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 173c112d578Sandi */ 1744b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') { 175c112d578Sandi global $conf; 17647b2d319SAndreas Gohr static $run = null; 17747b2d319SAndreas Gohr if(is_null($run)) $run = array(); 178c112d578Sandi 1794b5f4f4eSchris $cache = new cache_instructions($id, $file); 180c112d578Sandi 18147b2d319SAndreas Gohr if ($cacheonly || $cache->useCache() || isset($run[$file])) { 1824b5f4f4eSchris return $cache->retrieveCache(); 183c112d578Sandi } else if (@file_exists($file)) { 184c112d578Sandi // no cache - do some work 1856bbae538Sandi $ins = p_get_instructions(io_readfile($file)); 186cbaf4259SChris Smith if ($cache->storeCache($ins)) { 18747b2d319SAndreas Gohr $run[$file] = true; // we won't rebuild these instructions in the same run again 188cbaf4259SChris Smith } else { 189cbaf4259SChris Smith msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1); 190cbaf4259SChris Smith } 191c112d578Sandi return $ins; 192c112d578Sandi } 193c112d578Sandi 1943b3f8916SAndreas Gohr return null; 195c112d578Sandi} 196c112d578Sandi 197c112d578Sandi/** 198c112d578Sandi * turns a page into a list of instructions 199c112d578Sandi * 200c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 201c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 202c112d578Sandi */ 2036bbae538Sandifunction p_get_instructions($text){ 204c112d578Sandi 205107b01d6Sandi $modes = p_get_parsermodes(); 206ee20e7d1Sandi 207c112d578Sandi // Create the parser 208c112d578Sandi $Parser = & new Doku_Parser(); 209c112d578Sandi 210c112d578Sandi // Add the Handler 211c112d578Sandi $Parser->Handler = & new Doku_Handler(); 212c112d578Sandi 213107b01d6Sandi //add modes to parser 214107b01d6Sandi foreach($modes as $mode){ 215107b01d6Sandi $Parser->addMode($mode['mode'],$mode['obj']); 216c112d578Sandi } 217c112d578Sandi 218c112d578Sandi // Do the parsing 219677844afSchris trigger_event('PARSER_WIKITEXT_PREPROCESS', $text); 220a2d649c4Sandi $p = $Parser->parse($text); 221ee20e7d1Sandi// dbg($p); 222a2d649c4Sandi return $p; 223c112d578Sandi} 224c112d578Sandi 225c112d578Sandi/** 22639a89382SEsther Brunner * returns the metadata of a page 22739a89382SEsther Brunner * 22839a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 22939a89382SEsther Brunner */ 23039a89382SEsther Brunnerfunction p_get_metadata($id, $key=false, $render=false){ 2310a7e3bceSchris global $ID, $INFO, $cache_metadata; 2326afe8dcaSchris 2330a7e3bceSchris // cache the current page 2340a7e3bceSchris // Benchmarking shows the current page's metadata is generally the only page metadata 2350a7e3bceSchris // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 2360a7e3bceSchris $cache = ($ID == $id); 2370a7e3bceSchris $meta = p_read_metadata($id, $cache); 23839a89382SEsther Brunner 23966d29756SChris Smith // metadata has never been rendered before - do it! (but not for non-existent pages) 24066d29756SChris Smith if ($render && !$meta['current']['description']['abstract'] && page_exists($id)){ 24139a89382SEsther Brunner $meta = p_render_metadata($id, $meta); 242fc18c0fbSchris io_saveFile(metaFN($id, '.meta'), serialize($meta)); 2430a7e3bceSchris 2440a7e3bceSchris // sync cached copies, including $INFO metadata 2450a7e3bceSchris if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta; 2460a7e3bceSchris if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 2476afe8dcaSchris } 24839a89382SEsther Brunner 24939a89382SEsther Brunner // filter by $key 25039a89382SEsther Brunner if ($key){ 25139a89382SEsther Brunner list($key, $subkey) = explode(' ', $key, 2); 25266d29756SChris Smith $subkey = trim($subkey); 2530a7e3bceSchris 25466d29756SChris Smith if ($subkey) { 25566d29756SChris Smith return isset($meta['current'][$key][$subkey]) ? $meta['current'][$key][$subkey] : null; 25666d29756SChris Smith } 25766d29756SChris Smith 25866d29756SChris Smith return isset($meta['current'][$key]) ? $meta['current'][$key] : null; 25939a89382SEsther Brunner } 26039a89382SEsther Brunner 2610a7e3bceSchris return $meta['current']; 26239a89382SEsther Brunner} 26339a89382SEsther Brunner 26439a89382SEsther Brunner/** 26539a89382SEsther Brunner * sets metadata elements of a page 26639a89382SEsther Brunner * 26739a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 26839a89382SEsther Brunner */ 2690a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){ 27039a89382SEsther Brunner if (!is_array($data)) return false; 27139a89382SEsther Brunner 2720a7e3bceSchris global $ID; 2730a7e3bceSchris 2740a7e3bceSchris // cache the current page 2750a7e3bceSchris $cache = ($ID == $id); 2760a7e3bceSchris $orig = p_read_metadata($id, $cache); 27739a89382SEsther Brunner 27839a89382SEsther Brunner // render metadata first? 2790a7e3bceSchris $meta = $render ? p_render_metadata($id, $orig) : $orig; 28039a89382SEsther Brunner 28139a89382SEsther Brunner // now add the passed metadata 28239a89382SEsther Brunner $protected = array('description', 'date', 'contributor'); 28339a89382SEsther Brunner foreach ($data as $key => $value){ 28439a89382SEsther Brunner 28539a89382SEsther Brunner // be careful with sub-arrays of $meta['relation'] 28639a89382SEsther Brunner if ($key == 'relation'){ 2870a7e3bceSchris 28839a89382SEsther Brunner foreach ($value as $subkey => $subvalue){ 28966d29756SChris Smith $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue; 2900a7e3bceSchris if ($persistent) 29166d29756SChris Smith $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue; 29239a89382SEsther Brunner } 29339a89382SEsther Brunner 29439a89382SEsther Brunner // be careful with some senisitive arrays of $meta 29539a89382SEsther Brunner } elseif (in_array($key, $protected)){ 2960a7e3bceSchris 29766d29756SChris Smith // these keys, must have subkeys - a legitimate value must be an array 29839a89382SEsther Brunner if (is_array($value)) { 29966d29756SChris Smith $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key],$value) : $value; 3000a7e3bceSchris 3010a7e3bceSchris if ($persistent) { 30266d29756SChris Smith $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key],$value) : $value; 3030a7e3bceSchris } 30439a89382SEsther Brunner } 30539a89382SEsther Brunner 30639a89382SEsther Brunner // no special treatment for the rest 30739a89382SEsther Brunner } else { 3080a7e3bceSchris $meta['current'][$key] = $value; 3090a7e3bceSchris if ($persistent) $meta['persistent'][$key] = $value; 31039a89382SEsther Brunner } 31139a89382SEsther Brunner } 31239a89382SEsther Brunner 31339a89382SEsther Brunner // save only if metadata changed 31439a89382SEsther Brunner if ($meta == $orig) return true; 3156afe8dcaSchris 3160a7e3bceSchris // sync cached copies, including $INFO metadata 3170a7e3bceSchris global $cache_metadata, $INFO; 3180a7e3bceSchris 3190a7e3bceSchris if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta; 3200a7e3bceSchris if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 3216afe8dcaSchris 32239a89382SEsther Brunner return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 32339a89382SEsther Brunner} 32439a89382SEsther Brunner 32539a89382SEsther Brunner/** 3260a7e3bceSchris * read the metadata from source/cache for $id 3270a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata) 3280a7e3bceSchris * 3290a7e3bceSchris * this function also converts the metadata from the original format to 3300a7e3bceSchris * the current format ('current' & 'persistent' arrays) 3310a7e3bceSchris * 3320a7e3bceSchris * @author Christopher Smith <chris@jalakai.co.uk> 3330a7e3bceSchris * 3340a7e3bceSchris * @param string $id absolute wiki page id 3350a7e3bceSchris * @param bool $cache whether or not to cache metadata in memory 3360a7e3bceSchris * (only use for metadata likely to be accessed several times) 3370a7e3bceSchris * 3380a7e3bceSchris * @return array metadata 3390a7e3bceSchris */ 3400a7e3bceSchrisfunction p_read_metadata($id,$cache=false) { 3410a7e3bceSchris global $cache_metadata; 3420a7e3bceSchris 3430a7e3bceSchris if (isset($cache_metadata[$id])) return $cache_metadata[$id]; 3440a7e3bceSchris 3450a7e3bceSchris $file = metaFN($id, '.meta'); 3460a7e3bceSchris $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array()); 3470a7e3bceSchris 3480a7e3bceSchris // convert $meta from old format to new (current+persistent) format 3490a7e3bceSchris if (!isset($meta['current'])) { 3500a7e3bceSchris $meta = array('current'=>$meta,'persistent'=>$meta); 3510a7e3bceSchris 3520a7e3bceSchris // remove non-persistent keys 3530a7e3bceSchris unset($meta['persistent']['title']); 3540a7e3bceSchris unset($meta['persistent']['description']['abstract']); 3550a7e3bceSchris unset($meta['persistent']['description']['tableofcontents']); 3560a7e3bceSchris unset($meta['persistent']['relation']['haspart']); 3570a7e3bceSchris unset($meta['persistent']['relation']['references']); 3580a7e3bceSchris unset($meta['persistent']['date']['valid']); 3590a7e3bceSchris 3600a7e3bceSchris if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']); 3610a7e3bceSchris if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']); 3620a7e3bceSchris if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']); 3630a7e3bceSchris 3640a7e3bceSchris // save converted metadata 3650a7e3bceSchris io_saveFile($file, serialize($meta)); 3660a7e3bceSchris } 3670a7e3bceSchris 3680a7e3bceSchris if ($cache) { 3690a7e3bceSchris $cache_metadata[$id] = $meta; 3700a7e3bceSchris } 3710a7e3bceSchris 3720a7e3bceSchris return $meta; 3730a7e3bceSchris} 3740a7e3bceSchris 3750a7e3bceSchris/** 37639a89382SEsther Brunner * renders the metadata of a page 37739a89382SEsther Brunner * 37839a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 37939a89382SEsther Brunner */ 38039a89382SEsther Brunnerfunction p_render_metadata($id, $orig){ 38148924015SAndreas Gohr // make sure the correct ID is in global ID 38248924015SAndreas Gohr global $ID; 38348924015SAndreas Gohr $keep = $ID; 38448924015SAndreas Gohr $ID = $id; 38548924015SAndreas Gohr 3860a7e3bceSchris 3870a7e3bceSchris // add an extra key for the event - to tell event handlers the page whose metadata this is 3880a7e3bceSchris $orig['page'] = $id; 3890a7e3bceSchris $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig); 3900a7e3bceSchris if ($evt->advise_before()) { 3910a7e3bceSchris 39239a89382SEsther Brunner require_once DOKU_INC."inc/parser/metadata.php"; 39339a89382SEsther Brunner 39439a89382SEsther Brunner // get instructions 3954b5f4f4eSchris $instructions = p_cached_instructions(wikiFN($id),false,$id); 39648924015SAndreas Gohr if(is_null($instructions)){ 39748924015SAndreas Gohr $ID = $keep; 39848924015SAndreas Gohr return null; // something went wrong with the instructions 39948924015SAndreas Gohr } 40039a89382SEsther Brunner 40139a89382SEsther Brunner // set up the renderer 40239a89382SEsther Brunner $renderer = & new Doku_Renderer_metadata(); 4030a7e3bceSchris $renderer->meta = $orig['current']; 4040a7e3bceSchris $renderer->persistent = $orig['persistent']; 40539a89382SEsther Brunner 40639a89382SEsther Brunner // loop through the instructions 40739a89382SEsther Brunner foreach ($instructions as $instruction){ 40839a89382SEsther Brunner // execute the callback against the renderer 40939a89382SEsther Brunner call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]); 41039a89382SEsther Brunner } 41139a89382SEsther Brunner 4120a7e3bceSchris $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent); 4130a7e3bceSchris } 4140a7e3bceSchris $evt->advise_after(); 4150a7e3bceSchris 41648924015SAndreas Gohr $ID = $keep; 4170a7e3bceSchris return $evt->result; 41839a89382SEsther Brunner} 41939a89382SEsther Brunner 42039a89382SEsther Brunner/** 421107b01d6Sandi * returns all available parser syntax modes in correct order 422107b01d6Sandi * 423107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 424107b01d6Sandi */ 425107b01d6Sandifunction p_get_parsermodes(){ 426107b01d6Sandi global $conf; 427107b01d6Sandi 428107b01d6Sandi //reuse old data 429107b01d6Sandi static $modes = null; 430107b01d6Sandi if($modes != null){ 431107b01d6Sandi return $modes; 432107b01d6Sandi } 433107b01d6Sandi 434107b01d6Sandi //import parser classes and mode definitions 435107b01d6Sandi require_once DOKU_INC . 'inc/parser/parser.php'; 436107b01d6Sandi 437107b01d6Sandi // we now collect all syntax modes and their objects, then they will 438107b01d6Sandi // be sorted and added to the parser in correct order 439107b01d6Sandi $modes = array(); 440107b01d6Sandi 441107b01d6Sandi // add syntax plugins 442107b01d6Sandi $pluginlist = plugin_list('syntax'); 443107b01d6Sandi if(count($pluginlist)){ 444107b01d6Sandi global $PARSER_MODES; 445107b01d6Sandi $obj = null; 446107b01d6Sandi foreach($pluginlist as $p){ 447c90b2fb1Schris if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 448107b01d6Sandi $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 449107b01d6Sandi //add to modes 450107b01d6Sandi $modes[] = array( 451107b01d6Sandi 'sort' => $obj->getSort(), 452107b01d6Sandi 'mode' => "plugin_$p", 453107b01d6Sandi 'obj' => $obj, 454107b01d6Sandi ); 455a46d0d65SAndreas Gohr unset($obj); //remove the reference 456107b01d6Sandi } 457107b01d6Sandi } 458107b01d6Sandi 459107b01d6Sandi // add default modes 460107b01d6Sandi $std_modes = array('listblock','preformatted','notoc','nocache', 461107b01d6Sandi 'header','table','linebreak','footnote','hr', 462107b01d6Sandi 'unformatted','php','html','code','file','quote', 463e77ea1bcSAndreas Gohr 'internallink','rss','media','externallink', 464e77ea1bcSAndreas Gohr 'emaillink','windowssharelink','eol'); 465e77ea1bcSAndreas Gohr if($conf['typography']){ 466e77ea1bcSAndreas Gohr $std_modes[] = 'quotes'; 467e77ea1bcSAndreas Gohr $std_modes[] = 'multiplyentity'; 468e77ea1bcSAndreas Gohr } 469107b01d6Sandi foreach($std_modes as $m){ 470107b01d6Sandi $class = "Doku_Parser_Mode_$m"; 471107b01d6Sandi $obj = new $class(); 472107b01d6Sandi $modes[] = array( 473107b01d6Sandi 'sort' => $obj->getSort(), 474107b01d6Sandi 'mode' => $m, 475107b01d6Sandi 'obj' => $obj 476107b01d6Sandi ); 477107b01d6Sandi } 478107b01d6Sandi 479107b01d6Sandi // add formatting modes 480107b01d6Sandi $fmt_modes = array('strong','emphasis','underline','monospace', 481107b01d6Sandi 'subscript','superscript','deleted'); 482107b01d6Sandi foreach($fmt_modes as $m){ 483107b01d6Sandi $obj = new Doku_Parser_Mode_formatting($m); 484107b01d6Sandi $modes[] = array( 485107b01d6Sandi 'sort' => $obj->getSort(), 486107b01d6Sandi 'mode' => $m, 487107b01d6Sandi 'obj' => $obj 488107b01d6Sandi ); 489107b01d6Sandi } 490107b01d6Sandi 491107b01d6Sandi // add modes which need files 492107b01d6Sandi $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys())); 493107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 494107b01d6Sandi $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms())); 495107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 496107b01d6Sandi $obj = new Doku_Parser_Mode_entity(array_keys(getEntities())); 497107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 498107b01d6Sandi 499107b01d6Sandi 500107b01d6Sandi // add optional camelcase mode 501107b01d6Sandi if($conf['camelcase']){ 502107b01d6Sandi $obj = new Doku_Parser_Mode_camelcaselink(); 503107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 504107b01d6Sandi } 505107b01d6Sandi 506107b01d6Sandi //sort modes 507107b01d6Sandi usort($modes,'p_sort_modes'); 508107b01d6Sandi 509107b01d6Sandi return $modes; 510107b01d6Sandi} 511107b01d6Sandi 512107b01d6Sandi/** 513107b01d6Sandi * Callback function for usort 514107b01d6Sandi * 515107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 516107b01d6Sandi */ 517107b01d6Sandifunction p_sort_modes($a, $b){ 518107b01d6Sandi if($a['sort'] == $b['sort']) return 0; 519107b01d6Sandi return ($a['sort'] < $b['sort']) ? -1 : 1; 520107b01d6Sandi} 521107b01d6Sandi 522107b01d6Sandi/** 523ac83b9d8Sandi * Renders a list of instruction to the specified output mode 524c112d578Sandi * 5259dc2c2afSandi * In the $info array are informations from the renderer returned 5269dc2c2afSandi * 527c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 528c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 529c112d578Sandi */ 5309dc2c2afSandifunction p_render($mode,$instructions,&$info){ 531c112d578Sandi if(is_null($instructions)) return ''; 532c112d578Sandi 533*d968d3e5SChris Smith $Renderer =& p_get_renderer($mode); 534*d968d3e5SChris Smith if (is_null($Renderer)) return null; 535c327d6c4SAndreas Gohr 536*d968d3e5SChris Smith $Renderer->reset(); 537c112d578Sandi 538c112d578Sandi $Renderer->smileys = getSmileys(); 539c112d578Sandi $Renderer->entities = getEntities(); 540c112d578Sandi $Renderer->acronyms = getAcronyms(); 541c112d578Sandi $Renderer->interwiki = getInterwiki(); 542c112d578Sandi #$Renderer->badwords = getBadWords(); 543c112d578Sandi 544c112d578Sandi // Loop through the instructions 545c112d578Sandi foreach ( $instructions as $instruction ) { 546c112d578Sandi // Execute the callback against the Renderer 547c112d578Sandi call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]); 548c112d578Sandi } 5499dc2c2afSandi 5509dc2c2afSandi //set info array 5519dc2c2afSandi $info = $Renderer->info; 5529dc2c2afSandi 553677844afSchris // Post process and return the output 554677844afSchris $data = array($mode,& $Renderer->doc); 555677844afSchris trigger_event('RENDERER_CONTENT_POSTPROCESS',$data); 556c112d578Sandi return $Renderer->doc; 557c112d578Sandi} 558c112d578Sandi 559*d968d3e5SChris Smithfunction & p_get_renderer($mode) { 560*d968d3e5SChris Smith global $conf; 561*d968d3e5SChris Smith 562*d968d3e5SChris Smith $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode; 563*d968d3e5SChris Smith 564*d968d3e5SChris Smith // try default renderer first: 565*d968d3e5SChris Smith $file = DOKU_INC."inc/parser/$rname.php"; 566*d968d3e5SChris Smith if(@file_exists($file)){ 567*d968d3e5SChris Smith require_once $file; 568*d968d3e5SChris Smith $rclass = "Doku_Renderer_$rname"; 569*d968d3e5SChris Smith 570*d968d3e5SChris Smith if ( !class_exists($rclass) ) { 571*d968d3e5SChris Smith trigger_error("Unable to resolve render class $rclass",E_USER_WARNING); 572*d968d3e5SChris Smith msg("Renderer '$rname' for $mode not valid",-1); 573*d968d3e5SChris Smith return null; 574*d968d3e5SChris Smith } 575*d968d3e5SChris Smith $Renderer = & new $rclass(); 576*d968d3e5SChris Smith }else{ 577*d968d3e5SChris Smith // Maybe a plugin is available? 578*d968d3e5SChris Smith $Renderer =& plugin_load('renderer',$rname); 579*d968d3e5SChris Smith if(is_null($Renderer)){ 580*d968d3e5SChris Smith msg("No renderer '$rname' found for mode '$mode'",-1); 581*d968d3e5SChris Smith return null; 582*d968d3e5SChris Smith } 583*d968d3e5SChris Smith } 584*d968d3e5SChris Smith 585*d968d3e5SChris Smith return $Renderer; 586*d968d3e5SChris Smith} 587*d968d3e5SChris Smith 588bb0a59d4Sjan/** 589bb0a59d4Sjan * Gets the first heading from a file 590bb0a59d4Sjan * 591fc18c0fbSchris * @param string $id dokuwiki page id 592fc18c0fbSchris * @param bool $render rerender if first heading not known 5930770c0e5SChris Smith * default: true -- must be set to false for calls from the metadata renderer to 5940770c0e5SChris Smith * protects against loops and excessive resource usage when pages 5950770c0e5SChris Smith * for which only a first heading is required will attempt to 5960770c0e5SChris Smith * render metadata for all the pages for which they require first 5970770c0e5SChris Smith * headings ... and so on. 598fc18c0fbSchris * 59995dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 600bb0a59d4Sjan */ 6010770c0e5SChris Smithfunction p_get_first_heading($id, $render=true){ 602796bafb3SAndreas Gohr global $conf; 603fc18c0fbSchris return $conf['useheading'] ? p_get_metadata($id,'title',$render) : null; 604bb0a59d4Sjan} 605bb0a59d4Sjan 6068f7d700cSchris/** 6078f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output 6088f7d700cSchris * 6098f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk> 6108f7d700cSchris */ 6118f7d700cSchrisfunction p_xhtml_cached_geshi($code, $language) { 6128f7d700cSchris $cache = getCacheName($language.$code,".code"); 6138f7d700cSchris 6148cfffb85Schris if (@file_exists($cache) && !$_REQUEST['purge'] && 6158cfffb85Schris (filemtime($cache) > filemtime(DOKU_INC . 'inc/geshi.php'))) { 6168f7d700cSchris 6178f7d700cSchris $highlighted_code = io_readFile($cache, false); 618852896daSAndreas Gohr @touch($cache); 6198f7d700cSchris 6208f7d700cSchris } else { 6218f7d700cSchris 6228f7d700cSchris require_once(DOKU_INC . 'inc/geshi.php'); 6238f7d700cSchris 6248f7d700cSchris $geshi = new GeSHi($code, strtolower($language), DOKU_INC . 'inc/geshi'); 6258f7d700cSchris $geshi->set_encoding('utf-8'); 6268f7d700cSchris $geshi->enable_classes(); 6278f7d700cSchris $geshi->set_header_type(GESHI_HEADER_PRE); 6288f7d700cSchris $geshi->set_overall_class("code $language"); 6298f7d700cSchris $geshi->set_link_target($conf['target']['extern']); 6308f7d700cSchris 6318f7d700cSchris $highlighted_code = $geshi->parse_code(); 6328f7d700cSchris 6338f7d700cSchris io_saveFile($cache,$highlighted_code); 6348f7d700cSchris } 6358f7d700cSchris 6368f7d700cSchris return $highlighted_code; 6378f7d700cSchris} 6388f7d700cSchris 639c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 640