Skip to content

Commit

Permalink
NEW Add quiet mode to RebuildStaticCacheTask
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Mar 6, 2015
1 parent 52f04b7 commit c8d1b4b
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions code/tasks/RebuildStaticCacheTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
class RebuildStaticCacheTask extends BuildTask {

private static $quiet = false;

public function run($request) {
Config::inst()->update('StaticPublisher', 'echo_progress', true);

Expand All @@ -25,6 +27,16 @@ public function run($request) {

$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
Expand All @@ -40,9 +52,10 @@ public function rebuildCache($urls, $removeAll = true) {
return;
};

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";
if(!Director::is_cli()) {
$this->log("<pre>\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');
$cacheBaseDir = $page->getDestDir();
Expand All @@ -51,8 +64,12 @@ public function rebuildCache($urls, $removeAll = true) {
Filesystem::makeFolder($cacheBaseDir);
}

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.");
$quiet = Config::inst()->get('RebuildStaticCacheTask', 'quiet');

if(!$quiet) {
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.");
}
}

touch($cacheBaseDir.'/lock');
Expand Down Expand Up @@ -82,7 +99,8 @@ public function rebuildCache($urls, $removeAll = true) {
$mappedUrls = $page->urlsToPaths($urls);

if($removeAll && !isset($_GET['urls']) && $start == 0 && file_exists("../cache")) {
echo "Removing stale cache files... \n";
$this->log("Removing stale cache files... \n");

flush();

if (Config::inst()->get('FilesystemPublisher', 'domain_based_caching')) {
Expand All @@ -92,24 +110,24 @@ public function rebuildCache($urls, $removeAll = true) {
$searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/');

if (!in_array($searchCacheFile, $mappedUrls)) {
echo " * Deleting $cacheFile\n";
$this->log(" * Deleting $cacheFile\n");

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

if (file_exists($cacheBaseDir.'/lock')) {
unlink($cacheBaseDir.'/lock');
}

echo "\n\n== Done! ==\n";
$this->log("\n\n== Done! ==\n");
}
}

0 comments on commit c8d1b4b

Please sign in to comment.