API: Remove CMS constraint.

This also changes getObjectsToExport() to getExportUrls() to allow extensions to provide plain text url's for Director::test().

Pushes the controller to dev/staticexporter as some framework install doesn't include default class routing.
This commit is contained in:
Will Rossiter 2014-01-14 20:36:46 +13:00
parent 02c2e66d69
commit ea1461acd7
6 changed files with 108 additions and 26 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
Deprecation::notification_version('1.0', 'staticpublisher'); Deprecation::notification_version('1.0', 'staticpublisher');
define('STATIC_MODULE_DIR', dirname(__FILE__)); define('STATIC_MODULE_DIR', dirname(__FILE__));

View File

@ -3,4 +3,5 @@ Name: staticpublisherroutes
--- ---
Director: Director:
rules: rules:
'dev/staticexporter': 'StaticExporter'
'dev/buildcache': '->dev/tasks/RebuildStaticCacheTask' 'dev/buildcache': '->dev/tasks/RebuildStaticCacheTask'

View File

@ -16,24 +16,79 @@
*/ */
class StaticExporter extends Controller { class StaticExporter extends Controller {
/**
* @config
*
* @var array $export_objects
*/
private static $export_objects = array();
/**
* @config
*
* @var bool
*/
private static $disable_sitetree_export = false;
/**
* @var array
*/
private static $allowed_actions = array( private static $allowed_actions = array(
'index', 'index',
'export', 'export',
'StaticExportForm' 'StaticExportForm'
); );
/**
*
*/
public function __construct() {
parent::__construct();
if(class_exists('SiteTree')) {
if(!$this->config()->get('disable_sitetree_export')) {
$objs[] = $this->config()->get('export_objects');
$objs[] = "SiteTree";
$this->config()->set('export_objects', $objs);
}
}
}
/**
*
*/
public function init() { public function init() {
parent::init(); parent::init();
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN")); $canAccess = (Director::isDev() || Director::is_cli());
if(!$canAccess) return Security::permissionFailure($this);
if(!Permission::check("ADMIN") && !$canAccess) {
return Security::permissionFailure($this);
}
} }
/**
* @param string $action
*
* @return string
*/
public function Link($action = null) { public function Link($action = null) {
return "StaticExporter/$action"; return "dev/staticexporter/$action";
}
/**
* @param string $action
*
* @return string
*/
public function AbsoluteLink($action = null) {
return Director::absoluteURL($this->Link($action));
} }
/**
* @return array
*/
public function index() { public function index() {
return array( return array(
'Title' => _t('StaticExporter.NAME','Static exporter'), 'Title' => _t('StaticExporter.NAME','Static exporter'),
@ -41,6 +96,9 @@ class StaticExporter extends Controller {
); );
} }
/**
* @return Form
*/
public function StaticExportForm() { public function StaticExportForm() {
$form = new Form($this, 'StaticExportForm', new FieldList( $form = new Form($this, 'StaticExportForm', new FieldList(
new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL')) new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
@ -53,9 +111,9 @@ class StaticExporter extends Controller {
public function export() { public function export() {
if(isset($_REQUEST['baseurl'])) { if(isset($_REQUEST['baseurl'])) {
$base = $_REQUEST['baseurl']; $base = $_REQUEST['baseurl'];
if(substr($base,-1) != '/') $base .= '/'; if(substr($base,-1) != '/') $base .= '/';
Config::inst()->update('Director', 'alternate_base_url', $base); Config::inst()->update('Director', 'alternate_base_url', $base);
@ -118,17 +176,15 @@ class StaticExporter extends Controller {
} }
// iterate through items we need to export // iterate through items we need to export
$objs = $this->getObjectsToExport(); $urls = $this->getExportUrls();
if($objs) { if($urls) {
$total = $objs->count(); $total = count($urls);
$i = 1; $i = 1;
foreach($objs as $obj) { foreach($urls as $url) {
$link = $obj->RelativeLink(null, true); $subfolder = "$folder/" . trim($url, '/');
$contentfile = "$folder/" . trim($url, '/') . '/index.html';
$subfolder = "$folder/" . trim($link, '/');
$contentfile = "$folder/" . trim($link, '/') . '/index.html';
// Make the folder // Make the folder
if(!file_exists($subfolder)) { if(!file_exists($subfolder)) {
@ -137,14 +193,20 @@ class StaticExporter extends Controller {
// Run the page // Run the page
Requirements::clear(); Requirements::clear();
$link = Director::makeRelative($obj->Link());
DataObject::flush_and_destroy_cache(); DataObject::flush_and_destroy_cache();
$response = Director::test($link);
$response = Director::test($url);
// Write to file // Write to file
if($fh = fopen($contentfile, 'w')) { if($fh = fopen($contentfile, 'w')) {
if(!$quiet) printf("-- (%s/%s) Outputting page (%s)%s", $i, $total, $obj->RelativeLink(null, true), PHP_EOL); if(!$quiet) {
printf("-- (%s/%s) Outputting page (%s)%s",
$i,
$total,
$url,
PHP_EOL
);
}
fwrite($fh, $response->getBody()); fwrite($fh, $response->getBody());
fclose($fh); fclose($fh);
@ -158,16 +220,34 @@ class StaticExporter extends Controller {
} }
/** /**
* Return a list of publishable instances for the exporter to include. The * Return an array of urls to publish
* only requirement is that for this list of objects, each one implements
* the RelativeLink() and Link() method.
* *
* @return SS_List * @return array
*/ */
public function getObjectsToExport() { public function getExportUrls() {
$objs = SiteTree::get(); $classes = $this->config()->get('export_objects');
$urls = array();
foreach($classes as $obj) {
$link = $obj->Link;
$urls[$link] = $link;
}
$this->extend('alterExportUrls', $urls);
// older api, keep around to ensure backwards compatibility
$objs = new ArrayList();
$this->extend('alterObjectsToExport', $objs); $this->extend('alterObjectsToExport', $objs);
return $objs; if($objs) {
foreach($objs as $obj) {
$link = $obj->Link;
$urls[$link] = $link;
}
}
return $urls;
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @package staticpublisher * @package staticpublisher
*/ */

View File

@ -11,7 +11,6 @@
"email": "sam@silverstripe.com" "email": "sam@silverstripe.com"
}], }],
"require": { "require": {
"silverstripe/framework": "~3.1", "silverstripe/framework": "~3.1"
"silverstripe/cms": "~3.1"
} }
} }