Took me a while to realize that register couldn't find my class because I used an underscore in the classname/file :-( I'll not make that mistake again. OK, this is still very beta, all comments are very welcome. So for my first StaticPublisher batch action here goes:
<?php
/**
* Batch action to create static pages. Pages are not republished to database
*
* Register the batchaction by adding this to your _config.php:
* CMSBatchActionHandler::register('createstatic', 'CMSBatchActionCreateStatic');
*
* @author M. Bloem, Balbus Design
*
* @TODO remove (selected) pages from cache that are no longer valid
*/
class CMSBatchActionCreateStatic extends CMSBatchAction {
function getActionTitle() {
return _t('CMSBatchActionCreateStatic.CREATE_STATIC_PAGES', 'Create static pages');
}
function getDoingText() {
return _t('CMSBatchActionCreateStatic.CREATING_STATIC_PAGES', 'Creating static pages');
}
function run(DataObjectSet $pages) {
return $this->batchaction(
$pages,
null,
'');
}
public function batchaction(DataObjectSet $pages, $helperMethod, $successMessage)
{
// start with a page object so you can use its methods
$pageObject = singleton('Page');
// check if the StaticPublisher extension is enabled
if (!$pageObject->hasMethod('publishPages')) {
$message = _t('CMSBatchActionCreateStatic.NO_STATICPUBLISHER','StaticPublisher not enabled');
}
// check if the allPagesToCache method exists in the page, that defines
// which url's may be cached
elseif (!$pageObject->hasMethod('allPagesToCache')) {
$message = _t('CMSBatchActionCreateStatic.NO_ALLPAGESTOCACHE',
'AllPagesToCache method not defined');
}
else {
$urls = array();
$cacheableUrls = $pageObject->allPagesToCache();
// check if the page can be cached
foreach($pages as $page) {
// Need the details of the live page
$livePage = Versioned::get_one_by_stage(
'SiteTree', 'Live', "`SiteTree_Live`.`ID` = {$page->ID}");
if (!empty($livePage) && $livePage->Status == 'Published' &&
$livePage->canView()) {
$url = $livePage->Link();
// check if this page belongs to the cacheable ones
if (in_array($url, $cacheableUrls)) {
// make it a relative url without trailing slash
$url = Director::makeRelative($url);
if(substr($url,-1) == '/') $url = substr($url,0,-1);
$urls[] = $url;
}
}
$page->destroy();
unset($page);
}
// if there's still some urls left to process...
if (!empty($urls)) {
// call FileSystemPublisher::publishPages
$pageObject->publishPages($urls);
$pageObject->destroy();
unset($pageObject);
// this message assumes that nothing went wrong during publishing
// must check this
$message = sprintf(_t('CMSBatchActionCreateStatic.CREATED_STATIC_PAGES',
'Processed %d page(s) and created %d static file(s)'),
$pages->Count(),count($urls));
FormResponse::add('statusMessage("'.$message.'","good");');
}
else {
$message = _t('CMSBatchActionCreateStatic.NO_STATICPAGES','No static pages to create');
FormResponse::add('statusMessage("'.$message.'","good");');
}
}
return FormResponse::respond();
}
}
?>
I have a dutch translation for whoever might want it...