xref: /dokuwiki/inc/parserutils.php (revision a19c9aa0217112e3ab7ebc160354c7e9fbabe8eb)
1<?php
2/**
3 * Utilities for accessing the parser
4 *
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 */
9use dokuwiki\Extension\PluginInterface;
10use dokuwiki\Cache\CacheInstructions;
11use dokuwiki\Cache\CacheRenderer;
12use dokuwiki\ChangeLog\PageChangeLog;
13use dokuwiki\Extension\PluginController;
14use dokuwiki\Extension\Event;
15use dokuwiki\Extension\SyntaxPlugin;
16use dokuwiki\Parsing\Parser;
17use dokuwiki\Parsing\ParserMode\Acronym;
18use dokuwiki\Parsing\ParserMode\Camelcaselink;
19use dokuwiki\Parsing\ParserMode\Entity;
20use dokuwiki\Parsing\ParserMode\Formatting;
21use dokuwiki\Parsing\ParserMode\Smiley;
22
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.
27 */
28if (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5);
29
30/** Don't render metadata even if it is outdated or doesn't exist */
31define('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).
41 */
42define('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.
51 */
52define('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.
59 */
60define('METADATA_RENDER_UNLIMITED', 4);
61
62/**
63 * Returns the parsed Wikitext in XHTML for the given id and revision.
64 *
65 * If $excuse is true an explanation is returned if the file
66 * wasn't found
67 *
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
72 *
73 * @return null|string
74 * @author Andreas Gohr <andi@splitbrain.org>
75 *
76 */
77function p_wiki_xhtml($id, $rev = '', $excuse = true, $date_at = '')
78{
79    $file = wikiFN($id, $rev);
80    $ret = '';
81
82    //ensure $id is in global $ID (needed for parsing)
83    global $ID;
84    $keep = $ID;
85    $ID = $id;
86
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');
93        }
94    } elseif (file_exists($file)) {
95        $ret = p_cached_output($file, 'xhtml', $id);
96    } elseif ($excuse) {
97        //check if the page once existed
98        $changelog = new PageChangeLog($id);
99        if ($changelog->hasRevisions()) {
100            $ret = p_locale_xhtml('onceexisted');
101        } else {
102            $ret = p_locale_xhtml('newpage');
103        }
104    }
105
106    //restore ID (just in case)
107    $ID = $keep;
108
109    return $ret;
110}
111
112/**
113 * Returns the specified local text in parsed format
114 *
115 * @param string $id page id
116 * @return null|string
117 * @author Andreas Gohr <andi@splitbrain.org>
118 *
119 */
120function p_locale_xhtml($id)
121{
122    //fetch parsed locale
123    $data = ['id' => $id, 'html' => ''];
124
125    $event = new Event('PARSER_LOCALE_XHTML', $data);
126    if ($event->advise_before()) {
127        $data['html'] = p_cached_output(localeFN($data['id']));
128    }
129    $event->advise_after();
130
131    return $data['html'];
132}
133
134/**
135 * Returns the given file parsed into the requested output format
136 *
137 * @param string $file filename, path to file
138 * @param string $format
139 * @param string $id page id
140 * @return null|string
141 * @author Andreas Gohr <andi@splitbrain.org>
142 * @author Chris Smith <chris@jalakai.co.uk>
143 *
144 */
145function p_cached_output($file, $format = 'xhtml', $id = '')
146{
147    global $conf;
148
149    $cache = new CacheRenderer($id, $file, $format);
150    if ($cache->useCache()) {
151        $parsed = $cache->retrieveCache(false);
152        if ($conf['allowdebug'] && $format == 'xhtml') {
153            $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
154        }
155    } else {
156        $parsed = p_render($format, p_cached_instructions($file, false, $id), $info);
157
158        if (!empty($info['cache']) && $cache->storeCache($parsed)) { // storeCache() attempts to save cachefile
159            if ($conf['allowdebug'] && $format == 'xhtml') {
160                $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
161            }
162        } else {
163            $cache->removeCache(); //try to delete cachefile
164            if ($conf['allowdebug'] && $format == 'xhtml') {
165                $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
166            }
167        }
168    }
169
170    return $parsed;
171}
172
173/**
174 * Returns the render instructions for a file
175 *
176 * Uses and creates a serialized cache file
177 *
178 * @param string $file filename, path to file
179 * @param bool $cacheonly
180 * @param string $id page id
181 * @return array|null
182 * @author Andreas Gohr <andi@splitbrain.org>
183 *
184 */
185function p_cached_instructions($file, $cacheonly = false, $id = '')
186{
187    static $run = null;
188    if (is_null($run)) $run = [];
189
190    $cache = new CacheInstructions($id, $file);
191
192    if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) {
193        return $cache->retrieveCache();
194    } elseif (file_exists($file)) {
195        // no cache - do some work
196        $ins = p_get_instructions(io_readWikiPage($file, $id));
197        if ($cache->storeCache($ins)) {
198            $run[$file] = true; // we won't rebuild these instructions in the same run again
199        } else {
200            msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.', -1);
201        }
202        return $ins;
203    }
204
205    return null;
206}
207
208/**
209 * turns a page into a list of instructions
210 *
211 * @param string $text raw wiki syntax text
212 * @return array a list of instruction arrays
213 * @author Harry Fuecks <hfuecks@gmail.com>
214 * @author Andreas Gohr <andi@splitbrain.org>
215 *
216 */
217function p_get_instructions($text)
218{
219
220    $modes = p_get_parsermodes();
221
222    // Create the parser and handler
223    $Parser = new Parser(new Doku_Handler());
224
225    //add modes to parser
226    foreach ($modes as $mode) {
227        $Parser->addMode($mode['mode'], $mode['obj']);
228    }
229
230    // Do the parsing
231    Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text);
232    return $Parser->parse($text);
233}
234
235/**
236 * returns the metadata of a page
237 *
238 * @param string $id The id of the page the metadata should be returned from
239 * @param string $key The key of the metdata value that shall be read (by default everything)
240 *                        separate hierarchies by " " like "date created"
241 * @param int $render If the page should be rendererd - possible values:
242 *     METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE
243 *     METADATA_RENDER_UNLIMITED (also combined with the previous two options),
244 *     default: METADATA_RENDER_USING_CACHE
245 * @return mixed The requested metadata fields
246 *
247 * @author Esther Brunner <esther@kaffeehaus.ch>
248 * @author Michael Hamann <michael@content-space.de>
249 */
250function p_get_metadata($id, $key = '', $render = METADATA_RENDER_USING_CACHE)
251{
252    global $ID;
253    static $render_count = 0;
254    // track pages that have already been rendered in order to avoid rendering the same page
255    // again
256    static $rendered_pages = [];
257
258    // cache the current page
259    // Benchmarking shows the current page's metadata is generally the only page metadata
260    // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
261    $cache = ($ID == $id);
262    $meta = p_read_metadata($id, $cache);
263
264    if (!is_numeric($render)) {
265        if ($render) {
266            $render = METADATA_RENDER_USING_SIMPLE_CACHE;
267        } else {
268            $render = METADATA_DONT_RENDER;
269        }
270    }
271
272    // prevent recursive calls in the cache
273    static $recursion = false;
274    if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id]) && page_exists($id)) {
275        $recursion = true;
276
277        $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata');
278
279        $do_render = false;
280        if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) {
281            if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) {
282                $pagefn = wikiFN($id);
283                $metafn = metaFN($id, '.meta');
284                if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) {
285                    $do_render = true;
286                }
287            } elseif (!$cachefile->useCache()) {
288                $do_render = true;
289            }
290        }
291        if ($do_render) {
292            if (!defined('DOKU_UNITTEST')) {
293                ++$render_count;
294                $rendered_pages[$id] = true;
295            }
296            $old_meta = $meta;
297            $meta = p_render_metadata($id, $meta);
298            // only update the file when the metadata has been changed
299            if ($meta == $old_meta || p_save_metadata($id, $meta)) {
300                // store a timestamp in order to make sure that the cachefile is touched
301                // this timestamp is also stored when the meta data is still the same
302                $cachefile->storeCache(time());
303            } else {
304                msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1);
305            }
306        }
307
308        $recursion = false;
309    }
310
311    $val = $meta['current'];
312
313    // filter by $key
314    foreach (preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) {
315        if (!isset($val[$cur_key])) {
316            return null;
317        }
318        $val = $val[$cur_key];
319    }
320    return $val;
321}
322
323/**
324 * sets metadata elements of a page
325 *
326 * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
327 *
328 * @param string $id is the ID of a wiki page
329 * @param array $data is an array with key ⇒ value pairs to be set in the metadata
330 * @param boolean $render whether or not the page metadata should be generated with the renderer
331 * @param boolean $persistent indicates whether or not the particular metadata value will persist through
332 *                            the next metadata rendering.
333 * @return boolean true on success
334 *
335 * @author Esther Brunner <esther@kaffeehaus.ch>
336 * @author Michael Hamann <michael@content-space.de>
337 */
338function p_set_metadata($id, $data, $render = false, $persistent = true)
339{
340    if (!is_array($data)) return false;
341
342    global $ID, $METADATA_RENDERERS;
343
344    // if there is currently a renderer change the data in the renderer instead
345    if (isset($METADATA_RENDERERS[$id])) {
346        $orig =& $METADATA_RENDERERS[$id];
347        $meta = $orig;
348    } else {
349        // cache the current page
350        $cache = ($ID == $id);
351        $orig = p_read_metadata($id, $cache);
352
353        // render metadata first?
354        $meta = $render ? p_render_metadata($id, $orig) : $orig;
355    }
356
357    // now add the passed metadata
358    $protected = ['description', 'date', 'contributor'];
359    foreach ($data as $key => $value) {
360
361        // be careful with sub-arrays of $meta['relation']
362        if ($key == 'relation') {
363
364            foreach ($value as $subkey => $subvalue) {
365                if (isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
366                    $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue);
367                } else {
368                    $meta['current'][$key][$subkey] = $subvalue;
369                }
370                if ($persistent) {
371                    if (isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
372                        $meta['persistent'][$key][$subkey] = array_replace(
373                            $meta['persistent'][$key][$subkey],
374                            (array)$subvalue
375                        );
376                    } else {
377                        $meta['persistent'][$key][$subkey] = $subvalue;
378                    }
379                }
380            }
381
382            // be careful with some senisitive arrays of $meta
383        } elseif (in_array($key, $protected)) {
384
385            // these keys, must have subkeys - a legitimate value must be an array
386            if (is_array($value)) {
387                $meta['current'][$key] = empty($meta['current'][$key]) ?
388                    $value :
389                    array_replace((array)$meta['current'][$key], $value);
390
391                if ($persistent) {
392                    $meta['persistent'][$key] = empty($meta['persistent'][$key]) ?
393                        $value :
394                        array_replace((array)$meta['persistent'][$key], $value);
395                }
396            }
397
398            // no special treatment for the rest
399        } else {
400            $meta['current'][$key] = $value;
401            if ($persistent) $meta['persistent'][$key] = $value;
402        }
403    }
404
405    // save only if metadata changed
406    if ($meta == $orig) return true;
407
408    if (isset($METADATA_RENDERERS[$id])) {
409        // set both keys individually as the renderer has references to the individual keys
410        $METADATA_RENDERERS[$id]['current'] = $meta['current'];
411        $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
412        return true;
413    } else {
414        return p_save_metadata($id, $meta);
415    }
416}
417
418/**
419 * Purges the non-persistant part of the meta data
420 * used on page deletion
421 *
422 * @param string $id page id
423 * @return bool  success / fail
424 * @author Michael Klier <chi@chimeric.de>
425 *
426 */
427function p_purge_metadata($id)
428{
429    $meta = p_read_metadata($id);
430    foreach ($meta['current'] as $key => $value) {
431        if (isset($meta[$key]) && is_array($meta[$key])) {
432            $meta['current'][$key] = [];
433        } else {
434            $meta['current'][$key] = '';
435        }
436
437    }
438    return p_save_metadata($id, $meta);
439}
440
441/**
442 * read the metadata from source/cache for $id
443 * (internal use only - called by p_get_metadata & p_set_metadata)
444 *
445 * @param string $id absolute wiki page id
446 * @param bool $cache whether or not to cache metadata in memory
447 *                             (only use for metadata likely to be accessed several times)
448 *
449 * @return   array             metadata
450 * @author   Christopher Smith <chris@jalakai.co.uk>
451 *
452 */
453function p_read_metadata($id, $cache = false)
454{
455    global $cache_metadata;
456
457    if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
458
459    $file = metaFN($id, '.meta');
460    $meta = file_exists($file) ?
461        unserialize(io_readFile($file, false)) :
462        ['current' => [], 'persistent' => []];
463
464    if ($cache) {
465        $cache_metadata[(string)$id] = $meta;
466    }
467
468    return $meta;
469}
470
471/**
472 * This is the backend function to save a metadata array to a file
473 *
474 * @param string $id absolute wiki page id
475 * @param array $meta metadata
476 *
477 * @return   bool              success / fail
478 */
479function p_save_metadata($id, $meta)
480{
481    // sync cached copies, including $INFO metadata
482    global $cache_metadata, $INFO;
483
484    if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
485    if (!empty($INFO) && isset($INFO['id']) && ($id == $INFO['id'])) {
486        $INFO['meta'] = $meta['current'];
487    }
488
489    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
490}
491
492/**
493 * renders the metadata of a page
494 *
495 * @param string $id page id
496 * @param array $orig the original metadata
497 * @return array|null array('current'=> array,'persistent'=> array);
498 * @author Esther Brunner <esther@kaffeehaus.ch>
499 *
500 */
501function p_render_metadata($id, $orig)
502{
503    // make sure the correct ID is in global ID
504    global $ID, $METADATA_RENDERERS;
505
506    // avoid recursive rendering processes for the same id
507    if (isset($METADATA_RENDERERS[$id])) {
508        return $orig;
509    }
510
511    // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it
512    $METADATA_RENDERERS[$id] =& $orig;
513
514    $keep = $ID;
515    $ID = $id;
516
517    // add an extra key for the event - to tell event handlers the page whose metadata this is
518    $orig['page'] = $id;
519    $evt = new Event('PARSER_METADATA_RENDER', $orig);
520    if ($evt->advise_before()) {
521
522        // get instructions
523        $instructions = p_cached_instructions(wikiFN($id), false, $id);
524        if (is_null($instructions)) {
525            $ID = $keep;
526            unset($METADATA_RENDERERS[$id]);
527            return null; // something went wrong with the instructions
528        }
529
530        // set up the renderer
531        $renderer = new Doku_Renderer_metadata();
532        $renderer->meta =& $orig['current'];
533        $renderer->persistent =& $orig['persistent'];
534
535        // loop through the instructions
536        foreach ($instructions as $instruction) {
537            // execute the callback against the renderer
538            call_user_func_array([&$renderer, $instruction[0]], (array)$instruction[1]);
539        }
540
541        $evt->result = ['current' => &$renderer->meta, 'persistent' => &$renderer->persistent];
542    }
543    $evt->advise_after();
544
545    // clean up
546    $ID = $keep;
547    unset($METADATA_RENDERERS[$id]);
548    return $evt->result;
549}
550
551/**
552 * returns all available parser syntax modes in correct order
553 *
554 * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj'  => plugin object)
555 * @author Andreas Gohr <andi@splitbrain.org>
556 *
557 */
558function p_get_parsermodes()
559{
560    global $conf;
561
562    //reuse old data
563    static $modes = null;
564    if ($modes != null && !defined('DOKU_UNITTEST')) {
565        return $modes;
566    }
567
568    //import parser classes and mode definitions
569    require_once DOKU_INC . 'inc/parser/parser.php';
570
571    // we now collect all syntax modes and their objects, then they will
572    // be sorted and added to the parser in correct order
573    $modes = [];
574
575    // add syntax plugins
576    $pluginlist = plugin_list('syntax');
577    if ($pluginlist !== []) {
578        global $PARSER_MODES;
579        foreach ($pluginlist as $p) {
580            /** @var SyntaxPlugin $obj */
581            if (!($obj = plugin_load('syntax', $p)) instanceof PluginInterface) continue;
582            $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
583            //add to modes
584            $modes[] = [
585                'sort' => $obj->getSort(),
586                'mode' => "plugin_$p",
587                'obj' => $obj,
588            ];
589            unset($obj); //remove the reference
590        }
591    }
592
593    // add default modes
594    $std_modes = [
595        'listblock', 'preformatted', 'notoc', 'nocache', 'header', 'table', 'linebreak', 'footnote', 'hr',
596        'unformatted', 'code', 'file', 'quote', 'internallink', 'rss', 'media', 'externallink',
597        'emaillink', 'windowssharelink', 'eol'
598    ];
599    if ($conf['typography']) {
600        $std_modes[] = 'quotes';
601        $std_modes[] = 'multiplyentity';
602    }
603    foreach ($std_modes as $m) {
604        $class = 'dokuwiki\\Parsing\\ParserMode\\' . ucfirst($m);
605        $obj = new $class();
606        $modes[] = ['sort' => $obj->getSort(), 'mode' => $m, 'obj' => $obj];
607    }
608
609    // add formatting modes
610    $fmt_modes = [
611        'strong', 'emphasis', 'underline', 'monospace', 'subscript', 'superscript', 'deleted'
612    ];
613    foreach ($fmt_modes as $m) {
614        $obj = new Formatting($m);
615        $modes[] = [
616            'sort' => $obj->getSort(),
617            'mode' => $m,
618            'obj' => $obj
619        ];
620    }
621
622    // add modes which need files
623    $obj = new Smiley(array_keys(getSmileys()));
624    $modes[] = ['sort' => $obj->getSort(), 'mode' => 'smiley', 'obj' => $obj];
625    $obj = new Acronym(array_keys(getAcronyms()));
626    $modes[] = ['sort' => $obj->getSort(), 'mode' => 'acronym', 'obj' => $obj];
627    $obj = new Entity(array_keys(getEntities()));
628    $modes[] = ['sort' => $obj->getSort(), 'mode' => 'entity', 'obj' => $obj];
629
630    // add optional camelcase mode
631    if ($conf['camelcase']) {
632        $obj = new Camelcaselink();
633        $modes[] = ['sort' => $obj->getSort(), 'mode' => 'camelcaselink', 'obj' => $obj];
634    }
635
636    //sort modes
637    usort($modes, 'p_sort_modes');
638
639    return $modes;
640}
641
642/**
643 * Callback function for usort
644 *
645 * @param array $a
646 * @param array $b
647 * @return int $a is lower/equal/higher than $b
648 * @author Andreas Gohr <andi@splitbrain.org>
649 *
650 */
651function p_sort_modes($a, $b)
652{
653    return $a['sort'] <=> $b['sort'];
654}
655
656/**
657 * Renders a list of instruction to the specified output mode
658 *
659 * In the $info array is information from the renderer returned
660 *
661 * @param string $mode
662 * @param array|null|false $instructions
663 * @param array $info returns render info like enabled toc and cache
664 * @param string $date_at
665 * @return null|string rendered output
666 * @author Andreas Gohr <andi@splitbrain.org>
667 *
668 * @author Harry Fuecks <hfuecks@gmail.com>
669 */
670function p_render($mode, $instructions, &$info, $date_at = '')
671{
672    if (is_null($instructions)) return '';
673    if ($instructions === false) return '';
674
675    $Renderer = p_get_renderer($mode);
676    if (is_null($Renderer)) return null;
677
678    $Renderer->reset();
679
680    if ($date_at) {
681        $Renderer->date_at = $date_at;
682    }
683
684    $Renderer->smileys = getSmileys();
685    $Renderer->entities = getEntities();
686    $Renderer->acronyms = getAcronyms();
687    $Renderer->interwiki = getInterwiki();
688
689    // Loop through the instructions
690    foreach ($instructions as $instruction) {
691        // Execute the callback against the Renderer
692        if (method_exists($Renderer, $instruction[0])) {
693            call_user_func_array([&$Renderer, $instruction[0]], $instruction[1] ?: []);
694        }
695    }
696
697    //set info array
698    $info = $Renderer->info;
699
700    // Post process and return the output
701    $data = [$mode, & $Renderer->doc];
702    Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS', $data);
703    return $Renderer->doc;
704}
705
706/**
707 * Figure out the correct renderer class to use for $mode,
708 * instantiate and return it
709 *
710 * @param string $mode Mode of the renderer to get
711 * @return null|Doku_Renderer The renderer
712 *
713 * @author Christopher Smith <chris@jalakai.co.uk>
714 */
715function p_get_renderer($mode)
716{
717    /** @var PluginController $plugin_controller */
718    global $conf, $plugin_controller;
719
720    $rname = empty($conf['renderer_' . $mode]) ? $mode : $conf['renderer_' . $mode];
721    $rclass = "Doku_Renderer_$rname";
722
723    // if requested earlier or a bundled renderer
724    if (class_exists($rclass)) {
725        return new $rclass();
726    }
727
728    // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format.
729    /** @var Doku_Renderer $Renderer */
730    $Renderer = $plugin_controller->load('renderer', $rname);
731    if ($Renderer && is_a($Renderer, 'Doku_Renderer') && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) {
732        return $Renderer;
733    }
734
735    // there is a configuration error!
736    // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer
737    $rclass = "Doku_Renderer_$mode";
738    if (class_exists($rclass)) {
739        // viewers should see renderered output, so restrict the warning to admins only
740        $msg = "No renderer '$rname' found for mode '$mode', check your plugins";
741        if ($mode == 'xhtml') {
742            $msg .= " and the 'renderer_xhtml' config setting";
743        }
744        $msg .= ".<br/>Attempting to fallback to the bundled renderer.";
745        msg($msg, -1, '', '', MSG_ADMINS_ONLY);
746
747        $Renderer = new $rclass();
748        $Renderer->nocache();     // fallback only (and may include admin alerts), don't cache
749        return $Renderer;
750    }
751
752    // fallback failed, alert the world
753    msg("No renderer '$rname' found for mode '$mode'", -1);
754    return null;
755}
756
757/**
758 * Gets the first heading from a file
759 *
760 * @param string $id dokuwiki page id
761 * @param int $render rerender if first heading not known
762 *                             default: METADATA_RENDER_USING_SIMPLE_CACHE
763 *                             Possible values: METADATA_DONT_RENDER,
764 *                                              METADATA_RENDER_USING_SIMPLE_CACHE,
765 *                                              METADATA_RENDER_USING_CACHE,
766 *                                              METADATA_RENDER_UNLIMITED
767 * @return string|null The first heading
768 *
769 * @author Andreas Gohr <andi@splitbrain.org>
770 * @author Michael Hamann <michael@content-space.de>
771 */
772function p_get_first_heading($id, $render = METADATA_RENDER_USING_SIMPLE_CACHE)
773{
774    return p_get_metadata(cleanID($id), 'title', $render);
775}
776
777/**
778 * Wrapper for GeSHi Code Highlighter, provides caching of its output
779 *
780 * @param string $code source code to be highlighted
781 * @param string $language language to provide highlighting
782 * @param string $wrapper html element to wrap the returned highlighted text
783 * @return string xhtml code
784 *
785 * @author Christopher Smith <chris@jalakai.co.uk>
786 * @author Andreas Gohr <andi@splitbrain.org>
787 */
788function p_xhtml_cached_geshi($code, $language, $wrapper = 'pre', array $options = null)
789{
790    global $conf, $config_cascade, $INPUT;
791    $language = strtolower($language);
792
793    // remove any leading or trailing blank lines
794    $code = preg_replace('/^\s*?\n|\s*?\n$/', '', $code);
795
796    $optionsmd5 = md5(serialize($options));
797    $cache = getCacheName($language . $code . $optionsmd5, ".code");
798    $ctime = @filemtime($cache);
799    if ($ctime && !$INPUT->bool('purge') &&
800        $ctime > filemtime(DOKU_INC . 'vendor/composer/installed.json') &&  // libraries changed
801        $ctime > filemtime(reset($config_cascade['main']['default']))) { // dokuwiki changed
802        $highlighted_code = io_readFile($cache, false);
803    } else {
804
805        $geshi = new GeSHi($code, $language);
806        $geshi->set_encoding('utf-8');
807        $geshi->enable_classes();
808        $geshi->set_header_type(GESHI_HEADER_PRE);
809        $geshi->set_link_target($conf['target']['extern']);
810        if ($options !== null) {
811            foreach ($options as $function => $params) {
812                if (is_callable([$geshi, $function])) {
813                    $geshi->$function($params);
814                }
815            }
816        }
817
818        // remove GeSHi's wrapper element (we'll replace it with our own later)
819        // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
820        $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!', '', $geshi->parse_code()), "\n\r");
821        io_saveFile($cache, $highlighted_code);
822    }
823
824    // add a wrapper element if required
825    if ($wrapper) {
826        return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
827    } else {
828        return $highlighted_code;
829    }
830}
831