xref: /dokuwiki/inc/parserutils.php (revision cbb44eabe033d70affb048ec0daf4e579e09dd20)
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
100db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions;
110db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer;
12e1d9dcc8SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog;
13e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
14d4d8fb18SAndreas Gohruse dokuwiki\Parsing\Parser;
15d4d8fb18SAndreas Gohr
16c112d578Sandi/**
1765aa8490SMichael Hamann * How many pages shall be rendered for getting metadata during one request
1865aa8490SMichael Hamann * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED
1965aa8490SMichael Hamann * is passed as render parameter to p_get_metadata.
20ff725173SMichael Hamann */
2165aa8490SMichael Hamannif (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5);
2267c15eceSMichael Hamann
2367c15eceSMichael Hamann/** Don't render metadata even if it is outdated or doesn't exist */
2467c15eceSMichael Hamanndefine('METADATA_DONT_RENDER', 0);
2565aa8490SMichael Hamann/**
2665aa8490SMichael Hamann * Render metadata when the page is really newer or the metadata doesn't exist.
2765aa8490SMichael Hamann * Uses just a simple check, but should work pretty well for loading simple
2865aa8490SMichael Hamann * metadata values like the page title and avoids rendering a lot of pages in
2965aa8490SMichael Hamann * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode.
3065aa8490SMichael Hamann * Use this if it is unlikely that the metadata value you are requesting
3165aa8490SMichael Hamann * does depend e.g. on pages that are included in the current page using
3265aa8490SMichael Hamann * the include plugin (this is very likely the case for the page title, but
3365aa8490SMichael Hamann * not for relation references).
3465aa8490SMichael Hamann */
3567c15eceSMichael Hamanndefine('METADATA_RENDER_USING_SIMPLE_CACHE', 1);
3665aa8490SMichael Hamann/**
3765aa8490SMichael Hamann * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT
3865aa8490SMichael Hamann * is used in this mode. Use this mode when you are requesting more complex
3965aa8490SMichael Hamann * metadata. Although this will cause rendering more often it might actually have
4065aa8490SMichael Hamann * the effect that less current metadata is returned as it is more likely than in
4165aa8490SMichael Hamann * the simple cache mode that metadata needs to be rendered for all pages at once
4265aa8490SMichael Hamann * which means that when the metadata for the page is requested that actually needs
4365aa8490SMichael Hamann * to be updated the limit might have been reached already.
4465aa8490SMichael Hamann */
4567c15eceSMichael Hamanndefine('METADATA_RENDER_USING_CACHE', 2);
4665aa8490SMichael Hamann/**
4765aa8490SMichael Hamann * Render metadata without limiting the number of pages for which metadata is
4865aa8490SMichael Hamann * rendered. Use this mode with care, normally it should only be used in places
4965aa8490SMichael Hamann * like the indexer or in cli scripts where the execution time normally isn't
5065aa8490SMichael Hamann * limited. This can be combined with the simple cache using
5165aa8490SMichael Hamann * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED.
5265aa8490SMichael Hamann */
5365aa8490SMichael Hamanndefine('METADATA_RENDER_UNLIMITED', 4);
54ff725173SMichael Hamann
55ff725173SMichael Hamann/**
56c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision.
57c112d578Sandi *
58c112d578Sandi * If $excuse is true an explanation is returned if the file
59c112d578Sandi * wasn't found
60c112d578Sandi *
61c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
6242ea7f44SGerrit Uitslag *
6342ea7f44SGerrit Uitslag * @param string $id page id
6442ea7f44SGerrit Uitslag * @param string|int $rev revision timestamp or empty string
6542ea7f44SGerrit Uitslag * @param bool $excuse
66f50a239bSTakamura * @param string $date_at
67f50a239bSTakamura *
6842ea7f44SGerrit Uitslag * @return null|string
69c112d578Sandi */
705c2eed9aSlispsfunction p_wiki_xhtml($id, $rev='', $excuse=true,$date_at=''){
71c112d578Sandi    $file = wikiFN($id,$rev);
72c112d578Sandi    $ret  = '';
73c112d578Sandi
74c112d578Sandi    //ensure $id is in global $ID (needed for parsing)
751e76272cSandi    global $ID;
763ff8773bSAndreas Gohr    $keep = $ID;
771e76272cSandi    $ID   = $id;
78c112d578Sandi
795c2eed9aSlisps    if($rev || $date_at){
8079e79377SAndreas Gohr        if(file_exists($file)){
8164159a61SAndreas Gohr            //no caching on old revisions
8264159a61SAndreas Gohr            $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info,$date_at);
83c112d578Sandi        }elseif($excuse){
84c112d578Sandi            $ret = p_locale_xhtml('norev');
85c112d578Sandi        }
86c112d578Sandi    }else{
8779e79377SAndreas Gohr        if(file_exists($file)){
884b5f4f4eSchris            $ret = p_cached_output($file,'xhtml',$id);
89c112d578Sandi        }elseif($excuse){
902eaa0567Speterfromearth            //check if the page once existed
91e1d9dcc8SAndreas Gohr            $changelog = new PageChangeLog($id);
922eaa0567Speterfromearth            if($changelog->hasRevisions()) {
932eaa0567Speterfromearth                $ret = p_locale_xhtml('onceexisted');
942eaa0567Speterfromearth            } else {
95c112d578Sandi                $ret = p_locale_xhtml('newpage');
96c112d578Sandi            }
97c112d578Sandi        }
982eaa0567Speterfromearth    }
99c112d578Sandi
1003ff8773bSAndreas Gohr    //restore ID (just in case)
1013ff8773bSAndreas Gohr    $ID = $keep;
1023ff8773bSAndreas Gohr
103c112d578Sandi    return $ret;
104c112d578Sandi}
105c112d578Sandi
106c112d578Sandi/**
107c112d578Sandi * Returns the specified local text in parsed format
108c112d578Sandi *
109c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
11042ea7f44SGerrit Uitslag *
11142ea7f44SGerrit Uitslag * @param string $id page id
11242ea7f44SGerrit Uitslag * @return null|string
113c112d578Sandi */
114c112d578Sandifunction p_locale_xhtml($id){
115c112d578Sandi    //fetch parsed locale
1164b5f4f4eSchris    $html = p_cached_output(localeFN($id));
117c112d578Sandi    return $html;
118c112d578Sandi}
119c112d578Sandi
120c112d578Sandi/**
1214b5f4f4eSchris * Returns the given file parsed into the requested output format
1224b5f4f4eSchris *
1234b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org>
1244b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk>
12542ea7f44SGerrit Uitslag *
12642ea7f44SGerrit Uitslag * @param string $file filename, path to file
12742ea7f44SGerrit Uitslag * @param string $format
12842ea7f44SGerrit Uitslag * @param string $id page id
12942ea7f44SGerrit Uitslag * @return null|string
1304b5f4f4eSchris */
1314b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') {
132c112d578Sandi    global $conf;
133c112d578Sandi
1340db5771eSMichael Große    $cache = new CacheRenderer($id, $file, $format);
1354b5f4f4eSchris    if ($cache->useCache()) {
13685767031SAndreas Gohr        $parsed = $cache->retrieveCache(false);
1377de86af9SGerrit Uitslag        if($conf['allowdebug'] && $format=='xhtml') {
1387de86af9SGerrit Uitslag            $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
1397de86af9SGerrit Uitslag        }
140c112d578Sandi    } else {
1414b5f4f4eSchris        $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
142c112d578Sandi
14359b1d918SChristopher Smith        if ($info['cache'] && $cache->storeCache($parsed)) {              // storeCache() attempts to save cachefile
1447de86af9SGerrit Uitslag            if($conf['allowdebug'] && $format=='xhtml') {
1457de86af9SGerrit Uitslag                $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
1467de86af9SGerrit Uitslag            }
147c112d578Sandi        }else{
1484b5f4f4eSchris            $cache->removeCache();                     //try to delete cachefile
1497de86af9SGerrit Uitslag            if($conf['allowdebug'] && $format=='xhtml') {
1507de86af9SGerrit Uitslag                $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
1517de86af9SGerrit Uitslag            }
152c112d578Sandi        }
153c112d578Sandi    }
154c112d578Sandi
155c112d578Sandi    return $parsed;
156c112d578Sandi}
157c112d578Sandi
158c112d578Sandi/**
159c112d578Sandi * Returns the render instructions for a file
160c112d578Sandi *
161c112d578Sandi * Uses and creates a serialized cache file
162c112d578Sandi *
163c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
16442ea7f44SGerrit Uitslag *
16542ea7f44SGerrit Uitslag * @param string $file      filename, path to file
16642ea7f44SGerrit Uitslag * @param bool   $cacheonly
16742ea7f44SGerrit Uitslag * @param string $id        page id
16842ea7f44SGerrit Uitslag * @return array|null
169c112d578Sandi */
1704b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') {
17147b2d319SAndreas Gohr    static $run = null;
17247b2d319SAndreas Gohr    if(is_null($run)) $run = array();
173c112d578Sandi
1740db5771eSMichael Große    $cache = new CacheInstructions($id, $file);
175c112d578Sandi
1760d24b616SMichael Hamann    if ($cacheonly || $cache->useCache() || (isset($run[$file]) && !defined('DOKU_UNITTEST'))) {
1774b5f4f4eSchris        return $cache->retrieveCache();
17879e79377SAndreas Gohr    } else if (file_exists($file)) {
179c112d578Sandi        // no cache - do some work
180bde4e341SGalaxyMaster        $ins = p_get_instructions(io_readWikiPage($file,$id));
181cbaf4259SChris Smith        if ($cache->storeCache($ins)) {
18247b2d319SAndreas Gohr            $run[$file] = true; // we won't rebuild these instructions in the same run again
183cbaf4259SChris Smith        } else {
184cbaf4259SChris Smith            msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
185cbaf4259SChris Smith        }
186c112d578Sandi        return $ins;
187c112d578Sandi    }
188c112d578Sandi
1893b3f8916SAndreas Gohr    return null;
190c112d578Sandi}
191c112d578Sandi
192c112d578Sandi/**
193c112d578Sandi * turns a page into a list of instructions
194c112d578Sandi *
195c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
196c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
19742ea7f44SGerrit Uitslag *
19899ba9fe6SAndreas Gohr * @param string $text  raw wiki syntax text
19999ba9fe6SAndreas Gohr * @return array a list of instruction arrays
200c112d578Sandi */
2016bbae538Sandifunction p_get_instructions($text){
202c112d578Sandi
203107b01d6Sandi    $modes = p_get_parsermodes();
204ee20e7d1Sandi
20547f73ecfSAndreas Gohr    // Create the parser and handler
206d4d8fb18SAndreas Gohr    $Parser = new Parser(new Doku_Handler());
207c112d578Sandi
208107b01d6Sandi    //add modes to parser
209107b01d6Sandi    foreach($modes as $mode){
210107b01d6Sandi        $Parser->addMode($mode['mode'],$mode['obj']);
211c112d578Sandi    }
212c112d578Sandi
213c112d578Sandi    // Do the parsing
214*cbb44eabSAndreas Gohr    Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text);
215a2d649c4Sandi    $p = $Parser->parse($text);
216ee20e7d1Sandi    //  dbg($p);
217a2d649c4Sandi    return $p;
218c112d578Sandi}
219c112d578Sandi
220c112d578Sandi/**
22139a89382SEsther Brunner * returns the metadata of a page
22239a89382SEsther Brunner *
2234a819402SMichael Hamann * @param string $id      The id of the page the metadata should be returned from
22464159a61SAndreas Gohr * @param string $key     The key of the metdata value that shall be read (by default everything)
22564159a61SAndreas Gohr *                        separate hierarchies by " " like "date created"
22667c15eceSMichael Hamann * @param int    $render  If the page should be rendererd - possible values:
22765aa8490SMichael Hamann *     METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE
22865aa8490SMichael Hamann *     METADATA_RENDER_UNLIMITED (also combined with the previous two options),
22965aa8490SMichael Hamann *     default: METADATA_RENDER_USING_CACHE
2304a819402SMichael Hamann * @return mixed The requested metadata fields
2314a819402SMichael Hamann *
23239a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
23398214867SMichael Hamann * @author Michael Hamann <michael@content-space.de>
23439a89382SEsther Brunner */
23567c15eceSMichael Hamannfunction p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){
2361172f8dcSAdrian Lang    global $ID;
23765aa8490SMichael Hamann    static $render_count = 0;
23865aa8490SMichael Hamann    // track pages that have already been rendered in order to avoid rendering the same page
23965aa8490SMichael Hamann    // again
24065aa8490SMichael Hamann    static $rendered_pages = array();
2416afe8dcaSchris
2420a7e3bceSchris    // cache the current page
2430a7e3bceSchris    // Benchmarking shows the current page's metadata is generally the only page metadata
2440a7e3bceSchris    // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
2450a7e3bceSchris    $cache = ($ID == $id);
2460a7e3bceSchris    $meta = p_read_metadata($id, $cache);
24739a89382SEsther Brunner
24867c15eceSMichael Hamann    if (!is_numeric($render)) {
24967c15eceSMichael Hamann        if ($render) {
25067c15eceSMichael Hamann            $render = METADATA_RENDER_USING_SIMPLE_CACHE;
25167c15eceSMichael Hamann        } else {
25267c15eceSMichael Hamann            $render = METADATA_DONT_RENDER;
25367c15eceSMichael Hamann        }
25467c15eceSMichael Hamann    }
25567c15eceSMichael Hamann
25698214867SMichael Hamann    // prevent recursive calls in the cache
25798214867SMichael Hamann    static $recursion = false;
25865aa8490SMichael Hamann    if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){
25998214867SMichael Hamann        $recursion = true;
26098214867SMichael Hamann
2610db5771eSMichael Große        $cachefile = new CacheRenderer($id, wikiFN($id), 'metadata');
26298214867SMichael Hamann
26367c15eceSMichael Hamann        $do_render = false;
26465aa8490SMichael Hamann        if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) {
26565aa8490SMichael Hamann            if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) {
26667c15eceSMichael Hamann                $pagefn = wikiFN($id);
26767c15eceSMichael Hamann                $metafn = metaFN($id, '.meta');
26879e79377SAndreas Gohr                if (!file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) {
26967c15eceSMichael Hamann                    $do_render = true;
27067c15eceSMichael Hamann                }
27167c15eceSMichael Hamann            } elseif (!$cachefile->useCache()){
27267c15eceSMichael Hamann                $do_render = true;
27367c15eceSMichael Hamann            }
27465aa8490SMichael Hamann        }
27567c15eceSMichael Hamann        if ($do_render) {
2760d24b616SMichael Hamann            if (!defined('DOKU_UNITTEST')) {
27765aa8490SMichael Hamann                ++$render_count;
27865aa8490SMichael Hamann                $rendered_pages[$id] = true;
2790d24b616SMichael Hamann            }
28069ba640bSMichael Hamann            $old_meta = $meta;
28139a89382SEsther Brunner            $meta = p_render_metadata($id, $meta);
28269ba640bSMichael Hamann            // only update the file when the metadata has been changed
28369ba640bSMichael Hamann            if ($meta == $old_meta || p_save_metadata($id, $meta)) {
28498214867SMichael Hamann                // store a timestamp in order to make sure that the cachefile is touched
2853d6feb16SMichael Hamann                // this timestamp is also stored when the meta data is still the same
28698214867SMichael Hamann                $cachefile->storeCache(time());
2873d6feb16SMichael Hamann            } else {
28898214867SMichael Hamann                msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1);
28998214867SMichael Hamann            }
29098214867SMichael Hamann        }
29198214867SMichael Hamann
29298214867SMichael Hamann        $recursion = false;
2936afe8dcaSchris    }
29439a89382SEsther Brunner
295ebf65d37SAdrian Lang    $val = $meta['current'];
296ebf65d37SAdrian Lang
29739a89382SEsther Brunner    // filter by $key
298569a0019SAdrian Lang    foreach(preg_split('/\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) {
299ebf65d37SAdrian Lang        if (!isset($val[$cur_key])) {
300ebf65d37SAdrian Lang            return null;
30166d29756SChris Smith        }
302ebf65d37SAdrian Lang        $val = $val[$cur_key];
30339a89382SEsther Brunner    }
304ebf65d37SAdrian Lang    return $val;
30539a89382SEsther Brunner}
30639a89382SEsther Brunner
30739a89382SEsther Brunner/**
30839a89382SEsther Brunner * sets metadata elements of a page
30939a89382SEsther Brunner *
310a365baeeSDominik Eckelmann * @see http://www.dokuwiki.org/devel:metadata#functions_to_get_and_set_metadata
311a365baeeSDominik Eckelmann *
312e1d9dcc8SAndreas Gohr * @param string  $id         is the ID of a wiki page
313e1d9dcc8SAndreas Gohr * @param array   $data       is an array with key ⇒ value pairs to be set in the metadata
314e1d9dcc8SAndreas Gohr * @param boolean $render     whether or not the page metadata should be generated with the renderer
315e1d9dcc8SAndreas Gohr * @param boolean $persistent indicates whether or not the particular metadata value will persist through
316a365baeeSDominik Eckelmann *                            the next metadata rendering.
317a365baeeSDominik Eckelmann * @return boolean true on success
318a365baeeSDominik Eckelmann *
31939a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
3200e5fde48SMichael Hamann * @author Michael Hamann <michael@content-space.de>
32139a89382SEsther Brunner */
3220a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){
32339a89382SEsther Brunner    if (!is_array($data)) return false;
32439a89382SEsther Brunner
3250e5fde48SMichael Hamann    global $ID, $METADATA_RENDERERS;
3260a7e3bceSchris
3270e5fde48SMichael Hamann    // if there is currently a renderer change the data in the renderer instead
3280e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id])) {
3290e5fde48SMichael Hamann        $orig =& $METADATA_RENDERERS[$id];
3300e5fde48SMichael Hamann        $meta = $orig;
3310e5fde48SMichael Hamann    } else {
3320a7e3bceSchris        // cache the current page
3330a7e3bceSchris        $cache = ($ID == $id);
3340a7e3bceSchris        $orig = p_read_metadata($id, $cache);
33539a89382SEsther Brunner
33639a89382SEsther Brunner        // render metadata first?
3370a7e3bceSchris        $meta = $render ? p_render_metadata($id, $orig) : $orig;
3380e5fde48SMichael Hamann    }
33939a89382SEsther Brunner
34039a89382SEsther Brunner    // now add the passed metadata
34139a89382SEsther Brunner    $protected = array('description', 'date', 'contributor');
34239a89382SEsther Brunner    foreach ($data as $key => $value){
34339a89382SEsther Brunner
34439a89382SEsther Brunner        // be careful with sub-arrays of $meta['relation']
34539a89382SEsther Brunner        if ($key == 'relation'){
3460a7e3bceSchris
34739a89382SEsther Brunner            foreach ($value as $subkey => $subvalue){
34831b10b49SMichael Hamann                if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
3492d102494SPhy                    $meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue);
35031b10b49SMichael Hamann                } else {
35131b10b49SMichael Hamann                    $meta['current'][$key][$subkey] = $subvalue;
35231b10b49SMichael Hamann                }
35331b10b49SMichael Hamann                if($persistent) {
35431b10b49SMichael Hamann                    if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
35564159a61SAndreas Gohr                        $meta['persistent'][$key][$subkey] = array_replace(
35664159a61SAndreas Gohr                            $meta['persistent'][$key][$subkey],
35764159a61SAndreas Gohr                            (array) $subvalue
35864159a61SAndreas Gohr                        );
35931b10b49SMichael Hamann                    } else {
36031b10b49SMichael Hamann                        $meta['persistent'][$key][$subkey] = $subvalue;
36131b10b49SMichael Hamann                    }
36231b10b49SMichael Hamann                }
36339a89382SEsther Brunner            }
36439a89382SEsther Brunner
36539a89382SEsther Brunner            // be careful with some senisitive arrays of $meta
36639a89382SEsther Brunner        } elseif (in_array($key, $protected)){
3670a7e3bceSchris
36866d29756SChris Smith            // these keys, must have subkeys - a legitimate value must be an array
36939a89382SEsther Brunner            if (is_array($value)) {
37064159a61SAndreas Gohr                $meta['current'][$key] = !empty($meta['current'][$key]) ?
37164159a61SAndreas Gohr                    array_replace((array)$meta['current'][$key],$value) :
37264159a61SAndreas Gohr                    $value;
3730a7e3bceSchris
3740a7e3bceSchris                if ($persistent) {
37564159a61SAndreas Gohr                    $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ?
37664159a61SAndreas Gohr                        array_replace((array)$meta['persistent'][$key],$value) :
37764159a61SAndreas Gohr                        $value;
3780a7e3bceSchris                }
37939a89382SEsther Brunner            }
38039a89382SEsther Brunner
38139a89382SEsther Brunner            // no special treatment for the rest
38239a89382SEsther Brunner        } else {
3830a7e3bceSchris            $meta['current'][$key] = $value;
3840a7e3bceSchris            if ($persistent) $meta['persistent'][$key] = $value;
38539a89382SEsther Brunner        }
38639a89382SEsther Brunner    }
38739a89382SEsther Brunner
38839a89382SEsther Brunner    // save only if metadata changed
38939a89382SEsther Brunner    if ($meta == $orig) return true;
3906afe8dcaSchris
3910e5fde48SMichael Hamann    if (isset($METADATA_RENDERERS[$id])) {
3920e5fde48SMichael Hamann        // set both keys individually as the renderer has references to the individual keys
3930e5fde48SMichael Hamann        $METADATA_RENDERERS[$id]['current']    = $meta['current'];
3940e5fde48SMichael Hamann        $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent'];
3951c56be7bSMichael Hamann        return true;
3960e5fde48SMichael Hamann    } else {
3971172f8dcSAdrian Lang        return p_save_metadata($id, $meta);
39839a89382SEsther Brunner    }
3990e5fde48SMichael Hamann}
40039a89382SEsther Brunner
40139a89382SEsther Brunner/**
4023d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data
4033d1f9ec3SMichael Klier * used on page deletion
4043d1f9ec3SMichael Klier *
4053d1f9ec3SMichael Klier * @author Michael Klier <chi@chimeric.de>
40642ea7f44SGerrit Uitslag *
40742ea7f44SGerrit Uitslag * @param string $id page id
40842ea7f44SGerrit Uitslag * @return bool  success / fail
4093d1f9ec3SMichael Klier */
4103d1f9ec3SMichael Klierfunction p_purge_metadata($id) {
4113d1f9ec3SMichael Klier    $meta = p_read_metadata($id);
4123d1f9ec3SMichael Klier    foreach($meta['current'] as $key => $value) {
4133d1f9ec3SMichael Klier        if(is_array($meta[$key])) {
4143d1f9ec3SMichael Klier            $meta['current'][$key] = array();
4153d1f9ec3SMichael Klier        } else {
4163d1f9ec3SMichael Klier            $meta['current'][$key] = '';
4173d1f9ec3SMichael Klier        }
4181172f8dcSAdrian Lang
4193d1f9ec3SMichael Klier    }
4201172f8dcSAdrian Lang    return p_save_metadata($id, $meta);
4213d1f9ec3SMichael Klier}
4223d1f9ec3SMichael Klier
4233d1f9ec3SMichael Klier/**
4240a7e3bceSchris * read the metadata from source/cache for $id
4250a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata)
4260a7e3bceSchris *
4270a7e3bceSchris * @author   Christopher Smith <chris@jalakai.co.uk>
4280a7e3bceSchris *
4290a7e3bceSchris * @param    string   $id      absolute wiki page id
4300a7e3bceSchris * @param    bool     $cache   whether or not to cache metadata in memory
4310a7e3bceSchris *                             (only use for metadata likely to be accessed several times)
4320a7e3bceSchris *
4330a7e3bceSchris * @return   array             metadata
4340a7e3bceSchris */
4350a7e3bceSchrisfunction p_read_metadata($id,$cache=false) {
4360a7e3bceSchris    global $cache_metadata;
4370a7e3bceSchris
4383a50618cSgweissbach    if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
4390a7e3bceSchris
4400a7e3bceSchris    $file = metaFN($id, '.meta');
44164159a61SAndreas Gohr    $meta = file_exists($file) ?
44264159a61SAndreas Gohr        unserialize(io_readFile($file, false)) :
44364159a61SAndreas Gohr        array('current'=>array(),'persistent'=>array());
4440a7e3bceSchris
4450a7e3bceSchris    if ($cache) {
4463a50618cSgweissbach        $cache_metadata[(string)$id] = $meta;
4470a7e3bceSchris    }
4480a7e3bceSchris
4490a7e3bceSchris    return $meta;
4500a7e3bceSchris}
4510a7e3bceSchris
4520a7e3bceSchris/**
4531172f8dcSAdrian Lang * This is the backend function to save a metadata array to a file
4541172f8dcSAdrian Lang *
4551172f8dcSAdrian Lang * @param    string   $id      absolute wiki page id
4561172f8dcSAdrian Lang * @param    array    $meta    metadata
4571172f8dcSAdrian Lang *
4581172f8dcSAdrian Lang * @return   bool              success / fail
4591172f8dcSAdrian Lang */
4601172f8dcSAdrian Langfunction p_save_metadata($id, $meta) {
4611172f8dcSAdrian Lang    // sync cached copies, including $INFO metadata
4621172f8dcSAdrian Lang    global $cache_metadata, $INFO;
4631172f8dcSAdrian Lang
4641172f8dcSAdrian Lang    if (isset($cache_metadata[$id])) $cache_metadata[$id] = $meta;
4651172f8dcSAdrian Lang    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
4661172f8dcSAdrian Lang
4671172f8dcSAdrian Lang    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
4681172f8dcSAdrian Lang}
4691172f8dcSAdrian Lang
4701172f8dcSAdrian Lang/**
47139a89382SEsther Brunner * renders the metadata of a page
47239a89382SEsther Brunner *
47339a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
47442ea7f44SGerrit Uitslag *
47542ea7f44SGerrit Uitslag * @param string $id    page id
47642ea7f44SGerrit Uitslag * @param array  $orig  the original metadata
47742ea7f44SGerrit Uitslag * @return array|null array('current'=> array,'persistent'=> array);
47839a89382SEsther Brunner */
47939a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
48048924015SAndreas Gohr    // make sure the correct ID is in global ID
4810e5fde48SMichael Hamann    global $ID, $METADATA_RENDERERS;
4820e5fde48SMichael Hamann
4830e5fde48SMichael Hamann    // avoid recursive rendering processes for the same id
4845b76ad91SChristopher Smith    if (isset($METADATA_RENDERERS[$id])) {
4850e5fde48SMichael Hamann        return $orig;
4865b76ad91SChristopher Smith    }
4870e5fde48SMichael Hamann
4880e5fde48SMichael Hamann    // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it
4890e5fde48SMichael Hamann    $METADATA_RENDERERS[$id] =& $orig;
4900e5fde48SMichael Hamann
49148924015SAndreas Gohr    $keep = $ID;
49248924015SAndreas Gohr    $ID   = $id;
49348924015SAndreas Gohr
4940a7e3bceSchris    // add an extra key for the event - to tell event handlers the page whose metadata this is
4950a7e3bceSchris    $orig['page'] = $id;
496e1d9dcc8SAndreas Gohr    $evt = new Event('PARSER_METADATA_RENDER', $orig);
4970a7e3bceSchris    if ($evt->advise_before()) {
4980a7e3bceSchris
49939a89382SEsther Brunner        // get instructions
5004b5f4f4eSchris        $instructions = p_cached_instructions(wikiFN($id),false,$id);
50148924015SAndreas Gohr        if(is_null($instructions)){
50248924015SAndreas Gohr            $ID = $keep;
5030e5fde48SMichael Hamann            unset($METADATA_RENDERERS[$id]);
50448924015SAndreas Gohr            return null; // something went wrong with the instructions
50548924015SAndreas Gohr        }
50639a89382SEsther Brunner
50739a89382SEsther Brunner        // set up the renderer
50867f9913dSAndreas Gohr        $renderer = new Doku_Renderer_metadata();
5090e5fde48SMichael Hamann        $renderer->meta =& $orig['current'];
5100e5fde48SMichael Hamann        $renderer->persistent =& $orig['persistent'];
51139a89382SEsther Brunner
51239a89382SEsther Brunner        // loop through the instructions
51339a89382SEsther Brunner        foreach ($instructions as $instruction){
51439a89382SEsther Brunner            // execute the callback against the renderer
5153b748871SAndreas Gohr            call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]);
51639a89382SEsther Brunner        }
51739a89382SEsther Brunner
5180e5fde48SMichael Hamann        $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent);
5190a7e3bceSchris    }
5200a7e3bceSchris    $evt->advise_after();
5210a7e3bceSchris
5220e5fde48SMichael Hamann    // clean up
52348924015SAndreas Gohr    $ID = $keep;
5240e5fde48SMichael Hamann    unset($METADATA_RENDERERS[$id]);
5250a7e3bceSchris    return $evt->result;
52639a89382SEsther Brunner}
52739a89382SEsther Brunner
52839a89382SEsther Brunner/**
529107b01d6Sandi * returns all available parser syntax modes in correct order
530107b01d6Sandi *
531107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
53242ea7f44SGerrit Uitslag *
53342ea7f44SGerrit Uitslag * @return array[] with for each plugin the array('sort' => sortnumber, 'mode' => mode string, 'obj'  => plugin object)
534107b01d6Sandi */
535107b01d6Sandifunction p_get_parsermodes(){
536107b01d6Sandi    global $conf;
537107b01d6Sandi
538107b01d6Sandi    //reuse old data
539107b01d6Sandi    static $modes = null;
5400d24b616SMichael Hamann    if($modes != null && !defined('DOKU_UNITTEST')){
541107b01d6Sandi        return $modes;
542107b01d6Sandi    }
543107b01d6Sandi
544107b01d6Sandi    //import parser classes and mode definitions
545107b01d6Sandi    require_once DOKU_INC . 'inc/parser/parser.php';
546107b01d6Sandi
547107b01d6Sandi    // we now collect all syntax modes and their objects, then they will
548107b01d6Sandi    // be sorted and added to the parser in correct order
549107b01d6Sandi    $modes = array();
550107b01d6Sandi
551107b01d6Sandi    // add syntax plugins
552107b01d6Sandi    $pluginlist = plugin_list('syntax');
553107b01d6Sandi    if(count($pluginlist)){
554107b01d6Sandi        global $PARSER_MODES;
555107b01d6Sandi        $obj = null;
556107b01d6Sandi        foreach($pluginlist as $p){
557e1d9dcc8SAndreas Gohr            /** @var \dokuwiki\Extension\SyntaxPlugin $obj */
558e8b5a4f9SAndreas Gohr            if(!$obj = plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
559107b01d6Sandi            $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
560107b01d6Sandi            //add to modes
561107b01d6Sandi            $modes[] = array(
562107b01d6Sandi                    'sort' => $obj->getSort(),
563107b01d6Sandi                    'mode' => "plugin_$p",
564107b01d6Sandi                    'obj'  => $obj,
565107b01d6Sandi                    );
566a46d0d65SAndreas Gohr            unset($obj); //remove the reference
567107b01d6Sandi        }
568107b01d6Sandi    }
569107b01d6Sandi
570107b01d6Sandi    // add default modes
571107b01d6Sandi    $std_modes = array('listblock','preformatted','notoc','nocache',
572107b01d6Sandi            'header','table','linebreak','footnote','hr',
573107b01d6Sandi            'unformatted','php','html','code','file','quote',
574e77ea1bcSAndreas Gohr            'internallink','rss','media','externallink',
575e77ea1bcSAndreas Gohr            'emaillink','windowssharelink','eol');
576e77ea1bcSAndreas Gohr    if($conf['typography']){
577e77ea1bcSAndreas Gohr        $std_modes[] = 'quotes';
578e77ea1bcSAndreas Gohr        $std_modes[] = 'multiplyentity';
579e77ea1bcSAndreas Gohr    }
580107b01d6Sandi    foreach($std_modes as $m){
581be906b56SAndreas Gohr        $class = 'dokuwiki\\Parsing\\ParserMode\\'.ucfirst($m);
582107b01d6Sandi        $obj   = new $class();
583107b01d6Sandi        $modes[] = array(
584107b01d6Sandi                'sort' => $obj->getSort(),
585107b01d6Sandi                'mode' => $m,
586107b01d6Sandi                'obj'  => $obj
587107b01d6Sandi                );
588107b01d6Sandi    }
589107b01d6Sandi
590107b01d6Sandi    // add formatting modes
591107b01d6Sandi    $fmt_modes = array('strong','emphasis','underline','monospace',
592107b01d6Sandi            'subscript','superscript','deleted');
593107b01d6Sandi    foreach($fmt_modes as $m){
594be906b56SAndreas Gohr        $obj   = new \dokuwiki\Parsing\ParserMode\Formatting($m);
595107b01d6Sandi        $modes[] = array(
596107b01d6Sandi                'sort' => $obj->getSort(),
597107b01d6Sandi                'mode' => $m,
598107b01d6Sandi                'obj'  => $obj
599107b01d6Sandi                );
600107b01d6Sandi    }
601107b01d6Sandi
602107b01d6Sandi    // add modes which need files
603be906b56SAndreas Gohr    $obj     = new \dokuwiki\Parsing\ParserMode\Smiley(array_keys(getSmileys()));
604107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
605be906b56SAndreas Gohr    $obj     = new \dokuwiki\Parsing\ParserMode\Acronym(array_keys(getAcronyms()));
606107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
607be906b56SAndreas Gohr    $obj     = new \dokuwiki\Parsing\ParserMode\Entity(array_keys(getEntities()));
608107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
609107b01d6Sandi
610107b01d6Sandi    // add optional camelcase mode
611107b01d6Sandi    if($conf['camelcase']){
612be906b56SAndreas Gohr        $obj     = new \dokuwiki\Parsing\ParserMode\Camelcaselink();
613107b01d6Sandi        $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
614107b01d6Sandi    }
615107b01d6Sandi
616107b01d6Sandi    //sort modes
617107b01d6Sandi    usort($modes,'p_sort_modes');
618107b01d6Sandi
619107b01d6Sandi    return $modes;
620107b01d6Sandi}
621107b01d6Sandi
622107b01d6Sandi/**
623107b01d6Sandi * Callback function for usort
624107b01d6Sandi *
625107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
62642ea7f44SGerrit Uitslag *
62742ea7f44SGerrit Uitslag * @param array $a
62842ea7f44SGerrit Uitslag * @param array $b
62942ea7f44SGerrit Uitslag * @return int $a is lower/equal/higher than $b
630107b01d6Sandi */
631107b01d6Sandifunction p_sort_modes($a, $b){
632107b01d6Sandi    if($a['sort'] == $b['sort']) return 0;
633107b01d6Sandi    return ($a['sort'] < $b['sort']) ? -1 : 1;
634107b01d6Sandi}
635107b01d6Sandi
636107b01d6Sandi/**
637ac83b9d8Sandi * Renders a list of instruction to the specified output mode
638c112d578Sandi *
6397a8cc57eSElan Ruusamäe * In the $info array is information from the renderer returned
6409dc2c2afSandi *
641c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
642c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
64342ea7f44SGerrit Uitslag *
64442ea7f44SGerrit Uitslag * @param string $mode
645e3710957SGerrit Uitslag * @param array|null|false $instructions
64642ea7f44SGerrit Uitslag * @param array $info returns render info like enabled toc and cache
6477de86af9SGerrit Uitslag * @param string $date_at
64842ea7f44SGerrit Uitslag * @return null|string rendered output
649c112d578Sandi */
6504bde2196Slispsfunction p_render($mode,$instructions,&$info,$date_at=''){
651c112d578Sandi    if(is_null($instructions)) return '';
652e3710957SGerrit Uitslag    if($instructions === false) return '';
653c112d578Sandi
654252398f0SChristopher Smith    $Renderer = p_get_renderer($mode);
655d968d3e5SChris Smith    if (is_null($Renderer)) return null;
656c327d6c4SAndreas Gohr
657d968d3e5SChris Smith    $Renderer->reset();
658c112d578Sandi
6595c2eed9aSlisps    if($date_at) {
6605c2eed9aSlisps        $Renderer->date_at = $date_at;
6615c2eed9aSlisps    }
6625c2eed9aSlisps
663c112d578Sandi    $Renderer->smileys = getSmileys();
664c112d578Sandi    $Renderer->entities = getEntities();
665c112d578Sandi    $Renderer->acronyms = getAcronyms();
666c112d578Sandi    $Renderer->interwiki = getInterwiki();
667c112d578Sandi
668c112d578Sandi    // Loop through the instructions
669c112d578Sandi    foreach ( $instructions as $instruction ) {
670c112d578Sandi        // Execute the callback against the Renderer
6717c62086bSAndreas Gohr        if(method_exists($Renderer, $instruction[0])){
6726340dabcSAndreas Gohr            call_user_func_array(array(&$Renderer, $instruction[0]), $instruction[1] ? $instruction[1] : array());
673c112d578Sandi        }
6747c62086bSAndreas Gohr    }
6759dc2c2afSandi
6769dc2c2afSandi    //set info array
6779dc2c2afSandi    $info = $Renderer->info;
6789dc2c2afSandi
679677844afSchris    // Post process and return the output
680677844afSchris    $data = array($mode,& $Renderer->doc);
681*cbb44eabSAndreas Gohr    Event::createAndTrigger('RENDERER_CONTENT_POSTPROCESS',$data);
682c112d578Sandi    return $Renderer->doc;
683c112d578Sandi}
684c112d578Sandi
685e3ab6fc5SMichael Hamann/**
686548d801fSChristopher Smith * Figure out the correct renderer class to use for $mode,
687548d801fSChristopher Smith * instantiate and return it
688548d801fSChristopher Smith *
6897e8500eeSGerrit Uitslag * @param string $mode Mode of the renderer to get
690e3ab6fc5SMichael Hamann * @return null|Doku_Renderer The renderer
691548d801fSChristopher Smith *
692548d801fSChristopher Smith * @author Christopher Smith <chris@jalakai.co.uk>
693e3ab6fc5SMichael Hamann */
694252398f0SChristopher Smithfunction p_get_renderer($mode) {
695e3ab6fc5SMichael Hamann    /** @var Doku_Plugin_Controller $plugin_controller */
6967aea91afSChris Smith    global $conf, $plugin_controller;
697d968d3e5SChris Smith
698d968d3e5SChris Smith    $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;
6990cacf91fSLucas    $rclass = "Doku_Renderer_$rname";
7000cacf91fSLucas
701548d801fSChristopher Smith    // if requested earlier or a bundled renderer
7020cacf91fSLucas    if( class_exists($rclass) ) {
7035e40b274SChristopher Smith        $Renderer = new $rclass();
7045e40b274SChristopher Smith        return $Renderer;
7050cacf91fSLucas    }
706d968d3e5SChris Smith
7076e6d16edSChristopher Smith    // not bundled, see if its an enabled renderer plugin & when $mode is 'xhtml', the renderer can supply that format.
7080440ca46SGerrit Uitslag    /** @var Doku_Renderer $Renderer */
70911ac6abdSChristopher Smith    $Renderer = $plugin_controller->load('renderer',$rname);
7106e6d16edSChristopher Smith    if ($Renderer && is_a($Renderer, 'Doku_Renderer')  && ($mode != 'xhtml' || $mode == $Renderer->getFormat())) {
71111ac6abdSChristopher Smith        return $Renderer;
71211ac6abdSChristopher Smith    }
713d968d3e5SChris Smith
71411ac6abdSChristopher Smith    // there is a configuration error!
715548d801fSChristopher Smith    // not bundled, not a valid enabled plugin, use $mode to try to fallback to a bundled renderer
71611ac6abdSChristopher Smith    $rclass = "Doku_Renderer_$mode";
717548d801fSChristopher Smith    if ( class_exists($rclass) ) {
71811ac6abdSChristopher Smith        // viewers should see renderered output, so restrict the warning to admins only
71911ac6abdSChristopher Smith        $msg = "No renderer '$rname' found for mode '$mode', check your plugins";
72011ac6abdSChristopher Smith        if ($mode == 'xhtml') {
72111ac6abdSChristopher Smith            $msg .= " and the 'renderer_xhtml' config setting";
72211ac6abdSChristopher Smith        }
72311ac6abdSChristopher Smith        $msg .= ".<br/>Attempting to fallback to the bundled renderer.";
724548d801fSChristopher Smith        msg($msg,-1,'','',MSG_ADMINS_ONLY);
72511ac6abdSChristopher Smith
726548d801fSChristopher Smith        $Renderer = new $rclass;
727548d801fSChristopher Smith        $Renderer->nocache();     // fallback only (and may include admin alerts), don't cache
728d968d3e5SChris Smith        return $Renderer;
729d968d3e5SChris Smith    }
730d968d3e5SChris Smith
731548d801fSChristopher Smith    // fallback failed, alert the world
732548d801fSChristopher Smith    msg("No renderer '$rname' found for mode '$mode'",-1);
733548d801fSChristopher Smith    return null;
734548d801fSChristopher Smith}
735548d801fSChristopher Smith
736bb0a59d4Sjan/**
737bb0a59d4Sjan * Gets the first heading from a file
738bb0a59d4Sjan *
739fc18c0fbSchris * @param   string   $id       dokuwiki page id
74067c15eceSMichael Hamann * @param   int      $render   rerender if first heading not known
74167c15eceSMichael Hamann *                             default: METADATA_RENDER_USING_SIMPLE_CACHE
74267c15eceSMichael Hamann *                             Possible values: METADATA_DONT_RENDER,
74367c15eceSMichael Hamann *                                              METADATA_RENDER_USING_SIMPLE_CACHE,
74465aa8490SMichael Hamann *                                              METADATA_RENDER_USING_CACHE,
74565aa8490SMichael Hamann *                                              METADATA_RENDER_UNLIMITED
746e3ab6fc5SMichael Hamann * @return string|null The first heading
74742ea7f44SGerrit Uitslag *
74895dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
749bf0c93c2SMichael Hamann * @author Michael Hamann <michael@content-space.de>
750bb0a59d4Sjan */
75167c15eceSMichael Hamannfunction p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){
75265aa8490SMichael Hamann    return p_get_metadata(cleanID($id),'title',$render);
753bb0a59d4Sjan}
754bb0a59d4Sjan
7558f7d700cSchris/**
7568f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output
7578f7d700cSchris *
7585d568b99SChris Smith * @param  string   $code       source code to be highlighted
7595d568b99SChris Smith * @param  string   $language   language to provide highlighting
7605d568b99SChris Smith * @param  string   $wrapper    html element to wrap the returned highlighted text
761e3ab6fc5SMichael Hamann * @return string xhtml code
76242ea7f44SGerrit Uitslag *
7638f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk>
76435fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7658f7d700cSchris */
766e2d88156SLarsDW223function p_xhtml_cached_geshi($code, $language, $wrapper='pre', array $options=null) {
767ff1769deSAndreas Gohr    global $conf, $config_cascade, $INPUT;
76835fbe9efSAndreas Gohr    $language = strtolower($language);
7695d568b99SChris Smith
7705d568b99SChris Smith    // remove any leading or trailing blank lines
7715d568b99SChris Smith    $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
7725d568b99SChris Smith
773e2d88156SLarsDW223    $optionsmd5 = md5(serialize($options));
774e2d88156SLarsDW223    $cache = getCacheName($language.$code.$optionsmd5,".code");
77535fbe9efSAndreas Gohr    $ctime = @filemtime($cache);
776ff1769deSAndreas Gohr    if($ctime && !$INPUT->bool('purge') &&
77741d51802SAndreas Gohr            $ctime > filemtime(DOKU_INC.'vendor/composer/installed.json') &&  // libraries changed
778f8121585SChris Smith            $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
7798f7d700cSchris        $highlighted_code = io_readFile($cache, false);
7808f7d700cSchris    } else {
7818f7d700cSchris
78241d51802SAndreas Gohr        $geshi = new GeSHi($code, $language);
7838f7d700cSchris        $geshi->set_encoding('utf-8');
7848f7d700cSchris        $geshi->enable_classes();
7858f7d700cSchris        $geshi->set_header_type(GESHI_HEADER_PRE);
7868f7d700cSchris        $geshi->set_link_target($conf['target']['extern']);
787e2d88156SLarsDW223        if($options !== null) {
788e2d88156SLarsDW223            foreach ($options as $function => $params) {
789e2d88156SLarsDW223                if(is_callable(array($geshi, $function))) {
790e2d88156SLarsDW223                    $geshi->$function($params);
791e2d88156SLarsDW223                }
792e2d88156SLarsDW223            }
793e2d88156SLarsDW223        }
7948f7d700cSchris
7955d568b99SChris Smith        // remove GeSHi's wrapper element (we'll replace it with our own later)
7965d568b99SChris Smith        // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
79769ddc332SAnika Henke        $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r");
7988f7d700cSchris        io_saveFile($cache,$highlighted_code);
7998f7d700cSchris    }
8008f7d700cSchris
8015d568b99SChris Smith    // add a wrapper element if required
8025d568b99SChris Smith    if ($wrapper) {
8035d568b99SChris Smith        return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
8045d568b99SChris Smith    } else {
8058f7d700cSchris        return $highlighted_code;
8068f7d700cSchris    }
8075d568b99SChris Smith}
8088f7d700cSchris
809