2012-09-21 07:47:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-09-25 10:02:26 +02:00
|
|
|
* @package staticpublisher
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
|
|
|
abstract class StaticPublisher extends DataExtension {
|
2013-05-24 09:46:15 +02:00
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
/**
|
|
|
|
* Defines whether to output information about publishing or not. By
|
|
|
|
* default, this is off, and should be turned on when you want debugging
|
2012-09-25 10:02:26 +02:00
|
|
|
* (for example, in a cron task).
|
|
|
|
*
|
|
|
|
* @var boolean
|
2013-05-24 09:46:15 +02:00
|
|
|
*
|
|
|
|
* @config
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2013-05-24 09:46:15 +02:00
|
|
|
private static $echo_progress = false;
|
2012-09-21 07:47:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-09-25 10:02:26 +02:00
|
|
|
* Realtime static publishing... the second a page is saved, it is
|
|
|
|
* written to the cache.
|
|
|
|
*
|
|
|
|
* @var boolean
|
2013-05-24 09:46:15 +02:00
|
|
|
*
|
|
|
|
* @config
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2013-05-24 09:46:15 +02:00
|
|
|
private static $disable_realtime = false;
|
2012-09-21 07:47:26 +02:00
|
|
|
|
2012-09-25 10:02:26 +02:00
|
|
|
/**
|
|
|
|
* This is the current static publishing theme, which can be set at any
|
|
|
|
* point. If it's not set, then the last non-null theme, set via
|
|
|
|
* SSViewer::set_theme() is used. The obvious place to set this is in
|
|
|
|
* _config.php
|
|
|
|
*
|
|
|
|
* @var string
|
2013-05-24 09:46:15 +02:00
|
|
|
*
|
|
|
|
* @config
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2013-05-24 09:46:15 +02:00
|
|
|
private static $static_publisher_theme = false;
|
2012-09-21 07:47:26 +02:00
|
|
|
|
2013-05-24 08:21:51 +02:00
|
|
|
/**
|
|
|
|
* @var boolean includes a timestamp at the bottom of the generated HTML
|
|
|
|
* of each file, which can be useful for debugging issues with stale
|
|
|
|
* caches etc.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $include_caching_metadata = false;
|
|
|
|
|
2012-09-25 10:02:26 +02:00
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
2012-09-21 07:47:26 +02:00
|
|
|
abstract function publishPages($pages);
|
2012-09-25 10:02:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
2012-09-21 07:47:26 +02:00
|
|
|
abstract function unpublishPages($pages);
|
|
|
|
|
2012-09-25 10:02:26 +02:00
|
|
|
/**
|
2013-05-24 09:46:15 +02:00
|
|
|
* @deprecated
|
2012-09-25 10:02:26 +02:00
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
public static function set_static_publisher_theme($theme) {
|
2013-05-24 09:46:15 +02:00
|
|
|
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
|
|
|
|
|
|
|
|
Config::inst()->update('StaticPublisher', 'static_publisher_theme', $theme);
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
2012-09-25 10:02:26 +02:00
|
|
|
/**
|
2013-05-24 09:46:15 +02:00
|
|
|
* @deprecated
|
|
|
|
*
|
2012-09-25 10:02:26 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function static_publisher_theme() {
|
2013-05-24 09:46:15 +02:00
|
|
|
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
|
|
|
|
|
|
|
|
return Config::inst()->get('StaticPublisher', 'static_publisher_theme');
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
2012-09-25 10:02:26 +02:00
|
|
|
/**
|
2013-05-24 09:46:15 +02:00
|
|
|
* @deprecated
|
|
|
|
*
|
2012-09-25 10:02:26 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function echo_progress() {
|
2013-05-24 09:46:15 +02:00
|
|
|
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
|
|
|
|
|
|
|
|
return Config::inst()->get('StaticPublisher', 'echo_progress');
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-24 09:46:15 +02:00
|
|
|
* @deprecated
|
|
|
|
*
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2012-09-25 10:02:26 +02:00
|
|
|
public static function set_echo_progress($progress) {
|
2013-05-24 09:46:15 +02:00
|
|
|
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
|
|
|
|
|
|
|
|
Config::inst()->get('StaticPublisher', 'echo_progress', $progress);
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after a page is published.
|
2012-09-25 10:02:26 +02:00
|
|
|
*
|
|
|
|
* @param SiteTree
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2012-09-25 10:02:26 +02:00
|
|
|
public function onAfterPublish($original) {
|
2012-09-21 07:47:26 +02:00
|
|
|
$this->republish($original);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-25 10:02:26 +02:00
|
|
|
* Called after link assets have been renamed, and the live site has been
|
|
|
|
* updated, without an actual publish event.
|
2012-09-21 07:47:26 +02:00
|
|
|
*
|
|
|
|
* Only called if the published content exists and has been modified.
|
|
|
|
*/
|
2012-09-25 10:02:26 +02:00
|
|
|
public function onRenameLinkedAsset($original) {
|
2012-09-21 07:47:26 +02:00
|
|
|
$this->republish($original);
|
|
|
|
}
|
|
|
|
|
2012-09-25 10:02:26 +02:00
|
|
|
public function republish($original) {
|
2012-09-21 07:47:26 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Similiar to RebuildStaticCacheTask->rebuildCache()
|
|
|
|
foreach($urls as $i => $url) {
|
|
|
|
if(!is_string($url)) {
|
|
|
|
user_error("Bad URL: " . var_export($url, true), E_USER_WARNING);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove leading slashes from all URLs (apart from the homepage)
|
|
|
|
if(substr($url,-1) == '/' && $url != '/') $url = substr($url,0,-1);
|
|
|
|
|
|
|
|
$urls[$i] = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$urls = array_unique($urls);
|
|
|
|
|
|
|
|
$this->publishPages($urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-25 10:02:26 +02:00
|
|
|
* Get changes and hook into underlying functionality.
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2012-09-25 10:02:26 +02:00
|
|
|
public function onAfterUnpublish($page) {
|
2012-09-21 07:47:26 +02:00
|
|
|
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,
|
|
|
|
*/
|
2012-09-25 10:02:26 +02:00
|
|
|
public function externalReferencesFor($content) {
|
2012-09-21 07:47:26 +02:00
|
|
|
$CLI_content = escapeshellarg($content);
|
|
|
|
$tidy = `echo $CLI_content | tidy -numeric -asxhtml`;
|
|
|
|
$tidy = preg_replace('/xmlns="[^"]+"/','', $tidy);
|
|
|
|
$xContent = new SimpleXMLElement($tidy);
|
|
|
|
|
|
|
|
$xlinks = array(
|
|
|
|
"//link[@rel='stylesheet']/@href" => false,
|
|
|
|
"//script/@src" => false,
|
|
|
|
"//img/@src" => false,
|
|
|
|
"//a/@href" => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
$urls = array();
|
2012-09-25 10:02:26 +02:00
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
foreach($xlinks as $xlink => $assetsOnly) {
|
|
|
|
$matches = $xContent->xpath($xlink);
|
|
|
|
if($matches) foreach($matches as $item) {
|
|
|
|
$url = $item . '';
|
|
|
|
if($assetsOnly && substr($url,0,7) != ASSETS_DIR . '/') continue;
|
|
|
|
|
|
|
|
$urls[] = $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $urls;
|
|
|
|
}
|
2013-05-24 08:21:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMetadata($url) {
|
|
|
|
return array(
|
|
|
|
'Cache generated on ' . date('Y-m-d H:i:s T (O)')
|
|
|
|
);
|
|
|
|
}
|
2012-09-25 10:02:26 +02:00
|
|
|
}
|