diff --git a/administrator/language/en-GB/en-GB.plg_system_cache.ini b/administrator/language/en-GB/en-GB.plg_system_cache.ini index 99f7b83c24035..b2801ffa92845 100644 --- a/administrator/language/en-GB/en-GB.plg_system_cache.ini +++ b/administrator/language/en-GB/en-GB.plg_system_cache.ini @@ -5,7 +5,7 @@ PLG_CACHE_FIELD_BROWSERCACHE_DESC="If yes, use mechanism for storing page cache in the browser." PLG_CACHE_FIELD_BROWSERCACHE_LABEL="Use Browser Caching" -PLG_CACHE_FIELD_EXCLUDE_DESC="Specify which URLs you want to exclude from caching, each on a separate line. Regular expressions are supported, eg.
about\-[a-z]+ - will exclude all URLs that contain 'about-', for example 'about-us', 'about-me', 'about-joomla' etc.
\/component\/users\/ - will exclude all URLs that contain /component/users/" +PLG_CACHE_FIELD_EXCLUDE_DESC="Specify which URLs you want to exclude from caching, each on a separate line. Regular expressions are supported, eg.
about\-[a-z]+ - will exclude all URLs that contain 'about-', for example 'about-us', 'about-me', 'about-joomla' etc.
/component/users/ - will exclude all URLs that contain /component/users/.
com_users - will exclude all Users component pages." PLG_CACHE_FIELD_EXCLUDE_LABEL="Exclude URLs" PLG_CACHE_FIELD_EXCLUDE_MENU_ITEMS_DESC="Select which menu items you want to exclude from caching." PLG_CACHE_FIELD_EXCLUDE_MENU_ITEMS_LABEL="Exclude Menu Items" diff --git a/plugins/system/cache/cache.php b/plugins/system/cache/cache.php index 821242fd29aed..2b7f2e06c9b7d 100644 --- a/plugins/system/cache/cache.php +++ b/plugins/system/cache/cache.php @@ -32,19 +32,27 @@ public function __construct(& $subject, $config) { parent::__construct($subject, $config); - // Set the language in the class. + // Get the application if not done by JPlugin. + if (!isset($this->app)) + { + $this->app = JFactory::getApplication(); + } + + // Set the cache options. $options = array( 'defaultgroup' => 'page', 'browsercache' => $this->params->get('browsercache', false), 'caching' => false, ); + // Instantiate cache with previous options and create the cache key identifier. $this->_cache = JCache::getInstance('page', $options); $this->_cache_key = JUri::getInstance()->toString(); } /** - * Converting the site URL to fit to the HTTP request. + * After Initialise Event. + * Checks if URL exists in cache, if so dumps it directly and closes. * * @return void * @@ -52,68 +60,83 @@ public function __construct(& $subject, $config) */ public function onAfterInitialise() { - $app = JFactory::getApplication(); - $user = JFactory::getUser(); - - if ($app->isClient('administrator')) - { - return; - } - - if (count($app->getMessageQueue())) + if ($this->app->isClient('administrator') || $this->app->get('offline', '0') || count($this->app->getMessageQueue())) { return; } - if ($user->get('guest') && $app->input->getMethod() === 'GET') + // If user is guest and method is equal to GET enable caching. + if (JFactory::getUser()->get('guest') && $this->app->input->getMethod() === 'GET') { $this->_cache->setCaching(true); - } - $data = $this->_cache->get($this->_cache_key); + // Gets page from cache. + $data = $this->_cache->get($this->_cache_key); - if ($data !== false) - { - // Set cached body. - $app->setBody($data); + // If page exist in cache, show cached page. + if ($data !== false) + { + // Set HTML page from cache. + $this->app->setBody($data); - echo $app->toString(); + // Dumps HTML page. + echo $this->app->toString(); - if (JDEBUG) - { - JProfiler::getInstance('Application')->mark('afterCache'); - } + // Mark afterCache in debug and run debug onAfterRespond events. + // e.g., show Joomla Debug Console if debug is active. + if (JDEBUG) + { + JProfiler::getInstance('Application')->mark('afterCache'); + JEventDispatcher::getInstance()->trigger('onAfterRespond'); + } - $app->close(); + // Closes the application. + $this->app->close(); + } } } /** - * After render. + * After Route Event. + * Verify if current page is not excluded from cache. * * @return void * - * @since 1.5 + * @since __DEPLOY_VERSION__ */ - public function onAfterRespond() + public function onAfterRoute() { - $app = JFactory::getApplication(); - - if ($app->isClient('administrator')) + if ($this->app->isClient('administrator') || $this->app->get('offline', '0') || count($this->app->getMessageQueue())) { return; } - if (count($app->getMessageQueue())) + // Page is excluded if excluded in plugin settings. + if ($this->isExcluded()) { - return; + $this->_cache->setCaching(false); } + } - $user = JFactory::getUser(); + /** + * After Respond Event. + * Stores page in cache. + * + * @return void + * + * @since 1.5 + */ + public function onAfterRespond() + { + if ($this->app->isClient('administrator') || $this->app->get('offline', '0') || count($this->app->getMessageQueue())) + { + return; + } - if ($user->get('guest') && !$this->isExcluded()) + // We need to check if user is guest again here, because auto-login plugins have not been fired before the first aid check. + if (JFactory::getUser()->get('guest')) { - // We need to check again here, because auto-login plugins have not been fired before the first aid check. + // Saves current page in cache. $this->_cache->store(null, $this->_cache_key); } } @@ -127,11 +150,11 @@ public function onAfterRespond() */ protected function isExcluded() { - // Check if menu items have been excluded + // Check if menu items have been excluded. if ($exclusions = $this->params->get('exclude_menu_items', array())) { - // Get the current menu item - $active = JFactory::getApplication()->getMenu()->getActive(); + // Get the current menu item. + $active = $this->app->getMenu()->getActive(); if ($active && $active->id && in_array($active->id, (array) $exclusions)) { @@ -139,27 +162,28 @@ protected function isExcluded() } } - // Check if regular expressions are being used + // Check if regular expressions are being used. if ($exclusions = $this->params->get('exclude', '')) { - // Normalize line endings + // Normalize line endings. $exclusions = str_replace(array("\r\n", "\r"), "\n", $exclusions); - // Split them + // Split them. $exclusions = explode("\n", $exclusions); - // Get current path to match against - $path = JUri::getInstance()->toString(array('path', 'query', 'fragment')); + // Gets internal URI. + $internal_uri = '/index.php?' . JUri::getInstance()->buildQuery($this->app->getRouter()->getVars()); - // Loop through each pattern + // Loop through each pattern. if ($exclusions) { foreach ($exclusions as $exclusion) { - // Make sure the exclusion has some content + // Make sure the exclusion has some content. if (strlen($exclusion)) { - if (preg_match('/' . $exclusion . '/is', $path, $match)) + // Test both external and internal URI + if (preg_match('#' . $exclusion . '#i', $this->_cache_key . ' ' . $internal_uri, $match)) { return true; }