Merge pull request #3969 from dokuwiki-translate/lang_update_635_1684167422
[dokuwiki.git] / inc / parserutils.php
blob0f309cc3d98fbb8dee5b9f032a2e22dbbd27e191
1 <?php
2 /**
3 * Utilities for accessing the parser
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Harry Fuecks <hfuecks@gmail.com>
7 * @author Andreas Gohr <andi@splitbrain.org>
8 */
10 use dokuwiki\Cache\CacheInstructions;
11 use dokuwiki\Cache\CacheRenderer;
12 use dokuwiki\ChangeLog\PageChangeLog;
13 use dokuwiki\Extension\PluginController;
14 use dokuwiki\Extension\Event;
15 use dokuwiki\Extension\SyntaxPlugin;
16 use dokuwiki\Parsing\Parser;
17 use dokuwiki\Parsing\ParserMode\Acronym;
18 use dokuwiki\Parsing\ParserMode\Camelcaselink;
19 use dokuwiki\Parsing\ParserMode\Entity;
20 use dokuwiki\Parsing\ParserMode\Formatting;
21 use dokuwiki\Parsing\ParserMode\Smiley;
23 /**
24 * How many pages shall be rendered for getting metadata during one request
25 * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED
26 * is passed as render parameter to p_get_metadata.
28 if (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5);
30 /** Don't render metadata even if it is outdated or doesn't exist */
31 define('METADATA_DONT_RENDER', 0);
32 /**
33 * Render metadata when the page is really newer or the metadata doesn't exist.
34 * Uses just a simple check, but should work pretty well for loading simple
35 * metadata values like the page title and avoids rendering a lot of pages in
36 * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode.
37 * Use this if it is unlikely that the metadata value you are requesting
38 * does depend e.g. on pages that are included in the current page using
39 * the include plugin (this is very likely the case for the page title, but
40 * not for relation references).
42 define('METADATA_RENDER_USING_SIMPLE_CACHE', 1);
43 /**
44 * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT
45 * is used in this mode. Use this mode when you are requesting more complex
46 * metadata. Although this will cause rendering more often it might actually have
47 * the effect that less current metadata is returned as it is more likely than in
48 * the simple cache mode that metadata needs to be rendered for all pages at once
49 * which means that when the metadata for the page is requested that actually needs
50 * to be updated the limit might have been reached already.
52 define('METADATA_RENDER_USING_CACHE', 2);
53 /**
54 * Render metadata without limiting the number of pages for which metadata is
55 * rendered. Use this mode with care, normally it should only be used in places
56 * like the indexer or in cli scripts where the execution time normally isn't
57 * limited. This can be combined with the simple cache using
58 * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED.
60 define('METADATA_RENDER_UNLIMITED', 4);
62 /**
63 * Returns the parsed Wikitext in XHTML for the given id and revision.
65 * If $excuse is true an explanation is returned if the file
66 * wasn't found
68 * @param string $id page id
69 * @param string|int $rev revision timestamp or empty string
70 * @param bool $excuse
71 * @param string $date_at
73 * @return null|string
74 * @author Andreas Gohr <andi@splitbrain.org>
77 function p_wiki_xhtml($id, $rev = '', $excuse = true, $date_at = '')
79 $file = wikiFN($id, $rev);
80 $ret = '';
82 //ensure $id is in global $ID (needed for parsing)
83 global $ID;
84 $keep = $ID;
85 $ID = $id;
87 if ($rev || $date_at) {
88 if (file_exists($file)) {
89 //no caching on old revisions
90 $ret = p_render('xhtml', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at);
91 } elseif ($excuse) {
92 $ret = p_locale_xhtml('norev');
94 } else {
95 if (file_exists($file)) {
96 $ret = p_cached_output($file, 'xhtml', $id);
97 } elseif ($excuse) {
98 //check if the page once existed
99 $changelog = new PageChangeLog($id);
100 if ($changelog->hasRevisions()) {
101 $ret = p_locale_xhtml('onceexisted');
102 } else {
103 $ret = p_locale_xhtml('newpage');
108 //restore ID (just in case)
109 $ID = $keep;
111 return $ret;
115 * Returns the specified local text in parsed format
117 * @param string $id page id
118 * @return null|string
119 * @author Andreas Gohr <andi@splitbrain.org>
122 function p_locale_xhtml($id)
124 //fetch parsed locale
125 $data = ['id' => $id, 'html' => ''];
127 $event = new Event('PARSER_LOCALE_XHTML', $data);
128 if ($event->advise_before()) {
129 $data['html'] = p_cached_output(localeFN($data['id']));
131 $event->advise_after();
133 return $data['html'];
137 * Returns the given file parsed into the requested output format
139 * @param string $file filename, path to file
140 * @param string $format
141 * @param string $id page id
142 * @return null|string
143 * @author Andreas Gohr <andi@splitbrain.org>
144 * @author Chris Smith <chris@jalakai.co.uk>
147 function p_cached_output($file, $format = 'xhtml', $id = '')
149 global $conf;
151 $cache = new CacheRenderer($id, $file, $format);
152 if ($cache->useCache()) {
153 $parsed = $cache->retrieveCache(false);
154 if ($conf['allowdebug'] && $format == 'xhtml') {
155 $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
157 } else {
158 $parsed = p_render($format, p_cached_instructions($file, false, $id), $info);
160 if (!empty($info['cache']) && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile
161 if ($conf['allowdebug'] && $format == 'xhtml') {
162 $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
164 } else {
165 $cache->removeCache(); //try to delete cachefile
166 if ($conf['allowdebug'] && $format == 'xhtml') {
167 $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
172 return $parsed;
176 * Returns the render instructions for a file
178 * Uses and creates a serialized cache file
180 * @param string $file filename, path to file
181 * @param bool $cacheonly
182 * @param string $id page id
183 * @return array|null
184 * @author Andreas Gohr <andi@splitbrain.org>
187 function p_cached_instructions($file, $cacheonly = false, $id = '')
189 static $run = null;
190 if (is_null($run)) $run = [];
192 $cache = new CacheInstructions($id, $file);
194 if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) {
195 return $cache->retrieveCache();
196 } else if (file_exists($file)) {
197 // no cache - do some work
198 $ins = p_get_instructions(io_readWikiPage($file, $id));
199 if ($cache->storeCache($ins)) {
200 $run[$file] = true; // we won't rebuild these instructions in the same run again
201 } else {
202 msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.', -1);
204 return $ins;
207 return null;
211 * turns a page into a list of instructions
213 * @param string $text raw wiki syntax text
214 * @return array a list of instruction arrays
215 * @author Harry Fuecks <hfuecks@gmail.com>
216 * @author Andreas Gohr <andi@splitbrain.org>
219 function p_get_instructions($text)
222 $modes = p_get_parsermodes();
224 // Create the parser and handler
225 $Parser = new Parser(new Doku_Handler());
227 //add modes to parser
228 foreach ($modes as $mode) {
229 $Parser->addMode($mode['mode'], $mode['obj']);
232 // Do the parsing
233 Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text);
234 return $Parser->parse($text);
238 * returns the metadata of a page
240 * @param string $id The id of the page the metadata should be returned from
241 * @param string $key The key of the metdata value that shall be read (by default everything)
242 * separate hierarchies by " " like "date created"
243 * @param int $render If the page should be rendererd - possible values:
244 * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE
245 * METADATA_RENDER_UNLIMITED (also combined with the previous two options),
246 * default: METADATA_RENDER_USING_CACHE
247 * @return mixed The requested metadata fields
249 * @author Esther Brunner <esther@kaffeehaus.ch>
250 * @author Michael Hamann <michael@content-space.de>
252 function p_get_metadata($id, $key = '', $render = METADATA_RENDER_USING_CACHE)
254 global $ID;
255 static $render_count = 0;
256 // track pages that have already been rendered in order to avoid rendering the same page
257 // again
258 static $rendered_pages = [];
260 // cache the current page
261 // Benchmarking shows the current page's metadata is generally the only page metadata
262 // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
263 $cache = ($ID == $id);
264 $meta = p_read_metadata($id, $cache);
266 if (!is_numeric($render)) {
267 if ($render) {
268 $render = METADATA_RENDER_USING_SIMPLE_CACHE;
269 } else {
270 $render = METADATA_DONT_RENDER;
274 // prevent recursive calls in the cache
275 static $recursion = false;
276 if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id]) && page_exists($id)) {
277 $recursion = true;
279 $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata');
281 $do_render = false;
282 if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) {
283 if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) {
284 $pagefn = wikiFN($id);
285 $metafn = metaFN($id, '.meta');
286 if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) {
287 $do_render = true;
289 } elseif (!$cachefile->useCache()) {
290 $do_render = true;
293 if ($do_render) {
294 if (!defined('DOKU_UNITTEST')) {
295 ++$render_count;
296 $rendered_pages[$id] = true;
298 $old_meta = $meta;
299 $meta = p_render_metadata($id, $meta);
300 // only update the file when the metadata has been changed
301 if ($meta == $old_meta || p_save_metadata($id, $meta)) {
302 // store a timestamp in order to make sure that the cachefile is touched
303 // this timestamp is also stored when the meta data is still the same
304 $cachefile->storeCache(time());
305 } else {
306 msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1);
310 $recursion = false;
313 $val = $meta['current'];
315 // filter by $key
316 foreach (preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) {
317 if (!isset($val[$cur_key])) {
318 return null;
320 $val = $val[$cur_key];
322 return $val;
326 * sets metadata elements of a page
328 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
330 * @param string $id is the ID of a wiki page
331 * @param array $data is an array with key ⇒ value pairs to be set in the metadata
332 * @param boolean $render whether or not the page metadata should be generated with the renderer
333 * @param boolean $persistent indicates whether or not the particular metadata value will persist through
334 * the next metadata rendering.
335 * @return boolean true on success
337 * @author Esther Brunner <esther@kaffeehaus.ch>
338 * @author Michael Hamann <michael@content-space.de>
340 function p_set_metadata($id, $data, $render = false, $persistent = true)
342 if (!is_array($data)) return false;
344 global $ID, $METADATA_RENDERERS;
346 // if there is currently a renderer change the data in the renderer instead
347 if (isset($METADATA_RENDERERS[$id])) {
348 $orig =& $METADATA_RENDERERS[$id];
349 $meta = $orig;
350 } else {
351 // cache the current page
352 $cache = ($ID == $id);
353 $orig = p_read_metadata($id, $cache);
355 // render metadata first?
356 $meta = $render ? p_render_metadata($id, $orig) : $orig;
359 // now add the passed metadata
360 $protected = ['description', 'date', 'contributor'];
361 foreach ($data as $key => $value) {
363 // be careful with sub-arrays of $meta['relation']
364 if ($key == 'relation') {
366 foreach ($value as $subkey => $subvalue) {
367 if (isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
368 $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue);
369 } else {
370 $meta['current'][$key][$subkey] = $subvalue;
372 if ($persistent) {
373 if (isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
374 $meta['persistent'][$key][$subkey] = array_replace(
375 $meta['persistent'][$key][$subkey],
376 (array)$subvalue
378 } else {
379 $meta['persistent'][$key][$subkey] = $subvalue;
384 // be careful with some senisitive arrays of $meta
385 } elseif (in_array($key, $protected)) {
387 // these keys, must have subkeys - a legitimate value must be an array
388 if (is_array($value)) {
389 $meta['current'][$key] = !empty($meta['current'][$key]) ?
390 array_replace((array)$meta['current'][$key], $value) :
391 $value;
393 if ($persistent) {
394 $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ?
395 array_replace((array)$meta['persistent'][$key], $value) :
396 $value;
400 // no special treatment for the rest
401 } else {
402 $meta['current'][$key] = $value;
403 if ($persistent) $meta['persistent'][$key] = $value;
407 // save only if metadata changed
408 if ($meta == $orig) return true;
410 if (isset($METADATA_RENDERERS[$id])) {
411 // set both keys individually as the renderer has references to the individual keys
412 $METADATA_RENDERERS[$id]['current'] = $meta['current'];
413 $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
414 return true;
415 } else {
416 return p_save_metadata($id, $meta);
421 * Purges the non-persistant part of the meta data
422 * used on page deletion
424 * @param string $id page id
425 * @return bool success / fail
426 * @author Michael Klier <chi@chimeric.de>
429 function p_purge_metadata($id)
431 $meta = p_read_metadata($id);
432 foreach ($meta['current'] as $key => $value) {
433 if (isset($meta[$key]) && is_array($meta[$key])) {
434 $meta['current'][$key] = [];
435 } else {
436 $meta['current'][$key] = '';
440 return p_save_metadata($id, $meta);
444 * read the metadata from source/cache for $id
445 * (internal use only - called by p_get_metadata & p_set_metadata)
447 * @param string $id absolute wiki page id
448 * @param bool $cache whether or not to cache metadata in memory
449 * (only use for metadata likely to be accessed several times)
451 * @return array metadata
452 * @author Christopher Smith <chris@jalakai.co.uk>
455 function p_read_metadata($id, $cache = false)
457 global $cache_metadata;
459 if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
461 $file = metaFN($id, '.meta');
462 $meta = file_exists($file) ?
463 unserialize(io_readFile($file, false)) :
464 ['current' => [], 'persistent' => []];
466 if ($cache) {
467 $cache_metadata[(string)$id] = $meta;
470 return $meta;
474 * This is the backend function to save a metadata array to a file
476 * @param string $id absolute wiki page id
477 * @param array $meta metadata
479 * @return bool success / fail
481 function p_save_metadata($id, $meta)
483 // sync cached copies, including $INFO metadata
484 global $cache_metadata, $INFO;
486 if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
487 if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) {
488 $INFO['meta'] = $meta['current'];
491 return io_saveFile(metaFN($id, '.meta'), serialize($meta));
495 * renders the metadata of a page
497 * @param string $id page id
498 * @param array $orig the original metadata
499 * @return array|null array('current'=> array,'persistent'=> array);
500 * @author Esther Brunner <esther@kaffeehaus.ch>
503 function p_render_metadata($id, $orig)
505 // make sure the correct ID is in global ID
506 global $ID, $METADATA_RENDERERS;
508 // avoid recursive rendering processes for the same id
509 if (isset($METADATA_RENDERERS[$id])) {
510 return $orig;
513 // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it
514 $METADATA_RENDERERS[$id] =& $orig;
516 $keep = $ID;
517 $ID = $id;
519 // add an extra key for the event - to tell event handlers the page whose metadata this is
520 $orig['page'] = $id;
521 $evt = new Event('PARSER_METADATA_RENDER', $orig);
522 if ($evt->advise_before()) {
524 // get instructions
525 $instructions = p_cached_instructions(wikiFN($id), false, $id);
526 if (is_null($instructions)) {
527 $ID = $keep;
528 unset($METADATA_RENDERERS[$id]);
529 return null; // something went wrong with the instructions
532 // set up the renderer
533 $renderer = new Doku_Renderer_metadata();
534 $renderer->meta =& $orig['current'];
535 $renderer->persistent =& $orig['persistent'];
537 // loop through the instructions
538 foreach ($instructions as $instruction) {
539 // execute the callback against the renderer
540 call_user_func_array([&$renderer, $instruction[0]], (array)$instruction[1]);
543 $evt->result = ['current' => &$renderer->meta, 'persistent' => &$renderer->persistent];
545 $evt->advise_after();
547 // clean up
548 $ID = $keep;
549 unset($METADATA_RENDERERS[$id]);
550 return $evt->result;
554 * returns all available parser syntax modes in correct order
556 * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj' => plugin object)
557 * @author Andreas Gohr <andi@splitbrain.org>
560 function p_get_parsermodes()
562 global $conf;
564 //reuse old data
565 static $modes = null;
566 if ($modes != null && !defined('DOKU_UNITTEST')) {
567 return $modes;
570 //import parser classes and mode definitions
571 require_once DOKU_INC . 'inc/parser/parser.php';
573 // we now collect all syntax modes and their objects, then they will
574 // be sorted and added to the parser in correct order
575 $modes = [];
577 // add syntax plugins
578 $pluginlist = plugin_list('syntax');
579 if (count($pluginlist)) {
580 global $PARSER_MODES;
581 foreach ($pluginlist as $p) {
582 /** @var SyntaxPlugin $obj */
583 if (!$obj = plugin_load('syntax', $p)) continue; //attempt to load plugin into $obj
584 $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
585 //add to modes
586 $modes[] = [
587 'sort' => $obj->getSort(),
588 'mode' => "plugin_$p",
589 'obj' => $obj,
591 unset($obj); //remove the reference
595 // add default modes
596 $std_modes = [
597 'listblock', 'preformatted', 'notoc', 'nocache', 'header', 'table', 'linebreak', 'footnote', 'hr',
598 'unformatted', 'code', 'file', 'quote', 'internallink', 'rss', 'media', 'externallink',
599 'emaillink', 'windowssharelink', 'eol'
601 if ($conf['typography']) {
602 $std_modes[] = 'quotes';
603 $std_modes[] = 'multiplyentity';
605 foreach ($std_modes as $m) {
606 $class = 'dokuwiki\\Parsing\\ParserMode\\' . ucfirst($m);
607 $obj = new $class();
608 $modes[] = array(
609 'sort' => $obj->getSort(),
610 'mode' => $m,
611 'obj' => $obj
615 // add formatting modes
616 $fmt_modes = [
617 'strong', 'emphasis', 'underline', 'monospace', 'subscript', 'superscript', 'deleted'
619 foreach ($fmt_modes as $m) {
620 $obj = new Formatting($m);
621 $modes[] = array(
622 'sort' => $obj->getSort(),
623 'mode' => $m,
624 'obj' => $obj
628 // add modes which need files
629 $obj = new Smiley(array_keys(getSmileys()));
630 $modes[] = ['sort' => $obj->getSort(), 'mode' => 'smiley', 'obj' => $obj];
631 $obj = new Acronym(array_keys(getAcronyms()));
632 $modes[] = ['sort' => $obj->getSort(), 'mode' => 'acronym', 'obj' => $obj];
633 $obj = new Entity(array_keys(getEntities()));
634 $modes[] = ['sort' => $obj->getSort(), 'mode' => 'entity', 'obj' => $obj];
636 // add optional camelcase mode
637 if ($conf['camelcase']) {
638 $obj = new Camelcaselink();
639 $modes[] = ['sort' => $obj->getSort(), 'mode' => 'camelcaselink', 'obj' => $obj];
642 //sort modes
643 usort($modes, 'p_sort_modes');
645 return $modes;
649 * Callback function for usort
651 * @param array $a
652 * @param array $b
653 * @return int $a is lower/equal/higher than $b
654 * @author Andreas Gohr <andi@splitbrain.org>
657 function p_sort_modes($a, $b)
659 if ($a['sort'] == $b['sort']) return 0;
660 return ($a['sort'] < $b['sort']) ? -1 : 1;
664 * Renders a list of instruction to the specified output mode
666 * In the $info array is information from the renderer returned
668 * @param string $mode
669 * @param array|null|false $instructions
670 * @param array $info returns render info like enabled toc and cache
671 * @param string $date_at
672 * @return null|string rendered output
673 * @author Andreas Gohr <andi@splitbrain.org>
675 * @author Harry Fuecks <hfuecks@gmail.com>
677 function p_render($mode, $instructions, &$info, $date_at = '')
679 if (is_null($instructions)) return '';
680 if ($instructions === false) return '';
682 $Renderer = p_get_renderer($mode);
683 if (is_null($Renderer)) return null;
685 $Renderer->reset();
687 if ($date_at) {
688 $Renderer->date_at = $date_at;
691 $Renderer->smileys = getSmileys();
692 $Renderer->entities = getEntities();
693 $Renderer->acronyms = getAcronyms();
694 $Renderer->interwiki = getInterwiki();
696 // Loop through the instructions
697 foreach ($instructions as $instruction) {
698 // Execute the callback against the Renderer
699 if (method_exists($Renderer, $instruction[0])) {
700 call_user_func_array([&$Renderer, $instruction[0]], $instruction[1] ?: []);
704 //set info array
705 $info = $Renderer->info;
707 // Post process and return the output
708 $data = [$mode, & $Renderer->doc];
709 Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS', $data);
710 return $Renderer->doc;
714 * Figure out the correct renderer class to use for $mode,
715 * instantiate and return it
717 * @param string $mode Mode of the renderer to get
718 * @return null|Doku_Renderer The renderer
720 * @author Christopher Smith <chris@jalakai.co.uk>
722 function p_get_renderer($mode)
724 /** @var PluginController $plugin_controller */
725 global $conf, $plugin_controller;
727 $rname = !empty($conf['renderer_' . $mode]) ? $conf['renderer_' . $mode] : $mode;
728 $rclass = "Doku_Renderer_$rname";
730 // if requested earlier or a bundled renderer
731 if (class_exists($rclass)) {
732 return new $rclass();
735 // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format.
736 /** @var Doku_Renderer $Renderer */
737 $Renderer = $plugin_controller->load('renderer', $rname);
738 if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) {
739 return $Renderer;
742 // there is a configuration error!
743 // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer
744 $rclass = "Doku_Renderer_$mode";
745 if (class_exists($rclass)) {
746 // viewers should see renderered output, so restrict the warning to admins only
747 $msg = "No renderer '$rname' found for mode '$mode', check your plugins";
748 if ($mode == 'xhtml') {
749 $msg .= " and the 'renderer_xhtml' config setting";
751 $msg .= ".<br/>Attempting to fallback to the bundled renderer.";
752 msg($msg, -1, '', '', MSG_ADMINS_ONLY);
754 $Renderer = new $rclass;
755 $Renderer->nocache(); // fallback only (and may include admin alerts), don't cache
756 return $Renderer;
759 // fallback failed, alert the world
760 msg("No renderer '$rname' found for mode '$mode'", -1);
761 return null;
765 * Gets the first heading from a file
767 * @param string $id dokuwiki page id
768 * @param int $render rerender if first heading not known
769 * default: METADATA_RENDER_USING_SIMPLE_CACHE
770 * Possible values: METADATA_DONT_RENDER,
771 * METADATA_RENDER_USING_SIMPLE_CACHE,
772 * METADATA_RENDER_USING_CACHE,
773 * METADATA_RENDER_UNLIMITED
774 * @return string|null The first heading
776 * @author Andreas Gohr <andi@splitbrain.org>
777 * @author Michael Hamann <michael@content-space.de>
779 function p_get_first_heading($id, $render = METADATA_RENDER_USING_SIMPLE_CACHE)
781 return p_get_metadata(cleanID($id), 'title', $render);
785 * Wrapper for GeSHi Code Highlighter, provides caching of its output
787 * @param string $code source code to be highlighted
788 * @param string $language language to provide highlighting
789 * @param string $wrapper html element to wrap the returned highlighted text
790 * @return string xhtml code
792 * @author Christopher Smith <chris@jalakai.co.uk>
793 * @author Andreas Gohr <andi@splitbrain.org>
795 function p_xhtml_cached_geshi($code, $language, $wrapper = 'pre', array $options = null)
797 global $conf, $config_cascade, $INPUT;
798 $language = strtolower($language);
800 // remove any leading or trailing blank lines
801 $code = preg_replace('/^\s*?\n|\s*?\n$/', '', $code);
803 $optionsmd5 = md5(serialize($options));
804 $cache = getCacheName($language . $code . $optionsmd5, ".code");
805 $ctime = @filemtime($cache);
806 if ($ctime && !$INPUT->bool('purge') &&
807 $ctime > filemtime(DOKU_INC . 'vendor/composer/installed.json') && // libraries changed
808 $ctime > filemtime(reset($config_cascade['main']['default']))) { // dokuwiki changed
809 $highlighted_code = io_readFile($cache, false);
810 } else {
812 $geshi = new GeSHi($code, $language);
813 $geshi->set_encoding('utf-8');
814 $geshi->enable_classes();
815 $geshi->set_header_type(GESHI_HEADER_PRE);
816 $geshi->set_link_target($conf['target']['extern']);
817 if ($options !== null) {
818 foreach ($options as $function => $params) {
819 if (is_callable([$geshi, $function])) {
820 $geshi->$function($params);
825 // remove GeSHi's wrapper element (we'll replace it with our own later)
826 // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
827 $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!', '', $geshi->parse_code()), "\n\r");
828 io_saveFile($cache, $highlighted_code);
831 // add a wrapper element if required
832 if ($wrapper) {
833 return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
834 } else {
835 return $highlighted_code;