2012-09-21 08:03:20 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-09-25 10:02:26 +02:00
|
|
|
* @package staticpublisher
|
2012-09-21 08:03:20 +02:00
|
|
|
*/
|
2012-09-21 09:49:46 +02:00
|
|
|
class RebuildStaticCacheTask extends BuildTask {
|
2012-09-21 08:03:20 +02:00
|
|
|
|
2012-09-21 09:49:46 +02:00
|
|
|
public function run($request) {
|
2013-05-24 09:46:15 +02:00
|
|
|
Config::inst()->update('StaticPublisher', 'echo_progress', true);
|
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
$page = singleton('Page');
|
2013-04-06 07:35:08 +02:00
|
|
|
|
2012-09-21 08:03:20 +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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-06 07:35:08 +02:00
|
|
|
if(!empty($_GET['urls'])) {
|
|
|
|
$urls = $_GET['urls'];
|
|
|
|
} else {
|
|
|
|
$urls = $page->allPagesToCache();
|
|
|
|
}
|
2012-09-21 08:03:20 +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.
|
|
|
|
* @param bool $removeAll Remove all stale cache files (default TRUE).
|
|
|
|
*/
|
2013-04-06 07:35:08 +02:00
|
|
|
public function rebuildCache($urls, $removeAll = true) {
|
2012-09-21 08:03:20 +02:00
|
|
|
|
|
|
|
if(!is_array($urls)) {
|
|
|
|
// $urls must be an array
|
|
|
|
user_error("Rebuild cache must be passed an array of urls. Make sure your allPagesToCache function returns an array", E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!Director::is_cli()) echo "<pre>\n";
|
2013-04-06 07:35:08 +02:00
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
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');
|
|
|
|
$cacheBaseDir = $page->getDestDir();
|
|
|
|
|
|
|
|
if(!file_exists($cacheBaseDir)) {
|
|
|
|
Filesystem::makeFolder($cacheBaseDir);
|
|
|
|
}
|
|
|
|
|
2013-04-06 07:35:08 +02:00
|
|
|
if (file_exists($cacheBaseDir.'/lock') && !isset($_REQUEST['force'])) {
|
|
|
|
die("There already appears to be a publishing queue running. You can skip warning this by adding ?/&force to the URL.");
|
|
|
|
}
|
2012-09-21 08:03:20 +02:00
|
|
|
|
|
|
|
touch($cacheBaseDir.'/lock');
|
|
|
|
|
|
|
|
// Note: Similiar to StaticPublisher->republish()
|
|
|
|
foreach($urls as $i => $url) {
|
|
|
|
if($url && !is_string($url)) {
|
|
|
|
user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$urls[$i] = $url;
|
|
|
|
}
|
2013-04-06 07:35:08 +02:00
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
$urls = array_unique($urls);
|
|
|
|
sort($urls);
|
|
|
|
|
|
|
|
$start = isset($_GET['start']) ? $_GET['start'] : 0;
|
|
|
|
$count = isset($_GET['count']) ? $_GET['count'] : sizeof($urls);
|
2013-04-06 07:35:08 +02:00
|
|
|
|
|
|
|
if(($start + $count) > sizeof($urls)) {
|
|
|
|
$count = sizeof($urls) - $start;
|
|
|
|
}
|
2012-09-21 08:03:20 +02:00
|
|
|
|
|
|
|
$urls = array_slice($urls, $start, $count);
|
|
|
|
|
2014-02-28 12:14:42 +01:00
|
|
|
$mappedUrls = $page->urlsToPaths($urls);
|
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
if($removeAll && !isset($_GET['urls']) && $start == 0 && file_exists("../cache")) {
|
|
|
|
echo "Removing stale cache files... \n";
|
|
|
|
flush();
|
2013-04-06 07:35:08 +02:00
|
|
|
|
|
|
|
if (Config::inst()->get('FilesystemPublisher', 'domain_based_caching')) {
|
2012-09-21 08:03:20 +02:00
|
|
|
// Glob each dir, then glob each one of those
|
|
|
|
foreach(glob(BASE_PATH . '/cache/*', GLOB_ONLYDIR) as $cacheDir) {
|
|
|
|
foreach(glob($cacheDir.'/*') as $cacheFile) {
|
|
|
|
$searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/');
|
2012-09-21 09:49:46 +02:00
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
if (!in_array($searchCacheFile, $mappedUrls)) {
|
|
|
|
echo " * Deleting $cacheFile\n";
|
|
|
|
@unlink($cacheFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-06 07:35:08 +02:00
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
echo "done.\n\n";
|
|
|
|
}
|
2013-04-06 07:35:08 +02:00
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
echo "Rebuilding cache from " . sizeof($mappedUrls) . " urls...\n\n";
|
2013-04-06 07:35:08 +02:00
|
|
|
|
2012-09-21 08:03:20 +02:00
|
|
|
$page->extend('publishPages', $mappedUrls);
|
|
|
|
|
2013-04-06 07:35:08 +02:00
|
|
|
if (file_exists($cacheBaseDir.'/lock')) {
|
|
|
|
unlink($cacheBaseDir.'/lock');
|
|
|
|
}
|
2012-09-21 08:03:20 +02:00
|
|
|
|
2013-05-24 08:21:51 +02:00
|
|
|
echo "\n\n== Done! ==\n";
|
2012-09-21 08:03:20 +02:00
|
|
|
}
|
|
|
|
}
|