function setUp() { global $ID, $conf; parent::setUp(); $ID = 'cached'; $file = wikiFN($ID); $conf['cachetime'] = 0; // ensure the value is not -1, which disables caching saveWikiText($ID, 'Content', 'Created'); $this->cache = new cache_renderer($ID, $file, 'xhtml'); $this->cache->storeCache('Test'); // set the modification times explicitly (overcome Issue #694) $time = time(); touch($file, $time - 1); touch($this->cache->cache, $time); }
function test_staleness() { global $ID; $ID = 'stale'; $file = wikiFN($ID); # Prepare test page saveWikiText($ID, 'Fresh', 'Created'); # Create stale cache $cache = new cache_renderer($ID, $file, 'xhtml'); $cache->storeCache('Stale'); $stale = $cache->retrieveCache(); # Prepare stale cache for testing $time = filemtime($file); touch($cache->cache, $time); # Make the test $fresh = p_cached_output($file, 'xhtml', $ID); $this->assertNotEquals($fresh, $stale, 'Stale cache failed to expire'); }
/** * 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 <*****@*****.**> * @author Michael Hamann <*****@*****.**> */ 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 // accessed several times. This may catch a few other pages, but that shouldn't be an issue. $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 && $render != METADATA_DONT_RENDER && !isset($rendered_pages[$id]) && page_exists($id)) { $recursion = true; $cachefile = new cache_renderer($id, wikiFN($id), 'metadata'); $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); // 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()); } elseif ($meta != $old_meta) { msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1); } } $recursion = false; } $val = $meta['current']; // filter by $key foreach (preg_split('/\\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { if (!isset($val[$cur_key])) { return null; } $val = $val[$cur_key]; } return $val; }
/** * Returns the given file parsed into the requested output format * * @author Andreas Gohr <*****@*****.**> * @author Chris Smith <*****@*****.**> */ function p_cached_output($file, $format = 'xhtml', $id = '') { global $conf; $cache = new cache_renderer($id, $file, $format); if ($cache->useCache()) { $parsed = $cache->retrieveCache(false); if ($conf['allowdebug'] && $format == 'xhtml') { $parsed .= "\n<!-- cachefile {$cache->cache} used -->\n"; } } else { $parsed = p_render($format, p_cached_instructions($file, false, $id), $info); if ($info['cache']) { $cache->storeCache($parsed); //save cachefile if ($conf['allowdebug'] && $format == 'xhtml') { $parsed .= "\n<!-- no cachefile used, but created {$cache->cache} -->\n"; } } else { $cache->removeCache(); //try to delete cachefile if ($conf['allowdebug'] && $format == 'xhtml') { $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n"; } } } return $parsed; }
/** * 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 boolean $render If the page should be rendererd when the cache can't be used - default true * @return mixed The requested metadata fields * * @author Esther Brunner <*****@*****.**> * @author Michael Hamann <*****@*****.**> */ function p_get_metadata($id, $key = '', $render = true) { global $ID; // cache the current page // Benchmarking shows the current page's metadata is generally the only page metadata // accessed several times. This may catch a few other pages, but that shouldn't be an issue. $cache = $ID == $id; $meta = p_read_metadata($id, $cache); // prevent recursive calls in the cache static $recursion = false; if (!$recursion && $render) { $recursion = true; $cachefile = new cache_renderer($id, wikiFN($id), 'metadata'); if (page_exists($id) && !$cachefile->useCache()) { $old_meta = $meta; $meta = p_render_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 { msg('Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.', -1); } } $recursion = false; } $val = $meta['current']; // filter by $key foreach (preg_split('/\\s+/', $key, 2, PREG_SPLIT_NO_EMPTY) as $cur_key) { if (!isset($val[$cur_key])) { return null; } $val = $val[$cur_key]; } return $val; }