Updated and improved StaticExporter

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@42536 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2007-09-25 03:44:07 +00:00
parent 0c0bb09e24
commit bc246bf484

View File

@ -8,14 +8,15 @@
class StaticExporter extends Controller { class StaticExporter extends Controller {
function init() { function init() {
parent::init(); parent::init();
if(!$this->can('AdminCMS')) { if(!Permission::check('ADMIN')) {
$messageSet = array( $messageSet = array(
'default' => _t('LeftAndMain.PERMDEFAULT'), 'default' => "Enter your email address and password to access the CMS.",
'alreadyLoggedIn' => _t('LeftAndMain.PERMALREADY'), 'alreadyLoggedIn' => "I'm sorry, but you can't access that part of the CMS. If you want to log in as someone else, do so below",
'logInAgain' => _t('LeftAndMain.PERMAGAIN'), 'logInAgain' => "You have been logged out of the CMS. If you would like to log in again, enter a username and password below.",
); );
Security::permissionFailure($this, $messageSet); Security::permissionFailure($this, $messageSet);
return;
} }
} }
@ -25,16 +26,16 @@ class StaticExporter extends Controller {
} }
function index() { function index() {
echo "<h1>"._t('StaticExporter.NAME','Static exporter')."</h1>"; echo "<h1>Static exporter</h1>";
echo $this->StaticExportForm()->forTemplate(); echo $this->StaticExportForm()->forTemplate();
} }
function StaticExportForm() { function StaticExportForm() {
return new Form($this, 'StaticExportForm', new FieldSet( return new Form($this, 'StaticExportForm', new FieldSet(
new TextField('folder', _t('StaticExporter.FOLDEREXPORT','Folder to export to')), // new TextField('folder', 'Folder to export to'),
new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL')) new TextField('baseurl', 'Base URL')
), new FieldSet( ), new FieldSet(
new FormAction('export', _t('StaticExporter.EXPORTTOTHAT','Export to that folder')) new FormAction('export', 'Export to that folder')
)); ));
} }
@ -46,19 +47,26 @@ class StaticExporter extends Controller {
Director::setBaseURL($base); Director::setBaseURL($base);
} }
$folder = $_REQUEST['folder']; $folder = '/tmp/static-export/' . project();
if(!project()) $folder .= 'site';
if(!file_exists($folder)) mkdir($folder, 02775, true);
$f1 = Director::baseFolder() . '/assets';
$f2 = Director::baseFolder() . '/' . project();
`cd $folder; ln -s $f1; ln -s $f2`;
$baseFolder = basename($folder);
if($folder && file_exists($folder)) { if($folder && file_exists($folder)) {
$pages = DataObject::get("SiteTree"); $pages = DataObject::get("SiteTree");
foreach($pages as $page) { foreach($pages as $page) {
$subfolder = "$folder/$page->URLSegment"; $subfolder = "$folder/$page->URLSegment";
$contentfile = "$folder/$page->URLSegment/index.html"; $contentfile = "$folder/$page->URLSegment/index.html";
echo "<li>$page->URLSegment/index.html";
// Make the folder // Make the folder
if(!file_exists($subfolder)) { if(!file_exists($subfolder)) {
mkdir($subfolder); mkdir($subfolder, 02775);
chmod($subfolder,02775);
} }
// Run the page // Run the page
@ -66,19 +74,28 @@ class StaticExporter extends Controller {
$controllerClass = "{$page->class}_Controller"; $controllerClass = "{$page->class}_Controller";
if(class_exists($controllerClass)) { if(class_exists($controllerClass)) {
$controller = new $controllerClass($page); $controller = new $controllerClass($page);
$pageContent = $controller->run( array() );
$response = $controller->run( array() );
$pageContent = $response->getBody();
// Write to file // Write to file
if($fh = fopen($contentfile, 'w')) { if($fh = fopen($contentfile, 'w')) {
fwrite($fh, $pageContent); fwrite($fh, $pageContent->getBody());
fclose($fh); fclose($fh);
} }
} }
} }
copy("$folder/home/index.html", "$folder/index.html");
`cd /tmp/static-export; tar -czhf $baseFolder.tar.gz $baseFolder`;
$content = file_get_contents("/tmp/static-export/$baseFolder.tar.gz");
Filesystem::removeFolder('/tmp/static-export');
HTTP::sendFileToBrowser($content, "$baseFolder.tar.gz");
return null;
} else { } else {
echo _t('StaticExporter.ONETHATEXISTS',"Please specify a folder that exists"); echo "Please specify a folder that exists";
} }
} }