2008-08-12 04:59:27 +02:00
|
|
|
<?php
|
2009-02-04 21:18:12 +01:00
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage publishers
|
|
|
|
*/
|
2008-08-12 04:59:27 +02:00
|
|
|
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;
|
|
|
|
|
2010-02-16 04:36:58 +01:00
|
|
|
/**
|
|
|
|
* Realtime static publishing... the second a page
|
|
|
|
* is saved, it is written to the cache
|
|
|
|
*/
|
2010-04-20 04:15:59 +02:00
|
|
|
static $disable_realtime = false;
|
2009-10-16 00:44:14 +02:00
|
|
|
|
2008-08-12 04:59:27 +02:00
|
|
|
abstract function publishPages($pages);
|
2009-07-23 00:57:02 +02:00
|
|
|
abstract function unpublishPages($pages);
|
2008-08-12 04:59:27 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-01-13 01:00:33 +01:00
|
|
|
/**
|
|
|
|
* Called after a page is published.
|
|
|
|
*/
|
2008-08-12 04:59:27 +02:00
|
|
|
function onAfterPublish($original) {
|
2010-01-13 01:00:33 +01:00
|
|
|
$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) {
|
2009-10-16 00:44:14 +02:00
|
|
|
if (self::$disable_realtime) return;
|
2010-01-13 01:00:33 +01:00
|
|
|
|
2009-04-06 03:28:03 +02:00
|
|
|
$urls = array();
|
|
|
|
|
2008-08-12 04:59:27 +02:00
|
|
|
if($this->owner->hasMethod('pagesAffectedByChanges')) {
|
|
|
|
$urls = $this->owner->pagesAffectedByChanges($original);
|
|
|
|
} else {
|
2009-04-06 03:28:03 +02:00
|
|
|
$pages = Versioned::get_by_stage('SiteTree', 'Live', '', '', '', 10);
|
|
|
|
if($pages) {
|
|
|
|
foreach($pages as $page) {
|
2009-07-13 06:42:02 +02:00
|
|
|
$urls[] = $page->AbsoluteLink();
|
2009-04-06 03:28:03 +02:00
|
|
|
}
|
2008-08-12 04:59:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($urls as $i => $url) {
|
2008-12-15 02:31:01 +01:00
|
|
|
if(!is_string($url)) {
|
|
|
|
user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-12 04:59:27 +02:00
|
|
|
if(substr($url,-1) == '/') $url = substr($url,0,-1);
|
|
|
|
$urls[$i] = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$urls = array_unique($urls);
|
|
|
|
|
|
|
|
$this->publishPages($urls);
|
|
|
|
}
|
2009-07-23 00:57:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* On after unpublish, get changes and hook into underlying
|
|
|
|
* functionality
|
|
|
|
*/
|
|
|
|
function onAfterUnpublish($page) {
|
2009-10-16 00:44:14 +02:00
|
|
|
if (self::$disable_realtime) return;
|
|
|
|
|
2010-01-13 01:03:05 +01:00
|
|
|
// Get the affected URLs
|
2009-07-23 00:57:02 +02:00
|
|
|
if($this->owner->hasMethod('pagesAffectedByUnpublishing')) {
|
|
|
|
$urls = $this->owner->pagesAffectedByUnpublishing();
|
|
|
|
$urls = array_unique($urls);
|
|
|
|
} else {
|
|
|
|
$urls = array($this->owner->AbsoluteLink());
|
|
|
|
}
|
|
|
|
|
2010-01-13 01:03:28 +01:00
|
|
|
$legalPages = singleton('Page')->allPagesToCache();
|
2010-01-13 01:03:05 +01:00
|
|
|
|
|
|
|
$urlsToRepublish = array_intersect($urls, $legalPages);
|
|
|
|
$urlsToUnpublish = array_diff($urls, $legalPages);
|
|
|
|
|
|
|
|
$this->unpublishPages($urlsToUnpublish);
|
|
|
|
$this->publishPages($urlsToRepublish);
|
2009-07-23 00:57:02 +02:00
|
|
|
}
|
2008-08-12 04:59:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:38:58 +01:00
|
|
|
|
2008-12-15 02:31:01 +01:00
|
|
|
?>
|