xref: /dokuwiki/inc/parserutils.php (revision 31b10b49cde775ab7d0196b7824403dc45fcf370)
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>
58c112d578Sandi */
59c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){
60c112d578Sandi    $file = wikiFN($id,$rev);
61c112d578Sandi    $ret  = '';
62c112d578Sandi
63c112d578Sandi    //ensure $id is in global $ID (needed for parsing)
641e76272cSandi    global $ID;
653ff8773bSAndreas Gohr    $keep = $ID;
661e76272cSandi    $ID   = $id;
67c112d578Sandi
68c112d578Sandi    if($rev){
69c112d578Sandi        if(@file_exists($file)){
70bde4e341SGalaxyMaster            $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions
71c112d578Sandi        }elseif($excuse){
72c112d578Sandi            $ret = p_locale_xhtml('norev');
73c112d578Sandi        }
74c112d578Sandi    }else{
75c112d578Sandi        if(@file_exists($file)){
764b5f4f4eSchris            $ret = p_cached_output($file,'xhtml',$id);
77c112d578Sandi        }elseif($excuse){
78c112d578Sandi            $ret = p_locale_xhtml('newpage');
79c112d578Sandi        }
80c112d578Sandi    }
81c112d578Sandi
823ff8773bSAndreas Gohr    //restore ID (just in case)
833ff8773bSAndreas Gohr    $ID = $keep;
843ff8773bSAndreas Gohr
85c112d578Sandi    return $ret;
86c112d578Sandi}
87c112d578Sandi
88c112d578Sandi/**
896b7b33dcShfuecks * Returns starting summary for a page (e.g. the first few
906b7b33dcShfuecks * paragraphs), marked up in XHTML.
916b7b33dcShfuecks *
926b7b33dcShfuecks * If $excuse is true an explanation is returned if the file
936b7b33dcShfuecks * wasn't found
946b7b33dcShfuecks *
95e3ab6fc5SMichael Hamann * @param string $id wiki page id
96e3ab6fc5SMichael Hamann * @param string $title populated with page title from heading or page id
97e3ab6fc5SMichael Hamann * @param string $rev revision string
98e3ab6fc5SMichael Hamann * @param bool   $excuse if an excuse shall be renderer when no content is found
99e3ab6fc5SMichael Hamann * @return string xhtml code
1008716966dSAndreas Gohr * @deprecated
1018716966dSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com>
1026b7b33dcShfuecks */
1036b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
1046b7b33dcShfuecks    $file = wikiFN($id,$rev);
1056b7b33dcShfuecks    $ret  = '';
1060a6ae52fSMichael Hamann    $ins  = null;
1076b7b33dcShfuecks
1086b7b33dcShfuecks    //ensure $id is in global $ID (needed for parsing)
1096b7b33dcShfuecks    global $ID;
1106b7b33dcShfuecks    $keep = $ID;
1116b7b33dcShfuecks    $ID   = $id;
1126b7b33dcShfuecks
1136b7b33dcShfuecks    if($rev){
1146b7b33dcShfuecks        if(@file_exists($file)){
1156b7b33dcShfuecks            //no caching on old revisions
116bde4e341SGalaxyMaster            $ins = p_get_instructions(io_readWikiPage($file,$id,$rev));
1176b7b33dcShfuecks        }elseif($excuse){
1186b7b33dcShfuecks            $ret = p_locale_xhtml('norev');
1196b7b33dcShfuecks            //restore ID (just in case)
1206b7b33dcShfuecks            $ID = $keep;
1216b7b33dcShfuecks            return $ret;
1226b7b33dcShfuecks        }
1236b7b33dcShfuecks
1246b7b33dcShfuecks    }else{
1256b7b33dcShfuecks
1266b7b33dcShfuecks        if(@file_exists($file)){
1276b7b33dcShfuecks            // The XHTML for a summary is not cached so use the instruction cache
1286b7b33dcShfuecks            $ins = p_cached_instructions($file);
1296b7b33dcShfuecks        }elseif($excuse){
1306b7b33dcShfuecks            $ret = p_locale_xhtml('newpage');
1316b7b33dcShfuecks            //restore ID (just in case)
1326b7b33dcShfuecks            $ID = $keep;
1336b7b33dcShfuecks            return $ret;
1346b7b33dcShfuecks        }
1356b7b33dcShfuecks    }
1366b7b33dcShfuecks
1376b7b33dcShfuecks    $ret = p_render('xhtmlsummary',$ins,$info);
1386b7b33dcShfuecks
1396b7b33dcShfuecks    if ( $info['sum_pagetitle'] ) {
1406b7b33dcShfuecks        $title = $info['sum_pagetitle'];
1416b7b33dcShfuecks    } else {
1426b7b33dcShfuecks        $title = $id;
1436b7b33dcShfuecks    }
1446b7b33dcShfuecks
1456b7b33dcShfuecks    $ID = $keep;
1466b7b33dcShfuecks    return $ret;
1476b7b33dcShfuecks}
1486b7b33dcShfuecks
1496b7b33dcShfuecks/**
150c112d578Sandi * Returns the specified local text in parsed format
151c112d578Sandi *
152c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
153c112d578Sandi */
154c112d578Sandifunction p_locale_xhtml($id){
155c112d578Sandi    //fetch parsed locale
1564b5f4f4eSchris    $html = p_cached_output(localeFN($id));
157c112d578Sandi    return $html;
158c112d578Sandi}
159c112d578Sandi
160c112d578Sandi/**
1614b5f4f4eSchris *     *** DEPRECATED ***
1624b5f4f4eSchris *
1634b5f4f4eSchris * use p_cached_output()
1644b5f4f4eSchris *
165c112d578Sandi * Returns the given file parsed to XHTML
166c112d578Sandi *
167c112d578Sandi * Uses and creates a cachefile
168c112d578Sandi *
1694b5f4f4eSchris * @deprecated
170c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
1719dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
172c112d578Sandi */
173c112d578Sandifunction p_cached_xhtml($file){
1744b5f4f4eSchris    return p_cached_output($file);
1754b5f4f4eSchris}
1764b5f4f4eSchris
1774b5f4f4eSchris/**
1784b5f4f4eSchris * Returns the given file parsed into the requested output format
1794b5f4f4eSchris *
1804b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org>
1814b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk>
1824b5f4f4eSchris */
1834b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') {
184c112d578Sandi    global $conf;
185c112d578Sandi
1864b5f4f4eSchris    $cache = new cache_renderer($id, $file, $format);
1874b5f4f4eSchris    if ($cache->useCache()) {
18885767031SAndreas Gohr        $parsed = $cache->retrieveCache(false);
189ea2a4271SAndreas Gohr        if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
190c112d578Sandi    } else {
1914b5f4f4eSchris        $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
192c112d578Sandi
1939dc2c2afSandi        if ($info['cache']) {
1944b5f4f4eSchris            $cache->storeCache($parsed);               //save cachefile
195ea2a4271SAndreas Gohr            if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
196c112d578Sandi        }else{
1974b5f4f4eSchris            $cache->removeCache();                     //try to delete cachefile
198ea2a4271SAndreas Gohr            if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
199c112d578Sandi        }
200c112d578Sandi    }
201c112d578Sandi
202c112d578Sandi    return $parsed;
203c112d578Sandi}
204c112d578Sandi
205c112d578Sandi/**
206c112d578Sandi * Returns the render instructions for a file
207c112d578Sandi *
208c112d578Sandi * Uses and creates a serialized cache file
209c112d578Sandi *
210c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
211c112d578Sandi */
2124b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') {
21347b2d319SAndreas Gohr    static $run = null;
21447b2d319SAndreas Gohr    if(is_null($run)) $run = array();
215c112d578Sandi
2164b5f4f4eSchris    $cache = new cache_instructions($id, $file);
217c112d578Sandi
2180d24b616SMichael Hamann    if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) {
2194b5f4f4eSchris        return $cache->retrieveCache();
220c112d578Sandi    } else if (@file_exists($file)) {
221c112d578Sandi        // no cache - do some work
222bde4e341SGalaxyMaster        $ins = p_get_instructions(io_readWikiPage($file,$id));
223cbaf4259SChris Smith        if ($cache->storeCache($ins)) {
22447b2d319SAndreas Gohr            $run[$file] = true; // we won't rebuild these instructions in the same run again
225cbaf4259SChris Smith        } else {
226cbaf4259SChris Smith            msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
227cbaf4259SChris Smith        }
228c112d578Sandi        return $ins;
229c112d578Sandi    }
230c112d578Sandi
2313b3f8916SAndreas Gohr    return null;
232c112d578Sandi}
233c112d578Sandi
234c112d578Sandi/**
235c112d578Sandi * turns a page into a list of instructions
236c112d578Sandi *
237c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
238c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
239c112d578Sandi */
2406bbae538Sandifunction p_get_instructions($text){
241c112d578Sandi
242107b01d6Sandi    $modes = p_get_parsermodes();
243ee20e7d1Sandi
244c112d578Sandi    // Create the parser
24567f9913dSAndreas Gohr    $Parser = new Doku_Parser();
246c112d578Sandi
247c112d578Sandi    // Add the Handler
24867f9913dSAndreas Gohr    $Parser->Handler = new Doku_Handler();
249c112d578Sandi
250107b01d6Sandi    //add modes to parser
251107b01d6Sandi    foreach($modes as $mode){
252107b01d6Sandi        $Parser->addMode($mode['mode'],$mode['obj']);
253c112d578Sandi    }
254c112d578Sandi
255c112d578Sandi    // Do the parsing
256677844afSchris    trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
257a2d649c4Sandi    $p = $Parser->parse($text);
258ee20e7d1Sandi    //  dbg($p);
259a2d649c4Sandi    return $p;
260c112d578Sandi}
261c112d578Sandi
262c112d578Sandi/**
26339a89382SEsther Brunner * returns the metadata of a page
26439a89382SEsther Brunner *
2654a819402SMichael Hamann * @param string $id The id of the page the metadata should be returned from
2664a819402SMichael Hamann * @param string $key The key of the metdata value that shall be read (by default everything) - separate hierarchies by " " like "date created"
26767c15eceSMichael Hamann * @param int $render If the page should be rendererd - possible values:
26865aa8490SMichael Hamann *     METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE
26965aa8490SMichael Hamann *     METADATA_RENDER_UNLIMITED (also combined with the previous two options),
27065aa8490SMichael Hamann *     default: METADATA_RENDER_USING_CACHE
2714a819402SMichael Hamann * @return mixed The requested metadata fields
2724a819402SMichael Hamann *
27339a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
27498214867SMichael Hamann * @author Michael Hamann <michael@content-space.de>
27539a89382SEsther Brunner */
27667c15eceSMichael Hamannfunction p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){
2771172f8dcSAdrian Lang    global $ID;
27865aa8490SMichael Hamann    static $render_count = 0;
27965aa8490SMichael Hamann    // track pages that have already been rendered in order to avoid rendering the same page
28065aa8490SMichael Hamann    // again
28165aa8490SMichael Hamann    static $rendered_pages = array();
2826afe8dcaSchris
2830a7e3bceSchris    // cache the current page
2840a7e3bceSchris    // Benchmarking shows the current page's metadata is generally the only page metadata
2850a7e3bceSchris    // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
2860a7e3bceSchris    $cache = ($ID == $id);
2870a7e3bceSchris    $meta = p_read_metadata($id, $cache);
28839a89382SEsther Brunner
28967c15eceSMichael Hamann    if (!is_numeric($render)) {
29067c15eceSMichael Hamann        if ($render) {
29167c15eceSMichael Hamann            $render = METADATA_RENDER_USING_SIMPLE_CACHE;
29267c15eceSMichael Hamann        } else {
29367c15eceSMichael Hamann            $render = METADATA_DONT_RENDER;
29467c15eceSMichael Hamann        }
29567c15eceSMichael Hamann    }
29667c15eceSMichael Hamann
29798214867SMichael Hamann    // prevent recursive calls in the cache
29898214867SMichael Hamann    static $recursion = false;
29965aa8490SMichael Hamann    if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){
30098214867SMichael Hamann        $recursion = true;
30198214867SMichael Hamann
30298214867SMichael Hamann        $cachefile = new cache_renderer($id, wikiFN($id), 'metadata');
30398214867SMichael Hamann
30467c15eceSMichael Hamann        $do_render = false;
30565aa8490SMichael Hamann        if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) {
30665aa8490SMichael Hamann            if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) {
30767c15eceSMichael Hamann                $pagefn = wikiFN($id);
30867c15eceSMichael Hamann                $metafn = metaFN($id, '.meta');
30967c15eceSMichael Hamann                if (!@file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) {
31067c15eceSMichael Hamann                    $do_render = true;
31167c15eceSMichael Hamann                }
31267c15eceSMichael Hamann            } elseif (!$cachefile->useCache()){
31367c15eceSMichael Hamann                $do_render = true;
31467c15eceSMichael Hamann            }
31565aa8490SMichael Hamann        }
31667c15eceSMichael Hamann        if ($do_render) {
3170d24b616SMichael Hamann            if (!defined('DOKU_UNITTEST')) {
31865aa8490SMichael Hamann                ++$render_count;
31965aa8490SMichael Hamann                $rendered_pages[$id] = true;
3200d24b616SMichael Hamann            }
32169ba640bSMichael Hamann            $old_meta = $meta;
32239a89382SEsther Brunner            $meta = p_render_metadata($id, $meta);
32369ba640bSMichael Hamann            // only update the file when the metadata has been changed
32469ba640bSMichael Hamann            if ($meta == $old_meta || p_save_metadata($id, $meta)) {
32598214867SMichael Hamann                // store a timestamp in order to make sure that the cachefile is touched
3263d6feb16SMichael Hamann                // this timestamp is also stored when the meta data is still the same
32798214867SMichael Hamann                $cachefile->storeCache(time());
3283d6feb16SMichael Hamann            } else {
32998214867SMichael Hamann                msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1);
33098214867SMichael Hamann            }
33198214867SMichael Hamann        }
33298214867SMichael Hamann
33398214867SMichael Hamann        $recursion = false;
3346afe8dcaSchris    }
33539a89382SEsther Brunner
336ebf65d37SAdrian Lang    $val = $meta['current'];
337ebf65d37SAdrian Lang
33839a89382SEsther Brunner    // filter by $key
339569a0019SAdrian Lang    foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) {
340ebf65d37SAdrian Lang        if (!isset($val[$cur_key])) {
341ebf65d37SAdrian Lang            return null;
34266d29756SChris Smith        }
343ebf65d37SAdrian Lang        $val = $val[$cur_key];
34439a89382SEsther Brunner    }
345ebf65d37SAdrian Lang    return $val;
34639a89382SEsther Brunner}
34739a89382SEsther Brunner
34839a89382SEsther Brunner/**
34939a89382SEsther Brunner * sets metadata elements of a page
35039a89382SEsther Brunner *
351a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
352a365baeeSDominik Eckelmann *
353a365baeeSDominik Eckelmann * @param String  $id         is the ID of a wiki page
354a365baeeSDominik Eckelmann * @param Array   $data       is an array with key ⇒ value pairs to be set in the metadata
355a365baeeSDominik Eckelmann * @param Boolean $render     whether or not the page metadata should be generated with the renderer
356a365baeeSDominik Eckelmann * @param Boolean $persistent indicates whether or not the particular metadata value will persist through
357a365baeeSDominik Eckelmann *                            the next metadata rendering.
358a365baeeSDominik Eckelmann * @return boolean true on success
359a365baeeSDominik Eckelmann *
36039a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
3610e5fde48SMichael Hamann * @author Michael Hamann <michael@content-space.de>
36239a89382SEsther Brunner */
3630a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){
36439a89382SEsther Brunner    if (!is_array($data)) return false;
36539a89382SEsther Brunner
3660e5fde48SMichael Hamann    global $ID, $METADATA_RENDERERS;
3670a7e3bceSchris
3680e5fde48SMichael Hamann    // if there is currently a renderer change the data in the renderer instead
3690e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id])) {
3700e5fde48SMichael Hamann        $orig =& $METADATA_RENDERERS[$id];
3710e5fde48SMichael Hamann        $meta = $orig;
3720e5fde48SMichael Hamann    } else {
3730a7e3bceSchris        // cache the current page
3740a7e3bceSchris        $cache = ($ID == $id);
3750a7e3bceSchris        $orig = p_read_metadata($id, $cache);
37639a89382SEsther Brunner
37739a89382SEsther Brunner        // render metadata first?
3780a7e3bceSchris        $meta = $render ? p_render_metadata($id, $orig) : $orig;
3790e5fde48SMichael Hamann    }
38039a89382SEsther Brunner
38139a89382SEsther Brunner    // now add the passed metadata
38239a89382SEsther Brunner    $protected = array('description', 'date', 'contributor');
38339a89382SEsther Brunner    foreach ($data as $key => $value){
38439a89382SEsther Brunner
38539a89382SEsther Brunner        // be careful with sub-arrays of $meta['relation']
38639a89382SEsther Brunner        if ($key == 'relation'){
3870a7e3bceSchris
38839a89382SEsther Brunner            foreach ($value as $subkey => $subvalue){
389*31b10b49SMichael Hamann                if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
390*31b10b49SMichael Hamann                    $meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], (array)$subvalue);
391*31b10b49SMichael Hamann                } else {
392*31b10b49SMichael Hamann                    $meta['current'][$key][$subkey] = $subvalue;
393*31b10b49SMichael Hamann                }
394*31b10b49SMichael Hamann                if($persistent) {
395*31b10b49SMichael Hamann                    if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
396*31b10b49SMichael Hamann                        $meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], (array)$subvalue);
397*31b10b49SMichael Hamann                    } else {
398*31b10b49SMichael Hamann                        $meta['persistent'][$key][$subkey] = $subvalue;
399*31b10b49SMichael Hamann                    }
400*31b10b49SMichael Hamann                }
40139a89382SEsther Brunner            }
40239a89382SEsther Brunner
40339a89382SEsther Brunner            // be careful with some senisitive arrays of $meta
40439a89382SEsther Brunner        } elseif (in_array($key, $protected)){
4050a7e3bceSchris
40666d29756SChris Smith            // these keys, must have subkeys - a legitimate value must be an array
40739a89382SEsther Brunner            if (is_array($value)) {
408*31b10b49SMichael Hamann                $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge((array)$meta['current'][$key],$value) : $value;
4090a7e3bceSchris
4100a7e3bceSchris                if ($persistent) {
411*31b10b49SMichael Hamann                    $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge((array)$meta['persistent'][$key],$value) : $value;
4120a7e3bceSchris                }
41339a89382SEsther Brunner            }
41439a89382SEsther Brunner
41539a89382SEsther Brunner            // no special treatment for the rest
41639a89382SEsther Brunner        } else {
4170a7e3bceSchris            $meta['current'][$key] = $value;
4180a7e3bceSchris            if ($persistent) $meta['persistent'][$key] = $value;
41939a89382SEsther Brunner        }
42039a89382SEsther Brunner    }
42139a89382SEsther Brunner
42239a89382SEsther Brunner    // save only if metadata changed
42339a89382SEsther Brunner    if ($meta == $orig) return true;
4246afe8dcaSchris
4250e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id])) {
4260e5fde48SMichael Hamann        // set both keys individually as the renderer has references to the individual keys
4270e5fde48SMichael Hamann        $METADATA_RENDERERS[$id]['current']    = $meta['current'];
4280e5fde48SMichael Hamann        $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
4291c56be7bSMichael Hamann        return true;
4300e5fde48SMichael Hamann    } else {
4311172f8dcSAdrian Lang        return p_save_metadata($id, $meta);
43239a89382SEsther Brunner    }
4330e5fde48SMichael Hamann}
43439a89382SEsther Brunner
43539a89382SEsther Brunner/**
4363d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data
4373d1f9ec3SMichael Klier * used on page deletion
4383d1f9ec3SMichael Klier *
4393d1f9ec3SMichael Klier * @author Michael Klier <chi@chimeric.de>
4403d1f9ec3SMichael Klier */
4413d1f9ec3SMichael Klierfunction p_purge_metadata($id) {
4423d1f9ec3SMichael Klier    $meta = p_read_metadata($id);
4433d1f9ec3SMichael Klier    foreach($meta['current'] as $key => $value) {
4443d1f9ec3SMichael Klier        if(is_array($meta[$key])) {
4453d1f9ec3SMichael Klier            $meta['current'][$key] = array();
4463d1f9ec3SMichael Klier        } else {
4473d1f9ec3SMichael Klier            $meta['current'][$key] = '';
4483d1f9ec3SMichael Klier        }
4491172f8dcSAdrian Lang
4503d1f9ec3SMichael Klier    }
4511172f8dcSAdrian Lang    return p_save_metadata($id, $meta);
4523d1f9ec3SMichael Klier}
4533d1f9ec3SMichael Klier
4543d1f9ec3SMichael Klier/**
4550a7e3bceSchris * read the metadata from source/cache for $id
4560a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata)
4570a7e3bceSchris *
4580a7e3bceSchris * @author   Christopher Smith <chris@jalakai.co.uk>
4590a7e3bceSchris *
4600a7e3bceSchris * @param    string   $id      absolute wiki page id
4610a7e3bceSchris * @param    bool     $cache   whether or not to cache metadata in memory
4620a7e3bceSchris *                             (only use for metadata likely to be accessed several times)
4630a7e3bceSchris *
4640a7e3bceSchris * @return   array             metadata
4650a7e3bceSchris */
4660a7e3bceSchrisfunction p_read_metadata($id,$cache=false) {
4670a7e3bceSchris    global $cache_metadata;
4680a7e3bceSchris
4693a50618cSgweissbach    if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
4700a7e3bceSchris
4710a7e3bceSchris    $file = metaFN($id, '.meta');
4720a7e3bceSchris    $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
4730a7e3bceSchris
4740a7e3bceSchris    if ($cache) {
4753a50618cSgweissbach        $cache_metadata[(string)$id] = $meta;
4760a7e3bceSchris    }
4770a7e3bceSchris
4780a7e3bceSchris    return $meta;
4790a7e3bceSchris}
4800a7e3bceSchris
4810a7e3bceSchris/**
4821172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file
4831172f8dcSAdrian Lang *
4841172f8dcSAdrian Lang * @param    string   $id      absolute wiki page id
4851172f8dcSAdrian Lang * @param    array    $meta    metadata
4861172f8dcSAdrian Lang *
4871172f8dcSAdrian Lang * @return   bool              success / fail
4881172f8dcSAdrian Lang */
4891172f8dcSAdrian Langfunction p_save_metadata($id, $meta) {
4901172f8dcSAdrian Lang    // sync cached copies, including $INFO metadata
4911172f8dcSAdrian Lang    global $cache_metadata, $INFO;
4921172f8dcSAdrian Lang
4931172f8dcSAdrian Lang    if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
4941172f8dcSAdrian Lang    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
4951172f8dcSAdrian Lang
4961172f8dcSAdrian Lang    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
4971172f8dcSAdrian Lang}
4981172f8dcSAdrian Lang
4991172f8dcSAdrian Lang/**
50039a89382SEsther Brunner * renders the metadata of a page
50139a89382SEsther Brunner *
50239a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
50339a89382SEsther Brunner */
50439a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
50548924015SAndreas Gohr    // make sure the correct ID is in global ID
5060e5fde48SMichael Hamann    global $ID, $METADATA_RENDERERS;
5070e5fde48SMichael Hamann
5080e5fde48SMichael Hamann    // avoid recursive rendering processes for the same id
5090e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id]))
5100e5fde48SMichael Hamann        return $orig;
5110e5fde48SMichael Hamann
5120e5fde48SMichael Hamann    // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it
5130e5fde48SMichael Hamann    $METADATA_RENDERERS[$id] =& $orig;
5140e5fde48SMichael Hamann
51548924015SAndreas Gohr    $keep = $ID;
51648924015SAndreas Gohr    $ID   = $id;
51748924015SAndreas Gohr
5180a7e3bceSchris    // add an extra key for the event - to tell event handlers the page whose metadata this is
5190a7e3bceSchris    $orig['page'] = $id;
5200a7e3bceSchris    $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
5210a7e3bceSchris    if ($evt->advise_before()) {
5220a7e3bceSchris
52339a89382SEsther Brunner        require_once DOKU_INC."inc/parser/metadata.php";
52439a89382SEsther Brunner
52539a89382SEsther Brunner        // get instructions
5264b5f4f4eSchris        $instructions = p_cached_instructions(wikiFN($id),false,$id);
52748924015SAndreas Gohr        if(is_null($instructions)){
52848924015SAndreas Gohr            $ID = $keep;
5290e5fde48SMichael Hamann            unset($METADATA_RENDERERS[$id]);
53048924015SAndreas Gohr            return null; // something went wrong with the instructions
53148924015SAndreas Gohr        }
53239a89382SEsther Brunner
53339a89382SEsther Brunner        // set up the renderer
53467f9913dSAndreas Gohr        $renderer = new Doku_Renderer_metadata();
5350e5fde48SMichael Hamann        $renderer->meta =& $orig['current'];
5360e5fde48SMichael Hamann        $renderer->persistent =& $orig['persistent'];
53739a89382SEsther Brunner
53839a89382SEsther Brunner        // loop through the instructions
53939a89382SEsther Brunner        foreach ($instructions as $instruction){
54039a89382SEsther Brunner            // execute the callback against the renderer
5413b748871SAndreas Gohr            call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]);
54239a89382SEsther Brunner        }
54339a89382SEsther Brunner
5440e5fde48SMichael Hamann        $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent);
5450a7e3bceSchris    }
5460a7e3bceSchris    $evt->advise_after();
5470a7e3bceSchris
5480e5fde48SMichael Hamann    // clean up
54948924015SAndreas Gohr    $ID = $keep;
5500e5fde48SMichael Hamann    unset($METADATA_RENDERERS[$id]);
5510a7e3bceSchris    return $evt->result;
55239a89382SEsther Brunner}
55339a89382SEsther Brunner
55439a89382SEsther Brunner/**
555107b01d6Sandi * returns all available parser syntax modes in correct order
556107b01d6Sandi *
557107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
558107b01d6Sandi */
559107b01d6Sandifunction p_get_parsermodes(){
560107b01d6Sandi    global $conf;
561107b01d6Sandi
562107b01d6Sandi    //reuse old data
563107b01d6Sandi    static $modes = null;
5640d24b616SMichael Hamann    if($modes != null && !defined('DOKU_UNITTEST')){
565107b01d6Sandi        return $modes;
566107b01d6Sandi    }
567107b01d6Sandi
568107b01d6Sandi    //import parser classes and mode definitions
569107b01d6Sandi    require_once DOKU_INC . 'inc/parser/parser.php';
570107b01d6Sandi
571107b01d6Sandi    // we now collect all syntax modes and their objects, then they will
572107b01d6Sandi    // be sorted and added to the parser in correct order
573107b01d6Sandi    $modes = array();
574107b01d6Sandi
575107b01d6Sandi    // add syntax plugins
576107b01d6Sandi    $pluginlist = plugin_list('syntax');
577107b01d6Sandi    if(count($pluginlist)){
578107b01d6Sandi        global $PARSER_MODES;
579107b01d6Sandi        $obj = null;
580107b01d6Sandi        foreach($pluginlist as $p){
581e3ab6fc5SMichael Hamann            /** @var DokuWiki_Syntax_Plugin $obj */
582c90b2fb1Schris            if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
583107b01d6Sandi            $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
584107b01d6Sandi            //add to modes
585107b01d6Sandi            $modes[] = array(
586107b01d6Sandi                    'sort' => $obj->getSort(),
587107b01d6Sandi                    'mode' => "plugin_$p",
588107b01d6Sandi                    'obj'  => $obj,
589107b01d6Sandi                    );
590a46d0d65SAndreas Gohr            unset($obj); //remove the reference
591107b01d6Sandi        }
592107b01d6Sandi    }
593107b01d6Sandi
594107b01d6Sandi    // add default modes
595107b01d6Sandi    $std_modes = array('listblock','preformatted','notoc','nocache',
596107b01d6Sandi            'header','table','linebreak','footnote','hr',
597107b01d6Sandi            'unformatted','php','html','code','file','quote',
598e77ea1bcSAndreas Gohr            'internallink','rss','media','externallink',
599e77ea1bcSAndreas Gohr            'emaillink','windowssharelink','eol');
600e77ea1bcSAndreas Gohr    if($conf['typography']){
601e77ea1bcSAndreas Gohr        $std_modes[] = 'quotes';
602e77ea1bcSAndreas Gohr        $std_modes[] = 'multiplyentity';
603e77ea1bcSAndreas Gohr    }
604107b01d6Sandi    foreach($std_modes as $m){
605107b01d6Sandi        $class = "Doku_Parser_Mode_$m";
606107b01d6Sandi        $obj   = new $class();
607107b01d6Sandi        $modes[] = array(
608107b01d6Sandi                'sort' => $obj->getSort(),
609107b01d6Sandi                'mode' => $m,
610107b01d6Sandi                'obj'  => $obj
611107b01d6Sandi                );
612107b01d6Sandi    }
613107b01d6Sandi
614107b01d6Sandi    // add formatting modes
615107b01d6Sandi    $fmt_modes = array('strong','emphasis','underline','monospace',
616107b01d6Sandi            'subscript','superscript','deleted');
617107b01d6Sandi    foreach($fmt_modes as $m){
618107b01d6Sandi        $obj   = new Doku_Parser_Mode_formatting($m);
619107b01d6Sandi        $modes[] = array(
620107b01d6Sandi                'sort' => $obj->getSort(),
621107b01d6Sandi                'mode' => $m,
622107b01d6Sandi                'obj'  => $obj
623107b01d6Sandi                );
624107b01d6Sandi    }
625107b01d6Sandi
626107b01d6Sandi    // add modes which need files
627107b01d6Sandi    $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
628107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
629107b01d6Sandi    $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
630107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
631107b01d6Sandi    $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
632107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
633107b01d6Sandi
634107b01d6Sandi    // add optional camelcase mode
635107b01d6Sandi    if($conf['camelcase']){
636107b01d6Sandi        $obj     = new Doku_Parser_Mode_camelcaselink();
637107b01d6Sandi        $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
638107b01d6Sandi    }
639107b01d6Sandi
640107b01d6Sandi    //sort modes
641107b01d6Sandi    usort($modes,'p_sort_modes');
642107b01d6Sandi
643107b01d6Sandi    return $modes;
644107b01d6Sandi}
645107b01d6Sandi
646107b01d6Sandi/**
647107b01d6Sandi * Callback function for usort
648107b01d6Sandi *
649107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
650107b01d6Sandi */
651107b01d6Sandifunction p_sort_modes($a, $b){
652107b01d6Sandi    if($a['sort'] == $b['sort']) return 0;
653107b01d6Sandi    return ($a['sort'] < $b['sort']) ? -1 : 1;
654107b01d6Sandi}
655107b01d6Sandi
656107b01d6Sandi/**
657ac83b9d8Sandi * Renders a list of instruction to the specified output mode
658c112d578Sandi *
6597a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned
6609dc2c2afSandi *
661c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
662c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
663c112d578Sandi */
6649dc2c2afSandifunction p_render($mode,$instructions,&$info){
665c112d578Sandi    if(is_null($instructions)) return '';
666c112d578Sandi
667d968d3e5SChris Smith    $Renderer =& p_get_renderer($mode);
668d968d3e5SChris Smith    if (is_null($Renderer)) return null;
669c327d6c4SAndreas Gohr
670d968d3e5SChris Smith    $Renderer->reset();
671c112d578Sandi
672c112d578Sandi    $Renderer->smileys = getSmileys();
673c112d578Sandi    $Renderer->entities = getEntities();
674c112d578Sandi    $Renderer->acronyms = getAcronyms();
675c112d578Sandi    $Renderer->interwiki = getInterwiki();
676c112d578Sandi
677c112d578Sandi    // Loop through the instructions
678c112d578Sandi    foreach ( $instructions as $instruction ) {
679c112d578Sandi        // Execute the callback against the Renderer
6806340dabcSAndreas Gohr        call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array());
681c112d578Sandi    }
6829dc2c2afSandi
6839dc2c2afSandi    //set info array
6849dc2c2afSandi    $info = $Renderer->info;
6859dc2c2afSandi
686677844afSchris    // Post process and return the output
687677844afSchris    $data = array($mode,& $Renderer->doc);
688677844afSchris    trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
689c112d578Sandi    return $Renderer->doc;
690c112d578Sandi}
691c112d578Sandi
692e3ab6fc5SMichael Hamann/**
693e3ab6fc5SMichael Hamann * @param $mode string Mode of the renderer to get
694e3ab6fc5SMichael Hamann * @return null|Doku_Renderer The renderer
695e3ab6fc5SMichael Hamann */
696d968d3e5SChris Smithfunction & p_get_renderer($mode) {
697e3ab6fc5SMichael Hamann    /** @var Doku_Plugin_Controller $plugin_controller */
6987aea91afSChris Smith    global $conf, $plugin_controller;
699d968d3e5SChris Smith
700d968d3e5SChris Smith    $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;
7010cacf91fSLucas    $rclass = "Doku_Renderer_$rname";
7020cacf91fSLucas
7030cacf91fSLucas    if( class_exists($rclass) ) {
7040cacf91fSLucas        return new $rclass();
7050cacf91fSLucas    }
706d968d3e5SChris Smith
707d968d3e5SChris Smith    // try default renderer first:
708d968d3e5SChris Smith    $file = DOKU_INC."inc/parser/$rname.php";
709d968d3e5SChris Smith    if(@file_exists($file)){
710d968d3e5SChris Smith        require_once $file;
711d968d3e5SChris Smith
712d968d3e5SChris Smith        if ( !class_exists($rclass) ) {
713d968d3e5SChris Smith            trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
714d968d3e5SChris Smith            msg("Renderer '$rname' for $mode not valid",-1);
715d968d3e5SChris Smith            return null;
716d968d3e5SChris Smith        }
71767f9913dSAndreas Gohr        $Renderer = new $rclass();
718d968d3e5SChris Smith    }else{
7197b27382cSGina Haeussge        // Maybe a plugin/component is available?
7207b27382cSGina Haeussge        list($plugin, $component) = $plugin_controller->_splitName($rname);
7217b27382cSGina Haeussge        if (!$plugin_controller->isdisabled($plugin)){
722f6ec8df8SAdrian Lang            $Renderer =& $plugin_controller->load('renderer',$rname);
7237aea91afSChris Smith        }
7247aea91afSChris Smith
7250a6ae52fSMichael Hamann        if(!isset($Renderer) || is_null($Renderer)){
726d968d3e5SChris Smith            msg("No renderer '$rname' found for mode '$mode'",-1);
727d968d3e5SChris Smith            return null;
728d968d3e5SChris Smith        }
729d968d3e5SChris Smith    }
730d968d3e5SChris Smith
731d968d3e5SChris Smith    return $Renderer;
732d968d3e5SChris Smith}
733d968d3e5SChris Smith
734bb0a59d4Sjan/**
735bb0a59d4Sjan * Gets the first heading from a file
736bb0a59d4Sjan *
737fc18c0fbSchris * @param   string   $id       dokuwiki page id
73867c15eceSMichael Hamann * @param   int      $render   rerender if first heading not known
73967c15eceSMichael Hamann *                             default: METADATA_RENDER_USING_SIMPLE_CACHE
74067c15eceSMichael Hamann *                             Possible values: METADATA_DONT_RENDER,
74167c15eceSMichael Hamann *                                              METADATA_RENDER_USING_SIMPLE_CACHE,
74265aa8490SMichael Hamann *                                              METADATA_RENDER_USING_CACHE,
74365aa8490SMichael Hamann *                                              METADATA_RENDER_UNLIMITED
744fc18c0fbSchris *
745e3ab6fc5SMichael Hamann * @return string|null The first heading
74695dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
747bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de>
748bb0a59d4Sjan */
74967c15eceSMichael Hamannfunction p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){
75065aa8490SMichael Hamann    return p_get_metadata(cleanID($id),'title',$render);
751bb0a59d4Sjan}
752bb0a59d4Sjan
7538f7d700cSchris/**
7548f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output
7558f7d700cSchris *
7565d568b99SChris Smith * @param  string   $code       source code to be highlighted
7575d568b99SChris Smith * @param  string   $language   language to provide highlighting
7585d568b99SChris Smith * @param  string   $wrapper    html element to wrap the returned highlighted text
7595d568b99SChris Smith *
760e3ab6fc5SMichael Hamann * @return string xhtml code
7618f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk>
76235fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7638f7d700cSchris */
7645d568b99SChris Smithfunction p_xhtml_cached_geshi($code, $language, $wrapper='pre') {
765ff1769deSAndreas Gohr    global $conf, $config_cascade, $INPUT;
76635fbe9efSAndreas Gohr    $language = strtolower($language);
7675d568b99SChris Smith
7685d568b99SChris Smith    // remove any leading or trailing blank lines
7695d568b99SChris Smith    $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
7705d568b99SChris Smith
7718f7d700cSchris    $cache = getCacheName($language.$code,".code");
77235fbe9efSAndreas Gohr    $ctime = @filemtime($cache);
773ff1769deSAndreas Gohr    if($ctime && !$INPUT->bool('purge') &&
774f8121585SChris Smith            $ctime > filemtime(DOKU_INC.'inc/geshi.php') &&                 // geshi changed
775f8121585SChris Smith            $ctime > @filemtime(DOKU_INC.'inc/geshi/'.$language.'.php') &&  // language syntax definition changed
776f8121585SChris Smith            $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
7778f7d700cSchris        $highlighted_code = io_readFile($cache, false);
7788f7d700cSchris
7798f7d700cSchris    } else {
7808f7d700cSchris
78135fbe9efSAndreas Gohr        $geshi = new GeSHi($code, $language, DOKU_INC . 'inc/geshi');
7828f7d700cSchris        $geshi->set_encoding('utf-8');
7838f7d700cSchris        $geshi->enable_classes();
7848f7d700cSchris        $geshi->set_header_type(GESHI_HEADER_PRE);
7858f7d700cSchris        $geshi->set_link_target($conf['target']['extern']);
7868f7d700cSchris
7875d568b99SChris Smith        // remove GeSHi's wrapper element (we'll replace it with our own later)
7885d568b99SChris Smith        // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
78969ddc332SAnika Henke        $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r");
7908f7d700cSchris        io_saveFile($cache,$highlighted_code);
7918f7d700cSchris    }
7928f7d700cSchris
7935d568b99SChris Smith    // add a wrapper element if required
7945d568b99SChris Smith    if ($wrapper) {
7955d568b99SChris Smith        return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
7965d568b99SChris Smith    } else {
7978f7d700cSchris        return $highlighted_code;
7988f7d700cSchris    }
7995d568b99SChris Smith}
8008f7d700cSchris
801