NEW Add quiet mode to RebuildStaticCacheTask

This commit is contained in:
Will Rossiter 2015-03-07 00:22:45 +13:00
parent 52f04b7435
commit c8d1b4b2e4

View File

@ -4,6 +4,8 @@
*/ */
class RebuildStaticCacheTask extends BuildTask { class RebuildStaticCacheTask extends BuildTask {
private static $quiet = false;
public function run($request) { public function run($request) {
Config::inst()->update('StaticPublisher', 'echo_progress', true); Config::inst()->update('StaticPublisher', 'echo_progress', true);
@ -26,6 +28,16 @@ class RebuildStaticCacheTask extends BuildTask {
$this->rebuildCache($urls, true); $this->rebuildCache($urls, true);
} }
public function log($message) {
$quiet = Config::inst()->get('RebuildStaticCacheTask', 'quiet');
if($quiet) {
return;
}
echo $message;
}
/** /**
* Rebuilds the static cache for the pages passed through via $urls * Rebuilds the static cache for the pages passed through via $urls
* *
@ -40,9 +52,10 @@ class RebuildStaticCacheTask extends BuildTask {
return; return;
}; };
if(!Director::is_cli()) echo "<pre>\n"; if(!Director::is_cli()) {
$this->log("<pre>\n");
echo "Rebuilding cache.\nNOTE: Please ensure that this page ends with 'Done!' - if not, then something may have gone wrong.\n\n"; $this->log("Rebuilding cache.\nNOTE: Please ensure that this page ends with 'Done!' - if not, then something may have gone wrong.\n\n");
}
$page = singleton('Page'); $page = singleton('Page');
$cacheBaseDir = $page->getDestDir(); $cacheBaseDir = $page->getDestDir();
@ -51,9 +64,13 @@ class RebuildStaticCacheTask extends BuildTask {
Filesystem::makeFolder($cacheBaseDir); Filesystem::makeFolder($cacheBaseDir);
} }
$quiet = Config::inst()->get('RebuildStaticCacheTask', 'quiet');
if(!$quiet) {
if (file_exists($cacheBaseDir.'/lock') && !isset($_REQUEST['force'])) { 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."); die("There already appears to be a publishing queue running. You can skip warning this by adding ?/&force to the URL.");
} }
}
touch($cacheBaseDir.'/lock'); touch($cacheBaseDir.'/lock');
@ -82,7 +99,8 @@ class RebuildStaticCacheTask extends BuildTask {
$mappedUrls = $page->urlsToPaths($urls); $mappedUrls = $page->urlsToPaths($urls);
if($removeAll && !isset($_GET['urls']) && $start == 0 && file_exists("../cache")) { if($removeAll && !isset($_GET['urls']) && $start == 0 && file_exists("../cache")) {
echo "Removing stale cache files... \n"; $this->log("Removing stale cache files... \n");
flush(); flush();
if (Config::inst()->get('FilesystemPublisher', 'domain_based_caching')) { if (Config::inst()->get('FilesystemPublisher', 'domain_based_caching')) {
@ -92,24 +110,24 @@ class RebuildStaticCacheTask extends BuildTask {
$searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/'); $searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/');
if (!in_array($searchCacheFile, $mappedUrls)) { if (!in_array($searchCacheFile, $mappedUrls)) {
echo " * Deleting $cacheFile\n"; $this->log(" * Deleting $cacheFile\n");
@unlink($cacheFile); @unlink($cacheFile);
} }
} }
} }
} }
echo "done.\n\n"; $this->log("done.\n\n");
} }
echo "Rebuilding cache from " . sizeof($mappedUrls) . " urls...\n\n"; $this->log("Rebuilding cache from " . sizeof($mappedUrls) . " urls...\n\n");
$page->extend('publishPages', $mappedUrls); $page->extend('publishPages', $mappedUrls);
if (file_exists($cacheBaseDir.'/lock')) { if (file_exists($cacheBaseDir.'/lock')) {
unlink($cacheBaseDir.'/lock'); unlink($cacheBaseDir.'/lock');
} }
echo "\n\n== Done! ==\n"; $this->log("\n\n== Done! ==\n");
} }
} }