Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added resource cache updater and the ability to handle context changes #11

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion _build/build.transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.4.0');
define('PKG_RELEASE', 'pl');

/* define sources */
Expand Down
7 changes: 7 additions & 0 deletions _build/properties.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions _build/resolvers/resolve.plugin_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
if (isset($options['activatePlugin']) && !empty($options['activatePlugin'])) {
$events = array(
'OnSiteRefresh',
'OnDocFormSave',
'OnBeforeSaveWebPageCache'
);
foreach ($events as $eventName) {
Expand Down
135 changes: 95 additions & 40 deletions core/components/statcache/elements/plugins/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -79,48 +79,103 @@
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');
$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';
}
$statcacheFile .= $uri;
}
} elseif ($modx->resource->getTVValue($cacheTV) !== '1') {
break;
}
/* build the path/filename for writing the static representation */
$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';
} 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;
}
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 {

/* 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, '', '');

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);

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 {
$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');
break;
}