PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/tasks/RebuildStaticCacheTask.php

https://bitbucket.org/mwuits/mockup_nachz
PHP | 126 lines | 82 code | 28 blank | 16 comment | 28 complexity | b5078e10fa193303a3261e18105f318a MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-2.0, AGPL-1.0, LGPL-2.1, LGPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. * @package cms
  4. * @subpackage tasks
  5. *
  6. * @todo Make this use the Task interface once it gets merged back into trunk
  7. */
  8. class RebuildStaticCacheTask extends Controller {
  9. static $allowed_actions = array(
  10. 'index',
  11. );
  12. function init() {
  13. parent::init();
  14. Versioned::reading_stage('live');
  15. $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
  16. if(!$canAccess) return Security::permissionFailure($this);
  17. }
  18. function index() {
  19. StaticPublisher::set_echo_progress(true);
  20. $page = singleton('Page');
  21. if(!$page->hasMethod('allPagesToCache')) {
  22. user_error(
  23. 'RebuildStaticCacheTask::index(): Please define a method "allPagesToCache()" on your Page class to return all pages affected by a cache refresh.',
  24. E_USER_ERROR
  25. );
  26. }
  27. if(!empty($_GET['urls'])) $urls = $_GET['urls'];
  28. else $urls = $page->allPagesToCache();
  29. $this->rebuildCache($urls, true);
  30. }
  31. /**
  32. * Rebuilds the static cache for the pages passed through via $urls
  33. *
  34. * @param array $urls The URLs of pages to re-fetch and cache.
  35. * @param bool $removeAll Remove all stale cache files (default TRUE).
  36. */
  37. function rebuildCache($urls, $removeAll = true) {
  38. if(!is_array($urls)) {
  39. // $urls must be an array
  40. user_error("Rebuild cache must be passed an array of urls. Make sure your allPagesToCache function returns an array", E_USER_ERROR);
  41. 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. $page = singleton('Page');
  46. $cacheBaseDir = $page->getDestDir();
  47. if(!file_exists($cacheBaseDir)) {
  48. Filesystem::makeFolder($cacheBaseDir);
  49. }
  50. 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.");
  51. touch($cacheBaseDir.'/lock');
  52. // Note: Similiar to StaticPublisher->republish()
  53. foreach($urls as $i => $url) {
  54. if($url && !is_string($url)) {
  55. user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
  56. continue;
  57. }
  58. // Remove leading slashes from all URLs (apart from the homepage)
  59. if(substr($url,-1) == '/' && $url != '/') $url = substr($url,0,-1);
  60. $urls[$i] = $url;
  61. }
  62. $urls = array_unique($urls);
  63. sort($urls);
  64. $mappedUrls = $page->urlsToPaths($urls);
  65. $start = isset($_GET['start']) ? $_GET['start'] : 0;
  66. $count = isset($_GET['count']) ? $_GET['count'] : sizeof($urls);
  67. if(($start + $count) > sizeof($urls)) $count = sizeof($urls) - $start;
  68. $urls = array_slice($urls, $start, $count);
  69. if($removeAll && !isset($_GET['urls']) && $start == 0 && file_exists("../cache")) {
  70. echo "Removing stale cache files... \n";
  71. flush();
  72. if (FilesystemPublisher::$domain_based_caching) {
  73. // Glob each dir, then glob each one of those
  74. foreach(glob(BASE_PATH . '/cache/*', GLOB_ONLYDIR) as $cacheDir) {
  75. foreach(glob($cacheDir.'/*') as $cacheFile) {
  76. $searchCacheFile = trim(str_replace($cacheBaseDir, '', $cacheFile), '\/');
  77. if (!in_array($searchCacheFile, $mappedUrls)) {
  78. echo " * Deleting $cacheFile\n";
  79. @unlink($cacheFile);
  80. }
  81. }
  82. }
  83. } else {
  84. }
  85. echo "done.\n\n";
  86. }
  87. echo "Rebuilding cache from " . sizeof($mappedUrls) . " urls...\n\n";
  88. $page->extend('publishPages', $mappedUrls);
  89. if (file_exists($cacheBaseDir.'/lock')) unlink($cacheBaseDir.'/lock');
  90. echo "\n\n== Done! ==";
  91. }
  92. function show() {
  93. $urls = singleton('Page')->allPagesToCache();
  94. echo "<pre>\n";
  95. print_r($urls);
  96. echo "\n</pre>";
  97. }
  98. }