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 */ 924870174SAndreas Gohruse dokuwiki\Extension\PluginInterface; 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; 1578b498a7SAndreas Gohruse dokuwiki\Extension\SyntaxPlugin; 16d4d8fb18SAndreas Gohruse dokuwiki\Parsing\Parser; 1778b498a7SAndreas Gohruse dokuwiki\Parsing\ParserMode\Acronym; 1878b498a7SAndreas Gohruse dokuwiki\Parsing\ParserMode\Camelcaselink; 1978b498a7SAndreas Gohruse dokuwiki\Parsing\ParserMode\Entity; 2078b498a7SAndreas Gohruse dokuwiki\Parsing\ParserMode\Formatting; 2178b498a7SAndreas Gohruse dokuwiki\Parsing\ParserMode\Smiley; 22d4d8fb18SAndreas Gohr 23c112d578Sandi/** 2465aa8490SMichael Hamann * How many pages shall be rendered for getting metadata during one request 2565aa8490SMichael Hamann * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED 2665aa8490SMichael Hamann * is passed as render parameter to p_get_metadata. 27ff725173SMichael Hamann */ 2865aa8490SMichael Hamannif (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5); 2967c15eceSMichael Hamann 3067c15eceSMichael Hamann/** Don't render metadata even if it is outdated or doesn't exist */ 3167c15eceSMichael Hamanndefine('METADATA_DONT_RENDER', 0); 3265aa8490SMichael Hamann/** 3365aa8490SMichael Hamann * Render metadata when the page is really newer or the metadata doesn't exist. 3465aa8490SMichael Hamann * Uses just a simple check, but should work pretty well for loading simple 3565aa8490SMichael Hamann * metadata values like the page title and avoids rendering a lot of pages in 3665aa8490SMichael Hamann * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode. 3765aa8490SMichael Hamann * Use this if it is unlikely that the metadata value you are requesting 3865aa8490SMichael Hamann * does depend e.g. on pages that are included in the current page using 3965aa8490SMichael Hamann * the include plugin (this is very likely the case for the page title, but 4065aa8490SMichael Hamann * not for relation references). 4165aa8490SMichael Hamann */ 4267c15eceSMichael Hamanndefine('METADATA_RENDER_USING_SIMPLE_CACHE', 1); 4365aa8490SMichael Hamann/** 4465aa8490SMichael Hamann * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT 4565aa8490SMichael Hamann * is used in this mode. Use this mode when you are requesting more complex 4665aa8490SMichael Hamann * metadata. Although this will cause rendering more often it might actually have 4765aa8490SMichael Hamann * the effect that less current metadata is returned as it is more likely than in 4865aa8490SMichael Hamann * the simple cache mode that metadata needs to be rendered for all pages at once 4965aa8490SMichael Hamann * which means that when the metadata for the page is requested that actually needs 5065aa8490SMichael Hamann * to be updated the limit might have been reached already. 5165aa8490SMichael Hamann */ 5267c15eceSMichael Hamanndefine('METADATA_RENDER_USING_CACHE', 2); 5365aa8490SMichael Hamann/** 5465aa8490SMichael Hamann * Render metadata without limiting the number of pages for which metadata is 5565aa8490SMichael Hamann * rendered. Use this mode with care, normally it should only be used in places 5665aa8490SMichael Hamann * like the indexer or in cli scripts where the execution time normally isn't 5765aa8490SMichael Hamann * limited. This can be combined with the simple cache using 5865aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED. 5965aa8490SMichael Hamann */ 6065aa8490SMichael Hamanndefine('METADATA_RENDER_UNLIMITED', 4); 61ff725173SMichael Hamann 62ff725173SMichael Hamann/** 63c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision. 64c112d578Sandi * 65c112d578Sandi * If $excuse is true an explanation is returned if the file 66c112d578Sandi * wasn't found 67c112d578Sandi * 6842ea7f44SGerrit Uitslag * @param string $id page id 6942ea7f44SGerrit Uitslag * @param string|int $rev revision timestamp or empty string 7042ea7f44SGerrit Uitslag * @param bool $excuse 71f50a239bSTakamura * @param string $date_at 72f50a239bSTakamura * 7342ea7f44SGerrit Uitslag * @return null|string 7478b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7578b498a7SAndreas Gohr * 76c112d578Sandi */ 7778b498a7SAndreas Gohrfunction p_wiki_xhtml($id, $rev = '', $excuse = true, $date_at = '') 7878b498a7SAndreas Gohr{ 79c112d578Sandi $file = wikiFN($id, $rev); 80c112d578Sandi $ret = ''; 81c112d578Sandi 82c112d578Sandi //ensure $id is in global $ID (needed for parsing) 831e76272cSandi global $ID; 843ff8773bSAndreas Gohr $keep = $ID; 851e76272cSandi $ID = $id; 86c112d578Sandi 875c2eed9aSlisps if ($rev || $date_at) { 8879e79377SAndreas Gohr if (file_exists($file)) { 8964159a61SAndreas Gohr //no caching on old revisions 9064159a61SAndreas Gohr $ret = p_render('xhtml', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); 91c112d578Sandi } elseif ($excuse) { 92c112d578Sandi $ret = p_locale_xhtml('norev'); 93c112d578Sandi } 9424870174SAndreas Gohr } elseif (file_exists($file)) { 954b5f4f4eSchris $ret = p_cached_output($file, 'xhtml', $id); 96c112d578Sandi } elseif ($excuse) { 972eaa0567Speterfromearth //check if the page once existed 98e1d9dcc8SAndreas Gohr $changelog = new PageChangeLog($id); 992eaa0567Speterfromearth if ($changelog->hasRevisions()) { 1002eaa0567Speterfromearth $ret = p_locale_xhtml('onceexisted'); 1012eaa0567Speterfromearth } else { 102c112d578Sandi $ret = p_locale_xhtml('newpage'); 103c112d578Sandi } 104c112d578Sandi } 105c112d578Sandi 1063ff8773bSAndreas Gohr //restore ID (just in case) 1073ff8773bSAndreas Gohr $ID = $keep; 1083ff8773bSAndreas Gohr 109c112d578Sandi return $ret; 110c112d578Sandi} 111c112d578Sandi 112c112d578Sandi/** 113c112d578Sandi * Returns the specified local text in parsed format 114c112d578Sandi * 11542ea7f44SGerrit Uitslag * @param string $id page id 11642ea7f44SGerrit Uitslag * @return null|string 11778b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11878b498a7SAndreas Gohr * 119c112d578Sandi */ 12078b498a7SAndreas Gohrfunction p_locale_xhtml($id) 12178b498a7SAndreas Gohr{ 122c112d578Sandi //fetch parsed locale 12365f6b58fSAnna Dabrowska $data = ['id' => $id, 'html' => '']; 12465f6b58fSAnna Dabrowska 12565f6b58fSAnna Dabrowska $event = new Event('PARSER_LOCALE_XHTML', $data); 12665f6b58fSAnna Dabrowska if ($event->advise_before()) { 12765f6b58fSAnna Dabrowska $data['html'] = p_cached_output(localeFN($data['id'])); 12865f6b58fSAnna Dabrowska } 12965f6b58fSAnna Dabrowska $event->advise_after(); 13065f6b58fSAnna Dabrowska 13165f6b58fSAnna Dabrowska return $data['html']; 132c112d578Sandi} 133c112d578Sandi 134c112d578Sandi/** 1354b5f4f4eSchris * Returns the given file parsed into the requested output format 1364b5f4f4eSchris * 13742ea7f44SGerrit Uitslag * @param string $file filename, path to file 13842ea7f44SGerrit Uitslag * @param string $format 13942ea7f44SGerrit Uitslag * @param string $id page id 14042ea7f44SGerrit Uitslag * @return null|string 14178b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 14278b498a7SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 14378b498a7SAndreas Gohr * 1444b5f4f4eSchris */ 14578b498a7SAndreas Gohrfunction p_cached_output($file, $format = 'xhtml', $id = '') 14678b498a7SAndreas Gohr{ 147c112d578Sandi global $conf; 148c112d578Sandi 1490db5771eSMichael Große $cache = new CacheRenderer($id, $file, $format); 1504b5f4f4eSchris if ($cache->useCache()) { 15185767031SAndreas Gohr $parsed = $cache->retrieveCache(false); 1527de86af9SGerrit Uitslag if ($conf['allowdebug'] && $format == 'xhtml') { 1537de86af9SGerrit Uitslag $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 1547de86af9SGerrit Uitslag } 155c112d578Sandi } else { 1564b5f4f4eSchris $parsed = p_render($format, p_cached_instructions($file, false, $id), $info); 157c112d578Sandi 158abca2f79SEduardo Mozart de Oliveira if (!empty($info['cache']) && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile 1597de86af9SGerrit Uitslag if ($conf['allowdebug'] && $format == 'xhtml') { 1607de86af9SGerrit Uitslag $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 1617de86af9SGerrit Uitslag } 162c112d578Sandi } else { 1634b5f4f4eSchris $cache->removeCache(); //try to delete cachefile 1647de86af9SGerrit Uitslag if ($conf['allowdebug'] && $format == 'xhtml') { 1657de86af9SGerrit Uitslag $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 1667de86af9SGerrit Uitslag } 167c112d578Sandi } 168c112d578Sandi } 169c112d578Sandi 170c112d578Sandi return $parsed; 171c112d578Sandi} 172c112d578Sandi 173c112d578Sandi/** 174c112d578Sandi * Returns the render instructions for a file 175c112d578Sandi * 176c112d578Sandi * Uses and creates a serialized cache file 177c112d578Sandi * 17842ea7f44SGerrit Uitslag * @param string $file filename, path to file 17942ea7f44SGerrit Uitslag * @param bool $cacheonly 18042ea7f44SGerrit Uitslag * @param string $id page id 18142ea7f44SGerrit Uitslag * @return array|null 18278b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 18378b498a7SAndreas Gohr * 184c112d578Sandi */ 18578b498a7SAndreas Gohrfunction p_cached_instructions($file, $cacheonly = false, $id = '') 18678b498a7SAndreas Gohr{ 18747b2d319SAndreas Gohr static $run = null; 18878b498a7SAndreas Gohr if (is_null($run)) $run = []; 189c112d578Sandi 1900db5771eSMichael Große $cache = new CacheInstructions($id, $file); 191c112d578Sandi 1920d24b616SMichael Hamann if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) { 1934b5f4f4eSchris return $cache->retrieveCache(); 19479e79377SAndreas Gohr } elseif (file_exists($file)) { 195c112d578Sandi // no cache - do some work 196bde4e341SGalaxyMaster $ins = p_get_instructions(io_readWikiPage($file, $id)); 197cbaf4259SChris Smith if ($cache->storeCache($ins)) { 19847b2d319SAndreas Gohr $run[$file] = true; // we won't rebuild these instructions in the same run again 199cbaf4259SChris Smith } else { 200cbaf4259SChris Smith msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.', -1); 201cbaf4259SChris Smith } 202c112d578Sandi return $ins; 203c112d578Sandi } 204c112d578Sandi 2053b3f8916SAndreas Gohr return null; 206c112d578Sandi} 207c112d578Sandi 208c112d578Sandi/** 209c112d578Sandi * turns a page into a list of instructions 210c112d578Sandi * 21178b498a7SAndreas Gohr * @param string $text raw wiki syntax text 21278b498a7SAndreas Gohr * @return array a list of instruction arrays 213c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 214c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 21542ea7f44SGerrit Uitslag * 216c112d578Sandi */ 21778b498a7SAndreas Gohrfunction p_get_instructions($text) 21878b498a7SAndreas Gohr{ 219c112d578Sandi 220107b01d6Sandi $modes = p_get_parsermodes(); 221ee20e7d1Sandi 22247f73ecfSAndreas Gohr // Create the parser and handler 223d4d8fb18SAndreas Gohr $Parser = new Parser(new Doku_Handler()); 224c112d578Sandi 225107b01d6Sandi //add modes to parser 226107b01d6Sandi foreach ($modes as $mode) { 227107b01d6Sandi $Parser->addMode($mode['mode'], $mode['obj']); 228c112d578Sandi } 229c112d578Sandi 230c112d578Sandi // Do the parsing 231cbb44eabSAndreas Gohr Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text); 23278b498a7SAndreas Gohr return $Parser->parse($text); 233c112d578Sandi} 234c112d578Sandi 235c112d578Sandi/** 23639a89382SEsther Brunner * returns the metadata of a page 23739a89382SEsther Brunner * 2384a819402SMichael Hamann * @param string $id The id of the page the metadata should be returned from 23964159a61SAndreas Gohr * @param string $key The key of the metdata value that shall be read (by default everything) 24064159a61SAndreas Gohr * separate hierarchies by " " like "date created" 24167c15eceSMichael Hamann * @param int $render If the page should be rendererd - possible values: 24265aa8490SMichael Hamann * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE 24365aa8490SMichael Hamann * METADATA_RENDER_UNLIMITED (also combined with the previous two options), 24465aa8490SMichael Hamann * default: METADATA_RENDER_USING_CACHE 2454a819402SMichael Hamann * @return mixed The requested metadata fields 2464a819402SMichael Hamann * 24739a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 24898214867SMichael Hamann * @author Michael Hamann <michael@content-space.de> 24939a89382SEsther Brunner */ 25078b498a7SAndreas Gohrfunction p_get_metadata($id, $key = '', $render = METADATA_RENDER_USING_CACHE) 25178b498a7SAndreas Gohr{ 2521172f8dcSAdrian Lang global $ID; 25365aa8490SMichael Hamann static $render_count = 0; 25465aa8490SMichael Hamann // track pages that have already been rendered in order to avoid rendering the same page 25565aa8490SMichael Hamann // again 25678b498a7SAndreas Gohr static $rendered_pages = []; 2576afe8dcaSchris 2580a7e3bceSchris // cache the current page 2590a7e3bceSchris // Benchmarking shows the current page's metadata is generally the only page metadata 2600a7e3bceSchris // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 2610a7e3bceSchris $cache = ($ID == $id); 2620a7e3bceSchris $meta = p_read_metadata($id, $cache); 26339a89382SEsther Brunner 26467c15eceSMichael Hamann if (!is_numeric($render)) { 26567c15eceSMichael Hamann if ($render) { 26667c15eceSMichael Hamann $render = METADATA_RENDER_USING_SIMPLE_CACHE; 26767c15eceSMichael Hamann } else { 26867c15eceSMichael Hamann $render = METADATA_DONT_RENDER; 26967c15eceSMichael Hamann } 27067c15eceSMichael Hamann } 27167c15eceSMichael Hamann 27298214867SMichael Hamann // prevent recursive calls in the cache 27398214867SMichael Hamann static $recursion = false; 27465aa8490SMichael Hamann if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id]) && page_exists($id)) { 27598214867SMichael Hamann $recursion = true; 27698214867SMichael Hamann 2770db5771eSMichael Große $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata'); 27898214867SMichael Hamann 27967c15eceSMichael Hamann $do_render = false; 28065aa8490SMichael Hamann if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) { 28165aa8490SMichael Hamann if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) { 28267c15eceSMichael Hamann $pagefn = wikiFN($id); 28367c15eceSMichael Hamann $metafn = metaFN($id, '.meta'); 28479e79377SAndreas Gohr if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) { 28567c15eceSMichael Hamann $do_render = true; 28667c15eceSMichael Hamann } 28767c15eceSMichael Hamann } elseif (!$cachefile->useCache()) { 28867c15eceSMichael Hamann $do_render = true; 28967c15eceSMichael Hamann } 29065aa8490SMichael Hamann } 29167c15eceSMichael Hamann if ($do_render) { 2920d24b616SMichael Hamann if (!defined('DOKU_UNITTEST')) { 29365aa8490SMichael Hamann ++$render_count; 29465aa8490SMichael Hamann $rendered_pages[$id] = true; 2950d24b616SMichael Hamann } 29669ba640bSMichael Hamann $old_meta = $meta; 29739a89382SEsther Brunner $meta = p_render_metadata($id, $meta); 29869ba640bSMichael Hamann // only update the file when the metadata has been changed 29969ba640bSMichael Hamann if ($meta == $old_meta || p_save_metadata($id, $meta)) { 30098214867SMichael Hamann // store a timestamp in order to make sure that the cachefile is touched 3013d6feb16SMichael Hamann // this timestamp is also stored when the meta data is still the same 30298214867SMichael Hamann $cachefile->storeCache(time()); 3033d6feb16SMichael Hamann } else { 30498214867SMichael Hamann msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1); 30598214867SMichael Hamann } 30698214867SMichael Hamann } 30798214867SMichael Hamann 30898214867SMichael Hamann $recursion = false; 3096afe8dcaSchris } 31039a89382SEsther Brunner 311ebf65d37SAdrian Lang $val = $meta['current']; 312ebf65d37SAdrian Lang 31339a89382SEsther Brunner // filter by $key 314569a0019SAdrian Lang foreach (preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 315ebf65d37SAdrian Lang if (!isset($val[$cur_key])) { 316ebf65d37SAdrian Lang return null; 31766d29756SChris Smith } 318ebf65d37SAdrian Lang $val = $val[$cur_key]; 31939a89382SEsther Brunner } 320ebf65d37SAdrian Lang return $val; 32139a89382SEsther Brunner} 32239a89382SEsther Brunner 32339a89382SEsther Brunner/** 32439a89382SEsther Brunner * sets metadata elements of a page 32539a89382SEsther Brunner * 326a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 327a365baeeSDominik Eckelmann * 328e1d9dcc8SAndreas Gohr * @param string $id is the ID of a wiki page 329e1d9dcc8SAndreas Gohr * @param array $data is an array with key ⇒ value pairs to be set in the metadata 330e1d9dcc8SAndreas Gohr * @param boolean $render whether or not the page metadata should be generated with the renderer 331e1d9dcc8SAndreas Gohr * @param boolean $persistent indicates whether or not the particular metadata value will persist through 332a365baeeSDominik Eckelmann * the next metadata rendering. 333a365baeeSDominik Eckelmann * @return boolean true on success 334a365baeeSDominik Eckelmann * 33539a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 3360e5fde48SMichael Hamann * @author Michael Hamann <michael@content-space.de> 33739a89382SEsther Brunner */ 33878b498a7SAndreas Gohrfunction p_set_metadata($id, $data, $render = false, $persistent = true) 33978b498a7SAndreas Gohr{ 34039a89382SEsther Brunner if (!is_array($data)) return false; 34139a89382SEsther Brunner 3420e5fde48SMichael Hamann global $ID, $METADATA_RENDERERS; 3430a7e3bceSchris 3440e5fde48SMichael Hamann // if there is currently a renderer change the data in the renderer instead 3450e5fde48SMichael Hamann if (isset($METADATA_RENDERERS[$id])) { 3460e5fde48SMichael Hamann $orig =& $METADATA_RENDERERS[$id]; 3470e5fde48SMichael Hamann $meta = $orig; 3480e5fde48SMichael Hamann } else { 3490a7e3bceSchris // cache the current page 3500a7e3bceSchris $cache = ($ID == $id); 3510a7e3bceSchris $orig = p_read_metadata($id, $cache); 35239a89382SEsther Brunner 35339a89382SEsther Brunner // render metadata first? 3540a7e3bceSchris $meta = $render ? p_render_metadata($id, $orig) : $orig; 3550e5fde48SMichael Hamann } 35639a89382SEsther Brunner 35739a89382SEsther Brunner // now add the passed metadata 35878b498a7SAndreas Gohr $protected = ['description', 'date', 'contributor']; 35939a89382SEsther Brunner foreach ($data as $key => $value) { 36039a89382SEsther Brunner // be careful with sub-arrays of $meta['relation'] 36139a89382SEsther Brunner if ($key == 'relation') { 36239a89382SEsther Brunner foreach ($value as $subkey => $subvalue) { 36331b10b49SMichael Hamann if (isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) { 3642d102494SPhy $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue); 36531b10b49SMichael Hamann } else { 36631b10b49SMichael Hamann $meta['current'][$key][$subkey] = $subvalue; 36731b10b49SMichael Hamann } 36831b10b49SMichael Hamann if ($persistent) { 36931b10b49SMichael Hamann if (isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) { 37064159a61SAndreas Gohr $meta['persistent'][$key][$subkey] = array_replace( 37164159a61SAndreas Gohr $meta['persistent'][$key][$subkey], 37264159a61SAndreas Gohr (array)$subvalue 37364159a61SAndreas Gohr ); 37431b10b49SMichael Hamann } else { 37531b10b49SMichael Hamann $meta['persistent'][$key][$subkey] = $subvalue; 37631b10b49SMichael Hamann } 37731b10b49SMichael Hamann } 37839a89382SEsther Brunner } 37939a89382SEsther Brunner 38039a89382SEsther Brunner // be careful with some senisitive arrays of $meta 38139a89382SEsther Brunner } elseif (in_array($key, $protected)) { 38266d29756SChris Smith // these keys, must have subkeys - a legitimate value must be an array 38339a89382SEsther Brunner if (is_array($value)) { 38424870174SAndreas Gohr $meta['current'][$key] = empty($meta['current'][$key]) ? 38524870174SAndreas Gohr $value : 38624870174SAndreas Gohr array_replace((array)$meta['current'][$key], $value); 3870a7e3bceSchris 3880a7e3bceSchris if ($persistent) { 38924870174SAndreas Gohr $meta['persistent'][$key] = empty($meta['persistent'][$key]) ? 39024870174SAndreas Gohr $value : 39124870174SAndreas Gohr array_replace((array)$meta['persistent'][$key], $value); 3920a7e3bceSchris } 39339a89382SEsther Brunner } 39439a89382SEsther Brunner 39539a89382SEsther Brunner // no special treatment for the rest 39639a89382SEsther Brunner } else { 3970a7e3bceSchris $meta['current'][$key] = $value; 3980a7e3bceSchris if ($persistent) $meta['persistent'][$key] = $value; 39939a89382SEsther Brunner } 40039a89382SEsther Brunner } 40139a89382SEsther Brunner 40239a89382SEsther Brunner // save only if metadata changed 40339a89382SEsther Brunner if ($meta == $orig) return true; 4046afe8dcaSchris 4050e5fde48SMichael Hamann if (isset($METADATA_RENDERERS[$id])) { 4060e5fde48SMichael Hamann // set both keys individually as the renderer has references to the individual keys 4070e5fde48SMichael Hamann $METADATA_RENDERERS[$id]['current'] = $meta['current']; 4080e5fde48SMichael Hamann $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; 4091c56be7bSMichael Hamann return true; 4100e5fde48SMichael Hamann } else { 4111172f8dcSAdrian Lang return p_save_metadata($id, $meta); 41239a89382SEsther Brunner } 4130e5fde48SMichael Hamann} 41439a89382SEsther Brunner 41539a89382SEsther Brunner/** 4163d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data 4173d1f9ec3SMichael Klier * used on page deletion 4183d1f9ec3SMichael Klier * 41942ea7f44SGerrit Uitslag * @param string $id page id 42042ea7f44SGerrit Uitslag * @return bool success / fail 42178b498a7SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 42278b498a7SAndreas Gohr * 4233d1f9ec3SMichael Klier */ 42478b498a7SAndreas Gohrfunction p_purge_metadata($id) 42578b498a7SAndreas Gohr{ 4263d1f9ec3SMichael Klier $meta = p_read_metadata($id); 4273d1f9ec3SMichael Klier foreach ($meta['current'] as $key => $value) { 428056bf31fSDamien Regad if (isset($meta[$key]) && is_array($meta[$key])) { 42978b498a7SAndreas Gohr $meta['current'][$key] = []; 4303d1f9ec3SMichael Klier } else { 4313d1f9ec3SMichael Klier $meta['current'][$key] = ''; 4323d1f9ec3SMichael Klier } 4333d1f9ec3SMichael Klier } 4341172f8dcSAdrian Lang return p_save_metadata($id, $meta); 4353d1f9ec3SMichael Klier} 4363d1f9ec3SMichael Klier 4373d1f9ec3SMichael Klier/** 4380a7e3bceSchris * read the metadata from source/cache for $id 4390a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata) 4400a7e3bceSchris * 4410a7e3bceSchris * @param string $id absolute wiki page id 4420a7e3bceSchris * @param bool $cache whether or not to cache metadata in memory 4430a7e3bceSchris * (only use for metadata likely to be accessed several times) 4440a7e3bceSchris * 4450a7e3bceSchris * @return array metadata 44678b498a7SAndreas Gohr * @author Christopher Smith <chris@jalakai.co.uk> 44778b498a7SAndreas Gohr * 4480a7e3bceSchris */ 44978b498a7SAndreas Gohrfunction p_read_metadata($id, $cache = false) 45078b498a7SAndreas Gohr{ 4510a7e3bceSchris global $cache_metadata; 4520a7e3bceSchris 4533a50618cSgweissbach if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 4540a7e3bceSchris 4550a7e3bceSchris $file = metaFN($id, '.meta'); 45664159a61SAndreas Gohr $meta = file_exists($file) ? 45764159a61SAndreas Gohr unserialize(io_readFile($file, false)) : 45878b498a7SAndreas Gohr ['current' => [], 'persistent' => []]; 4590a7e3bceSchris 4600a7e3bceSchris if ($cache) { 4613a50618cSgweissbach $cache_metadata[(string)$id] = $meta; 4620a7e3bceSchris } 4630a7e3bceSchris 4640a7e3bceSchris return $meta; 4650a7e3bceSchris} 4660a7e3bceSchris 4670a7e3bceSchris/** 4681172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file 4691172f8dcSAdrian Lang * 4701172f8dcSAdrian Lang * @param string $id absolute wiki page id 4711172f8dcSAdrian Lang * @param array $meta metadata 4721172f8dcSAdrian Lang * 4731172f8dcSAdrian Lang * @return bool success / fail 4741172f8dcSAdrian Lang */ 47578b498a7SAndreas Gohrfunction p_save_metadata($id, $meta) 47678b498a7SAndreas Gohr{ 4771172f8dcSAdrian Lang // sync cached copies, including $INFO metadata 4781172f8dcSAdrian Lang global $cache_metadata, $INFO; 4791172f8dcSAdrian Lang 4801172f8dcSAdrian Lang if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 481056bf31fSDamien Regad if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) { 482056bf31fSDamien Regad $INFO['meta'] = $meta['current']; 483056bf31fSDamien Regad } 4841172f8dcSAdrian Lang 4851172f8dcSAdrian Lang return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 4861172f8dcSAdrian Lang} 4871172f8dcSAdrian Lang 4881172f8dcSAdrian Lang/** 48939a89382SEsther Brunner * renders the metadata of a page 49039a89382SEsther Brunner * 49142ea7f44SGerrit Uitslag * @param string $id page id 49242ea7f44SGerrit Uitslag * @param array $orig the original metadata 49342ea7f44SGerrit Uitslag * @return array|null array('current'=> array,'persistent'=> array); 49478b498a7SAndreas Gohr * @author Esther Brunner <esther@kaffeehaus.ch> 49578b498a7SAndreas Gohr * 49639a89382SEsther Brunner */ 49778b498a7SAndreas Gohrfunction p_render_metadata($id, $orig) 49878b498a7SAndreas Gohr{ 49948924015SAndreas Gohr // make sure the correct ID is in global ID 5000e5fde48SMichael Hamann global $ID, $METADATA_RENDERERS; 5010e5fde48SMichael Hamann 5020e5fde48SMichael Hamann // avoid recursive rendering processes for the same id 5035b76ad91SChristopher Smith if (isset($METADATA_RENDERERS[$id])) { 5040e5fde48SMichael Hamann return $orig; 5055b76ad91SChristopher Smith } 5060e5fde48SMichael Hamann 5070e5fde48SMichael Hamann // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it 5080e5fde48SMichael Hamann $METADATA_RENDERERS[$id] =& $orig; 5090e5fde48SMichael Hamann 51048924015SAndreas Gohr $keep = $ID; 51148924015SAndreas Gohr $ID = $id; 51248924015SAndreas Gohr 5130a7e3bceSchris // add an extra key for the event - to tell event handlers the page whose metadata this is 5140a7e3bceSchris $orig['page'] = $id; 515e1d9dcc8SAndreas Gohr $evt = new Event('PARSER_METADATA_RENDER', $orig); 5160a7e3bceSchris if ($evt->advise_before()) { 51739a89382SEsther Brunner // get instructions 5184b5f4f4eSchris $instructions = p_cached_instructions(wikiFN($id), false, $id); 51948924015SAndreas Gohr if (is_null($instructions)) { 52048924015SAndreas Gohr $ID = $keep; 5210e5fde48SMichael Hamann unset($METADATA_RENDERERS[$id]); 52248924015SAndreas Gohr return null; // something went wrong with the instructions 52348924015SAndreas Gohr } 52439a89382SEsther Brunner 52539a89382SEsther Brunner // set up the renderer 52667f9913dSAndreas Gohr $renderer = new Doku_Renderer_metadata(); 5270e5fde48SMichael Hamann $renderer->meta =& $orig['current']; 5280e5fde48SMichael Hamann $renderer->persistent =& $orig['persistent']; 52939a89382SEsther Brunner 53039a89382SEsther Brunner // loop through the instructions 53139a89382SEsther Brunner foreach ($instructions as $instruction) { 53239a89382SEsther Brunner // execute the callback against the renderer 53378b498a7SAndreas Gohr call_user_func_array([&$renderer, $instruction[0]], (array)$instruction[1]); 53439a89382SEsther Brunner } 53539a89382SEsther Brunner 53678b498a7SAndreas Gohr $evt->result = ['current' => &$renderer->meta, 'persistent' => &$renderer->persistent]; 5370a7e3bceSchris } 5380a7e3bceSchris $evt->advise_after(); 5390a7e3bceSchris 5400e5fde48SMichael Hamann // clean up 54148924015SAndreas Gohr $ID = $keep; 5420e5fde48SMichael Hamann unset($METADATA_RENDERERS[$id]); 5430a7e3bceSchris return $evt->result; 54439a89382SEsther Brunner} 54539a89382SEsther Brunner 54639a89382SEsther Brunner/** 547107b01d6Sandi * returns all available parser syntax modes in correct order 548107b01d6Sandi * 54978b498a7SAndreas Gohr * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj' => plugin object) 550107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org> 55142ea7f44SGerrit Uitslag * 552107b01d6Sandi */ 55378b498a7SAndreas Gohrfunction p_get_parsermodes() 55478b498a7SAndreas Gohr{ 555107b01d6Sandi global $conf; 556107b01d6Sandi 557107b01d6Sandi //reuse old data 558107b01d6Sandi static $modes = null; 5590d24b616SMichael Hamann if ($modes != null && !defined('DOKU_UNITTEST')) { 560107b01d6Sandi return $modes; 561107b01d6Sandi } 562107b01d6Sandi 563107b01d6Sandi //import parser classes and mode definitions 564107b01d6Sandi require_once DOKU_INC . 'inc/parser/parser.php'; 565107b01d6Sandi 566107b01d6Sandi // we now collect all syntax modes and their objects, then they will 567107b01d6Sandi // be sorted and added to the parser in correct order 56878b498a7SAndreas Gohr $modes = []; 569107b01d6Sandi 570107b01d6Sandi // add syntax plugins 571107b01d6Sandi $pluginlist = plugin_list('syntax'); 57224870174SAndreas Gohr if ($pluginlist !== []) { 573107b01d6Sandi global $PARSER_MODES; 574107b01d6Sandi foreach ($pluginlist as $p) { 57578b498a7SAndreas Gohr /** @var SyntaxPlugin $obj */ 57651bc2534SAndreas Gohr if (!($obj = plugin_load('syntax', $p)) instanceof PluginInterface) continue; 577107b01d6Sandi $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type 578107b01d6Sandi //add to modes 57978b498a7SAndreas Gohr $modes[] = [ 580107b01d6Sandi 'sort' => $obj->getSort(), 581107b01d6Sandi 'mode' => "plugin_$p", 582107b01d6Sandi 'obj' => $obj, 58378b498a7SAndreas Gohr ]; 584a46d0d65SAndreas Gohr unset($obj); //remove the reference 585107b01d6Sandi } 586107b01d6Sandi } 587107b01d6Sandi 588107b01d6Sandi // add default modes 58978b498a7SAndreas Gohr $std_modes = [ 59078b498a7SAndreas Gohr 'listblock', 'preformatted', 'notoc', 'nocache', 'header', 'table', 'linebreak', 'footnote', 'hr', 59178b498a7SAndreas Gohr 'unformatted', 'code', 'file', 'quote', 'internallink', 'rss', 'media', 'externallink', 59278b498a7SAndreas Gohr 'emaillink', 'windowssharelink', 'eol' 59378b498a7SAndreas Gohr ]; 594e77ea1bcSAndreas Gohr if ($conf['typography']) { 595e77ea1bcSAndreas Gohr $std_modes[] = 'quotes'; 596e77ea1bcSAndreas Gohr $std_modes[] = 'multiplyentity'; 597e77ea1bcSAndreas Gohr } 598107b01d6Sandi foreach ($std_modes as $m) { 599be906b56SAndreas Gohr $class = 'dokuwiki\\Parsing\\ParserMode\\' . ucfirst($m); 600107b01d6Sandi $obj = new $class(); 60124870174SAndreas Gohr $modes[] = ['sort' => $obj->getSort(), 'mode' => $m, 'obj' => $obj]; 602107b01d6Sandi } 603107b01d6Sandi 604107b01d6Sandi // add formatting modes 60578b498a7SAndreas Gohr $fmt_modes = [ 60678b498a7SAndreas Gohr 'strong', 'emphasis', 'underline', 'monospace', 'subscript', 'superscript', 'deleted' 60778b498a7SAndreas Gohr ]; 608107b01d6Sandi foreach ($fmt_modes as $m) { 60978b498a7SAndreas Gohr $obj = new Formatting($m); 61024870174SAndreas Gohr $modes[] = [ 611107b01d6Sandi 'sort' => $obj->getSort(), 612107b01d6Sandi 'mode' => $m, 613107b01d6Sandi 'obj' => $obj 61424870174SAndreas Gohr ]; 615107b01d6Sandi } 616107b01d6Sandi 617107b01d6Sandi // add modes which need files 61878b498a7SAndreas Gohr $obj = new Smiley(array_keys(getSmileys())); 61978b498a7SAndreas Gohr $modes[] = ['sort' => $obj->getSort(), 'mode' => 'smiley', 'obj' => $obj]; 62078b498a7SAndreas Gohr $obj = new Acronym(array_keys(getAcronyms())); 62178b498a7SAndreas Gohr $modes[] = ['sort' => $obj->getSort(), 'mode' => 'acronym', 'obj' => $obj]; 62278b498a7SAndreas Gohr $obj = new Entity(array_keys(getEntities())); 62378b498a7SAndreas Gohr $modes[] = ['sort' => $obj->getSort(), 'mode' => 'entity', 'obj' => $obj]; 624107b01d6Sandi 625107b01d6Sandi // add optional camelcase mode 626107b01d6Sandi if ($conf['camelcase']) { 62778b498a7SAndreas Gohr $obj = new Camelcaselink(); 62878b498a7SAndreas Gohr $modes[] = ['sort' => $obj->getSort(), 'mode' => 'camelcaselink', 'obj' => $obj]; 629107b01d6Sandi } 630107b01d6Sandi 631107b01d6Sandi //sort modes 632107b01d6Sandi usort($modes, 'p_sort_modes'); 633107b01d6Sandi 634107b01d6Sandi return $modes; 635107b01d6Sandi} 636107b01d6Sandi 637107b01d6Sandi/** 638107b01d6Sandi * Callback function for usort 639107b01d6Sandi * 64042ea7f44SGerrit Uitslag * @param array $a 64142ea7f44SGerrit Uitslag * @param array $b 64242ea7f44SGerrit Uitslag * @return int $a is lower/equal/higher than $b 64378b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 64478b498a7SAndreas Gohr * 645107b01d6Sandi */ 64678b498a7SAndreas Gohrfunction p_sort_modes($a, $b) 64778b498a7SAndreas Gohr{ 64824870174SAndreas Gohr return $a['sort'] <=> $b['sort']; 649107b01d6Sandi} 650107b01d6Sandi 651107b01d6Sandi/** 652ac83b9d8Sandi * Renders a list of instruction to the specified output mode 653c112d578Sandi * 6547a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned 6559dc2c2afSandi * 65642ea7f44SGerrit Uitslag * @param string $mode 657e3710957SGerrit Uitslag * @param array|null|false $instructions 65842ea7f44SGerrit Uitslag * @param array $info returns render info like enabled toc and cache 6597de86af9SGerrit Uitslag * @param string $date_at 66042ea7f44SGerrit Uitslag * @return null|string rendered output 66178b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 66278b498a7SAndreas Gohr * 66378b498a7SAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com> 664c112d578Sandi */ 66578b498a7SAndreas Gohrfunction p_render($mode, $instructions, &$info, $date_at = '') 66678b498a7SAndreas Gohr{ 667c112d578Sandi if (is_null($instructions)) return ''; 668e3710957SGerrit Uitslag if ($instructions === false) return ''; 669c112d578Sandi 670252398f0SChristopher Smith $Renderer = p_get_renderer($mode); 671d968d3e5SChris Smith if (is_null($Renderer)) return null; 672c327d6c4SAndreas Gohr 673d968d3e5SChris Smith $Renderer->reset(); 674c112d578Sandi 6755c2eed9aSlisps if ($date_at) { 6765c2eed9aSlisps $Renderer->date_at = $date_at; 6775c2eed9aSlisps } 6785c2eed9aSlisps 679c112d578Sandi $Renderer->smileys = getSmileys(); 680c112d578Sandi $Renderer->entities = getEntities(); 681c112d578Sandi $Renderer->acronyms = getAcronyms(); 682c112d578Sandi $Renderer->interwiki = getInterwiki(); 683c112d578Sandi 684c112d578Sandi // Loop through the instructions 685c112d578Sandi foreach ($instructions as $instruction) { 686c112d578Sandi // Execute the callback against the Renderer 6877c62086bSAndreas Gohr if (method_exists($Renderer, $instruction[0])) { 68878b498a7SAndreas Gohr call_user_func_array([&$Renderer, $instruction[0]], $instruction[1] ?: []); 689c112d578Sandi } 6907c62086bSAndreas Gohr } 6919dc2c2afSandi 6929dc2c2afSandi //set info array 6939dc2c2afSandi $info = $Renderer->info; 6949dc2c2afSandi 695677844afSchris // Post process and return the output 69678b498a7SAndreas Gohr $data = [$mode, & $Renderer->doc]; 697cbb44eabSAndreas Gohr Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS', $data); 698c112d578Sandi return $Renderer->doc; 699c112d578Sandi} 700c112d578Sandi 701e3ab6fc5SMichael Hamann/** 702548d801fSChristopher Smith * Figure out the correct renderer class to use for $mode, 703548d801fSChristopher Smith * instantiate and return it 704548d801fSChristopher Smith * 7057e8500eeSGerrit Uitslag * @param string $mode Mode of the renderer to get 706e3ab6fc5SMichael Hamann * @return null|Doku_Renderer The renderer 707548d801fSChristopher Smith * 708548d801fSChristopher Smith * @author Christopher Smith <chris@jalakai.co.uk> 709e3ab6fc5SMichael Hamann */ 71078b498a7SAndreas Gohrfunction p_get_renderer($mode) 71178b498a7SAndreas Gohr{ 7123a7140a1SAndreas Gohr /** @var PluginController $plugin_controller */ 7137aea91afSChris Smith global $conf, $plugin_controller; 714d968d3e5SChris Smith 71524870174SAndreas Gohr $rname = empty($conf['renderer_' . $mode]) ? $mode : $conf['renderer_' . $mode]; 7160cacf91fSLucas $rclass = "Doku_Renderer_$rname"; 7170cacf91fSLucas 718548d801fSChristopher Smith // if requested earlier or a bundled renderer 7190cacf91fSLucas if (class_exists($rclass)) { 72078b498a7SAndreas Gohr return new $rclass(); 7210cacf91fSLucas } 722d968d3e5SChris Smith 7236e6d16edSChristopher Smith // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format. 7240440ca46SGerrit Uitslag /** @var Doku_Renderer $Renderer */ 72511ac6abdSChristopher Smith $Renderer = $plugin_controller->load('renderer', $rname); 7266e6d16edSChristopher Smith if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) { 72711ac6abdSChristopher Smith return $Renderer; 72811ac6abdSChristopher Smith } 729d968d3e5SChris Smith 73011ac6abdSChristopher Smith // there is a configuration error! 731548d801fSChristopher Smith // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer 73211ac6abdSChristopher Smith $rclass = "Doku_Renderer_$mode"; 733548d801fSChristopher Smith if (class_exists($rclass)) { 73411ac6abdSChristopher Smith // viewers should see renderered output, so restrict the warning to admins only 73511ac6abdSChristopher Smith $msg = "No renderer '$rname' found for mode '$mode', check your plugins"; 73611ac6abdSChristopher Smith if ($mode == 'xhtml') { 73711ac6abdSChristopher Smith $msg .= " and the 'renderer_xhtml' config setting"; 73811ac6abdSChristopher Smith } 73911ac6abdSChristopher Smith $msg .= ".<br/>Attempting to fallback to the bundled renderer."; 740548d801fSChristopher Smith msg($msg, -1, '', '', MSG_ADMINS_ONLY); 74111ac6abdSChristopher Smith 74273022918SAndreas Gohr $Renderer = new $rclass(); 743548d801fSChristopher Smith $Renderer->nocache(); // fallback only (and may include admin alerts), don't cache 744d968d3e5SChris Smith return $Renderer; 745d968d3e5SChris Smith } 746d968d3e5SChris Smith 747548d801fSChristopher Smith // fallback failed, alert the world 748548d801fSChristopher Smith msg("No renderer '$rname' found for mode '$mode'", -1); 749548d801fSChristopher Smith return null; 750548d801fSChristopher Smith} 751548d801fSChristopher Smith 752bb0a59d4Sjan/** 753bb0a59d4Sjan * Gets the first heading from a file 754bb0a59d4Sjan * 755fc18c0fbSchris * @param string $id dokuwiki page id 75667c15eceSMichael Hamann * @param int $render rerender if first heading not known 75767c15eceSMichael Hamann * default: METADATA_RENDER_USING_SIMPLE_CACHE 75867c15eceSMichael Hamann * Possible values: METADATA_DONT_RENDER, 75967c15eceSMichael Hamann * METADATA_RENDER_USING_SIMPLE_CACHE, 76065aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE, 76165aa8490SMichael Hamann * METADATA_RENDER_UNLIMITED 762e3ab6fc5SMichael Hamann * @return string|null The first heading 76342ea7f44SGerrit Uitslag * 76495dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 765bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de> 766bb0a59d4Sjan */ 76778b498a7SAndreas Gohrfunction p_get_first_heading($id, $render = METADATA_RENDER_USING_SIMPLE_CACHE) 76878b498a7SAndreas Gohr{ 76965aa8490SMichael Hamann return p_get_metadata(cleanID($id), 'title', $render); 770bb0a59d4Sjan} 771bb0a59d4Sjan 7728f7d700cSchris/** 7738f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output 7748f7d700cSchris * 7755d568b99SChris Smith * @param string $code source code to be highlighted 7765d568b99SChris Smith * @param string $language language to provide highlighting 7775d568b99SChris Smith * @param string $wrapper html element to wrap the returned highlighted text 778e3ab6fc5SMichael Hamann * @return string xhtml code 77942ea7f44SGerrit Uitslag * 7808f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk> 78135fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7828f7d700cSchris */ 78378b498a7SAndreas Gohrfunction p_xhtml_cached_geshi($code, $language, $wrapper = 'pre', array $options = null) 78478b498a7SAndreas Gohr{ 785ff1769deSAndreas Gohr global $conf, $config_cascade, $INPUT; 78635fbe9efSAndreas Gohr $language = strtolower($language); 7875d568b99SChris Smith 7885d568b99SChris Smith // remove any leading or trailing blank lines 7895d568b99SChris Smith $code = preg_replace('/^\s*?\n|\s*?\n$/', '', $code); 7905d568b99SChris Smith 791e2d88156SLarsDW223 $optionsmd5 = md5(serialize($options)); 792e2d88156SLarsDW223 $cache = getCacheName($language . $code . $optionsmd5, ".code"); 79335fbe9efSAndreas Gohr $ctime = @filemtime($cache); 794*7d34963bSAndreas Gohr if ( 795*7d34963bSAndreas Gohr $ctime && !$INPUT->bool('purge') && 79641d51802SAndreas Gohr $ctime > filemtime(DOKU_INC . 'vendor/composer/installed.json') && // libraries changed 797*7d34963bSAndreas Gohr $ctime > filemtime(reset($config_cascade['main']['default'])) 798*7d34963bSAndreas Gohr ) { // dokuwiki changed 7998f7d700cSchris $highlighted_code = io_readFile($cache, false); 8008f7d700cSchris } else { 80141d51802SAndreas Gohr $geshi = new GeSHi($code, $language); 8028f7d700cSchris $geshi->set_encoding('utf-8'); 8038f7d700cSchris $geshi->enable_classes(); 8048f7d700cSchris $geshi->set_header_type(GESHI_HEADER_PRE); 8058f7d700cSchris $geshi->set_link_target($conf['target']['extern']); 806e2d88156SLarsDW223 if ($options !== null) { 807e2d88156SLarsDW223 foreach ($options as $function => $params) { 80878b498a7SAndreas Gohr if (is_callable([$geshi, $function])) { 809e2d88156SLarsDW223 $geshi->$function($params); 810e2d88156SLarsDW223 } 811e2d88156SLarsDW223 } 812e2d88156SLarsDW223 } 8138f7d700cSchris 8145d568b99SChris Smith // remove GeSHi's wrapper element (we'll replace it with our own later) 8155d568b99SChris Smith // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 81669ddc332SAnika Henke $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!', '', $geshi->parse_code()), "\n\r"); 8178f7d700cSchris io_saveFile($cache, $highlighted_code); 8188f7d700cSchris } 8198f7d700cSchris 8205d568b99SChris Smith // add a wrapper element if required 8215d568b99SChris Smith if ($wrapper) { 8225d568b99SChris Smith return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 8235d568b99SChris Smith } else { 8248f7d700cSchris return $highlighted_code; 8258f7d700cSchris } 8265d568b99SChris Smith} 827