From 4d3770c4cb67a3f8bb328b28b724dd574fb0d31b Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Thu, 4 Jul 2013 12:37:30 +0100 Subject: [PATCH 1/9] Added the current context name to the cache so every context will cache seperate pages --- core/components/statcache/elements/plugins/StaticCache.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index f874324..ea04c25 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -107,7 +107,8 @@ $statcacheFile = $modx->getOption('statcache_path', $scriptProperties, MODX_BASE_PATH . 'statcache'); if ($modx->resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { /* use ~index.html to represent the site_start Resource */ - $statcacheFile .= MODX_BASE_URL . '~index.html'; + $statcacheFile .= '/'.$modx->context->key . MODX_BASE_URL .'~index.html'; + // $statcacheFile .= MODX_BASE_URL . '~index.html'; } else { /* generate an absolute URI representation of the Resource to append to the statcache_path */ $uri = $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); From 84bb7e586e7e1d77723d371921283b7b0ea4d989 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Thu, 4 Jul 2013 12:42:04 +0100 Subject: [PATCH 2/9] moved statcache cache directory to inside core/cache/ folder --- core/components/statcache/elements/plugins/StaticCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index ea04c25..053ad2b 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -68,7 +68,7 @@ } else { /* Remove all static files OnSiteRefresh */ $modx->cacheManager->deleteTree( - $modx->getOption('statcache_path', $scriptProperties, MODX_BASE_PATH . 'statcache'), + $modx->getOption('core_path',null,MODX_CORE_PATH) . 'cache/' . $modx->getOption('statcache_path', $scriptProperties, 'statcache'), array( 'deleteTop' => false, 'skipDirs' => false, @@ -104,7 +104,7 @@ break; } /* build the path/filename for writing the static representation */ - $statcacheFile = $modx->getOption('statcache_path', $scriptProperties, MODX_BASE_PATH . 'statcache'); + $statcacheFile = $modx->getOption('core_path',null,MODX_CORE_PATH) . 'cache/' . $modx->getOption('statcache_path', $scriptProperties, 'statcache'); if ($modx->resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { /* use ~index.html to represent the site_start Resource */ $statcacheFile .= '/'.$modx->context->key . MODX_BASE_URL .'~index.html'; From fa457ef5272bda244a32115cbba931a9693c843b Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Thu, 4 Jul 2013 13:24:01 +0100 Subject: [PATCH 3/9] Remove cache file on resource update --- .../elements/plugins/StaticCache.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index 053ad2b..38f3889 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -124,4 +124,34 @@ } } break; -} + case 'OnDocFormSave': + $modx->resource =& $resource; + + /* build the path/filename for writing the static representation */ + $statcacheFile = $modx->getOption('core_path',null,MODX_CORE_PATH) . 'cache/' . $modx->getOption('statcache_path', $scriptProperties, 'statcache'); + + // get context of resource and switch + $ctx = ($resource->get('context_key') ? $resource->get('context_key') : 'web'); + $modx->switchContext($ctx); + + + if ($resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { + /* use ~index.html to represent the site_start Resource */ + $statcacheFile .= '/'.$modx->context->key . MODX_BASE_URL . '~index.html'; + } else { + // dirty switch to web context so the url is generated correctly + + /* generate an absolute URI representation of the Resource to append to the statcache_path */ + $uri = '/'. $modx->context->key . MODX_BASE_URL . $modx->makeUrl($resource->get('id'), 'web', '', 'abs'); + + if (substr($uri, strlen($uri) - 1) === '/' && $resource->ContentType->get('mime_type') == 'text/html') { + /* if Resource is HTML and ends with a /, use ~index.html for the filename */ + $uri .= '~index.html'; + } + $statcacheFile .= $uri; + } + // switch back to mgr + $modx->switchContext('mgr'); + if (file_exists($statcacheFile)) unlink($statcacheFile); + break; +} \ No newline at end of file From cf97d679f136d65d1a86b4a9ab3812736e9977c5 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Thu, 4 Jul 2013 13:56:21 +0100 Subject: [PATCH 4/9] Added function to rebuild single page cache on resource save --- .../elements/plugins/StaticCache.php | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index 38f3889..e1f7127 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -139,10 +139,9 @@ /* use ~index.html to represent the site_start Resource */ $statcacheFile .= '/'.$modx->context->key . MODX_BASE_URL . '~index.html'; } else { - // dirty switch to web context so the url is generated correctly /* generate an absolute URI representation of the Resource to append to the statcache_path */ - $uri = '/'. $modx->context->key . MODX_BASE_URL . $modx->makeUrl($resource->get('id'), 'web', '', 'abs'); + $uri = '/'. $modx->context->key . MODX_BASE_URL . $modx->makeUrl($resource->get('id'), $modx->context->key, '', 'abs'); if (substr($uri, strlen($uri) - 1) === '/' && $resource->ContentType->get('mime_type') == 'text/html') { /* if Resource is HTML and ends with a /, use ~index.html for the filename */ @@ -150,8 +149,30 @@ } $statcacheFile .= $uri; } + + if (file_exists($statcacheFile)) unlink($statcacheFile); + + if (!empty($regenerate)) { + if ($resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { + $absLink = ($modx->getOption('site_url') ? $modx->getOption('site_url') : MODX_HTTP_HOST ).'/'; + } else { + $absLink = ($modx->getOption('site_url') ? $modx->getOption('site_url') : MODX_HTTP_HOST ).'/'.$modx->makeUrl($resource->get('id')); + } + + set_time_limit(0); + $curl = curl_init(); + curl_setopt($curl, CURLOPT_USERAGENT, 'MODX RegenCache'); + curl_setopt($curl, CURLOPT_FAILONERROR, false); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_NOBODY, true); + curl_setopt($curl, CURLOPT_URL, $absLink); + curl_exec($curl); + curl_close($curl); + + } + // switch back to mgr $modx->switchContext('mgr'); - if (file_exists($statcacheFile)) unlink($statcacheFile); break; } \ No newline at end of file From 9fc87170980b9c3a74e3acc3bc70617e7760c890 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Thu, 4 Jul 2013 14:01:17 +0100 Subject: [PATCH 5/9] build update - new parameters --- _build/build.transport.php | 2 +- _build/properties.inc.php | 7 +++++++ _build/resolvers/resolve.plugin_events.php | 1 + core/components/statcache/elements/plugins/StaticCache.php | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/_build/build.transport.php b/_build/build.transport.php index 9dcb8c3..c3eaf3c 100644 --- a/_build/build.transport.php +++ b/_build/build.transport.php @@ -11,7 +11,7 @@ /* set package info */ define('PKG_NAME', 'statcache'); define('PKG_NAME_LOWER', strtolower(PKG_NAME)); -define('PKG_VERSION', '1.3.0'); +define('PKG_VERSION', '1.3.1'); define('PKG_RELEASE', 'pl'); /* define sources */ diff --git a/_build/properties.inc.php b/_build/properties.inc.php index ca477d9..a802b61 100644 --- a/_build/properties.inc.php +++ b/_build/properties.inc.php @@ -52,6 +52,13 @@ 'options' => '', 'value' => false, ), + array( + 'name' => 'regenerateSinglePage', + 'desc' => 'Regenerate static file after a resource has been updated.', + 'type' => 'combo-boolean', + 'options' => '', + 'value' => false, + ), ); return $properties; diff --git a/_build/resolvers/resolve.plugin_events.php b/_build/resolvers/resolve.plugin_events.php index 68dc959..fbe9999 100644 --- a/_build/resolvers/resolve.plugin_events.php +++ b/_build/resolvers/resolve.plugin_events.php @@ -7,6 +7,7 @@ if (isset($options['activatePlugin']) && !empty($options['activatePlugin'])) { $events = array( 'OnSiteRefresh', + 'OnDocFormSave', 'OnBeforeSaveWebPageCache' ); foreach ($events as $eventName) { diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index e1f7127..ff786c2 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -152,7 +152,7 @@ if (file_exists($statcacheFile)) unlink($statcacheFile); - if (!empty($regenerate)) { + if (!empty($regenerateSinglePage)) { if ($resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { $absLink = ($modx->getOption('site_url') ? $modx->getOption('site_url') : MODX_HTTP_HOST ).'/'; } else { From dd51bc1ebe16c9907f6b17f2cfbcce91f00c4e2d Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Thu, 4 Jul 2013 14:22:51 +0100 Subject: [PATCH 6/9] version bump --- _build/build.transport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_build/build.transport.php b/_build/build.transport.php index c3eaf3c..bfada1a 100644 --- a/_build/build.transport.php +++ b/_build/build.transport.php @@ -11,7 +11,7 @@ /* set package info */ define('PKG_NAME', 'statcache'); define('PKG_NAME_LOWER', strtolower(PKG_NAME)); -define('PKG_VERSION', '1.3.1'); +define('PKG_VERSION', '1.4.0'); define('PKG_RELEASE', 'pl'); /* define sources */ From d6652012f9b549d0df025934e5e163572682454d Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Fri, 5 Jul 2013 09:00:13 +0100 Subject: [PATCH 7/9] Updated statcache to not cache logged in requests --- .../elements/plugins/StaticCache.php | 80 ++++++++++--------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index ff786c2..7bf459d 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -79,48 +79,50 @@ break; case 'OnBeforeSaveWebPageCache': /* Write a static version of the file before caching it in MODX */ - if ($modx->resource->get('cacheable') && $modx->resource->get('published') && $modx->resource->_output != '') { - /* force caching of Resources with a value of '1' for a specified cacheTV */ - $forced = !empty($cacheTV); - if (!$forced) { - /* optionally skip binary content types */ - if (!empty($skipBinaryContentTypes) && $modx->resource->ContentType->get('binary')) break; - /* do not cache if the cacheable content still contains unprocessed tags */ - /* skip Resources with a non-empty value for the specified TV */ - if (!empty($skipTV) && $modx->resource->getTVValue($skipTV)) break; - $matches = array(); - if (!empty($skipIfTagsRemain) && $modx->parser->collectElementTags($modx->resource->_content, $matches)) break; - /* if specified, limit caching by mime-type */ - if (!empty($mimeTypes)) { - $validMimeTypes = array_walk(explode(',', strtolower($mimeTypes)), 'trim'); - if (!in_array(strtolower($modx->resource->ContentType->get('mime_type')), $validMimeTypes)) break; + if (!$modx->user->hasSessionContext($modx->context->get('key')) ) { /** only cache when a user is NOT logged in **/ + if ($modx->resource->get('cacheable') && $modx->resource->get('published') && $modx->resource->_output != '') { + /* force caching of Resources with a value of '1' for a specified cacheTV */ + $forced = !empty($cacheTV); + if (!$forced) { + /* optionally skip binary content types */ + if (!empty($skipBinaryContentTypes) && $modx->resource->ContentType->get('binary')) break; + /* do not cache if the cacheable content still contains unprocessed tags */ + /* skip Resources with a non-empty value for the specified TV */ + if (!empty($skipTV) && $modx->resource->getTVValue($skipTV)) break; + $matches = array(); + if (!empty($skipIfTagsRemain) && $modx->parser->collectElementTags($modx->resource->_content, $matches)) break; + /* if specified, limit caching by mime-type */ + if (!empty($mimeTypes)) { + $validMimeTypes = array_walk(explode(',', strtolower($mimeTypes)), 'trim'); + if (!in_array(strtolower($modx->resource->ContentType->get('mime_type')), $validMimeTypes)) break; + } + /* if specified, limit caching by ContentTypes */ + if (!empty($contentTypes)) { + $validContentTypes = array_walk(explode(',', $contentTypes), 'trim'); + if (!in_array($modx->resource->ContentType->get('id'), $validContentTypes)) break; + } + } elseif ($modx->resource->getTVValue($cacheTV) !== '1') { + break; } - /* if specified, limit caching by ContentTypes */ - if (!empty($contentTypes)) { - $validContentTypes = array_walk(explode(',', $contentTypes), 'trim'); - if (!in_array($modx->resource->ContentType->get('id'), $validContentTypes)) break; + /* build the path/filename for writing the static representation */ + $statcacheFile = $modx->getOption('core_path',null,MODX_CORE_PATH) . 'cache/' . $modx->getOption('statcache_path', $scriptProperties, 'statcache'); + if ($modx->resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { + /* use ~index.html to represent the site_start Resource */ + $statcacheFile .= '/'.$modx->context->key . MODX_BASE_URL .'~index.html'; + // $statcacheFile .= MODX_BASE_URL . '~index.html'; + } else { + /* generate an absolute URI representation of the Resource to append to the statcache_path */ + $uri = $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); + if (substr($uri, strlen($uri) - 1) === '/' && $modx->resource->ContentType->get('mime_type') == 'text/html') { + /* if Resource is HTML and ends with a /, use ~index.html for the filename */ + $uri .= '~index.html'; + } + $statcacheFile .= $uri; } - } elseif ($modx->resource->getTVValue($cacheTV) !== '1') { - break; - } - /* build the path/filename for writing the static representation */ - $statcacheFile = $modx->getOption('core_path',null,MODX_CORE_PATH) . 'cache/' . $modx->getOption('statcache_path', $scriptProperties, 'statcache'); - if ($modx->resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { - /* use ~index.html to represent the site_start Resource */ - $statcacheFile .= '/'.$modx->context->key . MODX_BASE_URL .'~index.html'; - // $statcacheFile .= MODX_BASE_URL . '~index.html'; - } else { - /* generate an absolute URI representation of the Resource to append to the statcache_path */ - $uri = $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); - if (substr($uri, strlen($uri) - 1) === '/' && $modx->resource->ContentType->get('mime_type') == 'text/html') { - /* if Resource is HTML and ends with a /, use ~index.html for the filename */ - $uri .= '~index.html'; + /* attempt to write the complete Resource output to the static file */ + if (!$modx->cacheManager->writeFile($statcacheFile, $modx->resource->_output)) { + $modx->log(modX::LOG_LEVEL_ERROR, "Error caching output from Resource {$modx->resource->get('id')} to static file {$statcacheFile}", '', __FUNCTION__, __FILE__, __LINE__); } - $statcacheFile .= $uri; - } - /* attempt to write the complete Resource output to the static file */ - if (!$modx->cacheManager->writeFile($statcacheFile, $modx->resource->_output)) { - $modx->log(modX::LOG_LEVEL_ERROR, "Error caching output from Resource {$modx->resource->get('id')} to static file {$statcacheFile}", '', __FUNCTION__, __FILE__, __LINE__); } } break; From 3b2bf5073487f1d2b9eace2a398a85272d432d77 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Fri, 5 Jul 2013 09:33:45 +0100 Subject: [PATCH 8/9] resolved context issue --- core/components/statcache/elements/plugins/StaticCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index 7bf459d..394ee21 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -112,7 +112,7 @@ // $statcacheFile .= MODX_BASE_URL . '~index.html'; } else { /* generate an absolute URI representation of the Resource to append to the statcache_path */ - $uri = $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); + $uri = '/'.$modx->context->key . MODX_BASE_URL . $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); if (substr($uri, strlen($uri) - 1) === '/' && $modx->resource->ContentType->get('mime_type') == 'text/html') { /* if Resource is HTML and ends with a /, use ~index.html for the filename */ $uri .= '~index.html'; From caebf22d53a42036ea63d0f7fde2555c50a88d11 Mon Sep 17 00:00:00 2001 From: Mark Willis Date: Mon, 8 Jul 2013 08:47:49 +0100 Subject: [PATCH 9/9] Updated to use rel links --- core/components/statcache/elements/plugins/StaticCache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index 394ee21..32c3a2e 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -112,7 +112,8 @@ // $statcacheFile .= MODX_BASE_URL . '~index.html'; } else { /* generate an absolute URI representation of the Resource to append to the statcache_path */ - $uri = '/'.$modx->context->key . MODX_BASE_URL . $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); +// $uri = $modx->makeUrl($modx->resource->get('id'), '', '', 'abs'); + $uri = '/'.$modx->context->key . MODX_BASE_URL . $modx->makeUrl($modx->resource->get('id'), '', '', ''); if (substr($uri, strlen($uri) - 1) === '/' && $modx->resource->ContentType->get('mime_type') == 'text/html') { /* if Resource is HTML and ends with a /, use ~index.html for the filename */ $uri .= '~index.html'; @@ -143,7 +144,7 @@ } else { /* generate an absolute URI representation of the Resource to append to the statcache_path */ - $uri = '/'. $modx->context->key . MODX_BASE_URL . $modx->makeUrl($resource->get('id'), $modx->context->key, '', 'abs'); + $uri = '/'. $modx->context->key . MODX_BASE_URL . $modx->makeUrl($resource->get('id'), $modx->context->key, '', ''); if (substr($uri, strlen($uri) - 1) === '/' && $resource->ContentType->get('mime_type') == 'text/html') { /* if Resource is HTML and ends with a /, use ~index.html for the filename */