1c112d578Sandi<?php 2d4f83172SAndreas Gohr 3c112d578Sandi/** 4b8595a66SAndreas Gohr * Utilities for accessing the parser 5c112d578Sandi * 6c112d578Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 8c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 9c112d578Sandi */ 10d4f83172SAndreas Gohr 110db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 120db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 13e1d9dcc8SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 143a7140a1SAndreas Gohruse dokuwiki\Extension\PluginController; 15e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 16*71096e46SAndreas Gohruse dokuwiki\Parsing\Handler; 17c8dd1b9dSAndreas Gohruse dokuwiki\Parsing\ModeRegistry; 18d4d8fb18SAndreas Gohruse dokuwiki\Parsing\Parser; 19d4d8fb18SAndreas Gohr 20c112d578Sandi/** 2165aa8490SMichael Hamann * How many pages shall be rendered for getting metadata during one request 2265aa8490SMichael Hamann * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED 2365aa8490SMichael Hamann * is passed as render parameter to p_get_metadata. 24ff725173SMichael Hamann */ 2565aa8490SMichael Hamannif (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5); 2667c15eceSMichael Hamann 2767c15eceSMichael Hamann/** Don't render metadata even if it is outdated or doesn't exist */ 2867c15eceSMichael Hamanndefine('METADATA_DONT_RENDER', 0); 2965aa8490SMichael Hamann/** 3065aa8490SMichael Hamann * Render metadata when the page is really newer or the metadata doesn't exist. 3165aa8490SMichael Hamann * Uses just a simple check, but should work pretty well for loading simple 3265aa8490SMichael Hamann * metadata values like the page title and avoids rendering a lot of pages in 3365aa8490SMichael Hamann * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode. 3465aa8490SMichael Hamann * Use this if it is unlikely that the metadata value you are requesting 3565aa8490SMichael Hamann * does depend e.g. on pages that are included in the current page using 3665aa8490SMichael Hamann * the include plugin (this is very likely the case for the page title, but 3765aa8490SMichael Hamann * not for relation references). 3865aa8490SMichael Hamann */ 3967c15eceSMichael Hamanndefine('METADATA_RENDER_USING_SIMPLE_CACHE', 1); 4065aa8490SMichael Hamann/** 4165aa8490SMichael Hamann * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT 4265aa8490SMichael Hamann * is used in this mode. Use this mode when you are requesting more complex 4365aa8490SMichael Hamann * metadata. Although this will cause rendering more often it might actually have 4465aa8490SMichael Hamann * the effect that less current metadata is returned as it is more likely than in 4565aa8490SMichael Hamann * the simple cache mode that metadata needs to be rendered for all pages at once 4665aa8490SMichael Hamann * which means that when the metadata for the page is requested that actually needs 4765aa8490SMichael Hamann * to be updated the limit might have been reached already. 4865aa8490SMichael Hamann */ 4967c15eceSMichael Hamanndefine('METADATA_RENDER_USING_CACHE', 2); 5065aa8490SMichael Hamann/** 5165aa8490SMichael Hamann * Render metadata without limiting the number of pages for which metadata is 5265aa8490SMichael Hamann * rendered. Use this mode with care, normally it should only be used in places 5365aa8490SMichael Hamann * like the indexer or in cli scripts where the execution time normally isn't 5465aa8490SMichael Hamann * limited. This can be combined with the simple cache using 5565aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED. 5665aa8490SMichael Hamann */ 5765aa8490SMichael Hamanndefine('METADATA_RENDER_UNLIMITED', 4); 58ff725173SMichael Hamann 59ff725173SMichael Hamann/** 60c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision. 61c112d578Sandi * 62c112d578Sandi * If $excuse is true an explanation is returned if the file 63c112d578Sandi * wasn't found 64c112d578Sandi * 6542ea7f44SGerrit Uitslag * @param string $id page id 6642ea7f44SGerrit Uitslag * @param string|int $rev revision timestamp or empty string 6742ea7f44SGerrit Uitslag * @param bool $excuse 68f50a239bSTakamura * @param string $date_at 69f50a239bSTakamura * 7042ea7f44SGerrit Uitslag * @return null|string 7178b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7278b498a7SAndreas Gohr * 73c112d578Sandi */ 7478b498a7SAndreas Gohrfunction p_wiki_xhtml($id, $rev = '', $excuse = true, $date_at = '') 7578b498a7SAndreas Gohr{ 76c112d578Sandi $file = wikiFN($id, $rev); 77c112d578Sandi $ret = ''; 78c112d578Sandi 79c112d578Sandi //ensure $id is in global $ID (needed for parsing) 801e76272cSandi global $ID; 813ff8773bSAndreas Gohr $keep = $ID; 821e76272cSandi $ID = $id; 83c112d578Sandi 845c2eed9aSlisps if ($rev || $date_at) { 8579e79377SAndreas Gohr if (file_exists($file)) { 8664159a61SAndreas Gohr //no caching on old revisions 8764159a61SAndreas Gohr $ret = p_render('xhtml', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); 88c112d578Sandi } elseif ($excuse) { 89c112d578Sandi $ret = p_locale_xhtml('norev'); 90c112d578Sandi } 9124870174SAndreas Gohr } elseif (file_exists($file)) { 924b5f4f4eSchris $ret = p_cached_output($file, 'xhtml', $id); 93c112d578Sandi } elseif ($excuse) { 942eaa0567Speterfromearth //check if the page once existed 95e1d9dcc8SAndreas Gohr $changelog = new PageChangeLog($id); 962eaa0567Speterfromearth if ($changelog->hasRevisions()) { 972eaa0567Speterfromearth $ret = p_locale_xhtml('onceexisted'); 982eaa0567Speterfromearth } else { 99c112d578Sandi $ret = p_locale_xhtml('newpage'); 100c112d578Sandi } 101c112d578Sandi } 102c112d578Sandi 1033ff8773bSAndreas Gohr //restore ID (just in case) 1043ff8773bSAndreas Gohr $ID = $keep; 1053ff8773bSAndreas Gohr 106c112d578Sandi return $ret; 107c112d578Sandi} 108c112d578Sandi 109c112d578Sandi/** 110c112d578Sandi * Returns the specified local text in parsed format 111c112d578Sandi * 11242ea7f44SGerrit Uitslag * @param string $id page id 11342ea7f44SGerrit Uitslag * @return null|string 11478b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11578b498a7SAndreas Gohr * 116c112d578Sandi */ 11778b498a7SAndreas Gohrfunction p_locale_xhtml($id) 11878b498a7SAndreas Gohr{ 119c112d578Sandi //fetch parsed locale 12065f6b58fSAnna Dabrowska $data = ['id' => $id, 'html' => '']; 12165f6b58fSAnna Dabrowska 12265f6b58fSAnna Dabrowska $event = new Event('PARSER_LOCALE_XHTML', $data); 12365f6b58fSAnna Dabrowska if ($event->advise_before()) { 12465f6b58fSAnna Dabrowska $data['html'] = p_cached_output(localeFN($data['id'])); 12565f6b58fSAnna Dabrowska } 12665f6b58fSAnna Dabrowska $event->advise_after(); 12765f6b58fSAnna Dabrowska 12865f6b58fSAnna Dabrowska return $data['html']; 129c112d578Sandi} 130c112d578Sandi 131c112d578Sandi/** 1324b5f4f4eSchris * Returns the given file parsed into the requested output format 1334b5f4f4eSchris * 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 13878b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 13978b498a7SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 14078b498a7SAndreas Gohr * 1414b5f4f4eSchris */ 14278b498a7SAndreas Gohrfunction p_cached_output($file, $format = 'xhtml', $id = '') 14378b498a7SAndreas Gohr{ 144c112d578Sandi global $conf; 145c112d578Sandi 1460db5771eSMichael Große $cache = new CacheRenderer($id, $file, $format); 1474b5f4f4eSchris if ($cache->useCache()) { 14885767031SAndreas Gohr $parsed = $cache->retrieveCache(false); 1497de86af9SGerrit Uitslag if ($conf['allowdebug'] && $format == 'xhtml') { 1507de86af9SGerrit Uitslag $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; 1517de86af9SGerrit Uitslag } 152c112d578Sandi } else { 1534b5f4f4eSchris $parsed = p_render($format, p_cached_instructions($file, false, $id), $info); 154c112d578Sandi 155abca2f79SEduardo Mozart de Oliveira if (!empty($info['cache']) && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile 1567de86af9SGerrit Uitslag if ($conf['allowdebug'] && $format == 'xhtml') { 1577de86af9SGerrit Uitslag $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; 1587de86af9SGerrit Uitslag } 159c112d578Sandi } else { 1604b5f4f4eSchris $cache->removeCache(); //try to delete cachefile 1617de86af9SGerrit Uitslag if ($conf['allowdebug'] && $format == 'xhtml') { 1627de86af9SGerrit Uitslag $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; 1637de86af9SGerrit Uitslag } 164c112d578Sandi } 165c112d578Sandi } 166c112d578Sandi 167c112d578Sandi return $parsed; 168c112d578Sandi} 169c112d578Sandi 170c112d578Sandi/** 171c112d578Sandi * Returns the render instructions for a file 172c112d578Sandi * 173c112d578Sandi * Uses and creates a serialized cache file 174c112d578Sandi * 17542ea7f44SGerrit Uitslag * @param string $file filename, path to file 17642ea7f44SGerrit Uitslag * @param bool $cacheonly 17742ea7f44SGerrit Uitslag * @param string $id page id 17842ea7f44SGerrit Uitslag * @return array|null 17978b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 18078b498a7SAndreas Gohr * 181c112d578Sandi */ 18278b498a7SAndreas Gohrfunction p_cached_instructions($file, $cacheonly = false, $id = '') 18378b498a7SAndreas Gohr{ 18447b2d319SAndreas Gohr static $run = null; 18578b498a7SAndreas Gohr if (is_null($run)) $run = []; 186c112d578Sandi 1870db5771eSMichael Große $cache = new CacheInstructions($id, $file); 188c112d578Sandi 1890d24b616SMichael Hamann if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) { 1904b5f4f4eSchris return $cache->retrieveCache(); 19179e79377SAndreas Gohr } elseif (file_exists($file)) { 192c112d578Sandi // no cache - do some work 193bde4e341SGalaxyMaster $ins = p_get_instructions(io_readWikiPage($file, $id)); 194cbaf4259SChris Smith if ($cache->storeCache($ins)) { 19547b2d319SAndreas Gohr $run[$file] = true; // we won't rebuild these instructions in the same run again 196cbaf4259SChris Smith } else { 197cbaf4259SChris Smith msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.', -1); 198cbaf4259SChris Smith } 199c112d578Sandi return $ins; 200c112d578Sandi } 201c112d578Sandi 2023b3f8916SAndreas Gohr return null; 203c112d578Sandi} 204c112d578Sandi 205c112d578Sandi/** 206c112d578Sandi * turns a page into a list of instructions 207c112d578Sandi * 20878b498a7SAndreas Gohr * @param string $text raw wiki syntax text 20978b498a7SAndreas Gohr * @return array a list of instruction arrays 210c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com> 211c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org> 21242ea7f44SGerrit Uitslag * 213c112d578Sandi */ 21478b498a7SAndreas Gohrfunction p_get_instructions($text) 21578b498a7SAndreas Gohr{ 216c112d578Sandi 217c8dd1b9dSAndreas Gohr $modes = ModeRegistry::getInstance()->getModes(); 218ee20e7d1Sandi 21947f73ecfSAndreas Gohr // Create the parser and handler 220*71096e46SAndreas Gohr $Parser = new Parser(new Handler()); 221c112d578Sandi 222107b01d6Sandi //add modes to parser 223107b01d6Sandi foreach ($modes as $mode) { 224107b01d6Sandi $Parser->addMode($mode['mode'], $mode['obj']); 225c112d578Sandi } 226c112d578Sandi 227c112d578Sandi // Do the parsing 228cbb44eabSAndreas Gohr Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text); 22978b498a7SAndreas Gohr return $Parser->parse($text); 230c112d578Sandi} 231c112d578Sandi 232c112d578Sandi/** 23339a89382SEsther Brunner * returns the metadata of a page 23439a89382SEsther Brunner * 2354a819402SMichael Hamann * @param string $id The id of the page the metadata should be returned from 23664159a61SAndreas Gohr * @param string $key The key of the metdata value that shall be read (by default everything) 23764159a61SAndreas Gohr * separate hierarchies by " " like "date created" 23867c15eceSMichael Hamann * @param int $render If the page should be rendererd - possible values: 23965aa8490SMichael Hamann * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE 24065aa8490SMichael Hamann * METADATA_RENDER_UNLIMITED (also combined with the previous two options), 24165aa8490SMichael Hamann * default: METADATA_RENDER_USING_CACHE 2424a819402SMichael Hamann * @return mixed The requested metadata fields 2434a819402SMichael Hamann * 24439a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 24598214867SMichael Hamann * @author Michael Hamann <michael@content-space.de> 24639a89382SEsther Brunner */ 24778b498a7SAndreas Gohrfunction p_get_metadata($id, $key = '', $render = METADATA_RENDER_USING_CACHE) 24878b498a7SAndreas Gohr{ 2491172f8dcSAdrian Lang global $ID; 25065aa8490SMichael Hamann static $render_count = 0; 25165aa8490SMichael Hamann // track pages that have already been rendered in order to avoid rendering the same page 25265aa8490SMichael Hamann // again 25378b498a7SAndreas Gohr static $rendered_pages = []; 2546afe8dcaSchris 2550a7e3bceSchris // cache the current page 2560a7e3bceSchris // Benchmarking shows the current page's metadata is generally the only page metadata 2570a7e3bceSchris // accessed several times. This may catch a few other pages, but that shouldn't be an issue. 2580a7e3bceSchris $cache = ($ID == $id); 2590a7e3bceSchris $meta = p_read_metadata($id, $cache); 26039a89382SEsther Brunner 26167c15eceSMichael Hamann if (!is_numeric($render)) { 26267c15eceSMichael Hamann if ($render) { 26367c15eceSMichael Hamann $render = METADATA_RENDER_USING_SIMPLE_CACHE; 26467c15eceSMichael Hamann } else { 26567c15eceSMichael Hamann $render = METADATA_DONT_RENDER; 26667c15eceSMichael Hamann } 26767c15eceSMichael Hamann } 26867c15eceSMichael Hamann 26998214867SMichael Hamann // prevent recursive calls in the cache 27098214867SMichael Hamann static $recursion = false; 27165aa8490SMichael Hamann if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id]) && page_exists($id)) { 27298214867SMichael Hamann $recursion = true; 27398214867SMichael Hamann 2740db5771eSMichael Große $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata'); 27598214867SMichael Hamann 27667c15eceSMichael Hamann $do_render = false; 27765aa8490SMichael Hamann if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) { 27865aa8490SMichael Hamann if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) { 27967c15eceSMichael Hamann $pagefn = wikiFN($id); 28067c15eceSMichael Hamann $metafn = metaFN($id, '.meta'); 28179e79377SAndreas Gohr if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) { 28267c15eceSMichael Hamann $do_render = true; 28367c15eceSMichael Hamann } 28467c15eceSMichael Hamann } elseif (!$cachefile->useCache()) { 28567c15eceSMichael Hamann $do_render = true; 28667c15eceSMichael Hamann } 28765aa8490SMichael Hamann } 28867c15eceSMichael Hamann if ($do_render) { 2890d24b616SMichael Hamann if (!defined('DOKU_UNITTEST')) { 29065aa8490SMichael Hamann ++$render_count; 29165aa8490SMichael Hamann $rendered_pages[$id] = true; 2920d24b616SMichael Hamann } 29369ba640bSMichael Hamann $old_meta = $meta; 29439a89382SEsther Brunner $meta = p_render_metadata($id, $meta); 29569ba640bSMichael Hamann // only update the file when the metadata has been changed 29669ba640bSMichael Hamann if ($meta == $old_meta || p_save_metadata($id, $meta)) { 29798214867SMichael Hamann // store a timestamp in order to make sure that the cachefile is touched 2983d6feb16SMichael Hamann // this timestamp is also stored when the meta data is still the same 29998214867SMichael Hamann $cachefile->storeCache(time()); 3003d6feb16SMichael Hamann } else { 30198214867SMichael Hamann msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1); 30298214867SMichael Hamann } 30398214867SMichael Hamann } 30498214867SMichael Hamann 30598214867SMichael Hamann $recursion = false; 3066afe8dcaSchris } 30739a89382SEsther Brunner 308fa1a0dc6STB Maulana Aghni $val = $meta['current'] ?? null; 309ebf65d37SAdrian Lang 31039a89382SEsther Brunner // filter by $key 311569a0019SAdrian Lang foreach (preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { 312ebf65d37SAdrian Lang if (!isset($val[$cur_key])) { 313ebf65d37SAdrian Lang return null; 31466d29756SChris Smith } 315ebf65d37SAdrian Lang $val = $val[$cur_key]; 31639a89382SEsther Brunner } 317ebf65d37SAdrian Lang return $val; 31839a89382SEsther Brunner} 31939a89382SEsther Brunner 32039a89382SEsther Brunner/** 32139a89382SEsther Brunner * sets metadata elements of a page 32239a89382SEsther Brunner * 323a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata 324a365baeeSDominik Eckelmann * 325e1d9dcc8SAndreas Gohr * @param string $id is the ID of a wiki page 326e1d9dcc8SAndreas Gohr * @param array $data is an array with key ⇒ value pairs to be set in the metadata 327e1d9dcc8SAndreas Gohr * @param boolean $render whether or not the page metadata should be generated with the renderer 328e1d9dcc8SAndreas Gohr * @param boolean $persistent indicates whether or not the particular metadata value will persist through 329a365baeeSDominik Eckelmann * the next metadata rendering. 330a365baeeSDominik Eckelmann * @return boolean true on success 331a365baeeSDominik Eckelmann * 33239a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 3330e5fde48SMichael Hamann * @author Michael Hamann <michael@content-space.de> 33439a89382SEsther Brunner */ 33578b498a7SAndreas Gohrfunction p_set_metadata($id, $data, $render = false, $persistent = true) 33678b498a7SAndreas Gohr{ 33739a89382SEsther Brunner if (!is_array($data)) return false; 33839a89382SEsther Brunner 3390e5fde48SMichael Hamann global $ID, $METADATA_RENDERERS; 3400a7e3bceSchris 3410e5fde48SMichael Hamann // if there is currently a renderer change the data in the renderer instead 3420e5fde48SMichael Hamann if (isset($METADATA_RENDERERS[$id])) { 3430e5fde48SMichael Hamann $orig =& $METADATA_RENDERERS[$id]; 3440e5fde48SMichael Hamann $meta = $orig; 3450e5fde48SMichael Hamann } else { 3460a7e3bceSchris // cache the current page 3470a7e3bceSchris $cache = ($ID == $id); 3480a7e3bceSchris $orig = p_read_metadata($id, $cache); 34939a89382SEsther Brunner 35039a89382SEsther Brunner // render metadata first? 3510a7e3bceSchris $meta = $render ? p_render_metadata($id, $orig) : $orig; 3520e5fde48SMichael Hamann } 35339a89382SEsther Brunner 35439a89382SEsther Brunner // now add the passed metadata 35578b498a7SAndreas Gohr $protected = ['description', 'date', 'contributor']; 35639a89382SEsther Brunner foreach ($data as $key => $value) { 35739a89382SEsther Brunner // be careful with sub-arrays of $meta['relation'] 35839a89382SEsther Brunner if ($key == 'relation') { 35939a89382SEsther Brunner foreach ($value as $subkey => $subvalue) { 36031b10b49SMichael Hamann if (isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) { 3612d102494SPhy $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue); 36231b10b49SMichael Hamann } else { 36331b10b49SMichael Hamann $meta['current'][$key][$subkey] = $subvalue; 36431b10b49SMichael Hamann } 36531b10b49SMichael Hamann if ($persistent) { 36631b10b49SMichael Hamann if (isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) { 36764159a61SAndreas Gohr $meta['persistent'][$key][$subkey] = array_replace( 36864159a61SAndreas Gohr $meta['persistent'][$key][$subkey], 36964159a61SAndreas Gohr (array)$subvalue 37064159a61SAndreas Gohr ); 37131b10b49SMichael Hamann } else { 37231b10b49SMichael Hamann $meta['persistent'][$key][$subkey] = $subvalue; 37331b10b49SMichael Hamann } 37431b10b49SMichael Hamann } 37539a89382SEsther Brunner } 37639a89382SEsther Brunner 37739a89382SEsther Brunner // be careful with some senisitive arrays of $meta 378743a6908Ssplitbrain } elseif (in_array($key, $protected, true)) { 37966d29756SChris Smith // these keys, must have subkeys - a legitimate value must be an array 38039a89382SEsther Brunner if (is_array($value)) { 38124870174SAndreas Gohr $meta['current'][$key] = empty($meta['current'][$key]) ? 38224870174SAndreas Gohr $value : 38324870174SAndreas Gohr array_replace((array)$meta['current'][$key], $value); 3840a7e3bceSchris 3850a7e3bceSchris if ($persistent) { 38624870174SAndreas Gohr $meta['persistent'][$key] = empty($meta['persistent'][$key]) ? 38724870174SAndreas Gohr $value : 38824870174SAndreas Gohr array_replace((array)$meta['persistent'][$key], $value); 3890a7e3bceSchris } 39039a89382SEsther Brunner } 39139a89382SEsther Brunner 39239a89382SEsther Brunner // no special treatment for the rest 39339a89382SEsther Brunner } else { 3940a7e3bceSchris $meta['current'][$key] = $value; 3950a7e3bceSchris if ($persistent) $meta['persistent'][$key] = $value; 39639a89382SEsther Brunner } 39739a89382SEsther Brunner } 39839a89382SEsther Brunner 39939a89382SEsther Brunner // save only if metadata changed 40039a89382SEsther Brunner if ($meta == $orig) return true; 4016afe8dcaSchris 4020e5fde48SMichael Hamann if (isset($METADATA_RENDERERS[$id])) { 4030e5fde48SMichael Hamann // set both keys individually as the renderer has references to the individual keys 4040e5fde48SMichael Hamann $METADATA_RENDERERS[$id]['current'] = $meta['current']; 4050e5fde48SMichael Hamann $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; 4061c56be7bSMichael Hamann return true; 4070e5fde48SMichael Hamann } else { 4081172f8dcSAdrian Lang return p_save_metadata($id, $meta); 40939a89382SEsther Brunner } 4100e5fde48SMichael Hamann} 41139a89382SEsther Brunner 41239a89382SEsther Brunner/** 4133d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data 4143d1f9ec3SMichael Klier * used on page deletion 4153d1f9ec3SMichael Klier * 41642ea7f44SGerrit Uitslag * @param string $id page id 41742ea7f44SGerrit Uitslag * @return bool success / fail 41878b498a7SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 41978b498a7SAndreas Gohr * 4203d1f9ec3SMichael Klier */ 42178b498a7SAndreas Gohrfunction p_purge_metadata($id) 42278b498a7SAndreas Gohr{ 4233d1f9ec3SMichael Klier $meta = p_read_metadata($id); 4243d1f9ec3SMichael Klier foreach ($meta['current'] as $key => $value) { 425056bf31fSDamien Regad if (isset($meta[$key]) && is_array($meta[$key])) { 42678b498a7SAndreas Gohr $meta['current'][$key] = []; 4273d1f9ec3SMichael Klier } else { 4283d1f9ec3SMichael Klier $meta['current'][$key] = ''; 4293d1f9ec3SMichael Klier } 4303d1f9ec3SMichael Klier } 4311172f8dcSAdrian Lang return p_save_metadata($id, $meta); 4323d1f9ec3SMichael Klier} 4333d1f9ec3SMichael Klier 4343d1f9ec3SMichael Klier/** 4350a7e3bceSchris * read the metadata from source/cache for $id 4360a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata) 4370a7e3bceSchris * 4380a7e3bceSchris * @param string $id absolute wiki page id 4390a7e3bceSchris * @param bool $cache whether or not to cache metadata in memory 4400a7e3bceSchris * (only use for metadata likely to be accessed several times) 4410a7e3bceSchris * 4420a7e3bceSchris * @return array metadata 44378b498a7SAndreas Gohr * @author Christopher Smith <chris@jalakai.co.uk> 44478b498a7SAndreas Gohr * 4450a7e3bceSchris */ 44678b498a7SAndreas Gohrfunction p_read_metadata($id, $cache = false) 44778b498a7SAndreas Gohr{ 4480a7e3bceSchris global $cache_metadata; 4490a7e3bceSchris 4503a50618cSgweissbach if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id]; 4510a7e3bceSchris 4520a7e3bceSchris $file = metaFN($id, '.meta'); 45364159a61SAndreas Gohr $meta = file_exists($file) ? 45464159a61SAndreas Gohr unserialize(io_readFile($file, false)) : 45578b498a7SAndreas Gohr ['current' => [], 'persistent' => []]; 4560a7e3bceSchris 4570a7e3bceSchris if ($cache) { 4583a50618cSgweissbach $cache_metadata[(string)$id] = $meta; 4590a7e3bceSchris } 4600a7e3bceSchris 4610a7e3bceSchris return $meta; 4620a7e3bceSchris} 4630a7e3bceSchris 4640a7e3bceSchris/** 4651172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file 4661172f8dcSAdrian Lang * 4671172f8dcSAdrian Lang * @param string $id absolute wiki page id 4681172f8dcSAdrian Lang * @param array $meta metadata 4691172f8dcSAdrian Lang * 4701172f8dcSAdrian Lang * @return bool success / fail 4711172f8dcSAdrian Lang */ 47278b498a7SAndreas Gohrfunction p_save_metadata($id, $meta) 47378b498a7SAndreas Gohr{ 4741172f8dcSAdrian Lang // sync cached copies, including $INFO metadata 4751172f8dcSAdrian Lang global $cache_metadata, $INFO; 4761172f8dcSAdrian Lang 4771172f8dcSAdrian Lang if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta; 478056bf31fSDamien Regad if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) { 479056bf31fSDamien Regad $INFO['meta'] = $meta['current']; 480056bf31fSDamien Regad } 4811172f8dcSAdrian Lang 4821172f8dcSAdrian Lang return io_saveFile(metaFN($id, '.meta'), serialize($meta)); 4831172f8dcSAdrian Lang} 4841172f8dcSAdrian Lang 4851172f8dcSAdrian Lang/** 48639a89382SEsther Brunner * renders the metadata of a page 48739a89382SEsther Brunner * 48842ea7f44SGerrit Uitslag * @param string $id page id 48942ea7f44SGerrit Uitslag * @param array $orig the original metadata 49042ea7f44SGerrit Uitslag * @return array|null array('current'=> array,'persistent'=> array); 49178b498a7SAndreas Gohr * @author Esther Brunner <esther@kaffeehaus.ch> 49278b498a7SAndreas Gohr * 49339a89382SEsther Brunner */ 49478b498a7SAndreas Gohrfunction p_render_metadata($id, $orig) 49578b498a7SAndreas Gohr{ 49648924015SAndreas Gohr // make sure the correct ID is in global ID 4970e5fde48SMichael Hamann global $ID, $METADATA_RENDERERS; 4980e5fde48SMichael Hamann 4990e5fde48SMichael Hamann // avoid recursive rendering processes for the same id 5005b76ad91SChristopher Smith if (isset($METADATA_RENDERERS[$id])) { 5010e5fde48SMichael Hamann return $orig; 5025b76ad91SChristopher Smith } 5030e5fde48SMichael Hamann 5040e5fde48SMichael Hamann // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it 5050e5fde48SMichael Hamann $METADATA_RENDERERS[$id] =& $orig; 5060e5fde48SMichael Hamann 50748924015SAndreas Gohr $keep = $ID; 50848924015SAndreas Gohr $ID = $id; 50948924015SAndreas Gohr 5100a7e3bceSchris // add an extra key for the event - to tell event handlers the page whose metadata this is 5110a7e3bceSchris $orig['page'] = $id; 512e1d9dcc8SAndreas Gohr $evt = new Event('PARSER_METADATA_RENDER', $orig); 5130a7e3bceSchris if ($evt->advise_before()) { 51439a89382SEsther Brunner // get instructions 5154b5f4f4eSchris $instructions = p_cached_instructions(wikiFN($id), false, $id); 51648924015SAndreas Gohr if (is_null($instructions)) { 51748924015SAndreas Gohr $ID = $keep; 5180e5fde48SMichael Hamann unset($METADATA_RENDERERS[$id]); 51948924015SAndreas Gohr return null; // something went wrong with the instructions 52048924015SAndreas Gohr } 52139a89382SEsther Brunner 52239a89382SEsther Brunner // set up the renderer 52367f9913dSAndreas Gohr $renderer = new Doku_Renderer_metadata(); 5240e5fde48SMichael Hamann $renderer->meta =& $orig['current']; 5250e5fde48SMichael Hamann $renderer->persistent =& $orig['persistent']; 52639a89382SEsther Brunner 52739a89382SEsther Brunner // loop through the instructions 52839a89382SEsther Brunner foreach ($instructions as $instruction) { 52939a89382SEsther Brunner // execute the callback against the renderer 53078b498a7SAndreas Gohr call_user_func_array([&$renderer, $instruction[0]], (array)$instruction[1]); 53139a89382SEsther Brunner } 53239a89382SEsther Brunner 53378b498a7SAndreas Gohr $evt->result = ['current' => &$renderer->meta, 'persistent' => &$renderer->persistent]; 5340a7e3bceSchris } 5350a7e3bceSchris $evt->advise_after(); 5360a7e3bceSchris 5370e5fde48SMichael Hamann // clean up 53848924015SAndreas Gohr $ID = $keep; 5390e5fde48SMichael Hamann unset($METADATA_RENDERERS[$id]); 5400a7e3bceSchris return $evt->result; 54139a89382SEsther Brunner} 54239a89382SEsther Brunner 54339a89382SEsther Brunner/** 544ac83b9d8Sandi * Renders a list of instruction to the specified output mode 545c112d578Sandi * 5467a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned 5479dc2c2afSandi * 54842ea7f44SGerrit Uitslag * @param string $mode 549e3710957SGerrit Uitslag * @param array|null|false $instructions 55042ea7f44SGerrit Uitslag * @param array $info returns render info like enabled toc and cache 5517de86af9SGerrit Uitslag * @param string $date_at 55242ea7f44SGerrit Uitslag * @return null|string rendered output 55378b498a7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 55478b498a7SAndreas Gohr * 55578b498a7SAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com> 556c112d578Sandi */ 55778b498a7SAndreas Gohrfunction p_render($mode, $instructions, &$info, $date_at = '') 55878b498a7SAndreas Gohr{ 559c112d578Sandi if (is_null($instructions)) return ''; 560e3710957SGerrit Uitslag if ($instructions === false) return ''; 561c112d578Sandi 562252398f0SChristopher Smith $Renderer = p_get_renderer($mode); 563d968d3e5SChris Smith if (is_null($Renderer)) return null; 564c327d6c4SAndreas Gohr 565d968d3e5SChris Smith $Renderer->reset(); 566c112d578Sandi 5675c2eed9aSlisps if ($date_at) { 5685c2eed9aSlisps $Renderer->date_at = $date_at; 5695c2eed9aSlisps } 5705c2eed9aSlisps 571c112d578Sandi $Renderer->smileys = getSmileys(); 572c112d578Sandi $Renderer->entities = getEntities(); 573c112d578Sandi $Renderer->acronyms = getAcronyms(); 574c112d578Sandi $Renderer->interwiki = getInterwiki(); 575c112d578Sandi 576c112d578Sandi // Loop through the instructions 577c112d578Sandi foreach ($instructions as $instruction) { 578c112d578Sandi // Execute the callback against the Renderer 5797c62086bSAndreas Gohr if (method_exists($Renderer, $instruction[0])) { 58078b498a7SAndreas Gohr call_user_func_array([&$Renderer, $instruction[0]], $instruction[1] ?: []); 581c112d578Sandi } 5827c62086bSAndreas Gohr } 5839dc2c2afSandi 5849dc2c2afSandi //set info array 5859dc2c2afSandi $info = $Renderer->info; 5869dc2c2afSandi 587677844afSchris // Post process and return the output 58878b498a7SAndreas Gohr $data = [$mode, & $Renderer->doc]; 589cbb44eabSAndreas Gohr Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS', $data); 590c112d578Sandi return $Renderer->doc; 591c112d578Sandi} 592c112d578Sandi 593e3ab6fc5SMichael Hamann/** 594548d801fSChristopher Smith * Figure out the correct renderer class to use for $mode, 595548d801fSChristopher Smith * instantiate and return it 596548d801fSChristopher Smith * 5977e8500eeSGerrit Uitslag * @param string $mode Mode of the renderer to get 598e3ab6fc5SMichael Hamann * @return null|Doku_Renderer The renderer 599548d801fSChristopher Smith * 600548d801fSChristopher Smith * @author Christopher Smith <chris@jalakai.co.uk> 601e3ab6fc5SMichael Hamann */ 60278b498a7SAndreas Gohrfunction p_get_renderer($mode) 60378b498a7SAndreas Gohr{ 6043a7140a1SAndreas Gohr /** @var PluginController $plugin_controller */ 6057aea91afSChris Smith global $conf, $plugin_controller; 606d968d3e5SChris Smith 60724870174SAndreas Gohr $rname = empty($conf['renderer_' . $mode]) ? $mode : $conf['renderer_' . $mode]; 6080cacf91fSLucas $rclass = "Doku_Renderer_$rname"; 6090cacf91fSLucas 610548d801fSChristopher Smith // if requested earlier or a bundled renderer 6110cacf91fSLucas if (class_exists($rclass)) { 61278b498a7SAndreas Gohr return new $rclass(); 6130cacf91fSLucas } 614d968d3e5SChris Smith 6156e6d16edSChristopher Smith // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format. 6160440ca46SGerrit Uitslag /** @var Doku_Renderer $Renderer */ 61711ac6abdSChristopher Smith $Renderer = $plugin_controller->load('renderer', $rname); 6186e6d16edSChristopher Smith if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) { 61911ac6abdSChristopher Smith return $Renderer; 62011ac6abdSChristopher Smith } 621d968d3e5SChris Smith 62211ac6abdSChristopher Smith // there is a configuration error! 623548d801fSChristopher Smith // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer 62411ac6abdSChristopher Smith $rclass = "Doku_Renderer_$mode"; 625548d801fSChristopher Smith if (class_exists($rclass)) { 62611ac6abdSChristopher Smith // viewers should see renderered output, so restrict the warning to admins only 62711ac6abdSChristopher Smith $msg = "No renderer '$rname' found for mode '$mode', check your plugins"; 62811ac6abdSChristopher Smith if ($mode == 'xhtml') { 62911ac6abdSChristopher Smith $msg .= " and the 'renderer_xhtml' config setting"; 63011ac6abdSChristopher Smith } 63111ac6abdSChristopher Smith $msg .= ".<br/>Attempting to fallback to the bundled renderer."; 632548d801fSChristopher Smith msg($msg, -1, '', '', MSG_ADMINS_ONLY); 63311ac6abdSChristopher Smith 63473022918SAndreas Gohr $Renderer = new $rclass(); 635548d801fSChristopher Smith $Renderer->nocache(); // fallback only (and may include admin alerts), don't cache 636d968d3e5SChris Smith return $Renderer; 637d968d3e5SChris Smith } 638d968d3e5SChris Smith 639548d801fSChristopher Smith // fallback failed, alert the world 640548d801fSChristopher Smith msg("No renderer '$rname' found for mode '$mode'", -1); 641548d801fSChristopher Smith return null; 642548d801fSChristopher Smith} 643548d801fSChristopher Smith 644bb0a59d4Sjan/** 645bb0a59d4Sjan * Gets the first heading from a file 646bb0a59d4Sjan * 647fc18c0fbSchris * @param string $id dokuwiki page id 64867c15eceSMichael Hamann * @param int $render rerender if first heading not known 64967c15eceSMichael Hamann * default: METADATA_RENDER_USING_SIMPLE_CACHE 65067c15eceSMichael Hamann * Possible values: METADATA_DONT_RENDER, 65167c15eceSMichael Hamann * METADATA_RENDER_USING_SIMPLE_CACHE, 65265aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE, 65365aa8490SMichael Hamann * METADATA_RENDER_UNLIMITED 654e3ab6fc5SMichael Hamann * @return string|null The first heading 65542ea7f44SGerrit Uitslag * 65695dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 657bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de> 658bb0a59d4Sjan */ 65978b498a7SAndreas Gohrfunction p_get_first_heading($id, $render = METADATA_RENDER_USING_SIMPLE_CACHE) 66078b498a7SAndreas Gohr{ 66165aa8490SMichael Hamann return p_get_metadata(cleanID($id), 'title', $render); 662bb0a59d4Sjan} 663bb0a59d4Sjan 6648f7d700cSchris/** 6658f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output 6668f7d700cSchris * 6675d568b99SChris Smith * @param string $code source code to be highlighted 6685d568b99SChris Smith * @param string $language language to provide highlighting 6695d568b99SChris Smith * @param string $wrapper html element to wrap the returned highlighted text 670e3ab6fc5SMichael Hamann * @return string xhtml code 67142ea7f44SGerrit Uitslag * 6728f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk> 67335fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6748f7d700cSchris */ 67599a0b426SAndreas Gohrfunction p_xhtml_cached_geshi($code, $language, $wrapper = 'pre', ?array $options = null) 67678b498a7SAndreas Gohr{ 677ff1769deSAndreas Gohr global $conf, $config_cascade, $INPUT; 67835fbe9efSAndreas Gohr $language = strtolower($language); 6795d568b99SChris Smith 6805d568b99SChris Smith // remove any leading or trailing blank lines 6815d568b99SChris Smith $code = preg_replace('/^\s*?\n|\s*?\n$/', '', $code); 6825d568b99SChris Smith 683e2d88156SLarsDW223 $optionsmd5 = md5(serialize($options)); 684e2d88156SLarsDW223 $cache = getCacheName($language . $code . $optionsmd5, ".code"); 68535fbe9efSAndreas Gohr $ctime = @filemtime($cache); 6867d34963bSAndreas Gohr if ( 6877d34963bSAndreas Gohr $ctime && !$INPUT->bool('purge') && 68841d51802SAndreas Gohr $ctime > filemtime(DOKU_INC . 'vendor/composer/installed.json') && // libraries changed 6897d34963bSAndreas Gohr $ctime > filemtime(reset($config_cascade['main']['default'])) 6907d34963bSAndreas Gohr ) { // dokuwiki changed 6918f7d700cSchris $highlighted_code = io_readFile($cache, false); 6928f7d700cSchris } else { 69341d51802SAndreas Gohr $geshi = new GeSHi($code, $language); 6948f7d700cSchris $geshi->set_encoding('utf-8'); 6958f7d700cSchris $geshi->enable_classes(); 6968f7d700cSchris $geshi->set_header_type(GESHI_HEADER_PRE); 6978f7d700cSchris $geshi->set_link_target($conf['target']['extern']); 698e2d88156SLarsDW223 if ($options !== null) { 699e2d88156SLarsDW223 foreach ($options as $function => $params) { 70078b498a7SAndreas Gohr if (is_callable([$geshi, $function])) { 701e2d88156SLarsDW223 $geshi->$function($params); 702e2d88156SLarsDW223 } 703e2d88156SLarsDW223 } 704e2d88156SLarsDW223 } 7058f7d700cSchris 7065d568b99SChris Smith // remove GeSHi's wrapper element (we'll replace it with our own later) 7075d568b99SChris Smith // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text 70869ddc332SAnika Henke $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!', '', $geshi->parse_code()), "\n\r"); 7098f7d700cSchris io_saveFile($cache, $highlighted_code); 7108f7d700cSchris } 7118f7d700cSchris 7125d568b99SChris Smith // add a wrapper element if required 7135d568b99SChris Smith if ($wrapper) { 7145d568b99SChris Smith return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>"; 7155d568b99SChris Smith } else { 7168f7d700cSchris return $highlighted_code; 7178f7d700cSchris } 7185d568b99SChris Smith} 719