mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
API: remove static related classes, now staticpublisher module
This commit is contained in:
parent
6457ab2433
commit
6e2906d01e
@ -1,114 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This class lets you export a static copy of your site.
|
||||
* It creates a huge number of folders each containing an index.html file.
|
||||
* This preserves the URL naming format.
|
||||
*
|
||||
* Requirements: Unix Filesystem supporting symlinking. Doesn't work on Windows.
|
||||
*
|
||||
* <b>Usage</b>
|
||||
*
|
||||
* The exporter can only be invoked through a URL. Usage on commandline (through [sake](sake)) is not possible at the moment, as we're sending a file to the browser for download.
|
||||
*
|
||||
* <pre>http://localhost/StaticExporter/export</pre>
|
||||
*
|
||||
* Specify a custom baseurl in case you want to deploy the static HTML pages on a different host:
|
||||
* <pre>http://localhost/StaticExporter/export?baseurl=http://example.com</pre>
|
||||
*
|
||||
* @see StaticPublisher
|
||||
*
|
||||
* @package cms
|
||||
* @subpackage export
|
||||
*/
|
||||
class StaticExporter extends Controller {
|
||||
|
||||
private static $allowed_actions = array(
|
||||
'index',
|
||||
'export',
|
||||
);
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
|
||||
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
|
||||
if(!$canAccess) return Security::permissionFailure($this);
|
||||
}
|
||||
|
||||
|
||||
public function Link($action = null) {
|
||||
return "StaticExporter/$action";
|
||||
}
|
||||
|
||||
public function index() {
|
||||
echo "<h1>"._t('StaticExporter.NAME','Static exporter')."</h1>";
|
||||
echo $this->StaticExportForm()->forTemplate();
|
||||
}
|
||||
|
||||
public function StaticExportForm() {
|
||||
return new Form($this, 'StaticExportForm', new FieldList(
|
||||
// new TextField('folder', _t('StaticExporter.FOLDEREXPORT','Folder to export to')),
|
||||
new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
|
||||
), new FieldList(
|
||||
new FormAction('export', _t('StaticExporter.EXPORTTO','Export to that folder'))
|
||||
));
|
||||
}
|
||||
|
||||
public function export() {
|
||||
// specify custom baseurl for publishing to other webroot
|
||||
if(isset($_REQUEST['baseurl'])) {
|
||||
$base = $_REQUEST['baseurl'];
|
||||
if(substr($base,-1) != '/') $base .= '/';
|
||||
Config::inst()->update('Director', 'alternate_base_url', $base);
|
||||
}
|
||||
|
||||
// setup temporary folders
|
||||
$tmpBaseFolder = TEMP_FOLDER . '/static-export';
|
||||
$tmpFolder = (project()) ? "$tmpBaseFolder/" . project() : "$tmpBaseFolder/site";
|
||||
if(!file_exists($tmpFolder)) Filesystem::makeFolder($tmpFolder);
|
||||
$baseFolderName = basename($tmpFolder);
|
||||
|
||||
// symlink /assets
|
||||
$f1 = ASSETS_PATH;
|
||||
$f2 = Director::baseFolder() . '/' . project();
|
||||
`cd $tmpFolder; ln -s $f1; ln -s $f2`;
|
||||
|
||||
// iterate through all instances of SiteTree
|
||||
$pages = DataObject::get("SiteTree");
|
||||
foreach($pages as $page) {
|
||||
$subfolder = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/');
|
||||
$contentfile = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/') . '/index.html';
|
||||
|
||||
// Make the folder
|
||||
if(!file_exists($subfolder)) {
|
||||
Filesystem::makeFolder($subfolder);
|
||||
}
|
||||
|
||||
// Run the page
|
||||
Requirements::clear();
|
||||
$link = Director::makeRelative($page->Link());
|
||||
$response = Director::test($link);
|
||||
|
||||
// Write to file
|
||||
if($fh = fopen($contentfile, 'w')) {
|
||||
fwrite($fh, $response->getBody());
|
||||
fclose($fh);
|
||||
}
|
||||
}
|
||||
|
||||
// copy homepage (URLSegment: "home") to webroot
|
||||
copy("$tmpFolder/home/index.html", "$tmpFolder/index.html");
|
||||
|
||||
// archive all generated files
|
||||
`cd $tmpBaseFolder; tar -czhf $baseFolderName.tar.gz $baseFolderName`;
|
||||
$archiveContent = file_get_contents("$tmpBaseFolder/$baseFolderName.tar.gz");
|
||||
|
||||
// remove temporary files and folder
|
||||
Filesystem::removeFolder($tmpBaseFolder);
|
||||
|
||||
// return as download to the client
|
||||
$response = SS_HTTPRequest::send_file($archiveContent, "$baseFolderName.tar.gz", 'application/x-tar-gz');
|
||||
echo $response->output();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,371 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Usage: SiteTree::add_extension("FilesystemPublisher('static-folder', 'html')");
|
||||
*
|
||||
* Usage: To work with Subsite module you need to:
|
||||
* - Add FilesystemPublisher::$domain_based_caching = true; in mysite/_config.php
|
||||
* - Added main site host mapping in subsites/host-map.php after everytime a new subsite is created or modified
|
||||
*
|
||||
* You may also have a method $page->pagesAffectedByUnpublishing() to return other URLS
|
||||
* that should be de-cached if $page is unpublished.
|
||||
*
|
||||
* @see http://doc.silverstripe.com/doku.php?id=staticpublisher
|
||||
*
|
||||
* @package cms
|
||||
* @subpackage publishers
|
||||
*/
|
||||
class FilesystemPublisher extends StaticPublisher {
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
protected $destFolder = 'cache';
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
protected $fileExtension = 'html';
|
||||
|
||||
/**
|
||||
* @config
|
||||
* @var String
|
||||
*/
|
||||
private static $static_base_url = null;
|
||||
|
||||
/**
|
||||
* @config
|
||||
* @var Boolean Use domain based cacheing (put cache files into a domain subfolder)
|
||||
* This must be true if you are using this with the "subsites" module.
|
||||
* Please note that this form of caching requires all URLs to be provided absolute
|
||||
* (not relative to the webroot) via {@link SiteTree->AbsoluteLink()}.
|
||||
*/
|
||||
private static $domain_based_caching = false;
|
||||
|
||||
/**
|
||||
* Set a different base URL for the static copy of the site.
|
||||
* This can be useful if you are running the CMS on a different domain from the website.
|
||||
*
|
||||
* @deprecated 3.2 Use the "FilesystemPublisher.static_base_url" config setting instead
|
||||
*/
|
||||
static public function set_static_base_url($url) {
|
||||
Deprecation::notice('3.2', 'Use the "FilesystemPublisher.static_base_url" config setting instead');
|
||||
Config::inst()->update('FilesystemPublisher', 'static_base_url', $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $destFolder The folder to save the cached site into.
|
||||
* This needs to be set in framework/static-main.php as well through the {@link $cacheBaseDir} variable.
|
||||
* @param $fileExtension The file extension to use, e.g 'html'.
|
||||
* If omitted, then each page will be placed in its own directory,
|
||||
* with the filename 'index.html'. If you set the extension to PHP, then a simple PHP script will
|
||||
* be generated that can do appropriate cache & redirect header negotation.
|
||||
*/
|
||||
public function __construct($destFolder = 'cache', $fileExtension = null) {
|
||||
// Remove trailing slash from folder
|
||||
if(substr($destFolder, -1) == '/') $destFolder = substr($destFolder, 0, -1);
|
||||
|
||||
$this->destFolder = $destFolder;
|
||||
$this->fileExtension = $fileExtension;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms relative or absolute URLs to their static path equivalent.
|
||||
* This needs to be the same logic that's used to look up these paths through
|
||||
* framework/static-main.php. Does not include the {@link $destFolder} prefix.
|
||||
*
|
||||
* URL filtering will have already taken place for direct SiteTree links via SiteTree->generateURLSegment()).
|
||||
* For all other links (e.g. custom controller actions), we assume that they're pre-sanitized
|
||||
* to suit the filesystem needs, as its impossible to sanitize them without risking to break
|
||||
* the underlying naming assumptions in URL routing (e.g. controller method names).
|
||||
*
|
||||
* Examples (without $domain_based_caching):
|
||||
* - http://mysite.com/mywebroot/ => /index.html (assuming your webroot is in a subfolder)
|
||||
* - http://mysite.com/about-us => /about-us.html
|
||||
* - http://mysite.com/parent/child => /parent/child.html
|
||||
*
|
||||
* Examples (with $domain_based_caching):
|
||||
* - http://mysite.com/mywebroot/ => /mysite.com/index.html (assuming your webroot is in a subfolder)
|
||||
* - http://mysite.com/about-us => /mysite.com/about-us.html
|
||||
* - http://myothersite.com/about-us => /myothersite.com/about-us.html
|
||||
* - http://subdomain.mysite.com/parent/child => /subdomain.mysite.com/parent/child.html
|
||||
*
|
||||
* @param Array $urls Absolute or relative URLs
|
||||
* @return Array Map of original URLs to filesystem paths (relative to {@link $destFolder}).
|
||||
*/
|
||||
public function urlsToPaths($urls) {
|
||||
$mappedUrls = array();
|
||||
foreach($urls as $url) {
|
||||
|
||||
// parse_url() is not multibyte safe, see https://bugs.php.net/bug.php?id=52923.
|
||||
// We assume that the URL hsa been correctly encoded either on storage (for SiteTree->URLSegment),
|
||||
// or through URL collection (for controller method names etc.).
|
||||
$urlParts = @parse_url($url);
|
||||
|
||||
// Remove base folders from the URL if webroot is hosted in a subfolder (same as static-main.php)
|
||||
$path = isset($urlParts['path']) ? $urlParts['path'] : '';
|
||||
if(mb_substr(mb_strtolower($path), 0, mb_strlen(BASE_URL)) == mb_strtolower(BASE_URL)) {
|
||||
$urlSegment = mb_substr($path, mb_strlen(BASE_URL));
|
||||
} else {
|
||||
$urlSegment = $path;
|
||||
}
|
||||
|
||||
// Normalize URLs
|
||||
$urlSegment = trim($urlSegment, '/');
|
||||
|
||||
$filename = $urlSegment ? "$urlSegment.$this->fileExtension" : "index.$this->fileExtension";
|
||||
|
||||
if (Config::inst()->get('FilesystemPublisher', 'domain_based_caching')) {
|
||||
if (!$urlParts) continue; // seriously malformed url here...
|
||||
$filename = $urlParts['host'] . '/' . $filename;
|
||||
}
|
||||
|
||||
$mappedUrls[$url] = ((dirname($filename) == '/') ? '' : (dirname($filename).'/')).basename($filename);
|
||||
}
|
||||
|
||||
return $mappedUrls;
|
||||
}
|
||||
|
||||
public function unpublishPages($urls) {
|
||||
// Do we need to map these?
|
||||
// Detect a numerically indexed arrays
|
||||
if (is_numeric(join('', array_keys($urls)))) $urls = $this->urlsToPaths($urls);
|
||||
|
||||
// This can be quite memory hungry and time-consuming
|
||||
// @todo - Make a more memory efficient publisher
|
||||
increase_time_limit_to();
|
||||
increase_memory_limit_to();
|
||||
|
||||
$cacheBaseDir = $this->getDestDir();
|
||||
|
||||
foreach($urls as $url => $path) {
|
||||
if (file_exists($cacheBaseDir.'/'.$path)) {
|
||||
@unlink($cacheBaseDir.'/'.$path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses {@link Director::test()} to perform in-memory HTTP requests
|
||||
* on the passed-in URLs.
|
||||
*
|
||||
* @param array $urls Relative URLs
|
||||
* @return array Result, keyed by URL. Keys:
|
||||
* - "statuscode": The HTTP status code
|
||||
* - "redirect": A redirect location (if applicable)
|
||||
* - "path": The filesystem path where the cache has been written
|
||||
*/
|
||||
public function publishPages($urls) {
|
||||
$result = array();
|
||||
|
||||
// Do we need to map these?
|
||||
// Detect a numerically indexed arrays
|
||||
if (is_numeric(join('', array_keys($urls)))) $urls = $this->urlsToPaths($urls);
|
||||
|
||||
// This can be quite memory hungry and time-consuming
|
||||
// @todo - Make a more memory efficient publisher
|
||||
increase_time_limit_to();
|
||||
increase_memory_limit_to();
|
||||
|
||||
// Set the appropriate theme for this publication batch.
|
||||
// This may have been set explicitly via StaticPublisher::static_publisher_theme,
|
||||
// or we can use the last non-null theme.
|
||||
$customTheme = Config::inst()->get('StaticPublisher', 'static_publisher_theme');
|
||||
if(!$customTheme)
|
||||
Config::inst()->update('SSViewer', 'theme', Config::inst()->get('SSViewer', 'custom_theme'));
|
||||
else
|
||||
Config::inst()->update('SSViewer', 'theme', $customTheme);
|
||||
|
||||
$currentBaseURL = Director::baseURL();
|
||||
$staticBaseUrl = Config::inst()->get('FilesystemPublisher', 'static_base_url');
|
||||
if($staticBaseUrl) Config::inst()->update('Director', 'alternate_base_url', $staticBaseUrl);
|
||||
if($this->fileExtension == 'php') {
|
||||
Config::inst()->update('SSViewer', 'rewrite_hash_links', 'php');
|
||||
}
|
||||
if(Config::inst()->get('StaticPublisher', 'echo_progress')) {
|
||||
echo $this->class.": Publishing to " . $staticBaseUrl . "\n";
|
||||
}
|
||||
$files = array();
|
||||
$i = 0;
|
||||
$totalURLs = sizeof($urls);
|
||||
foreach($urls as $url => $path) {
|
||||
$origUrl = $url;
|
||||
$result[$origUrl] = array('statuscode' => null, 'redirect' => null, 'path' => null);
|
||||
|
||||
if($staticBaseUrl) Config::inst()->update('Director', 'alternate_base_url', $staticBaseUrl);
|
||||
$i++;
|
||||
|
||||
if($url && !is_string($url)) {
|
||||
user_error("Bad url:" . var_export($url,true), E_USER_WARNING);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(Config::inst()->get('StaticPublisher', 'echo_progress')) {
|
||||
echo " * Publishing page $i/$totalURLs: $url\n";
|
||||
flush();
|
||||
}
|
||||
|
||||
Requirements::clear();
|
||||
|
||||
if($url == "") $url = "/";
|
||||
if(Director::is_relative_url($url)) $url = Director::absoluteURL($url);
|
||||
$response = Director::test(str_replace('+', ' ', $url));
|
||||
|
||||
if($response) {
|
||||
$result[$origUrl]['statuscode'] = $response->getStatusCode();
|
||||
}
|
||||
|
||||
Requirements::clear();
|
||||
|
||||
singleton('DataObject')->flushCache();
|
||||
|
||||
//skip any responses with a 404 status code. We don't want to turn those into statically cached pages
|
||||
if (!$response || $response->getStatusCode() == '404') continue;
|
||||
|
||||
// Generate file content
|
||||
// PHP file caching will generate a simple script from a template
|
||||
if($this->fileExtension == 'php') {
|
||||
if(is_object($response)) {
|
||||
if($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
|
||||
$content = $this->generatePHPCacheRedirection($response->getHeader('Location'));
|
||||
} else {
|
||||
$content = $this->generatePHPCacheFile($response->getBody(), HTTP::get_cache_age(), date('Y-m-d H:i:s'));
|
||||
}
|
||||
} else {
|
||||
$content = $this->generatePHPCacheFile($response . '', HTTP::get_cache_age(), date('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
// HTML file caching generally just creates a simple file
|
||||
} else {
|
||||
if(is_object($response)) {
|
||||
if($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
|
||||
$absoluteURL = Director::absoluteURL($response->getHeader('Location'));
|
||||
$result[$origUrl]['redirect'] = $response->getHeader('Location');
|
||||
$content = "<meta http-equiv=\"refresh\" content=\"2; URL=$absoluteURL\">";
|
||||
} else {
|
||||
$content = $response->getBody();
|
||||
}
|
||||
} else {
|
||||
$content = $response . '';
|
||||
}
|
||||
}
|
||||
|
||||
if(Config::inst()->get('StaticPublisher', 'include_caching_metadata')) {
|
||||
$content = str_replace(
|
||||
'</html>',
|
||||
sprintf("</html>\n\n<!-- %s -->", implode(" ", $this->getMetadata($url))),
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
$files[$origUrl] = array(
|
||||
'Content' => $content,
|
||||
'Folder' => dirname($path).'/',
|
||||
'Filename' => basename($path),
|
||||
);
|
||||
|
||||
// Add externals
|
||||
/*
|
||||
$externals = $this->externalReferencesFor($content);
|
||||
if($externals) foreach($externals as $external) {
|
||||
// Skip absolute URLs
|
||||
if(preg_match('/^[a-zA-Z]+:\/\//', $external)) continue;
|
||||
// Drop querystring parameters
|
||||
$external = strtok($external, '?');
|
||||
|
||||
if(file_exists("../" . $external)) {
|
||||
// Break into folder and filename
|
||||
if(preg_match('/^(.*\/)([^\/]+)$/', $external, $matches)) {
|
||||
$files[$external] = array(
|
||||
"Copy" => "../$external",
|
||||
"Folder" => $matches[1],
|
||||
"Filename" => $matches[2],
|
||||
);
|
||||
|
||||
} else {
|
||||
user_error("Can't parse external: $external", E_USER_WARNING);
|
||||
}
|
||||
} else {
|
||||
$missingFiles[$external] = true;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
if(Config::inst()->get('FilesystemPublisher', 'static_base_url')) Config::inst()->update('Director', 'alternate_base_url', $currentBaseURL);
|
||||
if($this->fileExtension == 'php') {
|
||||
Config::inst()->update('SSViewer', 'rewrite_hash_links', true);
|
||||
}
|
||||
|
||||
$base = BASE_PATH . "/$this->destFolder";
|
||||
foreach($files as $origUrl => $file) {
|
||||
Filesystem::makeFolder("$base/$file[Folder]");
|
||||
|
||||
$path = "$base/$file[Folder]$file[Filename]";
|
||||
$result[$origUrl]['path'] = $path;
|
||||
|
||||
if(isset($file['Content'])) {
|
||||
$fh = fopen($path, "w");
|
||||
fwrite($fh, $file['Content']);
|
||||
fclose($fh);
|
||||
} else if(isset($file['Copy'])) {
|
||||
copy($file['Copy'], $path);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the templated content for a PHP script that can serve up the given piece of content with the given age and expiry
|
||||
*/
|
||||
protected function generatePHPCacheFile($content, $age, $lastModified) {
|
||||
$template = file_get_contents(BASE_PATH . '/cms/code/staticpublisher/CachedPHPPage.tmpl');
|
||||
return str_replace(
|
||||
array('**MAX_AGE**', '**LAST_MODIFIED**', '**CONTENT**'),
|
||||
array((int)$age, $lastModified, $content),
|
||||
$template);
|
||||
}
|
||||
/**
|
||||
* Generate the templated content for a PHP script that can serve up a 301 redirect to the given destionation
|
||||
*/
|
||||
protected function generatePHPCacheRedirection($destination) {
|
||||
$template = file_get_contents(BASE_PATH . '/cms/code/staticpublisher/CachedPHPRedirection.tmpl');
|
||||
return str_replace(
|
||||
array('**DESTINATION**'),
|
||||
array($destination),
|
||||
$template);
|
||||
}
|
||||
|
||||
public function getDestDir() {
|
||||
return BASE_PATH . '/' . $this->destFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of all the existing static cache files, as a map of URL => file.
|
||||
* Only returns cache files that will actually map to a URL, based on urlsToPaths.
|
||||
*/
|
||||
public function getExistingStaticCacheFiles() {
|
||||
$cacheDir = BASE_PATH . '/' . $this->destFolder;
|
||||
|
||||
$urlMapper = array_flip($this->urlsToPaths($this->owner->allPagesToCache()));
|
||||
|
||||
$output = array();
|
||||
|
||||
// Glob each dir, then glob each one of those
|
||||
foreach(glob("$cacheDir/*", GLOB_ONLYDIR) as $cacheDir) {
|
||||
foreach(glob($cacheDir.'/*') as $cacheFile) {
|
||||
$mapKey = str_replace(BASE_PATH . "/cache/","",$cacheFile);
|
||||
if(isset($urlMapper[$mapKey])) {
|
||||
$url = $urlMapper[$mapKey];
|
||||
$output[$url] = $cacheFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,73 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This static publisher can be used to deploy static content to multiple hosts, by generating the cache files locally and then rsyncing then to
|
||||
* each destination box. This can be used to set up a load-balanced collection of static servers.
|
||||
*
|
||||
* @see http://doc.silverstripe.com/doku.php?id=staticpublisher
|
||||
*
|
||||
* @package cms
|
||||
* @subpackage publishers
|
||||
*/
|
||||
class RsyncMultiHostPublisher extends FilesystemPublisher {
|
||||
/**
|
||||
* @config
|
||||
* Array of rsync targets to publish to. These can either be local file names, or scp-style targets, in the form "user@server:path"
|
||||
*/
|
||||
private static $targets = array();
|
||||
|
||||
/**
|
||||
* @config
|
||||
* @var array
|
||||
*/
|
||||
private static $excluded_folders = array();
|
||||
|
||||
/**
|
||||
* Set the targets to publish to.
|
||||
* If target is an scp-style remote path, no password is accepted - we assume key-based authentication to be set up on the application server
|
||||
* initiating the publication.
|
||||
*
|
||||
* @deprecated 3.2 Use the "RsyncMultiHostPublisher.targets" config setting instead
|
||||
* @param $targets An array of targets to publish to. These can either be local file names, or scp-style targets, in the form "user@server:path"
|
||||
*/
|
||||
static public function set_targets($targets) {
|
||||
Deprecation::notice('3.2', 'Use the "RsyncMultiHostPublisher.targets" config setting instead');
|
||||
Config::inst()->update('RsyncMultiHostPublisher', 'targets', $targets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify folders to exclude from the rsync
|
||||
* For example, you could exclude assets.
|
||||
*
|
||||
* @deprecated 3.2 Use the "RsyncMultiHostPublisher.excluded_folders" config setting instead
|
||||
*/
|
||||
static public function set_excluded_folders($folders) {
|
||||
Deprecation::notice('3.2', 'Use the "RsyncMultiHostPublisher.excluded_folders" config setting instead');
|
||||
Config::inst()->update('RsyncMultiHostPublisher', 'excluded_folders', $folders);
|
||||
}
|
||||
|
||||
public function publishPages($urls) {
|
||||
parent::publishPages($urls);
|
||||
$base = Director::baseFolder();
|
||||
$framework = FRAMEWORK_DIR;
|
||||
|
||||
// Get variable that can turn off the rsync component of publication
|
||||
if(isset($_GET['norsync']) && $_GET['norsync']) return;
|
||||
|
||||
$extraArg = "";
|
||||
if($this->config()->excluded_folders) foreach($this->config()->excluded_folders as $folder) {
|
||||
$extraArg .= " --exclude " . escapeshellarg($folder);
|
||||
}
|
||||
|
||||
foreach((array)$this->config()->targets as $target) {
|
||||
// Transfer non-PHP content from everything to the target; that will ensure that we have all the JS/CSS/etc
|
||||
$rsyncOutput = `cd $base; rsync -av -e ssh --exclude /.htaccess --exclude /web.config --exclude '*.php' --exclude '*.svn' --exclude '*.git' --exclude '*~' $extraArg --delete . $target`;
|
||||
// Then transfer "safe" PHP from the cache/ directory
|
||||
$rsyncOutput .= `cd $base; rsync -av -e ssh --exclude '*.svn' --exclude '*~' $extraArg --delete cache $target`;
|
||||
// Transfer framework/static-main.php to the target
|
||||
$rsyncOutput .= `cd $base; rsync -av -e ssh --delete $framework/static-main.php $target/$framework`;
|
||||
if(Config::inst()->get('StaticPublisher', 'echo_progress')) echo $rsyncOutput;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,199 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package cms
|
||||
* @subpackage publishers
|
||||
*/
|
||||
abstract class StaticPublisher extends DataExtension {
|
||||
/**
|
||||
* @config
|
||||
* @var boolean 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)
|
||||
*/
|
||||
private static $echo_progress = false;
|
||||
|
||||
/**
|
||||
* @config
|
||||
* @var boolean Realtime static publishing... the second a page
|
||||
* is saved, it is written to the cache
|
||||
*/
|
||||
private static $disable_realtime = false;
|
||||
|
||||
/*
|
||||
* @config
|
||||
* @var boolean 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 Config::inst()->update('SSViewer', 'theme', ) is used
|
||||
* The obvious place to set this is in _config.php
|
||||
*/
|
||||
private static $static_publisher_theme=false;
|
||||
|
||||
abstract public function publishPages($pages);
|
||||
abstract public function unpublishPages($pages);
|
||||
|
||||
/**
|
||||
* @deprecated 3.2 Use the "StaticPublisher.static_publisher_theme" config setting instead
|
||||
* @param [type] $theme [description]
|
||||
*/
|
||||
static public function set_static_publisher_theme($theme){
|
||||
Deprecation::notice('3.2', 'Use the "StaticPublisher.static_publisher_theme" config setting instead');
|
||||
Config::inst()->update('StaticPublisher', 'static_publisher_theme', $theme);
|
||||
}
|
||||
|
||||
/**
|
||||
* @config
|
||||
* @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.
|
||||
*/
|
||||
private static $include_caching_metadata = false;
|
||||
|
||||
/**
|
||||
* @deprecated 3.2 Use the "StaticPublisher.static_publisher_theme" config setting instead
|
||||
*/
|
||||
static public function static_publisher_theme(){
|
||||
Deprecation::notice('3.2', 'Use the "StaticPublisher.static_publisher_theme" config setting instead');
|
||||
return Config::inst()->get('StaticPublisher', 'static_publisher_theme');
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 3.2 Use the "StaticPublisher.echo_progress" config setting instead
|
||||
*/
|
||||
static public function echo_progress() {
|
||||
Deprecation::notice('3.2', 'Use the "StaticPublisher.echo_progress" config setting instead');
|
||||
return Config::inst()->get('StaticPublisher', 'echo_progress');
|
||||
}
|
||||
|
||||
/**
|
||||
* Either turns on (boolean true) or off (boolean false) the progress indicators.
|
||||
* @deprecated 3.2 Use the "StaticPublisher.echo_progress" config setting instead
|
||||
* @see StaticPublisher::$echo_progress
|
||||
*/
|
||||
static public function set_echo_progress($progress) {
|
||||
Deprecation::notice('3.2', 'Use the "StaticPublisher.echo_progress" config setting instead');
|
||||
Config::inst()->update('StaticPublisher', 'echo_progress', $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a page is published.
|
||||
*/
|
||||
public 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.
|
||||
*/
|
||||
public function onRenameLinkedAsset($original) {
|
||||
$this->republish($original);
|
||||
}
|
||||
|
||||
public function republish($original) {
|
||||
if (Config::inst()->get('StaticPublisher', '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);
|
||||
}
|
||||
|
||||
/**
|
||||
* On after unpublish, get changes and hook into underlying
|
||||
* functionality
|
||||
*/
|
||||
public function onAfterUnpublish($page) {
|
||||
if (Config::inst()->get('StaticPublisher', '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,
|
||||
*/
|
||||
public 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_DIR . '/') continue;
|
||||
|
||||
$urls[] = $url;
|
||||
}
|
||||
}
|
||||
|
||||
return $urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides context for this URL, written as an HTML comment to the static file cache,
|
||||
* which can be useful for debugging cache problems. For example, you could track the
|
||||
* event or related page which triggered this republication. The returned data
|
||||
* is unstructured and not intended to be consumed programmatically.
|
||||
* Consider injecting standard HTML <meta> tags instead where applicable.
|
||||
*
|
||||
* Note: Only used when {@link $include_caching_metadata} is enabled.
|
||||
*
|
||||
* @param String
|
||||
* @return Array A numeric array of all metadata.
|
||||
*/
|
||||
function getMetadata($url) {
|
||||
return array(
|
||||
'Cache generated on ' . date('Y-m-d H:i:s T (O)')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -377,10 +377,6 @@ af:
|
||||
many_many_LinkTracking: 'Link Tracking'
|
||||
SiteTreeURLSegmentField:
|
||||
HelpChars: ' Special characters are automatically converted or removed.'
|
||||
StaticExporter:
|
||||
BASEURL: 'Base URL'
|
||||
EXPORTTO: 'Export to that folder'
|
||||
NAME: 'Static exporter'
|
||||
TableListField:
|
||||
SELECT: 'Select:'
|
||||
TableListField.ss:
|
||||
|
@ -1,184 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the {@link FilesystemPublisher} class.
|
||||
*
|
||||
* @package cms
|
||||
* @subpackage tests
|
||||
*/
|
||||
class FilesystemPublisherTest extends SapphireTest {
|
||||
|
||||
protected $usesDatabase = true;
|
||||
|
||||
protected $orig = array();
|
||||
|
||||
protected static $fixture_file = 'cms/tests/staticpublisher/FilesystemPublisherTest.yml';
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
SiteTree::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
|
||||
|
||||
Config::inst()->update('FilesystemPublisher', 'domain_based_caching', false);
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
|
||||
SiteTree::remove_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
|
||||
|
||||
if(file_exists(BASE_PATH . '/assets/FilesystemPublisherTest-static-folder')) {
|
||||
Filesystem::removeFolder(BASE_PATH . '/assets/FilesystemPublisherTest-static-folder');
|
||||
}
|
||||
}
|
||||
|
||||
public function testUrlsToPathsWithRelativeUrls() {
|
||||
$fsp = new FilesystemPublisher('.', 'html');
|
||||
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array('/')),
|
||||
array('/' => './index.html'),
|
||||
'Root URL path mapping'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array('about-us')),
|
||||
array('about-us' => './about-us.html'),
|
||||
'URLsegment path mapping'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array('parent/child')),
|
||||
array('parent/child' => 'parent/child.html'),
|
||||
'Nested URLsegment path mapping'
|
||||
);
|
||||
}
|
||||
|
||||
public function testUrlsToPathsWithAbsoluteUrls() {
|
||||
$fsp = new FilesystemPublisher('.', 'html');
|
||||
|
||||
$url = Director::absoluteBaseUrl();
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array($url)),
|
||||
array($url => './index.html'),
|
||||
'Root URL path mapping'
|
||||
);
|
||||
|
||||
$url = Director::absoluteBaseUrl() . 'about-us';
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array($url)),
|
||||
array($url => './about-us.html'),
|
||||
'URLsegment path mapping'
|
||||
);
|
||||
|
||||
$url = Director::absoluteBaseUrl() . 'parent/child';
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array($url)),
|
||||
array($url => 'parent/child.html'),
|
||||
'Nested URLsegment path mapping'
|
||||
);
|
||||
}
|
||||
|
||||
public function testUrlsToPathsWithDomainBasedCaching() {
|
||||
Config::inst()->update('FilesystemPublisher', 'domain_based_caching', true);
|
||||
|
||||
$fsp = new FilesystemPublisher('.', 'html');
|
||||
|
||||
$url = 'http://domain1.com/';
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array($url)),
|
||||
array($url => 'domain1.com/index.html'),
|
||||
'Root URL path mapping'
|
||||
);
|
||||
|
||||
$url = 'http://domain1.com/about-us';
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array($url)),
|
||||
array($url => 'domain1.com/about-us.html'),
|
||||
'URLsegment path mapping'
|
||||
);
|
||||
|
||||
$url = 'http://domain2.com/parent/child';
|
||||
$this->assertEquals(
|
||||
$fsp->urlsToPaths(array($url)),
|
||||
array($url => 'domain2.com/parent/child.html'),
|
||||
'Nested URLsegment path mapping'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test to ensure that FileSystemPublisher::__construct()
|
||||
* has called parent::__construct() by checking the class property.
|
||||
* The class property is set on {@link Object::__construct()} and
|
||||
* this is therefore a good test to ensure it was called.
|
||||
*
|
||||
* If FilesystemPublisher doesn't call parent::__construct() then
|
||||
* it won't be enabled propery because {@link Object::__construct()}
|
||||
* is where extension instances are set up and subsequently used by
|
||||
* {@link DataObject::defineMethods()}.
|
||||
*/
|
||||
public function testHasCalledParentConstructor() {
|
||||
$fsp = new FilesystemPublisher('.', '.html');
|
||||
$this->assertEquals($fsp->class, 'FilesystemPublisher');
|
||||
}
|
||||
|
||||
/*
|
||||
* These are a few simple tests to check that we will be retrieving the correct theme when we need it
|
||||
* StaticPublishing needs to be able to retrieve a non-null theme at the time publishPages() is called.
|
||||
*/
|
||||
public function testStaticPublisherTheme(){
|
||||
|
||||
//This will be the name of the default theme of this particular project
|
||||
$default_theme= Config::inst()->get('SSViewer', 'theme');
|
||||
|
||||
$p1 = new Page();
|
||||
$p1->URLSegment = strtolower(__CLASS__).'-page-1';
|
||||
$p1->HomepageForDomain = '';
|
||||
$p1->write();
|
||||
$p1->doPublish();
|
||||
|
||||
$current_theme=Config::inst()->get('SSViewer', 'custom_theme');
|
||||
$this->assertEquals($current_theme, $default_theme, 'After a standard publication, the theme is correct');
|
||||
|
||||
//The CMS sometimes sets the theme to null. Check that the $current_custom_theme is still the default
|
||||
Config::inst()->update('SSViewer', 'theme', null);
|
||||
$current_theme=Config::inst()->get('SSViewer', 'custom_theme');
|
||||
$this->assertEquals($current_theme, $default_theme, 'After a setting the theme to null, the default theme is correct');
|
||||
}
|
||||
|
||||
function testPublishPages() {
|
||||
$cacheFolder = '/assets/FilesystemPublisherTest-static-folder/';
|
||||
$cachePath = Director::baseFolder() . $cacheFolder;
|
||||
$publisher = new FilesystemPublisher($cacheFolder, 'html');
|
||||
$page1 = $this->objFromFixture('Page', 'page1');
|
||||
$page1->publish('Stage', 'Live');
|
||||
$redirector1 = $this->objFromFixture('RedirectorPage', 'redirector1');
|
||||
$redirector1->publish('Stage', 'Live');
|
||||
|
||||
$results = $publisher->publishPages(array(
|
||||
$page1->Link(),
|
||||
$redirector1->regularLink(),
|
||||
'/notfound'
|
||||
));
|
||||
|
||||
$this->assertArrayHasKey($page1->Link(), $results);
|
||||
$this->assertEquals(200, $results[$page1->Link()]['statuscode']);
|
||||
$this->assertEquals(
|
||||
realpath($results[$page1->Link()]['path']),
|
||||
realpath($cachePath . './page1.html')
|
||||
);
|
||||
|
||||
$this->assertArrayHasKey($redirector1->regularLink(), $results);
|
||||
$this->assertEquals(301, $results[$redirector1->regularLink()]['statuscode']);
|
||||
$this->assertEquals(Director::baseURL() . 'page1/', $results[$redirector1->regularLink()]['redirect']);
|
||||
$this->assertEquals(
|
||||
realpath($results[$redirector1->regularLink()]['path']),
|
||||
realpath($cachePath . './redirect-to-page1.html')
|
||||
);
|
||||
|
||||
$this->assertArrayHasKey('/notfound', $results);
|
||||
$this->assertEquals(404, $results['/notfound']['statuscode']);
|
||||
$this->assertNull($results['/notfound']['redirect']);
|
||||
$this->assertNull($results['/notfound']['path']);
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
Page:
|
||||
page1:
|
||||
URLSegment: page1
|
||||
page1a:
|
||||
URLSegment: page1a
|
||||
Parent: =>Page.page1
|
||||
page2:
|
||||
URLSegment: page2
|
||||
RedirectorPage:
|
||||
redirector1:
|
||||
URLSegment: redirect-to-page1
|
||||
LinkTo: =>Page.page1
|
Loading…
Reference in New Issue
Block a user