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 100db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 110db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 12e1d9dcc8SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 133a7140a1SAndreas Gohruse dokuwiki\Extension\PluginController; 14e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 15d4d8fb18SAndreas Gohruse dokuwiki\Parsing\Parser; 16d4d8fb18SAndreas Gohr 17c112d578Sandi/** 1865aa8490SMichael Hamann * How many pages shall be rendered for getting metadata during one request 1965aa8490SMichael Hamann * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED 2065aa8490SMichael Hamann * is passed as render parameter to p_get_metadata. 21ff725173SMichael Hamann */ 2265aa8490SMichael Hamannif (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5); 2367c15eceSMichael Hamann 2467c15eceSMichael Hamann/** Don't render metadata even if it is outdated or doesn't exist */ 2567c15eceSMichael Hamanndefine('METADATA_DONT_RENDER', 0); 2665aa8490SMichael Hamann/** 2765aa8490SMichael Hamann * Render metadata when the page is really newer or the metadata doesn't exist. 2865aa8490SMichael Hamann * Uses just a simple check, but should work pretty well for loading simple 2965aa8490SMichael Hamann * metadata values like the page title and avoids rendering a lot of pages in 3065aa8490SMichael Hamann * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode. 3165aa8490SMichael Hamann * Use this if it is unlikely that the metadata value you are requesting 3265aa8490SMichael Hamann * does depend e.g. on pages that are included in the current page using 3365aa8490SMichael Hamann * the include plugin (this is very likely the case for the page title, but 3465aa8490SMichael Hamann * not for relation references). 3565aa8490SMichael Hamann */ 3667c15eceSMichael Hamanndefine('METADATA_RENDER_USING_SIMPLE_CACHE', 1); 3765aa8490SMichael Hamann/** 3865aa8490SMichael Hamann * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT 3965aa8490SMichael Hamann * is used in this mode. Use this mode when you are requesting more complex 4065aa8490SMichael Hamann * metadata. Although this will cause rendering more often it might actually have 4165aa8490SMichael Hamann * the effect that less current metadata is returned as it is more likely than in 4265aa8490SMichael Hamann * the simple cache mode that metadata needs to be rendered for all pages at once 4365aa8490SMichael Hamann * which means that when the metadata for the page is requested that actually needs 4465aa8490SMichael Hamann * to be updated the limit might have been reached already. 4565aa8490SMichael Hamann */ 4667c15eceSMichael Hamanndefine('METADATA_RENDER_USING_CACHE', 2); 4765aa8490SMichael Hamann/** 4865aa8490SMichael Hamann * Render metadata without limiting the number of pages for which metadata is 4965aa8490SMichael Hamann * rendered. Use this mode with care, normally it should only be used in places 5065aa8490SMichael Hamann * like the indexer or in cli scripts where the execution time normally isn't 5165aa8490SMichael Hamann * limited. This can be combined with the simple cache using 5265aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED. 5365aa8490SMichael Hamann */ 5465aa8490SMichael Hamanndefine('METADATA_RENDER_UNLIMITED', 4); 55ff725173SMichael Hamann 56ff725173SMichael Hamann/** 57c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision. 58c112d578Sandi * 59c112d578Sandi * If $excuse is true an explanation is returned if the file 60c112d578Sandi * wasn't found 61c112d578Sandi * 62c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 6342ea7f44SGerrit Uitslag * 6442ea7f44SGerrit Uitslag * @param string $id page id 6542ea7f44SGerrit Uitslag * @param string|int $rev revision timestamp or empty string 6642ea7f44SGerrit Uitslag * @param bool $excuse 67f50a239bSTakamura * @param string $date_at 68f50a239bSTakamura * 6942ea7f44SGerrit Uitslag * @return null|string 70c112d578Sandi */ 715c2eed9aSlispsfunction p_wiki_xhtml($id, $rev='', $excuse=true,$date_at=''){ 72c112d578Sandi $file = wikiFN($id,$rev); 73c112d578Sandi $ret = ''; 74c112d578Sandi 75c112d578Sandi //ensure $id is in global $ID (needed for parsing) 761e76272cSandi global $ID; 773ff8773bSAndreas Gohr $keep = $ID; 781e76272cSandi $ID = $id; 79c112d578Sandi 805c2eed9aSlisps if($rev || $date_at){ 8179e79377SAndreas Gohr if(file_exists($file)){ 8264159a61SAndreas Gohr //no caching on old revisions 8364159a61SAndreas Gohr $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info,$date_at); 84c112d578Sandi }elseif($excuse){ 85c112d578Sandi $ret = p_locale_xhtml('norev'); 86c112d578Sandi } 87c112d578Sandi }else{ 8879e79377SAndreas Gohr if(file_exists($file)){ 894b5f4f4eSchris $ret = p_cached_output($file,'xhtml',$id); 90c112d578Sandi }elseif($excuse){ 912eaa0567Speterfromearth //check if the page once existed 92e1d9dcc8SAndreas Gohr $changelog = new PageChangeLog($id); 932eaa0567Speterfromearth if($changelog->hasRevisions()) { 942eaa0567Speterfromearth $ret = p_locale_xhtml('onceexisted'); 952eaa0567Speterfromearth } else { 96c112d578Sandi $ret = p_locale_xhtml('newpage'); 97c112d578Sandi } 98c112d578Sandi } 992eaa0567Speterfromearth } 100c112d578Sandi 1013ff8773bSAndreas Gohr //restore ID (just in case) 1023ff8773bSAndreas Gohr $ID = $keep; 1033ff8773bSAndreas Gohr 104c112d578Sandi return $ret; 105c112d578Sandi} 106c112d578Sandi 107c112d578Sandi/** 108c112d578Sandi * Returns the specified local text in parsed format 109c112d578Sandi * 110c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 11142ea7f44SGerrit Uitslag * 11242ea7f44SGerrit Uitslag * @param string $id page id 11342ea7f44SGerrit Uitslag * @return null|string 114c112d578Sandi */ 115c112d578Sandifunction p_locale_xhtml($id){ 116c112d578Sandi //fetch parsed locale 11765f6b58fSAnna Dabrowska $data = ['id' => $id, 'html' => '']; 11865f6b58fSAnna Dabrowska 11965f6b58fSAnna Dabrowska $event = new Event('PARSER_LOCALE_XHTML', $data); 12065f6b58fSAnna Dabrowska if ($event->advise_before()) { 12165f6b58fSAnna Dabrowska $data['html'] = p_cached_output(localeFN($data['id'])); 12265f6b58fSAnna Dabrowska } 12365f6b58fSAnna Dabrowska $event->advise_after(); 12465f6b58fSAnna Dabrowska 12565f6b58fSAnna Dabrowska return $data['html']; 126c112d578Sandi} 127c112d578Sandi 128c112d578Sandi/** 1294b5f4f4eSchris * Returns the given file parsed into the requested output format 1304b5f4f4eSchris * 1314b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org> 1324b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk> 13342ea7f44SGerrit Uitslag * 13442ea7f44SGerrit Uitslag * @param string $file filename, path to file 13542ea7f44SGerrit Uitslag * @param string $format 13642ea7f44SGerrit Uitslag * @param string $id page id 13742ea7f44SGerrit Uitslag * @return null|string 1384b5f4f4eSchris */ 1394b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') { 140c112d578Sandi global $conf; 141c112d578Sandi 1420db5771eSMichael Große $cache = new CacheRenderer($id, $file, $format); 1434b5f4f4eSchris if ($cache->useCache()) { 14485767031SAndreas Gohr $parsed = $cache->retrieveCache(false); 1457de86af9SGerrit Uitslag if($conf['allowdebug'] && $format=='xhtml') { 1467de86af9SGerrit Uitslag $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 1477de86af9SGerrit Uitslag } 148c112d578Sandi } else { 1494b5f4f4eSchris $parsed = p_render($format, p_cached_instructions($file,false,$id), $info); 150c112d578Sandi 151*abca2f79SEduardo Mozart de Oliveira if (!empty($info['cache']) && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile 1527de86af9SGerrit Uitslag if($conf['allowdebug'] && $format=='xhtml') { 1537de86af9SGerrit Uitslag $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 1547de86af9SGerrit Uitslag } 155c112d578Sandi }else{ 1564b5f4f4eSchris $cache->removeCache(); //try to delete cachefile 1577de86af9SGerrit Uitslag if($conf['allowdebug'] && $format=='xhtml') { 1587de86af9SGerrit Uitslag $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 1597de86af9SGerrit Uitslag } 160c112d578Sandi } 161c112d578Sandi } 162c112d578Sandi 163c112d578Sandi return $parsed; 164c112d578Sandi} 165c112d578Sandi 166c112d578Sandi/** 167c112d578Sandi * Returns the render instructions for a file 168c112d578Sandi * 169c112d578Sandi * Uses and creates a serialized cache file 170c112d578Sandi * 171c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 17242ea7f44SGerrit Uitslag * 17342ea7f44SGerrit Uitslag * @param string $file filename, path to file 17442ea7f44SGerrit Uitslag * @param bool $cacheonly 17542ea7f44SGerrit Uitslag * @param string $id page id 17642ea7f44SGerrit Uitslag * @return array|null 177c112d578Sandi */ 1784b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') { 17947b2d319SAndreas Gohr static $run = null; 18047b2d319SAndreas Gohr if(is_null($run)) $run = array(); 181c112d578Sandi 1820db5771eSMichael Große $cache = new CacheInstructions($id, $file); 183c112d578Sandi 1840d24b616SMichael Hamann if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) { 1854b5f4f4eSchris return $cache->retrieveCache(); 18679e79377SAndreas Gohr } else if (file_exists($file)) { 187c112d578Sandi // no cache - do some work 188bde4e341SGalaxyMaster $ins = p_get_instructions(io_readWikiPage($file,$id)); 189cbaf4259SChris Smith if ($cache->storeCache($ins)) { 19047b2d319SAndreas Gohr $run[$file] = true; // we won't rebuild these instructions in the same run again 191cbaf4259SChris Smith } else { 192cbaf4259SChris Smith msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1); 193cbaf4259SChris Smith } 194c112d578Sandi return $ins; 195c112d578Sandi } 196c112d578Sandi 1973b3f8916SAndreas Gohr return null; 198c112d578Sandi} 199c112d578Sandi 200c112d578Sandi/** 201c112d578Sandi * turns a page into a list of instructions 202c112d578Sandi * 203c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 204c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 20542ea7f44SGerrit Uitslag * 20699ba9fe6SAndreas Gohr * @param string $text raw wiki syntax text 20799ba9fe6SAndreas Gohr * @return array a list of instruction arrays 208c112d578Sandi */ 2096bbae538Sandifunction p_get_instructions($text){ 210c112d578Sandi 211107b01d6Sandi $modes = p_get_parsermodes(); 212ee20e7d1Sandi 21347f73ecfSAndreas Gohr // Create the parser and handler 214d4d8fb18SAndreas Gohr $Parser = new Parser(new Doku_Handler()); 215c112d578Sandi 216107b01d6Sandi //add modes to parser 217107b01d6Sandi foreach($modes as $mode){ 218107b01d6Sandi $Parser->addMode($mode['mode'],$mode['obj']); 219c112d578Sandi } 220c112d578Sandi 221c112d578Sandi // Do the parsing 222cbb44eabSAndreas Gohr Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text); 223a2d649c4Sandi $p = $Parser->parse($text); 224ee20e7d1Sandi // dbg($p); 225a2d649c4Sandi return $p; 226c112d578Sandi} 227c112d578Sandi 228c112d578Sandi/** 22939a89382SEsther Brunner * returns the metadata of a page 23039a89382SEsther Brunner * 2314a819402SMichael Hamann * @param string $id The id of the page the metadata should be returned from 23264159a61SAndreas Gohr * @param string $key The key of the metdata value that shall be read (by default everything) 23364159a61SAndreas Gohr * separate hierarchies by " " like "date created" 23467c15eceSMichael Hamann * @param int $render If the page should be rendererd - possible values: 23565aa8490SMichael Hamann * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE 23665aa8490SMichael Hamann * METADATA_RENDER_UNLIMITED (also combined with the previous two options), 23765aa8490SMichael Hamann * default: METADATA_RENDER_USING_CACHE 2384a819402SMichael Hamann * @return mixed The requested metadata fields 2394a819402SMichael Hamann * 24039a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 24198214867SMichael Hamann * @author Michael Hamann <michael@content-space.de> 24239a89382SEsther Brunner */ 24367c15eceSMichael Hamannfunction p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){ 2441172f8dcSAdrian Lang global $ID; 24565aa8490SMichael Hamann static $render_count = 0; 24665aa8490SMichael Hamann // track pages that have already been rendered in order to avoid rendering the same page 24765aa8490SMichael Hamann // again 24865aa8490SMichael Hamann static $rendered_pages = array(); 2496afe8dcaSchris 2500a7e3bceSchris // cache the current page 2510a7e3bceSchris // Benchmarking shows the current page's metadata is generally the only page metadata 2520a7e3bceSchris // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 2530a7e3bceSchris $cache = ($ID == $id); 2540a7e3bceSchris $meta = p_read_metadata($id, $cache); 25539a89382SEsther Brunner 25667c15eceSMichael Hamann if (!is_numeric($render)) { 25767c15eceSMichael Hamann if ($render) { 25867c15eceSMichael Hamann $render = METADATA_RENDER_USING_SIMPLE_CACHE; 25967c15eceSMichael Hamann } else { 26067c15eceSMichael Hamann $render = METADATA_DONT_RENDER; 26167c15eceSMichael Hamann } 26267c15eceSMichael Hamann } 26367c15eceSMichael Hamann 26498214867SMichael Hamann // prevent recursive calls in the cache 26598214867SMichael Hamann static $recursion = false; 26665aa8490SMichael Hamann if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){ 26798214867SMichael Hamann $recursion = true; 26898214867SMichael Hamann 2690db5771eSMichael Große $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata'); 27098214867SMichael Hamann 27167c15eceSMichael Hamann $do_render = false; 27265aa8490SMichael Hamann if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) { 27365aa8490SMichael Hamann if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) { 27467c15eceSMichael Hamann $pagefn = wikiFN($id); 27567c15eceSMichael Hamann $metafn = metaFN($id, '.meta'); 27679e79377SAndreas Gohr if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) { 27767c15eceSMichael Hamann $do_render = true; 27867c15eceSMichael Hamann } 27967c15eceSMichael Hamann } elseif (!$cachefile->useCache()){ 28067c15eceSMichael Hamann $do_render = true; 28167c15eceSMichael Hamann } 28265aa8490SMichael Hamann } 28367c15eceSMichael Hamann if ($do_render) { 2840d24b616SMichael Hamann if (!defined('DOKU_UNITTEST')) { 28565aa8490SMichael Hamann ++$render_count; 28665aa8490SMichael Hamann $rendered_pages[$id] = true; 2870d24b616SMichael Hamann } 28869ba640bSMichael Hamann $old_meta = $meta; 28939a89382SEsther Brunner $meta = p_render_metadata($id, $meta); 29069ba640bSMichael Hamann // only update the file when the metadata has been changed 29169ba640bSMichael Hamann if ($meta == $old_meta || p_save_metadata($id, $meta)) { 29298214867SMichael Hamann // store a timestamp in order to make sure that the cachefile is touched 2933d6feb16SMichael Hamann // this timestamp is also stored when the meta data is still the same 29498214867SMichael Hamann $cachefile->storeCache(time()); 2953d6feb16SMichael Hamann } else { 29698214867SMichael Hamann msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1); 29798214867SMichael Hamann } 29898214867SMichael Hamann } 29998214867SMichael Hamann 30098214867SMichael Hamann $recursion = false; 3016afe8dcaSchris } 30239a89382SEsther Brunner 303ebf65d37SAdrian Lang $val = $meta['current']; 304ebf65d37SAdrian Lang 30539a89382SEsther Brunner // filter by $key 306569a0019SAdrian Lang foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 307ebf65d37SAdrian Lang if (!isset($val[$cur_key])) { 308ebf65d37SAdrian Lang return null; 30966d29756SChris Smith } 310ebf65d37SAdrian Lang $val = $val[$cur_key]; 31139a89382SEsther Brunner } 312ebf65d37SAdrian Lang return $val; 31339a89382SEsther Brunner} 31439a89382SEsther Brunner 31539a89382SEsther Brunner/** 31639a89382SEsther Brunner * sets metadata elements of a page 31739a89382SEsther Brunner * 318a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 319a365baeeSDominik Eckelmann * 320e1d9dcc8SAndreas Gohr * @param string $id is the ID of a wiki page 321e1d9dcc8SAndreas Gohr * @param array $data is an array with key ⇒ value pairs to be set in the metadata 322e1d9dcc8SAndreas Gohr * @param boolean $render whether or not the page metadata should be generated with the renderer 323e1d9dcc8SAndreas Gohr * @param boolean $persistent indicates whether or not the particular metadata value will persist through 324a365baeeSDominik Eckelmann * the next metadata rendering. 325a365baeeSDominik Eckelmann * @return boolean true on success 326a365baeeSDominik Eckelmann * 32739a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 3280e5fde48SMichael Hamann * @author Michael Hamann <michael@content-space.de> 32939a89382SEsther Brunner */ 3300a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){ 33139a89382SEsther Brunner if (!is_array($data)) return false; 33239a89382SEsther Brunner 3330e5fde48SMichael Hamann global $ID, $METADATA_RENDERERS; 3340a7e3bceSchris 3350e5fde48SMichael Hamann // if there is currently a renderer change the data in the renderer instead 3360e5fde48SMichael Hamann if (isset($METADATA_RENDERERS[$id])) { 3370e5fde48SMichael Hamann $orig =& $METADATA_RENDERERS[$id]; 3380e5fde48SMichael Hamann $meta = $orig; 3390e5fde48SMichael Hamann } else { 3400a7e3bceSchris // cache the current page 3410a7e3bceSchris $cache = ($ID == $id); 3420a7e3bceSchris $orig = p_read_metadata($id, $cache); 34339a89382SEsther Brunner 34439a89382SEsther Brunner // render metadata first? 3450a7e3bceSchris $meta = $render ? p_render_metadata($id, $orig) : $orig; 3460e5fde48SMichael Hamann } 34739a89382SEsther Brunner 34839a89382SEsther Brunner // now add the passed metadata 34939a89382SEsther Brunner $protected = array('description', 'date', 'contributor'); 35039a89382SEsther Brunner foreach ($data as $key => $value){ 35139a89382SEsther Brunner 35239a89382SEsther Brunner // be careful with sub-arrays of $meta['relation'] 35339a89382SEsther Brunner if ($key == 'relation'){ 3540a7e3bceSchris 35539a89382SEsther Brunner foreach ($value as $subkey => $subvalue){ 35631b10b49SMichael Hamann if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) { 3572d102494SPhy $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue); 35831b10b49SMichael Hamann } else { 35931b10b49SMichael Hamann $meta['current'][$key][$subkey] = $subvalue; 36031b10b49SMichael Hamann } 36131b10b49SMichael Hamann if($persistent) { 36231b10b49SMichael Hamann if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) { 36364159a61SAndreas Gohr $meta['persistent'][$key][$subkey] = array_replace( 36464159a61SAndreas Gohr $meta['persistent'][$key][$subkey], 36564159a61SAndreas Gohr (array) $subvalue 36664159a61SAndreas Gohr ); 36731b10b49SMichael Hamann } else { 36831b10b49SMichael Hamann $meta['persistent'][$key][$subkey] = $subvalue; 36931b10b49SMichael Hamann } 37031b10b49SMichael Hamann } 37139a89382SEsther Brunner } 37239a89382SEsther Brunner 37339a89382SEsther Brunner // be careful with some senisitive arrays of $meta 37439a89382SEsther Brunner } elseif (in_array($key, $protected)){ 3750a7e3bceSchris 37666d29756SChris Smith // these keys, must have subkeys - a legitimate value must be an array 37739a89382SEsther Brunner if (is_array($value)) { 37864159a61SAndreas Gohr $meta['current'][$key] = !empty($meta['current'][$key]) ? 37964159a61SAndreas Gohr array_replace((array)$meta['current'][$key],$value) : 38064159a61SAndreas Gohr $value; 3810a7e3bceSchris 3820a7e3bceSchris if ($persistent) { 38364159a61SAndreas Gohr $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? 38464159a61SAndreas Gohr array_replace((array)$meta['persistent'][$key],$value) : 38564159a61SAndreas Gohr $value; 3860a7e3bceSchris } 38739a89382SEsther Brunner } 38839a89382SEsther Brunner 38939a89382SEsther Brunner // no special treatment for the rest 39039a89382SEsther Brunner } else { 3910a7e3bceSchris $meta['current'][$key] = $value; 3920a7e3bceSchris if ($persistent) $meta['persistent'][$key] = $value; 39339a89382SEsther Brunner } 39439a89382SEsther Brunner } 39539a89382SEsther Brunner 39639a89382SEsther Brunner // save only if metadata changed 39739a89382SEsther Brunner if ($meta == $orig) return true; 3986afe8dcaSchris 3990e5fde48SMichael Hamann if (isset($METADATA_RENDERERS[$id])) { 4000e5fde48SMichael Hamann // set both keys individually as the renderer has references to the individual keys 4010e5fde48SMichael Hamann $METADATA_RENDERERS[$id]['current'] = $meta['current']; 4020e5fde48SMichael Hamann $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; 4031c56be7bSMichael Hamann return true; 4040e5fde48SMichael Hamann } else { 4051172f8dcSAdrian Lang return p_save_metadata($id, $meta); 40639a89382SEsther Brunner } 4070e5fde48SMichael Hamann} 40839a89382SEsther Brunner 40939a89382SEsther Brunner/** 4103d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data 4113d1f9ec3SMichael Klier * used on page deletion 4123d1f9ec3SMichael Klier * 4133d1f9ec3SMichael Klier * @author Michael Klier <chi@chimeric.de> 41442ea7f44SGerrit Uitslag * 41542ea7f44SGerrit Uitslag * @param string $id page id 41642ea7f44SGerrit Uitslag * @return bool success / fail 4173d1f9ec3SMichael Klier */ 4183d1f9ec3SMichael Klierfunction p_purge_metadata($id) { 4193d1f9ec3SMichael Klier $meta = p_read_metadata($id); 4203d1f9ec3SMichael Klier foreach($meta['current'] as $key => $value) { 421056bf31fSDamien Regad if(isset($meta[$key]) && is_array($meta[$key])) { 4223d1f9ec3SMichael Klier $meta['current'][$key] = array(); 4233d1f9ec3SMichael Klier } else { 4243d1f9ec3SMichael Klier $meta['current'][$key] = ''; 4253d1f9ec3SMichael Klier } 4261172f8dcSAdrian Lang 4273d1f9ec3SMichael Klier } 4281172f8dcSAdrian Lang return p_save_metadata($id, $meta); 4293d1f9ec3SMichael Klier} 4303d1f9ec3SMichael Klier 4313d1f9ec3SMichael Klier/** 4320a7e3bceSchris * read the metadata from source/cache for $id 4330a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata) 4340a7e3bceSchris * 4350a7e3bceSchris * @author Christopher Smith <chris@jalakai.co.uk> 4360a7e3bceSchris * 4370a7e3bceSchris * @param string $id absolute wiki page id 4380a7e3bceSchris * @param bool $cache whether or not to cache metadata in memory 4390a7e3bceSchris * (only use for metadata likely to be accessed several times) 4400a7e3bceSchris * 4410a7e3bceSchris * @return array metadata 4420a7e3bceSchris */ 4430a7e3bceSchrisfunction p_read_metadata($id,$cache=false) { 4440a7e3bceSchris global $cache_metadata; 4450a7e3bceSchris 4463a50618cSgweissbach if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 4470a7e3bceSchris 4480a7e3bceSchris $file = metaFN($id, '.meta'); 44964159a61SAndreas Gohr $meta = file_exists($file) ? 45064159a61SAndreas Gohr unserialize(io_readFile($file, false)) : 45164159a61SAndreas Gohr array('current'=>array(),'persistent'=>array()); 4520a7e3bceSchris 4530a7e3bceSchris if ($cache) { 4543a50618cSgweissbach $cache_metadata[(string)$id] = $meta; 4550a7e3bceSchris } 4560a7e3bceSchris 4570a7e3bceSchris return $meta; 4580a7e3bceSchris} 4590a7e3bceSchris 4600a7e3bceSchris/** 4611172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file 4621172f8dcSAdrian Lang * 4631172f8dcSAdrian Lang * @param string $id absolute wiki page id 4641172f8dcSAdrian Lang * @param array $meta metadata 4651172f8dcSAdrian Lang * 4661172f8dcSAdrian Lang * @return bool success / fail 4671172f8dcSAdrian Lang */ 4681172f8dcSAdrian Langfunction p_save_metadata($id, $meta) { 4691172f8dcSAdrian Lang // sync cached copies, including $INFO metadata 4701172f8dcSAdrian Lang global $cache_metadata, $INFO; 4711172f8dcSAdrian Lang 4721172f8dcSAdrian Lang if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 473056bf31fSDamien Regad if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) { 474056bf31fSDamien Regad $INFO['meta'] = $meta['current']; 475056bf31fSDamien Regad } 4761172f8dcSAdrian Lang 4771172f8dcSAdrian Lang return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 4781172f8dcSAdrian Lang} 4791172f8dcSAdrian Lang 4801172f8dcSAdrian Lang/** 48139a89382SEsther Brunner * renders the metadata of a page 48239a89382SEsther Brunner * 48339a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 48442ea7f44SGerrit Uitslag * 48542ea7f44SGerrit Uitslag * @param string $id page id 48642ea7f44SGerrit Uitslag * @param array $orig the original metadata 48742ea7f44SGerrit Uitslag * @return array|null array('current'=> array,'persistent'=> array); 48839a89382SEsther Brunner */ 48939a89382SEsther Brunnerfunction p_render_metadata($id, $orig){ 49048924015SAndreas Gohr // make sure the correct ID is in global ID 4910e5fde48SMichael Hamann global $ID, $METADATA_RENDERERS; 4920e5fde48SMichael Hamann 4930e5fde48SMichael Hamann // avoid recursive rendering processes for the same id 4945b76ad91SChristopher Smith if (isset($METADATA_RENDERERS[$id])) { 4950e5fde48SMichael Hamann return $orig; 4965b76ad91SChristopher Smith } 4970e5fde48SMichael Hamann 4980e5fde48SMichael Hamann // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it 4990e5fde48SMichael Hamann $METADATA_RENDERERS[$id] =& $orig; 5000e5fde48SMichael Hamann 50148924015SAndreas Gohr $keep = $ID; 50248924015SAndreas Gohr $ID = $id; 50348924015SAndreas Gohr 5040a7e3bceSchris // add an extra key for the event - to tell event handlers the page whose metadata this is 5050a7e3bceSchris $orig['page'] = $id; 506e1d9dcc8SAndreas Gohr $evt = new Event('PARSER_METADATA_RENDER', $orig); 5070a7e3bceSchris if ($evt->advise_before()) { 5080a7e3bceSchris 50939a89382SEsther Brunner // get instructions 5104b5f4f4eSchris $instructions = p_cached_instructions(wikiFN($id),false,$id); 51148924015SAndreas Gohr if(is_null($instructions)){ 51248924015SAndreas Gohr $ID = $keep; 5130e5fde48SMichael Hamann unset($METADATA_RENDERERS[$id]); 51448924015SAndreas Gohr return null; // something went wrong with the instructions 51548924015SAndreas Gohr } 51639a89382SEsther Brunner 51739a89382SEsther Brunner // set up the renderer 51867f9913dSAndreas Gohr $renderer = new Doku_Renderer_metadata(); 5190e5fde48SMichael Hamann $renderer->meta =& $orig['current']; 5200e5fde48SMichael Hamann $renderer->persistent =& $orig['persistent']; 52139a89382SEsther Brunner 52239a89382SEsther Brunner // loop through the instructions 52339a89382SEsther Brunner foreach ($instructions as $instruction){ 52439a89382SEsther Brunner // execute the callback against the renderer 5253b748871SAndreas Gohr call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); 52639a89382SEsther Brunner } 52739a89382SEsther Brunner 5280e5fde48SMichael Hamann $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent); 5290a7e3bceSchris } 5300a7e3bceSchris $evt->advise_after(); 5310a7e3bceSchris 5320e5fde48SMichael Hamann // clean up 53348924015SAndreas Gohr $ID = $keep; 5340e5fde48SMichael Hamann unset($METADATA_RENDERERS[$id]); 5350a7e3bceSchris return $evt->result; 53639a89382SEsther Brunner} 53739a89382SEsther Brunner 53839a89382SEsther Brunner/** 539107b01d6Sandi * returns all available parser syntax modes in correct order 540107b01d6Sandi * 541107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 54242ea7f44SGerrit Uitslag * 54342ea7f44SGerrit Uitslag * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj' => plugin object) 544107b01d6Sandi */ 545107b01d6Sandifunction p_get_parsermodes(){ 546107b01d6Sandi global $conf; 547107b01d6Sandi 548107b01d6Sandi //reuse old data 549107b01d6Sandi static $modes = null; 5500d24b616SMichael Hamann if($modes != null && !defined('DOKU_UNITTEST')){ 551107b01d6Sandi return $modes; 552107b01d6Sandi } 553107b01d6Sandi 554107b01d6Sandi //import parser classes and mode definitions 555107b01d6Sandi require_once DOKU_INC . 'inc/parser/parser.php'; 556107b01d6Sandi 557107b01d6Sandi // we now collect all syntax modes and their objects, then they will 558107b01d6Sandi // be sorted and added to the parser in correct order 559107b01d6Sandi $modes = array(); 560107b01d6Sandi 561107b01d6Sandi // add syntax plugins 562107b01d6Sandi $pluginlist = plugin_list('syntax'); 563107b01d6Sandi if(count($pluginlist)){ 564107b01d6Sandi global $PARSER_MODES; 565107b01d6Sandi $obj = null; 566107b01d6Sandi foreach($pluginlist as $p){ 567e1d9dcc8SAndreas Gohr /** @var \dokuwiki\Extension\SyntaxPlugin $obj */ 568e8b5a4f9SAndreas Gohr if(!$obj = plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj 569107b01d6Sandi $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 570107b01d6Sandi //add to modes 571107b01d6Sandi $modes[] = array( 572107b01d6Sandi 'sort' => $obj->getSort(), 573107b01d6Sandi 'mode' => "plugin_$p", 574107b01d6Sandi 'obj' => $obj, 575107b01d6Sandi ); 576a46d0d65SAndreas Gohr unset($obj); //remove the reference 577107b01d6Sandi } 578107b01d6Sandi } 579107b01d6Sandi 580107b01d6Sandi // add default modes 581107b01d6Sandi $std_modes = array('listblock','preformatted','notoc','nocache', 582107b01d6Sandi 'header','table','linebreak','footnote','hr', 583bbe6b3a7SAndreas Gohr 'unformatted','code','file','quote', 584e77ea1bcSAndreas Gohr 'internallink','rss','media','externallink', 585e77ea1bcSAndreas Gohr 'emaillink','windowssharelink','eol'); 586e77ea1bcSAndreas Gohr if($conf['typography']){ 587e77ea1bcSAndreas Gohr $std_modes[] = 'quotes'; 588e77ea1bcSAndreas Gohr $std_modes[] = 'multiplyentity'; 589e77ea1bcSAndreas Gohr } 590107b01d6Sandi foreach($std_modes as $m){ 591be906b56SAndreas Gohr $class = 'dokuwiki\\Parsing\\ParserMode\\'.ucfirst($m); 592107b01d6Sandi $obj = new $class(); 593107b01d6Sandi $modes[] = array( 594107b01d6Sandi 'sort' => $obj->getSort(), 595107b01d6Sandi 'mode' => $m, 596107b01d6Sandi 'obj' => $obj 597107b01d6Sandi ); 598107b01d6Sandi } 599107b01d6Sandi 600107b01d6Sandi // add formatting modes 601107b01d6Sandi $fmt_modes = array('strong','emphasis','underline','monospace', 602107b01d6Sandi 'subscript','superscript','deleted'); 603107b01d6Sandi foreach($fmt_modes as $m){ 604be906b56SAndreas Gohr $obj = new \dokuwiki\Parsing\ParserMode\Formatting($m); 605107b01d6Sandi $modes[] = array( 606107b01d6Sandi 'sort' => $obj->getSort(), 607107b01d6Sandi 'mode' => $m, 608107b01d6Sandi 'obj' => $obj 609107b01d6Sandi ); 610107b01d6Sandi } 611107b01d6Sandi 612107b01d6Sandi // add modes which need files 613be906b56SAndreas Gohr $obj = new \dokuwiki\Parsing\ParserMode\Smiley(array_keys(getSmileys())); 614107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj ); 615be906b56SAndreas Gohr $obj = new \dokuwiki\Parsing\ParserMode\Acronym(array_keys(getAcronyms())); 616107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj ); 617be906b56SAndreas Gohr $obj = new \dokuwiki\Parsing\ParserMode\Entity(array_keys(getEntities())); 618107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj ); 619107b01d6Sandi 620107b01d6Sandi // add optional camelcase mode 621107b01d6Sandi if($conf['camelcase']){ 622be906b56SAndreas Gohr $obj = new \dokuwiki\Parsing\ParserMode\Camelcaselink(); 623107b01d6Sandi $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj ); 624107b01d6Sandi } 625107b01d6Sandi 626107b01d6Sandi //sort modes 627107b01d6Sandi usort($modes,'p_sort_modes'); 628107b01d6Sandi 629107b01d6Sandi return $modes; 630107b01d6Sandi} 631107b01d6Sandi 632107b01d6Sandi/** 633107b01d6Sandi * Callback function for usort 634107b01d6Sandi * 635107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 63642ea7f44SGerrit Uitslag * 63742ea7f44SGerrit Uitslag * @param array $a 63842ea7f44SGerrit Uitslag * @param array $b 63942ea7f44SGerrit Uitslag * @return int $a is lower/equal/higher than $b 640107b01d6Sandi */ 641107b01d6Sandifunction p_sort_modes($a, $b){ 642107b01d6Sandi if($a['sort'] == $b['sort']) return 0; 643107b01d6Sandi return ($a['sort'] < $b['sort']) ? -1 : 1; 644107b01d6Sandi} 645107b01d6Sandi 646107b01d6Sandi/** 647ac83b9d8Sandi * Renders a list of instruction to the specified output mode 648c112d578Sandi * 6497a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned 6509dc2c2afSandi * 651c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 652c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 65342ea7f44SGerrit Uitslag * 65442ea7f44SGerrit Uitslag * @param string $mode 655e3710957SGerrit Uitslag * @param array|null|false $instructions 65642ea7f44SGerrit Uitslag * @param array $info returns render info like enabled toc and cache 6577de86af9SGerrit Uitslag * @param string $date_at 65842ea7f44SGerrit Uitslag * @return null|string rendered output 659c112d578Sandi */ 6604bde2196Slispsfunction p_render($mode,$instructions,&$info,$date_at=''){ 661c112d578Sandi if(is_null($instructions)) return ''; 662e3710957SGerrit Uitslag if($instructions === false) return ''; 663c112d578Sandi 664252398f0SChristopher Smith $Renderer = p_get_renderer($mode); 665d968d3e5SChris Smith if (is_null($Renderer)) return null; 666c327d6c4SAndreas Gohr 667d968d3e5SChris Smith $Renderer->reset(); 668c112d578Sandi 6695c2eed9aSlisps if($date_at) { 6705c2eed9aSlisps $Renderer->date_at = $date_at; 6715c2eed9aSlisps } 6725c2eed9aSlisps 673c112d578Sandi $Renderer->smileys = getSmileys(); 674c112d578Sandi $Renderer->entities = getEntities(); 675c112d578Sandi $Renderer->acronyms = getAcronyms(); 676c112d578Sandi $Renderer->interwiki = getInterwiki(); 677c112d578Sandi 678c112d578Sandi // Loop through the instructions 679c112d578Sandi foreach ( $instructions as $instruction ) { 680c112d578Sandi // Execute the callback against the Renderer 6817c62086bSAndreas Gohr if(method_exists($Renderer, $instruction[0])){ 6826340dabcSAndreas Gohr call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array()); 683c112d578Sandi } 6847c62086bSAndreas Gohr } 6859dc2c2afSandi 6869dc2c2afSandi //set info array 6879dc2c2afSandi $info = $Renderer->info; 6889dc2c2afSandi 689677844afSchris // Post process and return the output 690677844afSchris $data = array($mode,& $Renderer->doc); 691cbb44eabSAndreas Gohr Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS',$data); 692c112d578Sandi return $Renderer->doc; 693c112d578Sandi} 694c112d578Sandi 695e3ab6fc5SMichael Hamann/** 696548d801fSChristopher Smith * Figure out the correct renderer class to use for $mode, 697548d801fSChristopher Smith * instantiate and return it 698548d801fSChristopher Smith * 6997e8500eeSGerrit Uitslag * @param string $mode Mode of the renderer to get 700e3ab6fc5SMichael Hamann * @return null|Doku_Renderer The renderer 701548d801fSChristopher Smith * 702548d801fSChristopher Smith * @author Christopher Smith <chris@jalakai.co.uk> 703e3ab6fc5SMichael Hamann */ 704252398f0SChristopher Smithfunction p_get_renderer($mode) { 7053a7140a1SAndreas Gohr /** @var PluginController $plugin_controller */ 7067aea91afSChris Smith global $conf, $plugin_controller; 707d968d3e5SChris Smith 708d968d3e5SChris Smith $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode; 7090cacf91fSLucas $rclass = "Doku_Renderer_$rname"; 7100cacf91fSLucas 711548d801fSChristopher Smith // if requested earlier or a bundled renderer 7120cacf91fSLucas if( class_exists($rclass) ) { 7135e40b274SChristopher Smith $Renderer = new $rclass(); 7145e40b274SChristopher Smith return $Renderer; 7150cacf91fSLucas } 716d968d3e5SChris Smith 7176e6d16edSChristopher Smith // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format. 7180440ca46SGerrit Uitslag /** @var Doku_Renderer $Renderer */ 71911ac6abdSChristopher Smith $Renderer = $plugin_controller->load('renderer',$rname); 7206e6d16edSChristopher Smith if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) { 72111ac6abdSChristopher Smith return $Renderer; 72211ac6abdSChristopher Smith } 723d968d3e5SChris Smith 72411ac6abdSChristopher Smith // there is a configuration error! 725548d801fSChristopher Smith // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer 72611ac6abdSChristopher Smith $rclass = "Doku_Renderer_$mode"; 727548d801fSChristopher Smith if ( class_exists($rclass) ) { 72811ac6abdSChristopher Smith // viewers should see renderered output, so restrict the warning to admins only 72911ac6abdSChristopher Smith $msg = "No renderer '$rname' found for mode '$mode', check your plugins"; 73011ac6abdSChristopher Smith if ($mode == 'xhtml') { 73111ac6abdSChristopher Smith $msg .= " and the 'renderer_xhtml' config setting"; 73211ac6abdSChristopher Smith } 73311ac6abdSChristopher Smith $msg .= ".<br/>Attempting to fallback to the bundled renderer."; 734548d801fSChristopher Smith msg($msg,-1,'','',MSG_ADMINS_ONLY); 73511ac6abdSChristopher Smith 736548d801fSChristopher Smith $Renderer = new $rclass; 737548d801fSChristopher Smith $Renderer->nocache(); // fallback only (and may include admin alerts), don't cache 738d968d3e5SChris Smith return $Renderer; 739d968d3e5SChris Smith } 740d968d3e5SChris Smith 741548d801fSChristopher Smith // fallback failed, alert the world 742548d801fSChristopher Smith msg("No renderer '$rname' found for mode '$mode'",-1); 743548d801fSChristopher Smith return null; 744548d801fSChristopher Smith} 745548d801fSChristopher Smith 746bb0a59d4Sjan/** 747bb0a59d4Sjan * Gets the first heading from a file 748bb0a59d4Sjan * 749fc18c0fbSchris * @param string $id dokuwiki page id 75067c15eceSMichael Hamann * @param int $render rerender if first heading not known 75167c15eceSMichael Hamann * default: METADATA_RENDER_USING_SIMPLE_CACHE 75267c15eceSMichael Hamann * Possible values: METADATA_DONT_RENDER, 75367c15eceSMichael Hamann * METADATA_RENDER_USING_SIMPLE_CACHE, 75465aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE, 75565aa8490SMichael Hamann * METADATA_RENDER_UNLIMITED 756e3ab6fc5SMichael Hamann * @return string|null The first heading 75742ea7f44SGerrit Uitslag * 75895dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 759bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de> 760bb0a59d4Sjan */ 76167c15eceSMichael Hamannfunction p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){ 76265aa8490SMichael Hamann return p_get_metadata(cleanID($id),'title',$render); 763bb0a59d4Sjan} 764bb0a59d4Sjan 7658f7d700cSchris/** 7668f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output 7678f7d700cSchris * 7685d568b99SChris Smith * @param string $code source code to be highlighted 7695d568b99SChris Smith * @param string $language language to provide highlighting 7705d568b99SChris Smith * @param string $wrapper html element to wrap the returned highlighted text 771e3ab6fc5SMichael Hamann * @return string xhtml code 77242ea7f44SGerrit Uitslag * 7738f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk> 77435fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7758f7d700cSchris */ 776e2d88156SLarsDW223function p_xhtml_cached_geshi($code, $language, $wrapper='pre', array $options=null) { 777ff1769deSAndreas Gohr global $conf, $config_cascade, $INPUT; 77835fbe9efSAndreas Gohr $language = strtolower($language); 7795d568b99SChris Smith 7805d568b99SChris Smith // remove any leading or trailing blank lines 7815d568b99SChris Smith $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code); 7825d568b99SChris Smith 783e2d88156SLarsDW223 $optionsmd5 = md5(serialize($options)); 784e2d88156SLarsDW223 $cache = getCacheName($language.$code.$optionsmd5,".code"); 78535fbe9efSAndreas Gohr $ctime = @filemtime($cache); 786ff1769deSAndreas Gohr if($ctime && !$INPUT->bool('purge') && 78741d51802SAndreas Gohr $ctime > filemtime(DOKU_INC.'vendor/composer/installed.json') && // libraries changed 788f8121585SChris Smith $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed 7898f7d700cSchris $highlighted_code = io_readFile($cache, false); 7908f7d700cSchris } else { 7918f7d700cSchris 79241d51802SAndreas Gohr $geshi = new GeSHi($code, $language); 7938f7d700cSchris $geshi->set_encoding('utf-8'); 7948f7d700cSchris $geshi->enable_classes(); 7958f7d700cSchris $geshi->set_header_type(GESHI_HEADER_PRE); 7968f7d700cSchris $geshi->set_link_target($conf['target']['extern']); 797e2d88156SLarsDW223 if($options !== null) { 798e2d88156SLarsDW223 foreach ($options as $function => $params) { 799e2d88156SLarsDW223 if(is_callable(array($geshi, $function))) { 800e2d88156SLarsDW223 $geshi->$function($params); 801e2d88156SLarsDW223 } 802e2d88156SLarsDW223 } 803e2d88156SLarsDW223 } 8048f7d700cSchris 8055d568b99SChris Smith // remove GeSHi's wrapper element (we'll replace it with our own later) 8065d568b99SChris Smith // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 80769ddc332SAnika Henke $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r"); 8088f7d700cSchris io_saveFile($cache,$highlighted_code); 8098f7d700cSchris } 8108f7d700cSchris 8115d568b99SChris Smith // add a wrapper element if required 8125d568b99SChris Smith if ($wrapper) { 8135d568b99SChris Smith return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 8145d568b99SChris Smith } else { 8158f7d700cSchris return $highlighted_code; 8168f7d700cSchris } 8175d568b99SChris Smith} 8188f7d700cSchris 819