xref: /dokuwiki/inc/parserutils.php (revision 7de86af9758e71d251fad91063035a459d83d2eb)
1c112d578Sandi<?php
2c112d578Sandi/**
3b8595a66SAndreas Gohr * Utilities for accessing the parser
4c112d578Sandi *
5c112d578Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6c112d578Sandi * @author     Harry Fuecks <hfuecks@gmail.com>
7c112d578Sandi * @author     Andreas Gohr <andi@splitbrain.org>
8c112d578Sandi */
9c112d578Sandi
10fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
11c112d578Sandi
12c112d578Sandi/**
1365aa8490SMichael Hamann * How many pages shall be rendered for getting metadata during one request
1465aa8490SMichael Hamann * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED
1565aa8490SMichael Hamann * is passed as render parameter to p_get_metadata.
16ff725173SMichael Hamann */
1765aa8490SMichael Hamannif (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5);
1867c15eceSMichael Hamann
1967c15eceSMichael Hamann/** Don't render metadata even if it is outdated or doesn't exist */
2067c15eceSMichael Hamanndefine('METADATA_DONT_RENDER', 0);
2165aa8490SMichael Hamann/**
2265aa8490SMichael Hamann * Render metadata when the page is really newer or the metadata doesn't exist.
2365aa8490SMichael Hamann * Uses just a simple check, but should work pretty well for loading simple
2465aa8490SMichael Hamann * metadata values like the page title and avoids rendering a lot of pages in
2565aa8490SMichael Hamann * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode.
2665aa8490SMichael Hamann * Use this if it is unlikely that the metadata value you are requesting
2765aa8490SMichael Hamann * does depend e.g. on pages that are included in the current page using
2865aa8490SMichael Hamann * the include plugin (this is very likely the case for the page title, but
2965aa8490SMichael Hamann * not for relation references).
3065aa8490SMichael Hamann */
3167c15eceSMichael Hamanndefine('METADATA_RENDER_USING_SIMPLE_CACHE', 1);
3265aa8490SMichael Hamann/**
3365aa8490SMichael Hamann * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT
3465aa8490SMichael Hamann * is used in this mode. Use this mode when you are requesting more complex
3565aa8490SMichael Hamann * metadata. Although this will cause rendering more often it might actually have
3665aa8490SMichael Hamann * the effect that less current metadata is returned as it is more likely than in
3765aa8490SMichael Hamann * the simple cache mode that metadata needs to be rendered for all pages at once
3865aa8490SMichael Hamann * which means that when the metadata for the page is requested that actually needs
3965aa8490SMichael Hamann * to be updated the limit might have been reached already.
4065aa8490SMichael Hamann */
4167c15eceSMichael Hamanndefine('METADATA_RENDER_USING_CACHE', 2);
4265aa8490SMichael Hamann/**
4365aa8490SMichael Hamann * Render metadata without limiting the number of pages for which metadata is
4465aa8490SMichael Hamann * rendered. Use this mode with care, normally it should only be used in places
4565aa8490SMichael Hamann * like the indexer or in cli scripts where the execution time normally isn't
4665aa8490SMichael Hamann * limited. This can be combined with the simple cache using
4765aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED.
4865aa8490SMichael Hamann */
4965aa8490SMichael Hamanndefine('METADATA_RENDER_UNLIMITED', 4);
50ff725173SMichael Hamann
51ff725173SMichael Hamann/**
52c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision.
53c112d578Sandi *
54c112d578Sandi * If $excuse is true an explanation is returned if the file
55c112d578Sandi * wasn't found
56c112d578Sandi *
57c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
5842ea7f44SGerrit Uitslag *
5942ea7f44SGerrit Uitslag * @param string $id page id
6042ea7f44SGerrit Uitslag * @param string|int $rev revision timestamp or empty string
6142ea7f44SGerrit Uitslag * @param bool $excuse
6242ea7f44SGerrit Uitslag * @return null|string
63c112d578Sandi */
645c2eed9aSlispsfunction p_wiki_xhtml($id, $rev='', $excuse=true,$date_at=''){
65c112d578Sandi    $file = wikiFN($id,$rev);
66c112d578Sandi    $ret  = '';
67c112d578Sandi
68c112d578Sandi    //ensure $id is in global $ID (needed for parsing)
691e76272cSandi    global $ID;
703ff8773bSAndreas Gohr    $keep = $ID;
711e76272cSandi    $ID   = $id;
72c112d578Sandi
735c2eed9aSlisps    if($rev || $date_at){
7479e79377SAndreas Gohr        if(file_exists($file)){
751c8a8d7bSlisps            $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info,$date_at); //no caching on old revisions
76c112d578Sandi        }elseif($excuse){
77c112d578Sandi            $ret = p_locale_xhtml('norev');
78c112d578Sandi        }
79c112d578Sandi    }else{
8079e79377SAndreas Gohr        if(file_exists($file)){
814b5f4f4eSchris            $ret = p_cached_output($file,'xhtml',$id);
82c112d578Sandi        }elseif($excuse){
83c112d578Sandi            $ret = p_locale_xhtml('newpage');
84c112d578Sandi        }
85c112d578Sandi    }
86c112d578Sandi
873ff8773bSAndreas Gohr    //restore ID (just in case)
883ff8773bSAndreas Gohr    $ID = $keep;
893ff8773bSAndreas Gohr
90c112d578Sandi    return $ret;
91c112d578Sandi}
92c112d578Sandi
93c112d578Sandi/**
94c112d578Sandi * Returns the specified local text in parsed format
95c112d578Sandi *
96c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
9742ea7f44SGerrit Uitslag *
9842ea7f44SGerrit Uitslag * @param string $id page id
9942ea7f44SGerrit Uitslag * @return null|string
100c112d578Sandi */
101c112d578Sandifunction p_locale_xhtml($id){
102c112d578Sandi    //fetch parsed locale
1034b5f4f4eSchris    $html = p_cached_output(localeFN($id));
104c112d578Sandi    return $html;
105c112d578Sandi}
106c112d578Sandi
107c112d578Sandi/**
1084b5f4f4eSchris * Returns the given file parsed into the requested output format
1094b5f4f4eSchris *
1104b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org>
1114b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk>
11242ea7f44SGerrit Uitslag *
11342ea7f44SGerrit Uitslag * @param string $file filename, path to file
11442ea7f44SGerrit Uitslag * @param string $format
11542ea7f44SGerrit Uitslag * @param string $id page id
11642ea7f44SGerrit Uitslag * @return null|string
1174b5f4f4eSchris */
1184b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') {
119c112d578Sandi    global $conf;
120c112d578Sandi
1214b5f4f4eSchris    $cache = new cache_renderer($id, $file, $format);
1224b5f4f4eSchris    if ($cache->useCache()) {
12385767031SAndreas Gohr        $parsed = $cache->retrieveCache(false);
124*7de86af9SGerrit Uitslag        if($conf['allowdebug'] && $format=='xhtml') {
125*7de86af9SGerrit Uitslag            $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
126*7de86af9SGerrit Uitslag        }
127c112d578Sandi    } else {
1284b5f4f4eSchris        $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
129c112d578Sandi
13059b1d918SChristopher Smith        if ($info['cache'] && $cache->storeCache($parsed)) {              // storeCache() attempts to save cachefile
131*7de86af9SGerrit Uitslag            if($conf['allowdebug'] && $format=='xhtml') {
132*7de86af9SGerrit Uitslag                $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
133*7de86af9SGerrit Uitslag            }
134c112d578Sandi        }else{
1354b5f4f4eSchris            $cache->removeCache();                     //try to delete cachefile
136*7de86af9SGerrit Uitslag            if($conf['allowdebug'] && $format=='xhtml') {
137*7de86af9SGerrit Uitslag                $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
138*7de86af9SGerrit Uitslag            }
139c112d578Sandi        }
140c112d578Sandi    }
141c112d578Sandi
142c112d578Sandi    return $parsed;
143c112d578Sandi}
144c112d578Sandi
145c112d578Sandi/**
146c112d578Sandi * Returns the render instructions for a file
147c112d578Sandi *
148c112d578Sandi * Uses and creates a serialized cache file
149c112d578Sandi *
150c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
15142ea7f44SGerrit Uitslag *
15242ea7f44SGerrit Uitslag * @param string $file      filename, path to file
15342ea7f44SGerrit Uitslag * @param bool   $cacheonly
15442ea7f44SGerrit Uitslag * @param string $id        page id
15542ea7f44SGerrit Uitslag * @return array|null
156c112d578Sandi */
1574b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') {
15847b2d319SAndreas Gohr    static $run = null;
15947b2d319SAndreas Gohr    if(is_null($run)) $run = array();
160c112d578Sandi
1614b5f4f4eSchris    $cache = new cache_instructions($id, $file);
162c112d578Sandi
1630d24b616SMichael Hamann    if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) {
1644b5f4f4eSchris        return $cache->retrieveCache();
16579e79377SAndreas Gohr    } else if (file_exists($file)) {
166c112d578Sandi        // no cache - do some work
167bde4e341SGalaxyMaster        $ins = p_get_instructions(io_readWikiPage($file,$id));
168cbaf4259SChris Smith        if ($cache->storeCache($ins)) {
16947b2d319SAndreas Gohr            $run[$file] = true; // we won't rebuild these instructions in the same run again
170cbaf4259SChris Smith        } else {
171cbaf4259SChris Smith            msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
172cbaf4259SChris Smith        }
173c112d578Sandi        return $ins;
174c112d578Sandi    }
175c112d578Sandi
1763b3f8916SAndreas Gohr    return null;
177c112d578Sandi}
178c112d578Sandi
179c112d578Sandi/**
180c112d578Sandi * turns a page into a list of instructions
181c112d578Sandi *
182c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
183c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
18442ea7f44SGerrit Uitslag *
18599ba9fe6SAndreas Gohr * @param string $text  raw wiki syntax text
18699ba9fe6SAndreas Gohr * @return array a list of instruction arrays
187c112d578Sandi */
1886bbae538Sandifunction p_get_instructions($text){
189c112d578Sandi
190107b01d6Sandi    $modes = p_get_parsermodes();
191ee20e7d1Sandi
192c112d578Sandi    // Create the parser
19367f9913dSAndreas Gohr    $Parser = new Doku_Parser();
194c112d578Sandi
195c112d578Sandi    // Add the Handler
19667f9913dSAndreas Gohr    $Parser->Handler = new Doku_Handler();
197c112d578Sandi
198107b01d6Sandi    //add modes to parser
199107b01d6Sandi    foreach($modes as $mode){
200107b01d6Sandi        $Parser->addMode($mode['mode'],$mode['obj']);
201c112d578Sandi    }
202c112d578Sandi
203c112d578Sandi    // Do the parsing
204677844afSchris    trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
205a2d649c4Sandi    $p = $Parser->parse($text);
206ee20e7d1Sandi    //  dbg($p);
207a2d649c4Sandi    return $p;
208c112d578Sandi}
209c112d578Sandi
210c112d578Sandi/**
21139a89382SEsther Brunner * returns the metadata of a page
21239a89382SEsther Brunner *
2134a819402SMichael Hamann * @param string $id      The id of the page the metadata should be returned from
2144a819402SMichael Hamann * @param string $key     The key of the metdata value that shall be read (by default everything) - separate hierarchies by " " like "date created"
21567c15eceSMichael Hamann * @param int    $render  If the page should be rendererd - possible values:
21665aa8490SMichael Hamann *     METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE
21765aa8490SMichael Hamann *     METADATA_RENDER_UNLIMITED (also combined with the previous two options),
21865aa8490SMichael Hamann *     default: METADATA_RENDER_USING_CACHE
2194a819402SMichael Hamann * @return mixed The requested metadata fields
2204a819402SMichael Hamann *
22139a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
22298214867SMichael Hamann * @author Michael Hamann <michael@content-space.de>
22339a89382SEsther Brunner */
22467c15eceSMichael Hamannfunction p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){
2251172f8dcSAdrian Lang    global $ID;
22665aa8490SMichael Hamann    static $render_count = 0;
22765aa8490SMichael Hamann    // track pages that have already been rendered in order to avoid rendering the same page
22865aa8490SMichael Hamann    // again
22965aa8490SMichael Hamann    static $rendered_pages = array();
2306afe8dcaSchris
2310a7e3bceSchris    // cache the current page
2320a7e3bceSchris    // Benchmarking shows the current page's metadata is generally the only page metadata
2330a7e3bceSchris    // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
2340a7e3bceSchris    $cache = ($ID == $id);
2350a7e3bceSchris    $meta = p_read_metadata($id, $cache);
23639a89382SEsther Brunner
23767c15eceSMichael Hamann    if (!is_numeric($render)) {
23867c15eceSMichael Hamann        if ($render) {
23967c15eceSMichael Hamann            $render = METADATA_RENDER_USING_SIMPLE_CACHE;
24067c15eceSMichael Hamann        } else {
24167c15eceSMichael Hamann            $render = METADATA_DONT_RENDER;
24267c15eceSMichael Hamann        }
24367c15eceSMichael Hamann    }
24467c15eceSMichael Hamann
24598214867SMichael Hamann    // prevent recursive calls in the cache
24698214867SMichael Hamann    static $recursion = false;
24765aa8490SMichael Hamann    if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){
24898214867SMichael Hamann        $recursion = true;
24998214867SMichael Hamann
25098214867SMichael Hamann        $cachefile = new cache_renderer($id, wikiFN($id), 'metadata');
25198214867SMichael Hamann
25267c15eceSMichael Hamann        $do_render = false;
25365aa8490SMichael Hamann        if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) {
25465aa8490SMichael Hamann            if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) {
25567c15eceSMichael Hamann                $pagefn = wikiFN($id);
25667c15eceSMichael Hamann                $metafn = metaFN($id, '.meta');
25779e79377SAndreas Gohr                if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) {
25867c15eceSMichael Hamann                    $do_render = true;
25967c15eceSMichael Hamann                }
26067c15eceSMichael Hamann            } elseif (!$cachefile->useCache()){
26167c15eceSMichael Hamann                $do_render = true;
26267c15eceSMichael Hamann            }
26365aa8490SMichael Hamann        }
26467c15eceSMichael Hamann        if ($do_render) {
2650d24b616SMichael Hamann            if (!defined('DOKU_UNITTEST')) {
26665aa8490SMichael Hamann                ++$render_count;
26765aa8490SMichael Hamann                $rendered_pages[$id] = true;
2680d24b616SMichael Hamann            }
26969ba640bSMichael Hamann            $old_meta = $meta;
27039a89382SEsther Brunner            $meta = p_render_metadata($id, $meta);
27169ba640bSMichael Hamann            // only update the file when the metadata has been changed
27269ba640bSMichael Hamann            if ($meta == $old_meta || p_save_metadata($id, $meta)) {
27398214867SMichael Hamann                // store a timestamp in order to make sure that the cachefile is touched
2743d6feb16SMichael Hamann                // this timestamp is also stored when the meta data is still the same
27598214867SMichael Hamann                $cachefile->storeCache(time());
2763d6feb16SMichael Hamann            } else {
27798214867SMichael Hamann                msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1);
27898214867SMichael Hamann            }
27998214867SMichael Hamann        }
28098214867SMichael Hamann
28198214867SMichael Hamann        $recursion = false;
2826afe8dcaSchris    }
28339a89382SEsther Brunner
284ebf65d37SAdrian Lang    $val = $meta['current'];
285ebf65d37SAdrian Lang
28639a89382SEsther Brunner    // filter by $key
287569a0019SAdrian Lang    foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) {
288ebf65d37SAdrian Lang        if (!isset($val[$cur_key])) {
289ebf65d37SAdrian Lang            return null;
29066d29756SChris Smith        }
291ebf65d37SAdrian Lang        $val = $val[$cur_key];
29239a89382SEsther Brunner    }
293ebf65d37SAdrian Lang    return $val;
29439a89382SEsther Brunner}
29539a89382SEsther Brunner
29639a89382SEsther Brunner/**
29739a89382SEsther Brunner * sets metadata elements of a page
29839a89382SEsther Brunner *
299a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
300a365baeeSDominik Eckelmann *
301a365baeeSDominik Eckelmann * @param String  $id         is the ID of a wiki page
302a365baeeSDominik Eckelmann * @param Array   $data       is an array with key ⇒ value pairs to be set in the metadata
303a365baeeSDominik Eckelmann * @param Boolean $render     whether or not the page metadata should be generated with the renderer
304a365baeeSDominik Eckelmann * @param Boolean $persistent indicates whether or not the particular metadata value will persist through
305a365baeeSDominik Eckelmann *                            the next metadata rendering.
306a365baeeSDominik Eckelmann * @return boolean true on success
307a365baeeSDominik Eckelmann *
30839a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
3090e5fde48SMichael Hamann * @author Michael Hamann <michael@content-space.de>
31039a89382SEsther Brunner */
3110a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){
31239a89382SEsther Brunner    if (!is_array($data)) return false;
31339a89382SEsther Brunner
3140e5fde48SMichael Hamann    global $ID, $METADATA_RENDERERS;
3150a7e3bceSchris
3160e5fde48SMichael Hamann    // if there is currently a renderer change the data in the renderer instead
3170e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id])) {
3180e5fde48SMichael Hamann        $orig =& $METADATA_RENDERERS[$id];
3190e5fde48SMichael Hamann        $meta = $orig;
3200e5fde48SMichael Hamann    } else {
3210a7e3bceSchris        // cache the current page
3220a7e3bceSchris        $cache = ($ID == $id);
3230a7e3bceSchris        $orig = p_read_metadata($id, $cache);
32439a89382SEsther Brunner
32539a89382SEsther Brunner        // render metadata first?
3260a7e3bceSchris        $meta = $render ? p_render_metadata($id, $orig) : $orig;
3270e5fde48SMichael Hamann    }
32839a89382SEsther Brunner
32939a89382SEsther Brunner    // now add the passed metadata
33039a89382SEsther Brunner    $protected = array('description', 'date', 'contributor');
33139a89382SEsther Brunner    foreach ($data as $key => $value){
33239a89382SEsther Brunner
33339a89382SEsther Brunner        // be careful with sub-arrays of $meta['relation']
33439a89382SEsther Brunner        if ($key == 'relation'){
3350a7e3bceSchris
33639a89382SEsther Brunner            foreach ($value as $subkey => $subvalue){
33731b10b49SMichael Hamann                if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
33831b10b49SMichael Hamann                    $meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], (array)$subvalue);
33931b10b49SMichael Hamann                } else {
34031b10b49SMichael Hamann                    $meta['current'][$key][$subkey] = $subvalue;
34131b10b49SMichael Hamann                }
34231b10b49SMichael Hamann                if($persistent) {
34331b10b49SMichael Hamann                    if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
34431b10b49SMichael Hamann                        $meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], (array)$subvalue);
34531b10b49SMichael Hamann                    } else {
34631b10b49SMichael Hamann                        $meta['persistent'][$key][$subkey] = $subvalue;
34731b10b49SMichael Hamann                    }
34831b10b49SMichael Hamann                }
34939a89382SEsther Brunner            }
35039a89382SEsther Brunner
35139a89382SEsther Brunner            // be careful with some senisitive arrays of $meta
35239a89382SEsther Brunner        } elseif (in_array($key, $protected)){
3530a7e3bceSchris
35466d29756SChris Smith            // these keys, must have subkeys - a legitimate value must be an array
35539a89382SEsther Brunner            if (is_array($value)) {
35631b10b49SMichael Hamann                $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge((array)$meta['current'][$key],$value) : $value;
3570a7e3bceSchris
3580a7e3bceSchris                if ($persistent) {
35931b10b49SMichael Hamann                    $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge((array)$meta['persistent'][$key],$value) : $value;
3600a7e3bceSchris                }
36139a89382SEsther Brunner            }
36239a89382SEsther Brunner
36339a89382SEsther Brunner            // no special treatment for the rest
36439a89382SEsther Brunner        } else {
3650a7e3bceSchris            $meta['current'][$key] = $value;
3660a7e3bceSchris            if ($persistent) $meta['persistent'][$key] = $value;
36739a89382SEsther Brunner        }
36839a89382SEsther Brunner    }
36939a89382SEsther Brunner
37039a89382SEsther Brunner    // save only if metadata changed
37139a89382SEsther Brunner    if ($meta == $orig) return true;
3726afe8dcaSchris
3730e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id])) {
3740e5fde48SMichael Hamann        // set both keys individually as the renderer has references to the individual keys
3750e5fde48SMichael Hamann        $METADATA_RENDERERS[$id]['current']    = $meta['current'];
3760e5fde48SMichael Hamann        $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
3771c56be7bSMichael Hamann        return true;
3780e5fde48SMichael Hamann    } else {
3791172f8dcSAdrian Lang        return p_save_metadata($id, $meta);
38039a89382SEsther Brunner    }
3810e5fde48SMichael Hamann}
38239a89382SEsther Brunner
38339a89382SEsther Brunner/**
3843d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data
3853d1f9ec3SMichael Klier * used on page deletion
3863d1f9ec3SMichael Klier *
3873d1f9ec3SMichael Klier * @author Michael Klier <chi@chimeric.de>
38842ea7f44SGerrit Uitslag *
38942ea7f44SGerrit Uitslag * @param string $id page id
39042ea7f44SGerrit Uitslag * @return bool  success / fail
3913d1f9ec3SMichael Klier */
3923d1f9ec3SMichael Klierfunction p_purge_metadata($id) {
3933d1f9ec3SMichael Klier    $meta = p_read_metadata($id);
3943d1f9ec3SMichael Klier    foreach($meta['current'] as $key => $value) {
3953d1f9ec3SMichael Klier        if(is_array($meta[$key])) {
3963d1f9ec3SMichael Klier            $meta['current'][$key] = array();
3973d1f9ec3SMichael Klier        } else {
3983d1f9ec3SMichael Klier            $meta['current'][$key] = '';
3993d1f9ec3SMichael Klier        }
4001172f8dcSAdrian Lang
4013d1f9ec3SMichael Klier    }
4021172f8dcSAdrian Lang    return p_save_metadata($id, $meta);
4033d1f9ec3SMichael Klier}
4043d1f9ec3SMichael Klier
4053d1f9ec3SMichael Klier/**
4060a7e3bceSchris * read the metadata from source/cache for $id
4070a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata)
4080a7e3bceSchris *
4090a7e3bceSchris * @author   Christopher Smith <chris@jalakai.co.uk>
4100a7e3bceSchris *
4110a7e3bceSchris * @param    string   $id      absolute wiki page id
4120a7e3bceSchris * @param    bool     $cache   whether or not to cache metadata in memory
4130a7e3bceSchris *                             (only use for metadata likely to be accessed several times)
4140a7e3bceSchris *
4150a7e3bceSchris * @return   array             metadata
4160a7e3bceSchris */
4170a7e3bceSchrisfunction p_read_metadata($id,$cache=false) {
4180a7e3bceSchris    global $cache_metadata;
4190a7e3bceSchris
4203a50618cSgweissbach    if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
4210a7e3bceSchris
4220a7e3bceSchris    $file = metaFN($id, '.meta');
42379e79377SAndreas Gohr    $meta = file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
4240a7e3bceSchris
4250a7e3bceSchris    if ($cache) {
4263a50618cSgweissbach        $cache_metadata[(string)$id] = $meta;
4270a7e3bceSchris    }
4280a7e3bceSchris
4290a7e3bceSchris    return $meta;
4300a7e3bceSchris}
4310a7e3bceSchris
4320a7e3bceSchris/**
4331172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file
4341172f8dcSAdrian Lang *
4351172f8dcSAdrian Lang * @param    string   $id      absolute wiki page id
4361172f8dcSAdrian Lang * @param    array    $meta    metadata
4371172f8dcSAdrian Lang *
4381172f8dcSAdrian Lang * @return   bool              success / fail
4391172f8dcSAdrian Lang */
4401172f8dcSAdrian Langfunction p_save_metadata($id, $meta) {
4411172f8dcSAdrian Lang    // sync cached copies, including $INFO metadata
4421172f8dcSAdrian Lang    global $cache_metadata, $INFO;
4431172f8dcSAdrian Lang
4441172f8dcSAdrian Lang    if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
4451172f8dcSAdrian Lang    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
4461172f8dcSAdrian Lang
4471172f8dcSAdrian Lang    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
4481172f8dcSAdrian Lang}
4491172f8dcSAdrian Lang
4501172f8dcSAdrian Lang/**
45139a89382SEsther Brunner * renders the metadata of a page
45239a89382SEsther Brunner *
45339a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
45442ea7f44SGerrit Uitslag *
45542ea7f44SGerrit Uitslag * @param string $id    page id
45642ea7f44SGerrit Uitslag * @param array  $orig  the original metadata
45742ea7f44SGerrit Uitslag * @return array|null array('current'=> array,'persistent'=> array);
45839a89382SEsther Brunner */
45939a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
46048924015SAndreas Gohr    // make sure the correct ID is in global ID
4610e5fde48SMichael Hamann    global $ID, $METADATA_RENDERERS;
4620e5fde48SMichael Hamann
4630e5fde48SMichael Hamann    // avoid recursive rendering processes for the same id
4645b76ad91SChristopher Smith    if (isset($METADATA_RENDERERS[$id])) {
4650e5fde48SMichael Hamann        return $orig;
4665b76ad91SChristopher Smith    }
4670e5fde48SMichael Hamann
4680e5fde48SMichael Hamann    // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it
4690e5fde48SMichael Hamann    $METADATA_RENDERERS[$id] =& $orig;
4700e5fde48SMichael Hamann
47148924015SAndreas Gohr    $keep = $ID;
47248924015SAndreas Gohr    $ID   = $id;
47348924015SAndreas Gohr
4740a7e3bceSchris    // add an extra key for the event - to tell event handlers the page whose metadata this is
4750a7e3bceSchris    $orig['page'] = $id;
4760a7e3bceSchris    $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
4770a7e3bceSchris    if ($evt->advise_before()) {
4780a7e3bceSchris
47939a89382SEsther Brunner        // get instructions
4804b5f4f4eSchris        $instructions = p_cached_instructions(wikiFN($id),false,$id);
48148924015SAndreas Gohr        if(is_null($instructions)){
48248924015SAndreas Gohr            $ID = $keep;
4830e5fde48SMichael Hamann            unset($METADATA_RENDERERS[$id]);
48448924015SAndreas Gohr            return null; // something went wrong with the instructions
48548924015SAndreas Gohr        }
48639a89382SEsther Brunner
48739a89382SEsther Brunner        // set up the renderer
48867f9913dSAndreas Gohr        $renderer = new Doku_Renderer_metadata();
4890e5fde48SMichael Hamann        $renderer->meta =& $orig['current'];
4900e5fde48SMichael Hamann        $renderer->persistent =& $orig['persistent'];
49139a89382SEsther Brunner
49239a89382SEsther Brunner        // loop through the instructions
49339a89382SEsther Brunner        foreach ($instructions as $instruction){
49439a89382SEsther Brunner            // execute the callback against the renderer
4953b748871SAndreas Gohr            call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]);
49639a89382SEsther Brunner        }
49739a89382SEsther Brunner
4980e5fde48SMichael Hamann        $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent);
4990a7e3bceSchris    }
5000a7e3bceSchris    $evt->advise_after();
5010a7e3bceSchris
5020e5fde48SMichael Hamann    // clean up
50348924015SAndreas Gohr    $ID = $keep;
5040e5fde48SMichael Hamann    unset($METADATA_RENDERERS[$id]);
5050a7e3bceSchris    return $evt->result;
50639a89382SEsther Brunner}
50739a89382SEsther Brunner
50839a89382SEsther Brunner/**
509107b01d6Sandi * returns all available parser syntax modes in correct order
510107b01d6Sandi *
511107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
51242ea7f44SGerrit Uitslag *
51342ea7f44SGerrit Uitslag * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj'  => plugin object)
514107b01d6Sandi */
515107b01d6Sandifunction p_get_parsermodes(){
516107b01d6Sandi    global $conf;
517107b01d6Sandi
518107b01d6Sandi    //reuse old data
519107b01d6Sandi    static $modes = null;
5200d24b616SMichael Hamann    if($modes != null && !defined('DOKU_UNITTEST')){
521107b01d6Sandi        return $modes;
522107b01d6Sandi    }
523107b01d6Sandi
524107b01d6Sandi    //import parser classes and mode definitions
525107b01d6Sandi    require_once DOKU_INC . 'inc/parser/parser.php';
526107b01d6Sandi
527107b01d6Sandi    // we now collect all syntax modes and their objects, then they will
528107b01d6Sandi    // be sorted and added to the parser in correct order
529107b01d6Sandi    $modes = array();
530107b01d6Sandi
531107b01d6Sandi    // add syntax plugins
532107b01d6Sandi    $pluginlist = plugin_list('syntax');
533107b01d6Sandi    if(count($pluginlist)){
534107b01d6Sandi        global $PARSER_MODES;
535107b01d6Sandi        $obj = null;
536107b01d6Sandi        foreach($pluginlist as $p){
537e3ab6fc5SMichael Hamann            /** @var DokuWiki_Syntax_Plugin $obj */
538e8b5a4f9SAndreas Gohr            if(!$obj = plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
539107b01d6Sandi            $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
540107b01d6Sandi            //add to modes
541107b01d6Sandi            $modes[] = array(
542107b01d6Sandi                    'sort' => $obj->getSort(),
543107b01d6Sandi                    'mode' => "plugin_$p",
544107b01d6Sandi                    'obj'  => $obj,
545107b01d6Sandi                    );
546a46d0d65SAndreas Gohr            unset($obj); //remove the reference
547107b01d6Sandi        }
548107b01d6Sandi    }
549107b01d6Sandi
550107b01d6Sandi    // add default modes
551107b01d6Sandi    $std_modes = array('listblock','preformatted','notoc','nocache',
552107b01d6Sandi            'header','table','linebreak','footnote','hr',
553107b01d6Sandi            'unformatted','php','html','code','file','quote',
554e77ea1bcSAndreas Gohr            'internallink','rss','media','externallink',
555e77ea1bcSAndreas Gohr            'emaillink','windowssharelink','eol');
556e77ea1bcSAndreas Gohr    if($conf['typography']){
557e77ea1bcSAndreas Gohr        $std_modes[] = 'quotes';
558e77ea1bcSAndreas Gohr        $std_modes[] = 'multiplyentity';
559e77ea1bcSAndreas Gohr    }
560107b01d6Sandi    foreach($std_modes as $m){
561107b01d6Sandi        $class = "Doku_Parser_Mode_$m";
562107b01d6Sandi        $obj   = new $class();
563107b01d6Sandi        $modes[] = array(
564107b01d6Sandi                'sort' => $obj->getSort(),
565107b01d6Sandi                'mode' => $m,
566107b01d6Sandi                'obj'  => $obj
567107b01d6Sandi                );
568107b01d6Sandi    }
569107b01d6Sandi
570107b01d6Sandi    // add formatting modes
571107b01d6Sandi    $fmt_modes = array('strong','emphasis','underline','monospace',
572107b01d6Sandi            'subscript','superscript','deleted');
573107b01d6Sandi    foreach($fmt_modes as $m){
574107b01d6Sandi        $obj   = new Doku_Parser_Mode_formatting($m);
575107b01d6Sandi        $modes[] = array(
576107b01d6Sandi                'sort' => $obj->getSort(),
577107b01d6Sandi                'mode' => $m,
578107b01d6Sandi                'obj'  => $obj
579107b01d6Sandi                );
580107b01d6Sandi    }
581107b01d6Sandi
582107b01d6Sandi    // add modes which need files
583107b01d6Sandi    $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
584107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
585107b01d6Sandi    $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
586107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
587107b01d6Sandi    $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
588107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
589107b01d6Sandi
590107b01d6Sandi    // add optional camelcase mode
591107b01d6Sandi    if($conf['camelcase']){
592107b01d6Sandi        $obj     = new Doku_Parser_Mode_camelcaselink();
593107b01d6Sandi        $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
594107b01d6Sandi    }
595107b01d6Sandi
596107b01d6Sandi    //sort modes
597107b01d6Sandi    usort($modes,'p_sort_modes');
598107b01d6Sandi
599107b01d6Sandi    return $modes;
600107b01d6Sandi}
601107b01d6Sandi
602107b01d6Sandi/**
603107b01d6Sandi * Callback function for usort
604107b01d6Sandi *
605107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
60642ea7f44SGerrit Uitslag *
60742ea7f44SGerrit Uitslag * @param array $a
60842ea7f44SGerrit Uitslag * @param array $b
60942ea7f44SGerrit Uitslag * @return int $a is lower/equal/higher than $b
610107b01d6Sandi */
611107b01d6Sandifunction p_sort_modes($a, $b){
612107b01d6Sandi    if($a['sort'] == $b['sort']) return 0;
613107b01d6Sandi    return ($a['sort'] < $b['sort']) ? -1 : 1;
614107b01d6Sandi}
615107b01d6Sandi
616107b01d6Sandi/**
617ac83b9d8Sandi * Renders a list of instruction to the specified output mode
618c112d578Sandi *
6197a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned
6209dc2c2afSandi *
621c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
622c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
62342ea7f44SGerrit Uitslag *
62442ea7f44SGerrit Uitslag * @param string $mode
625e3710957SGerrit Uitslag * @param array|null|false $instructions
62642ea7f44SGerrit Uitslag * @param array $info returns render info like enabled toc and cache
627*7de86af9SGerrit Uitslag * @param string $date_at
62842ea7f44SGerrit Uitslag * @return null|string rendered output
629c112d578Sandi */
6304bde2196Slispsfunction p_render($mode,$instructions,&$info,$date_at=''){
631c112d578Sandi    if(is_null($instructions)) return '';
632e3710957SGerrit Uitslag    if($instructions === false) return '';
633c112d578Sandi
634252398f0SChristopher Smith    $Renderer = p_get_renderer($mode);
635d968d3e5SChris Smith    if (is_null($Renderer)) return null;
636c327d6c4SAndreas Gohr
637d968d3e5SChris Smith    $Renderer->reset();
638c112d578Sandi
6395c2eed9aSlisps    if($date_at) {
6405c2eed9aSlisps        $Renderer->date_at = $date_at;
6415c2eed9aSlisps    }
6425c2eed9aSlisps
643c112d578Sandi    $Renderer->smileys = getSmileys();
644c112d578Sandi    $Renderer->entities = getEntities();
645c112d578Sandi    $Renderer->acronyms = getAcronyms();
646c112d578Sandi    $Renderer->interwiki = getInterwiki();
647c112d578Sandi
648c112d578Sandi    // Loop through the instructions
649c112d578Sandi    foreach ( $instructions as $instruction ) {
650c112d578Sandi        // Execute the callback against the Renderer
6517c62086bSAndreas Gohr        if(method_exists($Renderer, $instruction[0])){
6526340dabcSAndreas Gohr            call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array());
653c112d578Sandi        }
6547c62086bSAndreas Gohr    }
6559dc2c2afSandi
6569dc2c2afSandi    //set info array
6579dc2c2afSandi    $info = $Renderer->info;
6589dc2c2afSandi
659677844afSchris    // Post process and return the output
660677844afSchris    $data = array($mode,& $Renderer->doc);
661677844afSchris    trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
662c112d578Sandi    return $Renderer->doc;
663c112d578Sandi}
664c112d578Sandi
665e3ab6fc5SMichael Hamann/**
666548d801fSChristopher Smith * Figure out the correct renderer class to use for $mode,
667548d801fSChristopher Smith * instantiate and return it
668548d801fSChristopher Smith *
6697e8500eeSGerrit Uitslag * @param string $mode Mode of the renderer to get
670e3ab6fc5SMichael Hamann * @return null|Doku_Renderer The renderer
671548d801fSChristopher Smith *
672548d801fSChristopher Smith * @author Christopher Smith <chris@jalakai.co.uk>
673e3ab6fc5SMichael Hamann */
674252398f0SChristopher Smithfunction p_get_renderer($mode) {
675e3ab6fc5SMichael Hamann    /** @var Doku_Plugin_Controller $plugin_controller */
6767aea91afSChris Smith    global $conf, $plugin_controller;
677d968d3e5SChris Smith
678d968d3e5SChris Smith    $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;
6790cacf91fSLucas    $rclass = "Doku_Renderer_$rname";
6800cacf91fSLucas
681548d801fSChristopher Smith    // if requested earlier or a bundled renderer
6820cacf91fSLucas    if( class_exists($rclass) ) {
6835e40b274SChristopher Smith        $Renderer = new $rclass();
6845e40b274SChristopher Smith        return $Renderer;
6850cacf91fSLucas    }
686d968d3e5SChris Smith
6876e6d16edSChristopher Smith    // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format.
6880440ca46SGerrit Uitslag    /** @var Doku_Renderer $Renderer */
68911ac6abdSChristopher Smith    $Renderer = $plugin_controller->load('renderer',$rname);
6906e6d16edSChristopher Smith    if ($Renderer && is_a($Renderer, 'Doku_Renderer')  && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) {
69111ac6abdSChristopher Smith        return $Renderer;
69211ac6abdSChristopher Smith    }
693d968d3e5SChris Smith
69411ac6abdSChristopher Smith    // there is a configuration error!
695548d801fSChristopher Smith    // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer
69611ac6abdSChristopher Smith    $rclass = "Doku_Renderer_$mode";
697548d801fSChristopher Smith    if ( class_exists($rclass) ) {
69811ac6abdSChristopher Smith        // viewers should see renderered output, so restrict the warning to admins only
69911ac6abdSChristopher Smith        $msg = "No renderer '$rname' found for mode '$mode', check your plugins";
70011ac6abdSChristopher Smith        if ($mode == 'xhtml') {
70111ac6abdSChristopher Smith            $msg .= " and the 'renderer_xhtml' config setting";
70211ac6abdSChristopher Smith        }
70311ac6abdSChristopher Smith        $msg .= ".<br/>Attempting to fallback to the bundled renderer.";
704548d801fSChristopher Smith        msg($msg,-1,'','',MSG_ADMINS_ONLY);
70511ac6abdSChristopher Smith
706548d801fSChristopher Smith        $Renderer = new $rclass;
707548d801fSChristopher Smith        $Renderer->nocache();     // fallback only (and may include admin alerts), don't cache
708d968d3e5SChris Smith        return $Renderer;
709d968d3e5SChris Smith    }
710d968d3e5SChris Smith
711548d801fSChristopher Smith    // fallback failed, alert the world
712548d801fSChristopher Smith    msg("No renderer '$rname' found for mode '$mode'",-1);
713548d801fSChristopher Smith    return null;
714548d801fSChristopher Smith}
715548d801fSChristopher Smith
716bb0a59d4Sjan/**
717bb0a59d4Sjan * Gets the first heading from a file
718bb0a59d4Sjan *
719fc18c0fbSchris * @param   string   $id       dokuwiki page id
72067c15eceSMichael Hamann * @param   int      $render   rerender if first heading not known
72167c15eceSMichael Hamann *                             default: METADATA_RENDER_USING_SIMPLE_CACHE
72267c15eceSMichael Hamann *                             Possible values: METADATA_DONT_RENDER,
72367c15eceSMichael Hamann *                                              METADATA_RENDER_USING_SIMPLE_CACHE,
72465aa8490SMichael Hamann *                                              METADATA_RENDER_USING_CACHE,
72565aa8490SMichael Hamann *                                              METADATA_RENDER_UNLIMITED
726e3ab6fc5SMichael Hamann * @return string|null The first heading
72742ea7f44SGerrit Uitslag *
72895dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
729bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de>
730bb0a59d4Sjan */
73167c15eceSMichael Hamannfunction p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){
73265aa8490SMichael Hamann    return p_get_metadata(cleanID($id),'title',$render);
733bb0a59d4Sjan}
734bb0a59d4Sjan
7358f7d700cSchris/**
7368f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output
7378f7d700cSchris *
7385d568b99SChris Smith * @param  string   $code       source code to be highlighted
7395d568b99SChris Smith * @param  string   $language   language to provide highlighting
7405d568b99SChris Smith * @param  string   $wrapper    html element to wrap the returned highlighted text
741e3ab6fc5SMichael Hamann * @return string xhtml code
74242ea7f44SGerrit Uitslag *
7438f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk>
74435fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7458f7d700cSchris */
7465d568b99SChris Smithfunction p_xhtml_cached_geshi($code, $language, $wrapper='pre') {
747ff1769deSAndreas Gohr    global $conf, $config_cascade, $INPUT;
74835fbe9efSAndreas Gohr    $language = strtolower($language);
7495d568b99SChris Smith
7505d568b99SChris Smith    // remove any leading or trailing blank lines
7515d568b99SChris Smith    $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
7525d568b99SChris Smith
7538f7d700cSchris    $cache = getCacheName($language.$code,".code");
75435fbe9efSAndreas Gohr    $ctime = @filemtime($cache);
755ff1769deSAndreas Gohr    if($ctime && !$INPUT->bool('purge') &&
75641d51802SAndreas Gohr            $ctime > filemtime(DOKU_INC.'vendor/composer/installed.json') &&  // libraries changed
757f8121585SChris Smith            $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
7588f7d700cSchris        $highlighted_code = io_readFile($cache, false);
7598f7d700cSchris
7608f7d700cSchris    } else {
7618f7d700cSchris
76241d51802SAndreas Gohr        $geshi = new GeSHi($code, $language);
7638f7d700cSchris        $geshi->set_encoding('utf-8');
7648f7d700cSchris        $geshi->enable_classes();
7658f7d700cSchris        $geshi->set_header_type(GESHI_HEADER_PRE);
7668f7d700cSchris        $geshi->set_link_target($conf['target']['extern']);
7678f7d700cSchris
7685d568b99SChris Smith        // remove GeSHi's wrapper element (we'll replace it with our own later)
7695d568b99SChris Smith        // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
77069ddc332SAnika Henke        $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r");
7718f7d700cSchris        io_saveFile($cache,$highlighted_code);
7728f7d700cSchris    }
7738f7d700cSchris
7745d568b99SChris Smith    // add a wrapper element if required
7755d568b99SChris Smith    if ($wrapper) {
7765d568b99SChris Smith        return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
7775d568b99SChris Smith    } else {
7788f7d700cSchris        return $highlighted_code;
7798f7d700cSchris    }
7805d568b99SChris Smith}
7818f7d700cSchris
782