2008-08-12 04:59:27 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-22 23:58:18 +01:00
|
|
|
* @package cms
|
|
|
|
* @subpackage tasks
|
|
|
|
*
|
2008-08-12 04:59:27 +02:00
|
|
|
* @todo Make this use the Task interface once it gets merged back into trunk
|
|
|
|
*/
|
|
|
|
class RebuildStaticCacheTask extends Controller {
|
|
|
|
function init() {
|
2009-09-10 03:49:07 +02:00
|
|
|
parent::init();
|
|
|
|
|
2008-12-15 02:31:01 +01:00
|
|
|
Versioned::reading_stage('live');
|
|
|
|
|
2009-09-10 03:49:07 +02:00
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
|
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
2008-08-12 04:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function index() {
|
|
|
|
StaticPublisher::set_echo_progress(true);
|
|
|
|
|
|
|
|
$page = singleton('Page');
|
2008-09-29 00:41:02 +02:00
|
|
|
if(!$page->hasMethod('allPagesToCache')) {
|
|
|
|
user_error(
|
|
|
|
'RebuildStaticCacheTask::index(): Please define a method "allPagesToCache()" on your Page class to return all pages affected by a cache refresh.',
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-05 07:17:59 +01:00
|
|
|
if(!empty($_GET['urls'])) $urls = $_GET['urls'];
|
2008-08-12 04:59:27 +02:00
|
|
|
else $urls = $page->allPagesToCache();
|
2008-12-15 02:31:01 +01:00
|
|
|
|
2008-08-12 04:59:27 +02:00
|
|
|
$this->rebuildCache($urls, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rebuilds the static cache for the pages passed through via $urls
|
|
|
|
* @param array $urls The URLs of pages to re-fetch and cache.
|
|
|
|
*/
|
|
|
|
function rebuildCache($urls, $removeAll = true) {
|
|
|
|
if(!is_array($urls)) return; // $urls must be an array
|
|
|
|
|
|
|
|
if(!Director::is_cli()) echo "<pre>\n";
|
|
|
|
echo "Rebuilding cache.\nNOTE: Please ensure that this page ends with 'Done!' - if not, then something may have gone wrong.\n\n";
|
|
|
|
|
|
|
|
$page = singleton('Page');
|
|
|
|
|
|
|
|
foreach($urls as $i => $url) {
|
2008-12-15 02:31:01 +01:00
|
|
|
if($url && !is_string($url)) {
|
|
|
|
user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-13 06:42:02 +02:00
|
|
|
if(substr($url,-1) == '/') $url = substr($url,0,-1);
|
|
|
|
$urls[$i] = $url;
|
2008-08-12 04:59:27 +02:00
|
|
|
}
|
|
|
|
$urls = array_unique($urls);
|
2008-12-15 02:31:01 +01:00
|
|
|
sort($urls);
|
|
|
|
|
2009-07-23 00:57:02 +02:00
|
|
|
$mappedUrls = $page->urlsToPaths($urls);
|
|
|
|
|
2008-12-15 02:31:01 +01:00
|
|
|
$start = isset($_GET['start']) ? $_GET['start'] : 0;
|
|
|
|
$count = isset($_GET['count']) ? $_GET['count'] : sizeof($urls);
|
|
|
|
if(($start + $count) > sizeof($urls)) $count = sizeof($urls) - $start;
|
|
|
|
|
|
|
|
$urls = array_slice($urls, $start, $count);
|
|
|
|
|
2009-01-10 12:36:30 +01:00
|
|
|
if(!isset($_GET['urls']) && $start == 0 && file_exists("../cache")) {
|
2009-07-23 00:57:02 +02:00
|
|
|
echo "Removing stale cache files... \n";
|
2009-01-10 12:36:30 +01:00
|
|
|
flush();
|
2009-07-23 00:57:02 +02:00
|
|
|
$cacheBaseDir = $page->getDestDir();
|
|
|
|
|
|
|
|
if (FilesystemPublisher::$domain_based_caching) {
|
|
|
|
// Glob each dir, then glob each one of those
|
|
|
|
foreach(glob('../cache/*', GLOB_ONLYDIR) as $cacheDir) {
|
|
|
|
foreach(glob($cacheDir.'/*') as $cacheFile) {
|
|
|
|
$searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/');
|
|
|
|
if (!in_array($searchCacheFile, $mappedUrls)) {
|
|
|
|
echo " * Deleting $cacheFile\n";
|
|
|
|
@unlink($cacheFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-12 04:59:27 +02:00
|
|
|
echo "done.\n\n";
|
|
|
|
}
|
2009-07-23 00:57:02 +02:00
|
|
|
echo "Rebuilding cache from " . sizeof($mappedUrls) . " urls...\n\n";
|
2009-10-12 05:39:44 +02:00
|
|
|
$page->extend('publishPages', $mappedUrls);
|
2008-08-12 04:59:27 +02:00
|
|
|
echo "\n\n== Done! ==";
|
|
|
|
}
|
2008-12-15 02:31:01 +01:00
|
|
|
|
|
|
|
function show() {
|
|
|
|
$urls = singleton('Page')->allPagesToCache();
|
|
|
|
echo "<pre>\n";
|
|
|
|
print_r($urls);
|
|
|
|
echo "\n</pre>";
|
|
|
|
}
|
2008-08-12 04:59:27 +02:00
|
|
|
}
|