1 <?php
2 3 4 5 6 7
8 class RebuildStaticCacheTask extends Controller {
9 function init() {
10 parent::init();
11
12 Versioned::reading_stage('live');
13
14 $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
15 if(!$canAccess) return Security::permissionFailure($this);
16 }
17
18 function index() {
19 StaticPublisher::set_echo_progress(true);
20
21 $page = singleton('Page');
22 if(!$page->hasMethod('allPagesToCache')) {
23 user_error(
24 'RebuildStaticCacheTask::index(): Please define a method "allPagesToCache()" on your Page class to return all pages affected by a cache refresh.',
25 E_USER_ERROR
26 );
27 }
28
29
30 if(!empty($_GET['urls'])) $urls = $_GET['urls'];
31 else $urls = $page->allPagesToCache();
32
33 $this->rebuildCache($urls, true);
34 }
35
36 37 38 39
40 function rebuildCache($urls, $removeAll = true) {
41 if(!is_array($urls)) return;
42
43 if(!Director::is_cli()) echo "<pre>\n";
44 echo "Rebuilding cache.\nNOTE: Please ensure that this page ends with 'Done!' - if not, then something may have gone wrong.\n\n";
45
46 $page = singleton('Page');
47 $cacheBaseDir = $page->getDestDir();
48
49 if(!file_exists($cacheBaseDir)) {
50 Filesystem::makeFolder($cacheBaseDir);
51 }
52
53 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.");
54
55 touch($cacheBaseDir.'/lock');
56
57
58 foreach($urls as $i => $url) {
59 if($url && !is_string($url)) {
60 user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
61 continue;
62 }
63
64
65 if(substr($url,-1) == '/' && $url != '/') $url = substr($url,0,-1);
66
67 $urls[$i] = $url;
68 }
69 $urls = array_unique($urls);
70 sort($urls);
71
72 $mappedUrls = $page->urlsToPaths($urls);
73
74 $start = isset($_GET['start']) ? $_GET['start'] : 0;
75 $count = isset($_GET['count']) ? $_GET['count'] : sizeof($urls);
76 if(($start + $count) > sizeof($urls)) $count = sizeof($urls) - $start;
77
78 $urls = array_slice($urls, $start, $count);
79
80 if(!isset($_GET['urls']) && $start == 0 && file_exists("../cache")) {
81 echo "Removing stale cache files... \n";
82 flush();
83 if (FilesystemPublisher::$domain_based_caching) {
84
85 foreach(glob(BASE_PATH . '/cache/*', GLOB_ONLYDIR) as $cacheDir) {
86 foreach(glob($cacheDir.'/*') as $cacheFile) {
87 $searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/');
88 if (!in_array($searchCacheFile, $mappedUrls)) {
89 echo " * Deleting $cacheFile\n";
90 @unlink($cacheFile);
91 }
92 }
93 }
94 } else {
95
96 }
97
98 echo "done.\n\n";
99 }
100 echo "Rebuilding cache from " . sizeof($mappedUrls) . " urls...\n\n";
101 $page->extend('publishPages', $mappedUrls);
102
103 if (file_exists($cacheBaseDir.'/lock')) unlink($cacheBaseDir.'/lock');
104
105 echo "\n\n== Done! ==";
106 }
107
108 function show() {
109 $urls = singleton('Page')->allPagesToCache();
110 echo "<pre>\n";
111 print_r($urls);
112 echo "\n</pre>";
113 }
114 }
115
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-