silverstripe-cms/code/staticpublisher/StaticPublisher.php
Sam Minnee ab776401db Static caching merges from dnc branch
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@68901 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-12-15 01:31:01 +00:00

84 lines
2.1 KiB
PHP

<?php
abstract class StaticPublisher extends DataObjectDecorator {
/**
* Defines whether to output information about publishing or not. By
* default, this is off, and should be turned on when you want debugging
* (for example, in a cron task)
*/
static $echo_progress = false;
abstract function publishPages($pages);
static function echo_progress() {
return (boolean)self::$echo_progress;
}
/**
* Either turns on (boolean true) or off (boolean false) the progress indicators.
* @see StaticPublisher::$echo_progress
*/
static function set_echo_progress($progress) {
self::$echo_progress = (boolean)$progress;
}
function onAfterPublish($original) {
if($this->owner->hasMethod('pagesAffectedByChanges')) {
$urls = $this->owner->pagesAffectedByChanges($original);
} else {
// $pages = array(Versioned::get_one_by_stage('SiteTree', 'Live', "\"SiteTree\".ID = {$this->owner->ID}"));
$pages = Versioned::get_by_stage('SiteTree', 'Live', '', '', 10);
foreach($pages as $page) {
$urls[] = $page->Link();
}
}
foreach($urls as $i => $url) {
if(!is_string($url)) {
user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
continue;
}
$url = Director::makeRelative($url);
if(substr($url,-1) == '/') $url = substr($url,0,-1);
$urls[$i] = $url;
}
$urls = array_unique($urls);
$this->publishPages($urls);
}
/**
* Get all external references to CSS, JS,
*/
function externalReferencesFor($content) {
$CLI_content = escapeshellarg($content);
$tidy = `echo $CLI_content | tidy -numeric -asxhtml`;
$tidy = preg_replace('/xmlns="[^"]+"/','', $tidy);
$xContent = new SimpleXMLElement($tidy);
//Debug::message($xContent->asXML());
$xlinks = array(
"//link[@rel='stylesheet']/@href" => false,
"//script/@src" => false,
"//img/@src" => false,
"//a/@href" => true,
);
$urls = array();
foreach($xlinks as $xlink => $assetsOnly) {
$matches = $xContent->xpath($xlink);
if($matches) foreach($matches as $item) {
$url = $item . '';
if($assetsOnly && substr($url,0,7) != 'assets/') continue;
$urls[] = $url;
}
}
return $urls;
}
}
?>