2007-07-19 12:40:05 +02:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
class StaticExporter extends Controller {
|
|
|
|
function init() {
|
|
|
|
parent::init();
|
2007-09-25 05:44:07 +02:00
|
|
|
if(!Permission::check('ADMIN')) {
|
2007-07-19 12:40:05 +02:00
|
|
|
$messageSet = array(
|
2007-09-25 05:44:07 +02:00
|
|
|
'default' => "Enter your email address and password to access the CMS.",
|
|
|
|
'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' => "You have been logged out of the CMS. If you would like to log in again, enter a username and password below.",
|
2007-07-19 12:40:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Security::permissionFailure($this, $messageSet);
|
2007-09-25 05:44:07 +02:00
|
|
|
return;
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function Link($action = null) {
|
|
|
|
return "StaticExporter/$action";
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function index() {
|
2007-09-25 05:44:07 +02:00
|
|
|
echo "<h1>Static exporter</h1>";
|
2007-07-19 12:40:05 +02:00
|
|
|
echo $this->StaticExportForm()->forTemplate();
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function StaticExportForm() {
|
|
|
|
return new Form($this, 'StaticExportForm', new FieldSet(
|
2007-09-25 05:44:07 +02:00
|
|
|
// new TextField('folder', 'Folder to export to'),
|
|
|
|
new TextField('baseurl', 'Base URL')
|
2007-07-19 12:40:05 +02:00
|
|
|
), new FieldSet(
|
2007-09-25 05:44:07 +02:00
|
|
|
new FormAction('export', 'Export to that folder')
|
2007-07-19 12:40:05 +02:00
|
|
|
));
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function export() {
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
if($_REQUEST['baseurl']) {
|
|
|
|
$base = $_REQUEST['baseurl'];
|
|
|
|
if(substr($base,-1) != '/') $base .= '/';
|
|
|
|
Director::setBaseURL($base);
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
if($folder && file_exists($folder)) {
|
|
|
|
$pages = DataObject::get("SiteTree");
|
|
|
|
foreach($pages as $page) {
|
|
|
|
$subfolder = "$folder/$page->URLSegment";
|
|
|
|
$contentfile = "$folder/$page->URLSegment/index.html";
|
2007-09-25 05:44:07 +02:00
|
|
|
|
|
|
|
// Make the folder
|
2007-07-19 12:40:05 +02:00
|
|
|
if(!file_exists($subfolder)) {
|
2007-09-25 05:44:07 +02:00
|
|
|
mkdir($subfolder, 02775);
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
// Run the page
|
|
|
|
Requirements::clear();
|
|
|
|
$controllerClass = "{$page->class}_Controller";
|
|
|
|
if(class_exists($controllerClass)) {
|
|
|
|
$controller = new $controllerClass($page);
|
2007-09-25 05:44:07 +02:00
|
|
|
$pageContent = $controller->run( array() );
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
// Write to file
|
|
|
|
if($fh = fopen($contentfile, 'w')) {
|
2007-09-25 05:44:07 +02:00
|
|
|
fwrite($fh, $pageContent->getBody());
|
2007-07-19 12:40:05 +02:00
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
} else {
|
2007-09-25 05:44:07 +02:00
|
|
|
echo "Please specify a folder that exists";
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|