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 10fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 11c112d578Sandi 12c112d578Sandi/** 13*ff725173SMichael Hamann * For how many different pages shall the first heading be loaded from the 14*ff725173SMichael Hamann * metadata? When this limit is reached the title index is loaded and used for 15*ff725173SMichael Hamann * all following requests. 16*ff725173SMichael Hamann */ 17*ff725173SMichael Hamannif (!defined('P_GET_FIRST_HEADING_METADATA_LIMIT')) define('P_GET_FIRST_HEADING_METADATA_LIMIT', 10); 18*ff725173SMichael Hamann 19*ff725173SMichael Hamann/** 20c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision. 21c112d578Sandi * 22c112d578Sandi * If $excuse is true an explanation is returned if the file 23c112d578Sandi * wasn't found 24c112d578Sandi * 25c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 26c112d578Sandi */ 27c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){ 28c112d578Sandi $file = wikiFN($id,$rev); 29c112d578Sandi $ret = ''; 30c112d578Sandi 31c112d578Sandi //ensure $id is in global $ID (needed for parsing) 321e76272cSandi global $ID; 333ff8773bSAndreas Gohr $keep = $ID; 341e76272cSandi $ID = $id; 35c112d578Sandi 36c112d578Sandi if($rev){ 37c112d578Sandi if(@file_exists($file)){ 38bde4e341SGalaxyMaster $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions 39c112d578Sandi }elseif($excuse){ 40c112d578Sandi $ret = p_locale_xhtml('norev'); 41c112d578Sandi } 42c112d578Sandi }else{ 43c112d578Sandi if(@file_exists($file)){ 444b5f4f4eSchris $ret = p_cached_output($file,'xhtml',$id); 45c112d578Sandi }elseif($excuse){ 46c112d578Sandi $ret = p_locale_xhtml('newpage'); 47c112d578Sandi } 48c112d578Sandi } 49c112d578Sandi 503ff8773bSAndreas Gohr //restore ID (just in case) 513ff8773bSAndreas Gohr $ID = $keep; 523ff8773bSAndreas Gohr 53c112d578Sandi return $ret; 54c112d578Sandi} 55c112d578Sandi 56c112d578Sandi/** 576b7b33dcShfuecks * Returns starting summary for a page (e.g. the first few 586b7b33dcShfuecks * paragraphs), marked up in XHTML. 596b7b33dcShfuecks * 606b7b33dcShfuecks * If $excuse is true an explanation is returned if the file 616b7b33dcShfuecks * wasn't found 626b7b33dcShfuecks * 636b7b33dcShfuecks * @param string wiki page id 646b7b33dcShfuecks * @param reference populated with page title from heading or page id 658716966dSAndreas Gohr * @deprecated 668716966dSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com> 676b7b33dcShfuecks */ 686b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){ 696b7b33dcShfuecks $file = wikiFN($id,$rev); 706b7b33dcShfuecks $ret = ''; 716b7b33dcShfuecks 726b7b33dcShfuecks //ensure $id is in global $ID (needed for parsing) 736b7b33dcShfuecks global $ID; 746b7b33dcShfuecks $keep = $ID; 756b7b33dcShfuecks $ID = $id; 766b7b33dcShfuecks 776b7b33dcShfuecks if($rev){ 786b7b33dcShfuecks if(@file_exists($file)){ 796b7b33dcShfuecks //no caching on old revisions 80bde4e341SGalaxyMaster $ins = p_get_instructions(io_readWikiPage($file,$id,$rev)); 816b7b33dcShfuecks }elseif($excuse){ 826b7b33dcShfuecks $ret = p_locale_xhtml('norev'); 836b7b33dcShfuecks //restore ID (just in case) 846b7b33dcShfuecks $ID = $keep; 856b7b33dcShfuecks return $ret; 866b7b33dcShfuecks } 876b7b33dcShfuecks 886b7b33dcShfuecks }else{ 896b7b33dcShfuecks 906b7b33dcShfuecks if(@file_exists($file)){ 916b7b33dcShfuecks // The XHTML for a summary is not cached so use the instruction cache 926b7b33dcShfuecks $ins = p_cached_instructions($file); 936b7b33dcShfuecks }elseif($excuse){ 946b7b33dcShfuecks $ret = p_locale_xhtml('newpage'); 956b7b33dcShfuecks //restore ID (just in case) 966b7b33dcShfuecks $ID = $keep; 976b7b33dcShfuecks return $ret; 986b7b33dcShfuecks } 996b7b33dcShfuecks } 1006b7b33dcShfuecks 1016b7b33dcShfuecks $ret = p_render('xhtmlsummary',$ins,$info); 1026b7b33dcShfuecks 1036b7b33dcShfuecks if ( $info['sum_pagetitle'] ) { 1046b7b33dcShfuecks $title = $info['sum_pagetitle']; 1056b7b33dcShfuecks } else { 1066b7b33dcShfuecks $title = $id; 1076b7b33dcShfuecks } 1086b7b33dcShfuecks 1096b7b33dcShfuecks $ID = $keep; 1106b7b33dcShfuecks return $ret; 1116b7b33dcShfuecks} 1126b7b33dcShfuecks 1136b7b33dcShfuecks/** 114c112d578Sandi * Returns the specified local text in parsed format 115c112d578Sandi * 116c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 117c112d578Sandi */ 118c112d578Sandifunction p_locale_xhtml($id){ 119c112d578Sandi //fetch parsed locale 1204b5f4f4eSchris $html = p_cached_output(localeFN($id)); 121c112d578Sandi return $html; 122c112d578Sandi} 123c112d578Sandi 124c112d578Sandi/** 1254b5f4f4eSchris * *** DEPRECATED *** 1264b5f4f4eSchris * 1274b5f4f4eSchris * use p_cached_output() 1284b5f4f4eSchris * 129c112d578Sandi * Returns the given file parsed to XHTML 130c112d578Sandi * 131c112d578Sandi * Uses and creates a cachefile 132c112d578Sandi * 1334b5f4f4eSchris * @deprecated 134c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 1359dc2c2afSandi * @todo rewrite to use mode instead of hardcoded XHTML 136c112d578Sandi */ 137c112d578Sandifunction p_cached_xhtml($file){ 1384b5f4f4eSchris return p_cached_output($file); 1394b5f4f4eSchris} 1404b5f4f4eSchris 1414b5f4f4eSchris/** 1424b5f4f4eSchris * Returns the given file parsed into the requested output format 1434b5f4f4eSchris * 1444b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org> 1454b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk> 1464b5f4f4eSchris */ 1474b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') { 148c112d578Sandi global $conf; 149c112d578Sandi 1504b5f4f4eSchris $cache = new cache_renderer($id, $file, $format); 1514b5f4f4eSchris if ($cache->useCache()) { 15285767031SAndreas Gohr $parsed = $cache->retrieveCache(false); 153ea2a4271SAndreas Gohr if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 154c112d578Sandi } else { 1554b5f4f4eSchris $parsed = p_render($format, p_cached_instructions($file,false,$id), $info); 156c112d578Sandi 1579dc2c2afSandi if ($info['cache']) { 1584b5f4f4eSchris $cache->storeCache($parsed); //save cachefile 159ea2a4271SAndreas Gohr if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 160c112d578Sandi }else{ 1614b5f4f4eSchris $cache->removeCache(); //try to delete cachefile 162ea2a4271SAndreas Gohr if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 163c112d578Sandi } 164c112d578Sandi } 165c112d578Sandi 166c112d578Sandi return $parsed; 167c112d578Sandi} 168c112d578Sandi 169c112d578Sandi/** 170c112d578Sandi * Returns the render instructions for a file 171c112d578Sandi * 172c112d578Sandi * Uses and creates a serialized cache file 173c112d578Sandi * 174c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 175c112d578Sandi */ 1764b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') { 177c112d578Sandi global $conf; 17847b2d319SAndreas Gohr static $run = null; 17947b2d319SAndreas Gohr if(is_null($run)) $run = array(); 180c112d578Sandi 1814b5f4f4eSchris $cache = new cache_instructions($id, $file); 182c112d578Sandi 18347b2d319SAndreas Gohr if ($cacheonly || $cache->useCache() || isset($run[$file])) { 1844b5f4f4eSchris return $cache->retrieveCache(); 185c112d578Sandi } else if (@file_exists($file)) { 186c112d578Sandi // no cache - do some work 187bde4e341SGalaxyMaster $ins = p_get_instructions(io_readWikiPage($file,$id)); 188cbaf4259SChris Smith if ($cache->storeCache($ins)) { 18947b2d319SAndreas Gohr $run[$file] = true; // we won't rebuild these instructions in the same run again 190cbaf4259SChris Smith } else { 191cbaf4259SChris Smith msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1); 192cbaf4259SChris Smith } 193c112d578Sandi return $ins; 194c112d578Sandi } 195c112d578Sandi 1963b3f8916SAndreas Gohr return null; 197c112d578Sandi} 198c112d578Sandi 199c112d578Sandi/** 200c112d578Sandi * turns a page into a list of instructions 201c112d578Sandi * 202c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 203c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 204c112d578Sandi */ 2056bbae538Sandifunction p_get_instructions($text){ 206c112d578Sandi 207107b01d6Sandi $modes = p_get_parsermodes(); 208ee20e7d1Sandi 209c112d578Sandi // Create the parser 21067f9913dSAndreas Gohr $Parser = new Doku_Parser(); 211c112d578Sandi 212c112d578Sandi // Add the Handler 21367f9913dSAndreas Gohr $Parser->Handler = new Doku_Handler(); 214c112d578Sandi 215107b01d6Sandi //add modes to parser 216107b01d6Sandi foreach($modes as $mode){ 217107b01d6Sandi $Parser->addMode($mode['mode'],$mode['obj']); 218c112d578Sandi } 219c112d578Sandi 220c112d578Sandi // Do the parsing 221677844afSchris trigger_event('PARSER_WIKITEXT_PREPROCESS', $text); 222a2d649c4Sandi $p = $Parser->parse($text); 223ee20e7d1Sandi // dbg($p); 224a2d649c4Sandi return $p; 225c112d578Sandi} 226c112d578Sandi 227c112d578Sandi/** 22839a89382SEsther Brunner * returns the metadata of a page 22939a89382SEsther Brunner * 2304a819402SMichael Hamann * @param string $id The id of the page the metadata should be returned from 2314a819402SMichael Hamann * @param string $key The key of the metdata value that shall be read (by default everything) - separate hierarchies by " " like "date created" 2324a819402SMichael Hamann * @param boolean $render If the page should be rendererd when the cache can't be used - default true 2334a819402SMichael Hamann * @return mixed The requested metadata fields 2344a819402SMichael Hamann * 23539a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 23698214867SMichael Hamann * @author Michael Hamann <michael@content-space.de> 23739a89382SEsther Brunner */ 2384a819402SMichael Hamannfunction p_get_metadata($id, $key='', $render=true){ 2391172f8dcSAdrian Lang global $ID; 2406afe8dcaSchris 2410a7e3bceSchris // cache the current page 2420a7e3bceSchris // Benchmarking shows the current page's metadata is generally the only page metadata 2430a7e3bceSchris // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 2440a7e3bceSchris $cache = ($ID == $id); 2450a7e3bceSchris $meta = p_read_metadata($id, $cache); 24639a89382SEsther Brunner 24798214867SMichael Hamann // prevent recursive calls in the cache 24898214867SMichael Hamann static $recursion = false; 2494a819402SMichael Hamann if (!$recursion && $render){ 25098214867SMichael Hamann $recursion = true; 25198214867SMichael Hamann 25298214867SMichael Hamann $cachefile = new cache_renderer($id, wikiFN($id), 'metadata'); 25398214867SMichael Hamann 25498214867SMichael Hamann if (page_exists($id) && !$cachefile->useCache()){ 25569ba640bSMichael Hamann $old_meta = $meta; 25639a89382SEsther Brunner $meta = p_render_metadata($id, $meta); 25769ba640bSMichael Hamann // only update the file when the metadata has been changed 25869ba640bSMichael Hamann if ($meta == $old_meta || p_save_metadata($id, $meta)) { 25998214867SMichael Hamann // store a timestamp in order to make sure that the cachefile is touched 26098214867SMichael Hamann $cachefile->storeCache(time()); 26198214867SMichael Hamann } else { 26298214867SMichael Hamann msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1); 26398214867SMichael Hamann } 26498214867SMichael Hamann } 26598214867SMichael Hamann 26698214867SMichael Hamann $recursion = false; 2676afe8dcaSchris } 26839a89382SEsther Brunner 269ebf65d37SAdrian Lang $val = $meta['current']; 270ebf65d37SAdrian Lang 27139a89382SEsther Brunner // filter by $key 272569a0019SAdrian Lang foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 273ebf65d37SAdrian Lang if (!isset($val[$cur_key])) { 274ebf65d37SAdrian Lang return null; 27566d29756SChris Smith } 276ebf65d37SAdrian Lang $val = $val[$cur_key]; 27739a89382SEsther Brunner } 278ebf65d37SAdrian Lang return $val; 27939a89382SEsther Brunner} 28039a89382SEsther Brunner 28139a89382SEsther Brunner/** 28239a89382SEsther Brunner * sets metadata elements of a page 28339a89382SEsther Brunner * 284a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 285a365baeeSDominik Eckelmann * 286a365baeeSDominik Eckelmann * @param String $id is the ID of a wiki page 287a365baeeSDominik Eckelmann * @param Array $data is an array with key ⇒ value pairs to be set in the metadata 288a365baeeSDominik Eckelmann * @param Boolean $render whether or not the page metadata should be generated with the renderer 289a365baeeSDominik Eckelmann * @param Boolean $persistent indicates whether or not the particular metadata value will persist through 290a365baeeSDominik Eckelmann * the next metadata rendering. 291a365baeeSDominik Eckelmann * @return boolean true on success 292a365baeeSDominik Eckelmann * 29339a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 29439a89382SEsther Brunner */ 2950a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){ 29639a89382SEsther Brunner if (!is_array($data)) return false; 29739a89382SEsther Brunner 2980a7e3bceSchris global $ID; 2990a7e3bceSchris 3000a7e3bceSchris // cache the current page 3010a7e3bceSchris $cache = ($ID == $id); 3020a7e3bceSchris $orig = p_read_metadata($id, $cache); 30339a89382SEsther Brunner 30439a89382SEsther Brunner // render metadata first? 3050a7e3bceSchris $meta = $render ? p_render_metadata($id, $orig) : $orig; 30639a89382SEsther Brunner 30739a89382SEsther Brunner // now add the passed metadata 30839a89382SEsther Brunner $protected = array('description', 'date', 'contributor'); 30939a89382SEsther Brunner foreach ($data as $key => $value){ 31039a89382SEsther Brunner 31139a89382SEsther Brunner // be careful with sub-arrays of $meta['relation'] 31239a89382SEsther Brunner if ($key == 'relation'){ 3130a7e3bceSchris 31439a89382SEsther Brunner foreach ($value as $subkey => $subvalue){ 31566d29756SChris Smith $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue; 3160a7e3bceSchris if ($persistent) 31766d29756SChris Smith $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue; 31839a89382SEsther Brunner } 31939a89382SEsther Brunner 32039a89382SEsther Brunner // be careful with some senisitive arrays of $meta 32139a89382SEsther Brunner } elseif (in_array($key, $protected)){ 3220a7e3bceSchris 32366d29756SChris Smith // these keys, must have subkeys - a legitimate value must be an array 32439a89382SEsther Brunner if (is_array($value)) { 32566d29756SChris Smith $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key],$value) : $value; 3260a7e3bceSchris 3270a7e3bceSchris if ($persistent) { 32866d29756SChris Smith $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key],$value) : $value; 3290a7e3bceSchris } 33039a89382SEsther Brunner } 33139a89382SEsther Brunner 33239a89382SEsther Brunner // no special treatment for the rest 33339a89382SEsther Brunner } else { 3340a7e3bceSchris $meta['current'][$key] = $value; 3350a7e3bceSchris if ($persistent) $meta['persistent'][$key] = $value; 33639a89382SEsther Brunner } 33739a89382SEsther Brunner } 33839a89382SEsther Brunner 33939a89382SEsther Brunner // save only if metadata changed 34039a89382SEsther Brunner if ($meta == $orig) return true; 3416afe8dcaSchris 3421172f8dcSAdrian Lang return p_save_metadata($id, $meta); 34339a89382SEsther Brunner} 34439a89382SEsther Brunner 34539a89382SEsther Brunner/** 3463d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data 3473d1f9ec3SMichael Klier * used on page deletion 3483d1f9ec3SMichael Klier * 3493d1f9ec3SMichael Klier * @author Michael Klier <chi@chimeric.de> 3503d1f9ec3SMichael Klier */ 3513d1f9ec3SMichael Klierfunction p_purge_metadata($id) { 3523d1f9ec3SMichael Klier $meta = p_read_metadata($id); 3533d1f9ec3SMichael Klier foreach($meta['current'] as $key => $value) { 3543d1f9ec3SMichael Klier if(is_array($meta[$key])) { 3553d1f9ec3SMichael Klier $meta['current'][$key] = array(); 3563d1f9ec3SMichael Klier } else { 3573d1f9ec3SMichael Klier $meta['current'][$key] = ''; 3583d1f9ec3SMichael Klier } 3591172f8dcSAdrian Lang 3603d1f9ec3SMichael Klier } 3611172f8dcSAdrian Lang return p_save_metadata($id, $meta); 3623d1f9ec3SMichael Klier} 3633d1f9ec3SMichael Klier 3643d1f9ec3SMichael Klier/** 3650a7e3bceSchris * read the metadata from source/cache for $id 3660a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata) 3670a7e3bceSchris * 3680a7e3bceSchris * @author Christopher Smith <chris@jalakai.co.uk> 3690a7e3bceSchris * 3700a7e3bceSchris * @param string $id absolute wiki page id 3710a7e3bceSchris * @param bool $cache whether or not to cache metadata in memory 3720a7e3bceSchris * (only use for metadata likely to be accessed several times) 3730a7e3bceSchris * 3740a7e3bceSchris * @return array metadata 3750a7e3bceSchris */ 3760a7e3bceSchrisfunction p_read_metadata($id,$cache=false) { 3770a7e3bceSchris global $cache_metadata; 3780a7e3bceSchris 3793a50618cSgweissbach if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 3800a7e3bceSchris 3810a7e3bceSchris $file = metaFN($id, '.meta'); 3820a7e3bceSchris $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array()); 3830a7e3bceSchris 3840a7e3bceSchris if ($cache) { 3853a50618cSgweissbach $cache_metadata[(string)$id] = $meta; 3860a7e3bceSchris } 3870a7e3bceSchris 3880a7e3bceSchris return $meta; 3890a7e3bceSchris} 3900a7e3bceSchris 3910a7e3bceSchris/** 3921172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file 3931172f8dcSAdrian Lang * 3941172f8dcSAdrian Lang * @param string $id absolute wiki page id 3951172f8dcSAdrian Lang * @param array $meta metadata 3961172f8dcSAdrian Lang * 3971172f8dcSAdrian Lang * @return bool success / fail 3981172f8dcSAdrian Lang */ 3991172f8dcSAdrian Langfunction p_save_metadata($id, $meta) { 4001172f8dcSAdrian Lang // sync cached copies, including $INFO metadata 4011172f8dcSAdrian Lang global $cache_metadata, $INFO; 4021172f8dcSAdrian Lang 4031172f8dcSAdrian Lang if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 4041172f8dcSAdrian Lang if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; } 4051172f8dcSAdrian Lang 4061172f8dcSAdrian Lang return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 4071172f8dcSAdrian Lang} 4081172f8dcSAdrian Lang 4091172f8dcSAdrian Lang/** 41039a89382SEsther Brunner * renders the metadata of a page 41139a89382SEsther Brunner * 41239a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 41339a89382SEsther Brunner */ 41439a89382SEsther Brunnerfunction p_render_metadata($id, $orig){ 41548924015SAndreas Gohr // make sure the correct ID is in global ID 41648924015SAndreas Gohr global $ID; 41748924015SAndreas Gohr $keep = $ID; 41848924015SAndreas Gohr $ID = $id; 41948924015SAndreas Gohr 4200a7e3bceSchris // add an extra key for the event - to tell event handlers the page whose metadata this is 4210a7e3bceSchris $orig['page'] = $id; 4220a7e3bceSchris $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig); 4230a7e3bceSchris if ($evt->advise_before()) { 4240a7e3bceSchris 42539a89382SEsther Brunner require_once DOKU_INC."inc/parser/metadata.php"; 42639a89382SEsther Brunner 42739a89382SEsther Brunner // get instructions 4284b5f4f4eSchris $instructions = p_cached_instructions(wikiFN($id),false,$id); 42948924015SAndreas Gohr if(is_null($instructions)){ 43048924015SAndreas Gohr $ID = $keep; 43148924015SAndreas Gohr return null; // something went wrong with the instructions 43248924015SAndreas Gohr } 43339a89382SEsther Brunner 43439a89382SEsther Brunner // set up the renderer 43567f9913dSAndreas Gohr $renderer = new Doku_Renderer_metadata(); 4360a7e3bceSchris $renderer->meta = $orig['current']; 4370a7e3bceSchris $renderer->persistent = $orig['persistent']; 43839a89382SEsther Brunner 43939a89382SEsther Brunner // loop through the instructions 44039a89382SEsther Brunner foreach ($instructions as $instruction){ 44139a89382SEsther Brunner // execute the callback against the renderer 4423b748871SAndreas Gohr call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); 44339a89382SEsther Brunner } 44439a89382SEsther Brunner 4450a7e3bceSchris $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent); 4460a7e3bceSchris } 4470a7e3bceSchris $evt->advise_after(); 4480a7e3bceSchris 44948924015SAndreas Gohr $ID = $keep; 4500a7e3bceSchris return $evt->result; 45139a89382SEsther Brunner} 45239a89382SEsther Brunner 45339a89382SEsther Brunner/** 454107b01d6Sandi * returns all available parser syntax modes in correct order 455107b01d6Sandi * 456107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 457107b01d6Sandi */ 458107b01d6Sandifunction p_get_parsermodes(){ 459107b01d6Sandi global $conf; 460107b01d6Sandi 461107b01d6Sandi //reuse old data 462107b01d6Sandi static $modes = null; 463107b01d6Sandi if($modes != null){ 464107b01d6Sandi return $modes; 465107b01d6Sandi } 466107b01d6Sandi 467107b01d6Sandi //import parser classes and mode definitions 468107b01d6Sandi require_once DOKU_INC . 'inc/parser/parser.php'; 469107b01d6Sandi 470107b01d6Sandi // we now collect all syntax modes and their objects, then they will 471107b01d6Sandi // be sorted and added to the parser in correct order 472107b01d6Sandi $modes = array(); 473107b01d6Sandi 474107b01d6Sandi // add syntax plugins 475107b01d6Sandi $pluginlist = plugin_list('syntax'); 476107b01d6Sandi if(count($pluginlist)){ 477107b01d6Sandi global $PARSER_MODES; 478107b01d6Sandi $obj = null; 479107b01d6Sandi foreach($pluginlist as $p){ 480c90b2fb1Schris if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 481107b01d6Sandi $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 482107b01d6Sandi //add to modes 483107b01d6Sandi $modes[] = array( 484107b01d6Sandi 'sort' => $obj->getSort(), 485107b01d6Sandi 'mode' => "plugin_$p", 486107b01d6Sandi 'obj' => $obj, 487107b01d6Sandi ); 488a46d0d65SAndreas Gohr unset($obj); //remove the reference 489107b01d6Sandi } 490107b01d6Sandi } 491107b01d6Sandi 492107b01d6Sandi // add default modes 493107b01d6Sandi $std_modes = array('listblock','preformatted','notoc','nocache', 494107b01d6Sandi 'header','table','linebreak','footnote','hr', 495107b01d6Sandi 'unformatted','php','html','code','file','quote', 496e77ea1bcSAndreas Gohr 'internallink','rss','media','externallink', 497e77ea1bcSAndreas Gohr 'emaillink','windowssharelink','eol'); 498e77ea1bcSAndreas Gohr if($conf['typography']){ 499e77ea1bcSAndreas Gohr $std_modes[] = 'quotes'; 500e77ea1bcSAndreas Gohr $std_modes[] = 'multiplyentity'; 501e77ea1bcSAndreas Gohr } 502107b01d6Sandi foreach($std_modes as $m){ 503107b01d6Sandi $class = "Doku_Parser_Mode_$m"; 504107b01d6Sandi $obj = new $class(); 505107b01d6Sandi $modes[] = array( 506107b01d6Sandi 'sort' => $obj->getSort(), 507107b01d6Sandi 'mode' => $m, 508107b01d6Sandi 'obj' => $obj 509107b01d6Sandi ); 510107b01d6Sandi } 511107b01d6Sandi 512107b01d6Sandi // add formatting modes 513107b01d6Sandi $fmt_modes = array('strong','emphasis','underline','monospace', 514107b01d6Sandi 'subscript','superscript','deleted'); 515107b01d6Sandi foreach($fmt_modes as $m){ 516107b01d6Sandi $obj = new Doku_Parser_Mode_formatting($m); 517107b01d6Sandi $modes[] = array( 518107b01d6Sandi 'sort' => $obj->getSort(), 519107b01d6Sandi 'mode' => $m, 520107b01d6Sandi 'obj' => $obj 521107b01d6Sandi ); 522107b01d6Sandi } 523107b01d6Sandi 524107b01d6Sandi // add modes which need files 525107b01d6Sandi $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys())); 526107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 527107b01d6Sandi $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms())); 528107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 529107b01d6Sandi $obj = new Doku_Parser_Mode_entity(array_keys(getEntities())); 530107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 531107b01d6Sandi 532107b01d6Sandi // add optional camelcase mode 533107b01d6Sandi if($conf['camelcase']){ 534107b01d6Sandi $obj = new Doku_Parser_Mode_camelcaselink(); 535107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 536107b01d6Sandi } 537107b01d6Sandi 538107b01d6Sandi //sort modes 539107b01d6Sandi usort($modes,'p_sort_modes'); 540107b01d6Sandi 541107b01d6Sandi return $modes; 542107b01d6Sandi} 543107b01d6Sandi 544107b01d6Sandi/** 545107b01d6Sandi * Callback function for usort 546107b01d6Sandi * 547107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 548107b01d6Sandi */ 549107b01d6Sandifunction p_sort_modes($a, $b){ 550107b01d6Sandi if($a['sort'] == $b['sort']) return 0; 551107b01d6Sandi return ($a['sort'] < $b['sort']) ? -1 : 1; 552107b01d6Sandi} 553107b01d6Sandi 554107b01d6Sandi/** 555ac83b9d8Sandi * Renders a list of instruction to the specified output mode 556c112d578Sandi * 5577a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned 5589dc2c2afSandi * 559c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 560c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 561c112d578Sandi */ 5629dc2c2afSandifunction p_render($mode,$instructions,&$info){ 563c112d578Sandi if(is_null($instructions)) return ''; 564c112d578Sandi 565d968d3e5SChris Smith $Renderer =& p_get_renderer($mode); 566d968d3e5SChris Smith if (is_null($Renderer)) return null; 567c327d6c4SAndreas Gohr 568d968d3e5SChris Smith $Renderer->reset(); 569c112d578Sandi 570c112d578Sandi $Renderer->smileys = getSmileys(); 571c112d578Sandi $Renderer->entities = getEntities(); 572c112d578Sandi $Renderer->acronyms = getAcronyms(); 573c112d578Sandi $Renderer->interwiki = getInterwiki(); 574c112d578Sandi 575c112d578Sandi // Loop through the instructions 576c112d578Sandi foreach ( $instructions as $instruction ) { 577c112d578Sandi // Execute the callback against the Renderer 578c112d578Sandi call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]); 579c112d578Sandi } 5809dc2c2afSandi 5819dc2c2afSandi //set info array 5829dc2c2afSandi $info = $Renderer->info; 5839dc2c2afSandi 584677844afSchris // Post process and return the output 585677844afSchris $data = array($mode,& $Renderer->doc); 586677844afSchris trigger_event('RENDERER_CONTENT_POSTPROCESS',$data); 587c112d578Sandi return $Renderer->doc; 588c112d578Sandi} 589c112d578Sandi 590d968d3e5SChris Smithfunction & p_get_renderer($mode) { 5917aea91afSChris Smith global $conf, $plugin_controller; 592d968d3e5SChris Smith 593d968d3e5SChris Smith $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode; 594d968d3e5SChris Smith 595d968d3e5SChris Smith // try default renderer first: 596d968d3e5SChris Smith $file = DOKU_INC."inc/parser/$rname.php"; 597d968d3e5SChris Smith if(@file_exists($file)){ 598d968d3e5SChris Smith require_once $file; 599d968d3e5SChris Smith $rclass = "Doku_Renderer_$rname"; 600d968d3e5SChris Smith 601d968d3e5SChris Smith if ( !class_exists($rclass) ) { 602d968d3e5SChris Smith trigger_error("Unable to resolve render class $rclass",E_USER_WARNING); 603d968d3e5SChris Smith msg("Renderer '$rname' for $mode not valid",-1); 604d968d3e5SChris Smith return null; 605d968d3e5SChris Smith } 60667f9913dSAndreas Gohr $Renderer = new $rclass(); 607d968d3e5SChris Smith }else{ 6087b27382cSGina Haeussge // Maybe a plugin/component is available? 6097b27382cSGina Haeussge list($plugin, $component) = $plugin_controller->_splitName($rname); 6107b27382cSGina Haeussge if (!$plugin_controller->isdisabled($plugin)){ 611f6ec8df8SAdrian Lang $Renderer =& $plugin_controller->load('renderer',$rname); 6127aea91afSChris Smith } 6137aea91afSChris Smith 614d968d3e5SChris Smith if(is_null($Renderer)){ 615d968d3e5SChris Smith msg("No renderer '$rname' found for mode '$mode'",-1); 616d968d3e5SChris Smith return null; 617d968d3e5SChris Smith } 618d968d3e5SChris Smith } 619d968d3e5SChris Smith 620d968d3e5SChris Smith return $Renderer; 621d968d3e5SChris Smith} 622d968d3e5SChris Smith 623bb0a59d4Sjan/** 624bb0a59d4Sjan * Gets the first heading from a file 625bb0a59d4Sjan * 626fc18c0fbSchris * @param string $id dokuwiki page id 627fc18c0fbSchris * @param bool $render rerender if first heading not known 6280770c0e5SChris Smith * default: true -- must be set to false for calls from the metadata renderer to 6290770c0e5SChris Smith * protects against loops and excessive resource usage when pages 6300770c0e5SChris Smith * for which only a first heading is required will attempt to 6310770c0e5SChris Smith * render metadata for all the pages for which they require first 6320770c0e5SChris Smith * headings ... and so on. 633fc18c0fbSchris * 63495dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 635bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de> 636bb0a59d4Sjan */ 6370770c0e5SChris Smithfunction p_get_first_heading($id, $render=true){ 638bf0c93c2SMichael Hamann // counter how many titles have been requested using p_get_metadata 639*ff725173SMichael Hamann static $count = 1; 640bf0c93c2SMichael Hamann // the index of all titles, only loaded when many titles are requested 641bf0c93c2SMichael Hamann static $title_index = null; 642bf0c93c2SMichael Hamann // cache for titles requested using p_get_metadata 643bf0c93c2SMichael Hamann static $title_cache = array(); 644bf0c93c2SMichael Hamann 645bf0c93c2SMichael Hamann $id = cleanID($id); 646bf0c93c2SMichael Hamann 647bf0c93c2SMichael Hamann // check if this title has already been requested 648bf0c93c2SMichael Hamann if (isset($title_cache[$id])) 649bf0c93c2SMichael Hamann return $title_cache[$id]; 650bf0c93c2SMichael Hamann 651bf0c93c2SMichael Hamann // check if already too many titles have been requested and probably 652bf0c93c2SMichael Hamann // using the title index is better 653*ff725173SMichael Hamann if ($count > P_GET_FIRST_HEADING_METADATA_LIMIT) { 654bf0c93c2SMichael Hamann if (is_null($title_index)) { 655bf0c93c2SMichael Hamann $pages = array_map('rtrim', idx_getIndex('page', '')); 656bf0c93c2SMichael Hamann $titles = array_map('rtrim', idx_getIndex('title', '')); 657bf0c93c2SMichael Hamann // check for corrupt title index #FS2076 658bf0c93c2SMichael Hamann if(count($pages) != count($titles)){ 659bf0c93c2SMichael Hamann $titles = array_fill(0,count($pages),''); 660bf0c93c2SMichael Hamann @unlink($conf['indexdir'].'/title.idx'); // will be rebuilt in inc/init.php 661bf0c93c2SMichael Hamann } 662bf0c93c2SMichael Hamann $title_index = array_combine($pages, $titles); 663bf0c93c2SMichael Hamann } 664bf0c93c2SMichael Hamann return $title_index[$id]; 665bf0c93c2SMichael Hamann } 666bf0c93c2SMichael Hamann 667bf0c93c2SMichael Hamann ++$count; 668bf0c93c2SMichael Hamann $title_cache[$id] = p_get_metadata($id,'title',$render); 669bf0c93c2SMichael Hamann return $title_cache[$id]; 670bb0a59d4Sjan} 671bb0a59d4Sjan 6728f7d700cSchris/** 6738f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output 6748f7d700cSchris * 6755d568b99SChris Smith * @param string $code source code to be highlighted 6765d568b99SChris Smith * @param string $language language to provide highlighting 6775d568b99SChris Smith * @param string $wrapper html element to wrap the returned highlighted text 6785d568b99SChris Smith * 6798f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk> 68035fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6818f7d700cSchris */ 6825d568b99SChris Smithfunction p_xhtml_cached_geshi($code, $language, $wrapper='pre') { 683f8121585SChris Smith global $conf, $config_cascade; 68435fbe9efSAndreas Gohr $language = strtolower($language); 6855d568b99SChris Smith 6865d568b99SChris Smith // remove any leading or trailing blank lines 6875d568b99SChris Smith $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code); 6885d568b99SChris Smith 6898f7d700cSchris $cache = getCacheName($language.$code,".code"); 69035fbe9efSAndreas Gohr $ctime = @filemtime($cache); 69135fbe9efSAndreas Gohr if($ctime && !$_REQUEST['purge'] && 692f8121585SChris Smith $ctime > filemtime(DOKU_INC.'inc/geshi.php') && // geshi changed 693f8121585SChris Smith $ctime > @filemtime(DOKU_INC.'inc/geshi/'.$language.'.php') && // language syntax definition changed 694f8121585SChris Smith $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed 6958f7d700cSchris $highlighted_code = io_readFile($cache, false); 6968f7d700cSchris 6978f7d700cSchris } else { 6988f7d700cSchris 69935fbe9efSAndreas Gohr $geshi = new GeSHi($code, $language, DOKU_INC . 'inc/geshi'); 7008f7d700cSchris $geshi->set_encoding('utf-8'); 7018f7d700cSchris $geshi->enable_classes(); 7028f7d700cSchris $geshi->set_header_type(GESHI_HEADER_PRE); 7038f7d700cSchris $geshi->set_link_target($conf['target']['extern']); 7048f7d700cSchris 7055d568b99SChris Smith // remove GeSHi's wrapper element (we'll replace it with our own later) 7065d568b99SChris Smith // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 70769ddc332SAnika Henke $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r"); 7088f7d700cSchris io_saveFile($cache,$highlighted_code); 7098f7d700cSchris } 7108f7d700cSchris 7115d568b99SChris Smith // add a wrapper element if required 7125d568b99SChris Smith if ($wrapper) { 7135d568b99SChris Smith return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 7145d568b99SChris Smith } else { 7158f7d700cSchris return $highlighted_code; 7168f7d700cSchris } 7175d568b99SChris Smith} 7188f7d700cSchris 719