From c0e0ecdd2de73dc8c791d1ab01fd7d97c7924bdf Mon Sep 17 00:00:00 2001 From: christianseel Date: Sat, 2 Feb 2013 01:53:46 +0100 Subject: [PATCH 1/3] added some OnDocFormSave handling to delete a cache file when a resource is saved --- .../elements/plugins/StaticCache.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index ff22742..8f0018f 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -51,4 +51,26 @@ } } break; + case 'OnDocFormSave': + $modx->resource =& $resource; + /* build the path/filename for writing the static representation */ + $statcacheFile = $modx->getOption('statcache_path', $scriptProperties, MODX_BASE_PATH . 'statcache'); + if ($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'; + } else { + // dirty switch to web context so the url is generated correctly + $modx->switchContext('web'); + /* generate an absolute URI representation of the Resource to append to the statcache_path */ + $uri = $modx->makeUrl($resource->get('id'), 'web', '', 'abs'); + $modx->switchContext('mgr'); + 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; + } + + if (file_exists($statcacheFile)) unlink($statcacheFile); + break; } From 58494d59f25abcd7072060b6b52996bb848f5b58 Mon Sep 17 00:00:00 2001 From: christianseel Date: Sat, 2 Feb 2013 10:28:57 +0100 Subject: [PATCH 2/3] moved statcache folder to the modx cache folder --- core/components/statcache/elements/plugins/StaticCache.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index 8f0018f..c9d333c 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -3,7 +3,7 @@ case 'OnSiteRefresh': /* 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, @@ -32,7 +32,7 @@ if (!in_array($modx->resource->ContentType->get('id'), $validContentTypes)) 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_BASE_URL . '~index.html'; @@ -54,7 +54,7 @@ case 'OnDocFormSave': $modx->resource =& $resource; /* 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 ($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'; From 9c3dcb05bec527b640c02fb1bd33e1210f14130c Mon Sep 17 00:00:00 2001 From: christianseel Date: Sun, 3 Feb 2013 15:41:58 +0100 Subject: [PATCH 3/3] create new cached file OnDocFormSave after old cached file was deleted --- .../statcache/elements/plugins/StaticCache.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/components/statcache/elements/plugins/StaticCache.php b/core/components/statcache/elements/plugins/StaticCache.php index c9d333c..63669d1 100644 --- a/core/components/statcache/elements/plugins/StaticCache.php +++ b/core/components/statcache/elements/plugins/StaticCache.php @@ -54,6 +54,7 @@ case 'OnDocFormSave': $modx->resource =& $resource; /* build the path/filename for writing the static representation */ + $url = $modx->makeUrl($resource->get('id'), '', '', 'full'); $statcacheFile = $modx->getOption('core_path',null,MODX_CORE_PATH) . 'cache/' . $modx->getOption('statcache_path', $scriptProperties, 'statcache'); if ($resource->get('id') === (integer) $modx->getOption('site_start', $scriptProperties, 1)) { /* use ~index.html to represent the site_start Resource */ @@ -62,7 +63,7 @@ // dirty switch to web context so the url is generated correctly $modx->switchContext('web'); /* generate an absolute URI representation of the Resource to append to the statcache_path */ - $uri = $modx->makeUrl($resource->get('id'), 'web', '', 'abs'); + $uri = $modx->makeUrl($resource->get('id'), '', '', 'abs'); $modx->switchContext('mgr'); 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 */ @@ -70,7 +71,12 @@ } $statcacheFile .= $uri; } - + // delete cached file if (file_exists($statcacheFile)) unlink($statcacheFile); + + if ($resource->get('published') == 1 && $resource->get('deleted') == 0 && $resource->get('cacheable') == 1) { + // let MODX create a new cached version by requesting the resource + @file_get_contents($url); + } break; }