xref: /dokuwiki/inc/parserutils.php (revision 3b7488710df8a72be396293874215445851b66b9)
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.');
11c112d578Sandirequire_once(DOKU_INC.'inc/confutils.php');
12c112d578Sandirequire_once(DOKU_INC.'inc/pageutils.php');
13ee20e7d1Sandirequire_once(DOKU_INC.'inc/pluginutils.php');
144b5f4f4eSchrisrequire_once(DOKU_INC.'inc/cache.php');
15c112d578Sandi
16c112d578Sandi/**
17c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision.
18c112d578Sandi *
19c112d578Sandi * If $excuse is true an explanation is returned if the file
20c112d578Sandi * wasn't found
21c112d578Sandi *
22c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
23c112d578Sandi */
24c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){
25c112d578Sandi    $file = wikiFN($id,$rev);
26c112d578Sandi    $ret  = '';
27c112d578Sandi
28c112d578Sandi    //ensure $id is in global $ID (needed for parsing)
291e76272cSandi    global $ID;
303ff8773bSAndreas Gohr    $keep = $ID;
311e76272cSandi    $ID   = $id;
32c112d578Sandi
33c112d578Sandi    if($rev){
34c112d578Sandi        if(@file_exists($file)){
35bde4e341SGalaxyMaster            $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info); //no caching on old revisions
36c112d578Sandi        }elseif($excuse){
37c112d578Sandi            $ret = p_locale_xhtml('norev');
38c112d578Sandi        }
39c112d578Sandi    }else{
40c112d578Sandi        if(@file_exists($file)){
414b5f4f4eSchris            $ret = p_cached_output($file,'xhtml',$id);
42c112d578Sandi        }elseif($excuse){
43c112d578Sandi            $ret = p_locale_xhtml('newpage');
44c112d578Sandi        }
45c112d578Sandi    }
46c112d578Sandi
473ff8773bSAndreas Gohr    //restore ID (just in case)
483ff8773bSAndreas Gohr    $ID = $keep;
493ff8773bSAndreas Gohr
50c112d578Sandi    return $ret;
51c112d578Sandi}
52c112d578Sandi
53c112d578Sandi/**
546b7b33dcShfuecks * Returns starting summary for a page (e.g. the first few
556b7b33dcShfuecks * paragraphs), marked up in XHTML.
566b7b33dcShfuecks *
576b7b33dcShfuecks * If $excuse is true an explanation is returned if the file
586b7b33dcShfuecks * wasn't found
596b7b33dcShfuecks *
606b7b33dcShfuecks * @param string wiki page id
616b7b33dcShfuecks * @param reference populated with page title from heading or page id
628716966dSAndreas Gohr * @deprecated
638716966dSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com>
646b7b33dcShfuecks */
656b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
666b7b33dcShfuecks    $file = wikiFN($id,$rev);
676b7b33dcShfuecks    $ret  = '';
686b7b33dcShfuecks
696b7b33dcShfuecks    //ensure $id is in global $ID (needed for parsing)
706b7b33dcShfuecks    global $ID;
716b7b33dcShfuecks    $keep = $ID;
726b7b33dcShfuecks    $ID   = $id;
736b7b33dcShfuecks
746b7b33dcShfuecks    if($rev){
756b7b33dcShfuecks        if(@file_exists($file)){
766b7b33dcShfuecks            //no caching on old revisions
77bde4e341SGalaxyMaster            $ins = p_get_instructions(io_readWikiPage($file,$id,$rev));
786b7b33dcShfuecks        }elseif($excuse){
796b7b33dcShfuecks            $ret = p_locale_xhtml('norev');
806b7b33dcShfuecks            //restore ID (just in case)
816b7b33dcShfuecks            $ID = $keep;
826b7b33dcShfuecks            return $ret;
836b7b33dcShfuecks        }
846b7b33dcShfuecks
856b7b33dcShfuecks    }else{
866b7b33dcShfuecks
876b7b33dcShfuecks        if(@file_exists($file)){
886b7b33dcShfuecks            // The XHTML for a summary is not cached so use the instruction cache
896b7b33dcShfuecks            $ins = p_cached_instructions($file);
906b7b33dcShfuecks        }elseif($excuse){
916b7b33dcShfuecks            $ret = p_locale_xhtml('newpage');
926b7b33dcShfuecks            //restore ID (just in case)
936b7b33dcShfuecks            $ID = $keep;
946b7b33dcShfuecks            return $ret;
956b7b33dcShfuecks        }
966b7b33dcShfuecks    }
976b7b33dcShfuecks
986b7b33dcShfuecks    $ret = p_render('xhtmlsummary',$ins,$info);
996b7b33dcShfuecks
1006b7b33dcShfuecks    if ( $info['sum_pagetitle'] ) {
1016b7b33dcShfuecks        $title = $info['sum_pagetitle'];
1026b7b33dcShfuecks    } else {
1036b7b33dcShfuecks        $title = $id;
1046b7b33dcShfuecks    }
1056b7b33dcShfuecks
1066b7b33dcShfuecks    $ID = $keep;
1076b7b33dcShfuecks    return $ret;
1086b7b33dcShfuecks}
1096b7b33dcShfuecks
1106b7b33dcShfuecks/**
111c112d578Sandi * Returns the specified local text in parsed format
112c112d578Sandi *
113c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
114c112d578Sandi */
115c112d578Sandifunction p_locale_xhtml($id){
116c112d578Sandi    //fetch parsed locale
1174b5f4f4eSchris    $html = p_cached_output(localeFN($id));
118c112d578Sandi    return $html;
119c112d578Sandi}
120c112d578Sandi
121c112d578Sandi/**
1224b5f4f4eSchris *     *** DEPRECATED ***
1234b5f4f4eSchris *
1244b5f4f4eSchris * use p_cached_output()
1254b5f4f4eSchris *
126c112d578Sandi * Returns the given file parsed to XHTML
127c112d578Sandi *
128c112d578Sandi * Uses and creates a cachefile
129c112d578Sandi *
1304b5f4f4eSchris * @deprecated
131c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
1329dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
133c112d578Sandi */
134c112d578Sandifunction p_cached_xhtml($file){
1354b5f4f4eSchris    return p_cached_output($file);
1364b5f4f4eSchris}
1374b5f4f4eSchris
1384b5f4f4eSchris/**
1394b5f4f4eSchris * Returns the given file parsed into the requested output format
1404b5f4f4eSchris *
1414b5f4f4eSchris * @author Andreas Gohr <andi@splitbrain.org>
1424b5f4f4eSchris * @author Chris Smith <chris@jalakai.co.uk>
1434b5f4f4eSchris */
1444b5f4f4eSchrisfunction p_cached_output($file, $format='xhtml', $id='') {
145c112d578Sandi    global $conf;
146c112d578Sandi
1474b5f4f4eSchris    $cache = new cache_renderer($id, $file, $format);
1484b5f4f4eSchris    if ($cache->useCache()) {
14985767031SAndreas Gohr        $parsed = $cache->retrieveCache(false);
150ea2a4271SAndreas Gohr        if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n";
151c112d578Sandi    } else {
1524b5f4f4eSchris        $parsed = p_render($format, p_cached_instructions($file,false,$id), $info);
153c112d578Sandi
1549dc2c2afSandi        if ($info['cache']) {
1554b5f4f4eSchris            $cache->storeCache($parsed);               //save cachefile
156ea2a4271SAndreas Gohr            if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n";
157c112d578Sandi        }else{
1584b5f4f4eSchris            $cache->removeCache();                     //try to delete cachefile
159ea2a4271SAndreas Gohr            if($conf['allowdebug'] && $format=='xhtml') $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
160c112d578Sandi        }
161c112d578Sandi    }
162c112d578Sandi
163c112d578Sandi    return $parsed;
164c112d578Sandi}
165c112d578Sandi
166c112d578Sandi/**
167c112d578Sandi * Returns the render instructions for a file
168c112d578Sandi *
169c112d578Sandi * Uses and creates a serialized cache file
170c112d578Sandi *
171c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
172c112d578Sandi */
1734b5f4f4eSchrisfunction p_cached_instructions($file,$cacheonly=false,$id='') {
174c112d578Sandi    global $conf;
17547b2d319SAndreas Gohr    static $run = null;
17647b2d319SAndreas Gohr    if(is_null($run)) $run = array();
177c112d578Sandi
1784b5f4f4eSchris    $cache = new cache_instructions($id, $file);
179c112d578Sandi
18047b2d319SAndreas Gohr    if ($cacheonly || $cache->useCache() || isset($run[$file])) {
1814b5f4f4eSchris        return $cache->retrieveCache();
182c112d578Sandi    } else if (@file_exists($file)) {
183c112d578Sandi        // no cache - do some work
184bde4e341SGalaxyMaster        $ins = p_get_instructions(io_readWikiPage($file,$id));
185cbaf4259SChris Smith        if ($cache->storeCache($ins)) {
18647b2d319SAndreas Gohr            $run[$file] = true; // we won't rebuild these instructions in the same run again
187cbaf4259SChris Smith        } else {
188cbaf4259SChris Smith            msg('Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.',-1);
189cbaf4259SChris Smith        }
190c112d578Sandi        return $ins;
191c112d578Sandi    }
192c112d578Sandi
1933b3f8916SAndreas Gohr    return null;
194c112d578Sandi}
195c112d578Sandi
196c112d578Sandi/**
197c112d578Sandi * turns a page into a list of instructions
198c112d578Sandi *
199c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
200c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
201c112d578Sandi */
2026bbae538Sandifunction p_get_instructions($text){
203c112d578Sandi
204107b01d6Sandi    $modes = p_get_parsermodes();
205ee20e7d1Sandi
206c112d578Sandi    // Create the parser
20767f9913dSAndreas Gohr    $Parser = new Doku_Parser();
208c112d578Sandi
209c112d578Sandi    // Add the Handler
21067f9913dSAndreas Gohr    $Parser->Handler = new Doku_Handler();
211c112d578Sandi
212107b01d6Sandi    //add modes to parser
213107b01d6Sandi    foreach($modes as $mode){
214107b01d6Sandi        $Parser->addMode($mode['mode'],$mode['obj']);
215c112d578Sandi    }
216c112d578Sandi
217c112d578Sandi    // Do the parsing
218677844afSchris    trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
219a2d649c4Sandi    $p = $Parser->parse($text);
220ee20e7d1Sandi    //  dbg($p);
221a2d649c4Sandi    return $p;
222c112d578Sandi}
223c112d578Sandi
224c112d578Sandi/**
22539a89382SEsther Brunner * returns the metadata of a page
22639a89382SEsther Brunner *
22739a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
22839a89382SEsther Brunner */
22939a89382SEsther Brunnerfunction p_get_metadata($id, $key=false, $render=false){
2300a7e3bceSchris    global $ID, $INFO, $cache_metadata;
2316afe8dcaSchris
2320a7e3bceSchris    // cache the current page
2330a7e3bceSchris    // Benchmarking shows the current page's metadata is generally the only page metadata
2340a7e3bceSchris    // accessed several times. This may catch a few other pages, but that shouldn't be an issue.
2350a7e3bceSchris    $cache = ($ID == $id);
2360a7e3bceSchris    $meta = p_read_metadata($id, $cache);
23739a89382SEsther Brunner
23866d29756SChris Smith    // metadata has never been rendered before - do it! (but not for non-existent pages)
239831be45dSAndreas Gohr    if ($render && !isset($meta['current']['description']['abstract']) && page_exists($id)){
24039a89382SEsther Brunner        $meta = p_render_metadata($id, $meta);
241fc18c0fbSchris        io_saveFile(metaFN($id, '.meta'), serialize($meta));
2420a7e3bceSchris
2430a7e3bceSchris        // sync cached copies, including $INFO metadata
2440a7e3bceSchris        if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
2450a7e3bceSchris        if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
2466afe8dcaSchris    }
24739a89382SEsther Brunner
24839a89382SEsther Brunner    // filter by $key
24939a89382SEsther Brunner    if ($key){
25039a89382SEsther Brunner        list($key, $subkey) = explode(' ', $key, 2);
25166d29756SChris Smith        $subkey = trim($subkey);
2520a7e3bceSchris
25366d29756SChris Smith        if ($subkey) {
25466d29756SChris Smith            return isset($meta['current'][$key][$subkey]) ? $meta['current'][$key][$subkey] : null;
25566d29756SChris Smith        }
25666d29756SChris Smith
25766d29756SChris Smith        return isset($meta['current'][$key]) ? $meta['current'][$key] : null;
25839a89382SEsther Brunner    }
25939a89382SEsther Brunner
2600a7e3bceSchris    return $meta['current'];
26139a89382SEsther Brunner}
26239a89382SEsther Brunner
26339a89382SEsther Brunner/**
26439a89382SEsther Brunner * sets metadata elements of a page
26539a89382SEsther Brunner *
26639a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
26739a89382SEsther Brunner */
2680a7e3bceSchrisfunction p_set_metadata($id, $data, $render=false, $persistent=true){
26939a89382SEsther Brunner    if (!is_array($data)) return false;
27039a89382SEsther Brunner
2710a7e3bceSchris    global $ID;
2720a7e3bceSchris
2730a7e3bceSchris    // cache the current page
2740a7e3bceSchris    $cache = ($ID == $id);
2750a7e3bceSchris    $orig = p_read_metadata($id, $cache);
27639a89382SEsther Brunner
27739a89382SEsther Brunner    // render metadata first?
2780a7e3bceSchris    $meta = $render ? p_render_metadata($id, $orig) : $orig;
27939a89382SEsther Brunner
28039a89382SEsther Brunner    // now add the passed metadata
28139a89382SEsther Brunner    $protected = array('description', 'date', 'contributor');
28239a89382SEsther Brunner    foreach ($data as $key => $value){
28339a89382SEsther Brunner
28439a89382SEsther Brunner        // be careful with sub-arrays of $meta['relation']
28539a89382SEsther Brunner        if ($key == 'relation'){
2860a7e3bceSchris
28739a89382SEsther Brunner            foreach ($value as $subkey => $subvalue){
28866d29756SChris Smith                $meta['current'][$key][$subkey] = !empty($meta['current'][$key][$subkey]) ? array_merge($meta['current'][$key][$subkey], $subvalue) : $subvalue;
2890a7e3bceSchris                if ($persistent)
29066d29756SChris Smith                    $meta['persistent'][$key][$subkey] = !empty($meta['persistent'][$key][$subkey]) ? array_merge($meta['persistent'][$key][$subkey], $subvalue) : $subvalue;
29139a89382SEsther Brunner            }
29239a89382SEsther Brunner
29339a89382SEsther Brunner            // be careful with some senisitive arrays of $meta
29439a89382SEsther Brunner        } elseif (in_array($key, $protected)){
2950a7e3bceSchris
29666d29756SChris Smith            // these keys, must have subkeys - a legitimate value must be an array
29739a89382SEsther Brunner            if (is_array($value)) {
29866d29756SChris Smith                $meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge($meta['current'][$key],$value) : $value;
2990a7e3bceSchris
3000a7e3bceSchris                if ($persistent) {
30166d29756SChris Smith                    $meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge($meta['persistent'][$key],$value) : $value;
3020a7e3bceSchris                }
30339a89382SEsther Brunner            }
30439a89382SEsther Brunner
30539a89382SEsther Brunner            // no special treatment for the rest
30639a89382SEsther Brunner        } else {
3070a7e3bceSchris            $meta['current'][$key] = $value;
3080a7e3bceSchris            if ($persistent) $meta['persistent'][$key] = $value;
30939a89382SEsther Brunner        }
31039a89382SEsther Brunner    }
31139a89382SEsther Brunner
31239a89382SEsther Brunner    // save only if metadata changed
31339a89382SEsther Brunner    if ($meta == $orig) return true;
3146afe8dcaSchris
3150a7e3bceSchris    // sync cached copies, including $INFO metadata
3160a7e3bceSchris    global $cache_metadata, $INFO;
3170a7e3bceSchris
3180a7e3bceSchris    if (!empty($cache_metadata[$id])) $cache_metadata[$id] = $meta;
3190a7e3bceSchris    if (!empty($INFO) && ($id == $INFO['id'])) { $INFO['meta'] = $meta['current']; }
3206afe8dcaSchris
32139a89382SEsther Brunner    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
32239a89382SEsther Brunner}
32339a89382SEsther Brunner
32439a89382SEsther Brunner/**
3253d1f9ec3SMichael Klier * Purges the non-persistant part of the meta data
3263d1f9ec3SMichael Klier * used on page deletion
3273d1f9ec3SMichael Klier *
3283d1f9ec3SMichael Klier * @author Michael Klier <chi@chimeric.de>
3293d1f9ec3SMichael Klier */
3303d1f9ec3SMichael Klierfunction p_purge_metadata($id) {
3313d1f9ec3SMichael Klier    $metafn = metaFN('id', '.meta');
3323d1f9ec3SMichael Klier    $meta   = p_read_metadata($id);
3333d1f9ec3SMichael Klier    foreach($meta['current'] as $key => $value) {
3343d1f9ec3SMichael Klier        if(is_array($meta[$key])) {
3353d1f9ec3SMichael Klier            $meta['current'][$key] = array();
3363d1f9ec3SMichael Klier        } else {
3373d1f9ec3SMichael Klier            $meta['current'][$key] = '';
3383d1f9ec3SMichael Klier        }
3393d1f9ec3SMichael Klier    }
3403d1f9ec3SMichael Klier    return io_saveFile(metaFN($id, '.meta'), serialize($meta));
3413d1f9ec3SMichael Klier}
3423d1f9ec3SMichael Klier
3433d1f9ec3SMichael Klier/**
3440a7e3bceSchris * read the metadata from source/cache for $id
3450a7e3bceSchris * (internal use only - called by p_get_metadata & p_set_metadata)
3460a7e3bceSchris *
3470a7e3bceSchris * this function also converts the metadata from the original format to
3480a7e3bceSchris * the current format ('current' & 'persistent' arrays)
3490a7e3bceSchris *
3500a7e3bceSchris * @author   Christopher Smith <chris@jalakai.co.uk>
3510a7e3bceSchris *
3520a7e3bceSchris * @param    string   $id      absolute wiki page id
3530a7e3bceSchris * @param    bool     $cache   whether or not to cache metadata in memory
3540a7e3bceSchris *                             (only use for metadata likely to be accessed several times)
3550a7e3bceSchris *
3560a7e3bceSchris * @return   array             metadata
3570a7e3bceSchris */
3580a7e3bceSchrisfunction p_read_metadata($id,$cache=false) {
3590a7e3bceSchris    global $cache_metadata;
3600a7e3bceSchris
3613a50618cSgweissbach    if (isset($cache_metadata[(string)$id])) return $cache_metadata[(string)$id];
3620a7e3bceSchris
3630a7e3bceSchris    $file = metaFN($id, '.meta');
3640a7e3bceSchris    $meta = @file_exists($file) ? unserialize(io_readFile($file, false)) : array('current'=>array(),'persistent'=>array());
3650a7e3bceSchris
3660a7e3bceSchris    // convert $meta from old format to new (current+persistent) format
3670a7e3bceSchris    if (!isset($meta['current'])) {
3680a7e3bceSchris        $meta = array('current'=>$meta,'persistent'=>$meta);
3690a7e3bceSchris
3700a7e3bceSchris        // remove non-persistent keys
3710a7e3bceSchris        unset($meta['persistent']['title']);
3720a7e3bceSchris        unset($meta['persistent']['description']['abstract']);
3730a7e3bceSchris        unset($meta['persistent']['description']['tableofcontents']);
3740a7e3bceSchris        unset($meta['persistent']['relation']['haspart']);
3750a7e3bceSchris        unset($meta['persistent']['relation']['references']);
3760a7e3bceSchris        unset($meta['persistent']['date']['valid']);
3770a7e3bceSchris
3780a7e3bceSchris        if (empty($meta['persistent']['description'])) unset($meta['persistent']['description']);
3790a7e3bceSchris        if (empty($meta['persistent']['relation'])) unset($meta['persistent']['relation']);
3800a7e3bceSchris        if (empty($meta['persistent']['date'])) unset($meta['persistent']['date']);
3810a7e3bceSchris
3820a7e3bceSchris        // save converted metadata
3830a7e3bceSchris        io_saveFile($file, serialize($meta));
3840a7e3bceSchris    }
3850a7e3bceSchris
3860a7e3bceSchris    if ($cache) {
3873a50618cSgweissbach        $cache_metadata[(string)$id] = $meta;
3880a7e3bceSchris    }
3890a7e3bceSchris
3900a7e3bceSchris    return $meta;
3910a7e3bceSchris}
3920a7e3bceSchris
3930a7e3bceSchris/**
39439a89382SEsther Brunner * renders the metadata of a page
39539a89382SEsther Brunner *
39639a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
39739a89382SEsther Brunner */
39839a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
39948924015SAndreas Gohr    // make sure the correct ID is in global ID
40048924015SAndreas Gohr    global $ID;
40148924015SAndreas Gohr    $keep = $ID;
40248924015SAndreas Gohr    $ID   = $id;
40348924015SAndreas Gohr
4040a7e3bceSchris    // add an extra key for the event - to tell event handlers the page whose metadata this is
4050a7e3bceSchris    $orig['page'] = $id;
4060a7e3bceSchris    $evt = new Doku_Event('PARSER_METADATA_RENDER', $orig);
4070a7e3bceSchris    if ($evt->advise_before()) {
4080a7e3bceSchris
40939a89382SEsther Brunner        require_once DOKU_INC."inc/parser/metadata.php";
41039a89382SEsther Brunner
41139a89382SEsther Brunner        // get instructions
4124b5f4f4eSchris        $instructions = p_cached_instructions(wikiFN($id),false,$id);
41348924015SAndreas Gohr        if(is_null($instructions)){
41448924015SAndreas Gohr            $ID = $keep;
41548924015SAndreas Gohr            return null; // something went wrong with the instructions
41648924015SAndreas Gohr        }
41739a89382SEsther Brunner
41839a89382SEsther Brunner        // set up the renderer
41967f9913dSAndreas Gohr        $renderer = new Doku_Renderer_metadata();
4200a7e3bceSchris        $renderer->meta = $orig['current'];
4210a7e3bceSchris        $renderer->persistent = $orig['persistent'];
42239a89382SEsther Brunner
42339a89382SEsther Brunner        // loop through the instructions
42439a89382SEsther Brunner        foreach ($instructions as $instruction){
42539a89382SEsther Brunner            // execute the callback against the renderer
426*3b748871SAndreas Gohr            call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]);
42739a89382SEsther Brunner        }
42839a89382SEsther Brunner
4290a7e3bceSchris        $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent);
4300a7e3bceSchris    }
4310a7e3bceSchris    $evt->advise_after();
4320a7e3bceSchris
43348924015SAndreas Gohr    $ID = $keep;
4340a7e3bceSchris    return $evt->result;
43539a89382SEsther Brunner}
43639a89382SEsther Brunner
43739a89382SEsther Brunner/**
438107b01d6Sandi * returns all available parser syntax modes in correct order
439107b01d6Sandi *
440107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
441107b01d6Sandi */
442107b01d6Sandifunction p_get_parsermodes(){
443107b01d6Sandi    global $conf;
444107b01d6Sandi
445107b01d6Sandi    //reuse old data
446107b01d6Sandi    static $modes = null;
447107b01d6Sandi    if($modes != null){
448107b01d6Sandi        return $modes;
449107b01d6Sandi    }
450107b01d6Sandi
451107b01d6Sandi    //import parser classes and mode definitions
452107b01d6Sandi    require_once DOKU_INC . 'inc/parser/parser.php';
453107b01d6Sandi
454107b01d6Sandi    // we now collect all syntax modes and their objects, then they will
455107b01d6Sandi    // be sorted and added to the parser in correct order
456107b01d6Sandi    $modes = array();
457107b01d6Sandi
458107b01d6Sandi    // add syntax plugins
459107b01d6Sandi    $pluginlist = plugin_list('syntax');
460107b01d6Sandi    if(count($pluginlist)){
461107b01d6Sandi        global $PARSER_MODES;
462107b01d6Sandi        $obj = null;
463107b01d6Sandi        foreach($pluginlist as $p){
464c90b2fb1Schris            if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
465107b01d6Sandi            $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
466107b01d6Sandi            //add to modes
467107b01d6Sandi            $modes[] = array(
468107b01d6Sandi                    'sort' => $obj->getSort(),
469107b01d6Sandi                    'mode' => "plugin_$p",
470107b01d6Sandi                    'obj'  => $obj,
471107b01d6Sandi                    );
472a46d0d65SAndreas Gohr            unset($obj); //remove the reference
473107b01d6Sandi        }
474107b01d6Sandi    }
475107b01d6Sandi
476107b01d6Sandi    // add default modes
477107b01d6Sandi    $std_modes = array('listblock','preformatted','notoc','nocache',
478107b01d6Sandi            'header','table','linebreak','footnote','hr',
479107b01d6Sandi            'unformatted','php','html','code','file','quote',
480e77ea1bcSAndreas Gohr            'internallink','rss','media','externallink',
481e77ea1bcSAndreas Gohr            'emaillink','windowssharelink','eol');
482e77ea1bcSAndreas Gohr    if($conf['typography']){
483e77ea1bcSAndreas Gohr        $std_modes[] = 'quotes';
484e77ea1bcSAndreas Gohr        $std_modes[] = 'multiplyentity';
485e77ea1bcSAndreas Gohr    }
486107b01d6Sandi    foreach($std_modes as $m){
487107b01d6Sandi        $class = "Doku_Parser_Mode_$m";
488107b01d6Sandi        $obj   = new $class();
489107b01d6Sandi        $modes[] = array(
490107b01d6Sandi                'sort' => $obj->getSort(),
491107b01d6Sandi                'mode' => $m,
492107b01d6Sandi                'obj'  => $obj
493107b01d6Sandi                );
494107b01d6Sandi    }
495107b01d6Sandi
496107b01d6Sandi    // add formatting modes
497107b01d6Sandi    $fmt_modes = array('strong','emphasis','underline','monospace',
498107b01d6Sandi            'subscript','superscript','deleted');
499107b01d6Sandi    foreach($fmt_modes as $m){
500107b01d6Sandi        $obj   = new Doku_Parser_Mode_formatting($m);
501107b01d6Sandi        $modes[] = array(
502107b01d6Sandi                'sort' => $obj->getSort(),
503107b01d6Sandi                'mode' => $m,
504107b01d6Sandi                'obj'  => $obj
505107b01d6Sandi                );
506107b01d6Sandi    }
507107b01d6Sandi
508107b01d6Sandi    // add modes which need files
509107b01d6Sandi    $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
510107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
511107b01d6Sandi    $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
512107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
513107b01d6Sandi    $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
514107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
515107b01d6Sandi
516107b01d6Sandi    // add optional camelcase mode
517107b01d6Sandi    if($conf['camelcase']){
518107b01d6Sandi        $obj     = new Doku_Parser_Mode_camelcaselink();
519107b01d6Sandi        $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
520107b01d6Sandi    }
521107b01d6Sandi
522107b01d6Sandi    //sort modes
523107b01d6Sandi    usort($modes,'p_sort_modes');
524107b01d6Sandi
525107b01d6Sandi    return $modes;
526107b01d6Sandi}
527107b01d6Sandi
528107b01d6Sandi/**
529107b01d6Sandi * Callback function for usort
530107b01d6Sandi *
531107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
532107b01d6Sandi */
533107b01d6Sandifunction p_sort_modes($a, $b){
534107b01d6Sandi    if($a['sort'] == $b['sort']) return 0;
535107b01d6Sandi    return ($a['sort'] < $b['sort']) ? -1 : 1;
536107b01d6Sandi}
537107b01d6Sandi
538107b01d6Sandi/**
539ac83b9d8Sandi * Renders a list of instruction to the specified output mode
540c112d578Sandi *
5419dc2c2afSandi * In the $info array are informations from the renderer returned
5429dc2c2afSandi *
543c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
544c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
545c112d578Sandi */
5469dc2c2afSandifunction p_render($mode,$instructions,&$info){
547c112d578Sandi    if(is_null($instructions)) return '';
548c112d578Sandi
549d968d3e5SChris Smith    $Renderer =& p_get_renderer($mode);
550d968d3e5SChris Smith    if (is_null($Renderer)) return null;
551c327d6c4SAndreas Gohr
552d968d3e5SChris Smith    $Renderer->reset();
553c112d578Sandi
554c112d578Sandi    $Renderer->smileys = getSmileys();
555c112d578Sandi    $Renderer->entities = getEntities();
556c112d578Sandi    $Renderer->acronyms = getAcronyms();
557c112d578Sandi    $Renderer->interwiki = getInterwiki();
558c112d578Sandi
559c112d578Sandi    // Loop through the instructions
560c112d578Sandi    foreach ( $instructions as $instruction ) {
561c112d578Sandi        // Execute the callback against the Renderer
562c112d578Sandi        call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
563c112d578Sandi    }
5649dc2c2afSandi
5659dc2c2afSandi    //set info array
5669dc2c2afSandi    $info = $Renderer->info;
5679dc2c2afSandi
568677844afSchris    // Post process and return the output
569677844afSchris    $data = array($mode,& $Renderer->doc);
570677844afSchris    trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
571c112d578Sandi    return $Renderer->doc;
572c112d578Sandi}
573c112d578Sandi
574d968d3e5SChris Smithfunction & p_get_renderer($mode) {
5757aea91afSChris Smith    global $conf, $plugin_controller;
576d968d3e5SChris Smith
577d968d3e5SChris Smith    $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;
578d968d3e5SChris Smith
579d968d3e5SChris Smith    // try default renderer first:
580d968d3e5SChris Smith    $file = DOKU_INC."inc/parser/$rname.php";
581d968d3e5SChris Smith    if(@file_exists($file)){
582d968d3e5SChris Smith        require_once $file;
583d968d3e5SChris Smith        $rclass = "Doku_Renderer_$rname";
584d968d3e5SChris Smith
585d968d3e5SChris Smith        if ( !class_exists($rclass) ) {
586d968d3e5SChris Smith            trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
587d968d3e5SChris Smith            msg("Renderer '$rname' for $mode not valid",-1);
588d968d3e5SChris Smith            return null;
589d968d3e5SChris Smith        }
59067f9913dSAndreas Gohr        $Renderer = new $rclass();
591d968d3e5SChris Smith    }else{
5927b27382cSGina Haeussge        // Maybe a plugin/component is available?
5937b27382cSGina Haeussge        list($plugin, $component) = $plugin_controller->_splitName($rname);
5947b27382cSGina Haeussge        if (!$plugin_controller->isdisabled($plugin)){
5957aea91afSChris Smith            $Renderer =& $plugin_controller->load('renderer',$rname, true);
5967aea91afSChris Smith        }
5977aea91afSChris Smith
598d968d3e5SChris Smith        if(is_null($Renderer)){
599d968d3e5SChris Smith            msg("No renderer '$rname' found for mode '$mode'",-1);
600d968d3e5SChris Smith            return null;
601d968d3e5SChris Smith        }
602d968d3e5SChris Smith    }
603d968d3e5SChris Smith
604d968d3e5SChris Smith    return $Renderer;
605d968d3e5SChris Smith}
606d968d3e5SChris Smith
607bb0a59d4Sjan/**
608bb0a59d4Sjan * Gets the first heading from a file
609bb0a59d4Sjan *
610fc18c0fbSchris * @param   string   $id       dokuwiki page id
611fc18c0fbSchris * @param   bool     $render   rerender if first heading not known
6120770c0e5SChris Smith *                             default: true  -- must be set to false for calls from the metadata renderer to
6130770c0e5SChris Smith *                                               protects against loops and excessive resource usage when pages
6140770c0e5SChris Smith *                                               for which only a first heading is required will attempt to
6150770c0e5SChris Smith *                                               render metadata for all the pages for which they require first
6160770c0e5SChris Smith *                                               headings ... and so on.
617fc18c0fbSchris *
61895dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
619bb0a59d4Sjan */
6200770c0e5SChris Smithfunction p_get_first_heading($id, $render=true){
621fe9ec250SChris Smith    return p_get_metadata($id,'title',$render);
622bb0a59d4Sjan}
623bb0a59d4Sjan
6248f7d700cSchris/**
6258f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output
6268f7d700cSchris *
6275d568b99SChris Smith * @param  string   $code       source code to be highlighted
6285d568b99SChris Smith * @param  string   $language   language to provide highlighting
6295d568b99SChris Smith * @param  string   $wrapper    html element to wrap the returned highlighted text
6305d568b99SChris Smith *
6318f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk>
63235fbe9efSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6338f7d700cSchris */
6345d568b99SChris Smithfunction p_xhtml_cached_geshi($code, $language, $wrapper='pre') {
635f8121585SChris Smith    global $conf, $config_cascade;
63635fbe9efSAndreas Gohr    $language = strtolower($language);
6375d568b99SChris Smith
6385d568b99SChris Smith    // remove any leading or trailing blank lines
6395d568b99SChris Smith    $code = preg_replace('/^\s*?\n|\s*?\n$/','',$code);
6405d568b99SChris Smith
6418f7d700cSchris    $cache = getCacheName($language.$code,".code");
64235fbe9efSAndreas Gohr    $ctime = @filemtime($cache);
64335fbe9efSAndreas Gohr    if($ctime && !$_REQUEST['purge'] &&
644f8121585SChris Smith            $ctime > filemtime(DOKU_INC.'inc/geshi.php') &&                 // geshi changed
645f8121585SChris Smith            $ctime > @filemtime(DOKU_INC.'inc/geshi/'.$language.'.php') &&  // language syntax definition changed
646f8121585SChris Smith            $ctime > filemtime(reset($config_cascade['main']['default']))){ // dokuwiki changed
6478f7d700cSchris        $highlighted_code = io_readFile($cache, false);
6488f7d700cSchris
6498f7d700cSchris    } else {
6508f7d700cSchris
6518f7d700cSchris        require_once(DOKU_INC . 'inc/geshi.php');
6528f7d700cSchris
65335fbe9efSAndreas Gohr        $geshi = new GeSHi($code, $language, DOKU_INC . 'inc/geshi');
6548f7d700cSchris        $geshi->set_encoding('utf-8');
6558f7d700cSchris        $geshi->enable_classes();
6568f7d700cSchris        $geshi->set_header_type(GESHI_HEADER_PRE);
6578f7d700cSchris        $geshi->set_link_target($conf['target']['extern']);
6588f7d700cSchris
6595d568b99SChris Smith        // remove GeSHi's wrapper element (we'll replace it with our own later)
6605d568b99SChris Smith        // we need to use a GeSHi wrapper to avoid <BR> throughout the highlighted text
66169ddc332SAnika Henke        $highlighted_code = trim(preg_replace('!^<pre[^>]*>|</pre>$!','',$geshi->parse_code()),"\n\r");
6628f7d700cSchris        io_saveFile($cache,$highlighted_code);
6638f7d700cSchris    }
6648f7d700cSchris
6655d568b99SChris Smith    // add a wrapper element if required
6665d568b99SChris Smith    if ($wrapper) {
6675d568b99SChris Smith        return "<$wrapper class=\"code $language\">$highlighted_code</$wrapper>";
6685d568b99SChris Smith    } else {
6698f7d700cSchris        return $highlighted_code;
6708f7d700cSchris    }
6715d568b99SChris Smith}
6728f7d700cSchris
673