silverstripe-cms/code/staticpublisher/StaticPublisher.php
Ingo Schommer 653d162939 MINOR pingStatus ping frequency reduced
MINOR updated lang file for SiteConfig
MINOR improved documentation in StaticPublisher
MINOR improved documentation in CMSMain surrounding reports and siteconfig
MINOR migrated headers in SiteConfig to i18n'd (from r86429) (from r99112)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@106135 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-06-03 01:34:52 +00:00

140 lines
3.4 KiB
PHP

<?php
/**
* @package cms
* @subpackage publishers
*/
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;
/**
* Realtime static publishing... the second a page
* is saved, it is written to the cache
*/
// @todo this is currently disabled until template problems can be fixed
static $disable_realtime = true;
abstract function publishPages($pages);
abstract function unpublishPages($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;
}
/**
* Called after a page is published.
*/
function onAfterPublish($original) {
$this->republish($original);
}
/**
* Called after link assets have been renamed, and the live site has been updated, without
* an actual publish event.
*
* Only called if the published content exists and has been modified.
*/
function onRenameLinkedAsset($original) {
$this->republish($original);
}
function republish($original) {
if (self::$disable_realtime) return;
$urls = array();
if($this->owner->hasMethod('pagesAffectedByChanges')) {
$urls = $this->owner->pagesAffectedByChanges($original);
} else {
$pages = Versioned::get_by_stage('SiteTree', 'Live', '', '', '', 10);
if($pages) {
foreach($pages as $page) {
$urls[] = $page->AbsoluteLink();
}
}
}
foreach($urls as $i => $url) {
if(!is_string($url)) {
user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
continue;
}
if(substr($url,-1) == '/') $url = substr($url,0,-1);
$urls[$i] = $url;
}
$urls = array_unique($urls);
$this->publishPages($urls);
}
/**
* On after unpublish, get changes and hook into underlying
* functionality
*/
function onAfterUnpublish($page) {
if (self::$disable_realtime) return;
// Get the affected URLs
if($this->owner->hasMethod('pagesAffectedByUnpublishing')) {
$urls = $this->owner->pagesAffectedByUnpublishing();
$urls = array_unique($urls);
} else {
$urls = array($this->owner->AbsoluteLink());
}
$legalPages = singleton('Page')->allPagesToCache();
$urlsToRepublish = array_intersect($urls, $legalPages);
$urlsToUnpublish = array_diff($urls, $legalPages);
$this->unpublishPages($urlsToUnpublish);
$this->publishPages($urlsToRepublish);
}
/**
* 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;
}
}
?>