2008-08-12 04:59:27 +02:00
< ? php
/**
2009-03-22 23:58:18 +01:00
* @ package cms
* @ subpackage tasks
*
2008-08-12 04:59:27 +02:00
* @ todo Make this use the Task interface once it gets merged back into trunk
*/
class RebuildStaticCacheTask extends Controller {
2011-12-18 18:19:55 +01:00
static $allowed_actions = array (
'index' ,
);
2008-08-12 04:59:27 +02:00
function init () {
2009-09-10 03:49:07 +02:00
parent :: init ();
2008-12-15 02:31:01 +01:00
Versioned :: reading_stage ( 'live' );
2009-09-10 03:49:07 +02:00
$canAccess = ( Director :: isDev () || Director :: is_cli () || Permission :: check ( " ADMIN " ));
if ( ! $canAccess ) return Security :: permissionFailure ( $this );
2008-08-12 04:59:27 +02:00
}
function index () {
StaticPublisher :: set_echo_progress ( true );
$page = singleton ( 'Page' );
2008-09-29 00:41:02 +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
);
}
2009-01-05 07:17:59 +01:00
if ( ! empty ( $_GET [ 'urls' ])) $urls = $_GET [ 'urls' ];
2008-08-12 04:59:27 +02:00
else $urls = $page -> allPagesToCache ();
2008-12-15 02:31:01 +01:00
2008-08-12 04:59:27 +02:00
$this -> rebuildCache ( $urls , true );
}
/**
* Rebuilds the static cache for the pages passed through via $urls
2010-10-15 05:41:44 +02:00
*
2008-08-12 04:59:27 +02:00
* @ param array $urls The URLs of pages to re - fetch and cache .
2010-10-15 05:41:44 +02:00
* @ param bool $removeAll Remove all stale cache files ( default TRUE ) .
2008-08-12 04:59:27 +02:00
*/
function rebuildCache ( $urls , $removeAll = true ) {
2010-10-15 05:41:38 +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 ;
};
2008-08-12 04:59:27 +02:00
if ( ! Director :: is_cli ()) echo " <pre> \n " ;
echo " Rebuilding cache. \n NOTE: Please ensure that this page ends with 'Done!' - if not, then something may have gone wrong. \n \n " ;
2010-10-15 05:41:38 +02:00
2008-08-12 04:59:27 +02:00
$page = singleton ( 'Page' );
2010-04-14 04:14:34 +02:00
$cacheBaseDir = $page -> getDestDir ();
2010-10-13 06:19:38 +02:00
if ( ! file_exists ( $cacheBaseDir )) {
2010-10-13 06:19:46 +02:00
Filesystem :: makeFolder ( $cacheBaseDir );
2010-10-13 06:19:38 +02:00
}
2010-04-14 04:14:34 +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. " );
touch ( $cacheBaseDir . '/lock' );
2008-08-12 04:59:27 +02:00
2010-10-15 03:31:54 +02:00
// Note: Similiar to StaticPublisher->republish()
2008-08-12 04:59:27 +02:00
foreach ( $urls as $i => $url ) {
2008-12-15 02:31:01 +01:00
if ( $url && ! is_string ( $url )) {
user_error ( " Bad URL: " . var_export ( $url , true ), E_USER_WARNING );
continue ;
}
2010-10-15 03:31:54 +02:00
// Remove leading slashes from all URLs (apart from the homepage)
if ( substr ( $url , - 1 ) == '/' && $url != '/' ) $url = substr ( $url , 0 , - 1 );
2009-07-13 06:42:02 +02:00
$urls [ $i ] = $url ;
2008-08-12 04:59:27 +02:00
}
$urls = array_unique ( $urls );
2008-12-15 02:31:01 +01:00
sort ( $urls );
2009-07-23 00:57:02 +02:00
$mappedUrls = $page -> urlsToPaths ( $urls );
2008-12-15 02:31:01 +01:00
$start = isset ( $_GET [ 'start' ]) ? $_GET [ 'start' ] : 0 ;
$count = isset ( $_GET [ 'count' ]) ? $_GET [ 'count' ] : sizeof ( $urls );
if (( $start + $count ) > sizeof ( $urls )) $count = sizeof ( $urls ) - $start ;
$urls = array_slice ( $urls , $start , $count );
2010-10-15 05:41:44 +02:00
if ( $removeAll && ! isset ( $_GET [ 'urls' ]) && $start == 0 && file_exists ( " ../cache " )) {
2009-07-23 00:57:02 +02:00
echo " Removing stale cache files... \n " ;
2009-01-10 12:36:30 +01:00
flush ();
2009-07-23 00:57:02 +02:00
if ( FilesystemPublisher :: $domain_based_caching ) {
// Glob each dir, then glob each one of those
2010-04-14 00:24:12 +02:00
foreach ( glob ( BASE_PATH . '/cache/*' , GLOB_ONLYDIR ) as $cacheDir ) {
2009-07-23 00:57:02 +02:00
foreach ( glob ( $cacheDir . '/*' ) as $cacheFile ) {
$searchCacheFile = trim ( str_replace ( $cacheBaseDir , '' , $cacheFile ), '\/' );
if ( ! in_array ( $searchCacheFile , $mappedUrls )) {
echo " * Deleting $cacheFile\n " ;
@ unlink ( $cacheFile );
}
}
}
} else {
}
2008-08-12 04:59:27 +02:00
echo " done. \n \n " ;
}
2009-07-23 00:57:02 +02:00
echo " Rebuilding cache from " . sizeof ( $mappedUrls ) . " urls... \n \n " ;
2009-10-12 05:39:44 +02:00
$page -> extend ( 'publishPages' , $mappedUrls );
2010-04-14 04:14:34 +02:00
if ( file_exists ( $cacheBaseDir . '/lock' )) unlink ( $cacheBaseDir . '/lock' );
2008-08-12 04:59:27 +02:00
echo " \n \n == Done! == " ;
}
2008-12-15 02:31:01 +01:00
function show () {
$urls = singleton ( 'Page' ) -> allPagesToCache ();
echo " <pre> \n " ;
print_r ( $urls );
echo " \n </pre> " ;
}
2008-08-12 04:59:27 +02:00
}