diff options
Diffstat (limited to 'inc/parserutils.php')
-rw-r--r-- | inc/parserutils.php | 155 |
1 files changed, 130 insertions, 25 deletions
diff --git a/inc/parserutils.php b/inc/parserutils.php index d4f55a6e4..9384929bf 100644 --- a/inc/parserutils.php +++ b/inc/parserutils.php @@ -10,6 +10,45 @@ if(!defined('DOKU_INC')) die('meh.'); /** + * How many pages shall be rendered for getting metadata during one request + * at maximum? Note that this limit isn't respected when METADATA_RENDER_UNLIMITED + * is passed as render parameter to p_get_metadata. + */ +if (!defined('P_GET_METADATA_RENDER_LIMIT')) define('P_GET_METADATA_RENDER_LIMIT', 5); + +/** Don't render metadata even if it is outdated or doesn't exist */ +define('METADATA_DONT_RENDER', 0); +/** + * Render metadata when the page is really newer or the metadata doesn't exist. + * Uses just a simple check, but should work pretty well for loading simple + * metadata values like the page title and avoids rendering a lot of pages in + * one request. The P_GET_METADATA_RENDER_LIMIT is used in this mode. + * Use this if it is unlikely that the metadata value you are requesting + * does depend e.g. on pages that are included in the current page using + * the include plugin (this is very likely the case for the page title, but + * not for relation references). + */ +define('METADATA_RENDER_USING_SIMPLE_CACHE', 1); +/** + * Render metadata using the metadata cache logic. The P_GET_METADATA_RENDER_LIMIT + * is used in this mode. Use this mode when you are requesting more complex + * metadata. Although this will cause rendering more often it might actually have + * the effect that less current metadata is returned as it is more likely than in + * the simple cache mode that metadata needs to be rendered for all pages at once + * which means that when the metadata for the page is requested that actually needs + * to be updated the limit might have been reached already. + */ +define('METADATA_RENDER_USING_CACHE', 2); +/** + * Render metadata without limiting the number of pages for which metadata is + * rendered. Use this mode with care, normally it should only be used in places + * like the indexer or in cli scripts where the execution time normally isn't + * limited. This can be combined with the simple cache using + * METADATA_RENDER_USING_CACHE | METADATA_RENDER_UNLIMITED. + */ +define('METADATA_RENDER_UNLIMITED', 4); + +/** * Returns the parsed Wikitext in XHTML for the given id and revision. * * If $excuse is true an explanation is returned if the file @@ -220,11 +259,23 @@ function p_get_instructions($text){ /** * returns the metadata of a page * + * @param string $id The id of the page the metadata should be returned from + * @param string $key The key of the metdata value that shall be read (by default everything) - separate hierarchies by " " like "date created" + * @param int $render If the page should be rendererd - possible values: + * METADATA_DONT_RENDER, METADATA_RENDER_USING_SIMPLE_CACHE, METADATA_RENDER_USING_CACHE + * METADATA_RENDER_UNLIMITED (also combined with the previous two options), + * default: METADATA_RENDER_USING_CACHE + * @return mixed The requested metadata fields + * * @author Esther Brunner <esther@kaffeehaus.ch> * @author Michael Hamann <michael@content-space.de> */ -function p_get_metadata($id, $key='', $render=false){ +function p_get_metadata($id, $key='', $render=METADATA_RENDER_USING_CACHE){ global $ID; + static $render_count = 0; + // track pages that have already been rendered in order to avoid rendering the same page + // again + static $rendered_pages = array(); // cache the current page // Benchmarking shows the current page's metadata is generally the only page metadata @@ -232,19 +283,43 @@ function p_get_metadata($id, $key='', $render=false){ $cache = ($ID == $id); $meta = p_read_metadata($id, $cache); + if (!is_numeric($render)) { + if ($render) { + $render = METADATA_RENDER_USING_SIMPLE_CACHE; + } else { + $render = METADATA_DONT_RENDER; + } + } + // prevent recursive calls in the cache static $recursion = false; - if (!$recursion){ + if (!$recursion && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id])&& page_exists($id)){ $recursion = true; $cachefile = new cache_renderer($id, wikiFN($id), 'metadata'); - if (page_exists($id) && !$cachefile->useCache()){ + $do_render = false; + if ($render & METADATA_RENDER_UNLIMITED || $render_count < P_GET_METADATA_RENDER_LIMIT) { + if ($render & METADATA_RENDER_USING_SIMPLE_CACHE) { + $pagefn = wikiFN($id); + $metafn = metaFN($id, '.meta'); + if (!@file_exists($metafn) || @filemtime($pagefn) > @filemtime($cachefile->cache)) { + $do_render = true; + } + } elseif (!$cachefile->useCache()){ + $do_render = true; + } + } + if ($do_render) { + ++$render_count; + $rendered_pages[$id] = true; + $old_meta = $meta; $meta = p_render_metadata($id, $meta); - if (p_save_metadata($id, $meta)) { + // only update the file when the metadata has been changed + if ($meta == $old_meta || p_save_metadata($id, $meta)) { // store a timestamp in order to make sure that the cachefile is touched $cachefile->storeCache(time()); - } else { + } elseif ($meta != $old_meta) { msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.',-1); } } @@ -277,18 +352,25 @@ function p_get_metadata($id, $key='', $render=false){ * @return boolean true on success * * @author Esther Brunner <esther@kaffeehaus.ch> + * @author Michael Hamann <michael@content-space.de> */ function p_set_metadata($id, $data, $render=false, $persistent=true){ if (!is_array($data)) return false; - global $ID; + global $ID, $METADATA_RENDERERS; - // cache the current page - $cache = ($ID == $id); - $orig = p_read_metadata($id, $cache); + // if there is currently a renderer change the data in the renderer instead + if (isset($METADATA_RENDERERS[$id])) { + $orig =& $METADATA_RENDERERS[$id]; + $meta = $orig; + } else { + // cache the current page + $cache = ($ID == $id); + $orig = p_read_metadata($id, $cache); - // render metadata first? - $meta = $render ? p_render_metadata($id, $orig) : $orig; + // render metadata first? + $meta = $render ? p_render_metadata($id, $orig) : $orig; + } // now add the passed metadata $protected = array('description', 'date', 'contributor'); @@ -325,7 +407,14 @@ function p_set_metadata($id, $data, $render=false, $persistent=true){ // save only if metadata changed if ($meta == $orig) return true; - return p_save_metadata($id, $meta); + if (isset($METADATA_RENDERERS[$id])) { + // set both keys individually as the renderer has references to the individual keys + $METADATA_RENDERERS[$id]['current'] = $meta['current']; + $METADATA_RENDERERS[$id]['persistent'] = $meta['persistent']; + return true; + } else { + return p_save_metadata($id, $meta); + } } /** @@ -399,7 +488,15 @@ function p_save_metadata($id, $meta) { */ function p_render_metadata($id, $orig){ // make sure the correct ID is in global ID - global $ID; + global $ID, $METADATA_RENDERERS; + + // avoid recursive rendering processes for the same id + if (isset($METADATA_RENDERERS[$id])) + return $orig; + + // store the original metadata in the global $METADATA_RENDERERS so p_set_metadata can use it + $METADATA_RENDERERS[$id] =& $orig; + $keep = $ID; $ID = $id; @@ -414,13 +511,14 @@ function p_render_metadata($id, $orig){ $instructions = p_cached_instructions(wikiFN($id),false,$id); if(is_null($instructions)){ $ID = $keep; + unset($METADATA_RENDERERS[$id]); return null; // something went wrong with the instructions } // set up the renderer $renderer = new Doku_Renderer_metadata(); - $renderer->meta = $orig['current']; - $renderer->persistent = $orig['persistent']; + $renderer->meta =& $orig['current']; + $renderer->persistent =& $orig['persistent']; // loop through the instructions foreach ($instructions as $instruction){ @@ -428,11 +526,13 @@ function p_render_metadata($id, $orig){ call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); } - $evt->result = array('current'=>$renderer->meta,'persistent'=>$renderer->persistent); + $evt->result = array('current'=>&$renderer->meta,'persistent'=>&$renderer->persistent); } $evt->advise_after(); + // clean up $ID = $keep; + unset($METADATA_RENDERERS[$id]); return $evt->result; } @@ -577,12 +677,16 @@ function & p_get_renderer($mode) { global $conf, $plugin_controller; $rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode; + $rclass = "Doku_Renderer_$rname"; + + if( class_exists($rclass) ) { + return new $rclass(); + } // try default renderer first: $file = DOKU_INC."inc/parser/$rname.php"; if(@file_exists($file)){ require_once $file; - $rclass = "Doku_Renderer_$rname"; if ( !class_exists($rclass) ) { trigger_error("Unable to resolve render class $rclass",E_USER_WARNING); @@ -610,17 +714,18 @@ function & p_get_renderer($mode) { * Gets the first heading from a file * * @param string $id dokuwiki page id - * @param bool $render rerender if first heading not known - * default: true -- must be set to false for calls from the metadata renderer to - * protects against loops and excessive resource usage when pages - * for which only a first heading is required will attempt to - * render metadata for all the pages for which they require first - * headings ... and so on. + * @param int $render rerender if first heading not known + * default: METADATA_RENDER_USING_SIMPLE_CACHE + * Possible values: METADATA_DONT_RENDER, + * METADATA_RENDER_USING_SIMPLE_CACHE, + * METADATA_RENDER_USING_CACHE, + * METADATA_RENDER_UNLIMITED * * @author Andreas Gohr <andi@splitbrain.org> + * @author Michael Hamann <michael@content-space.de> */ -function p_get_first_heading($id, $render=true){ - return p_get_metadata($id,'title',$render); +function p_get_first_heading($id, $render=METADATA_RENDER_USING_SIMPLE_CACHE){ + return p_get_metadata(cleanID($id),'title',$render); } /** |