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();
|
|
|
|
if(!$this->can('AdminCMS')) {
|
|
|
|
$messageSet = array(
|
2007-09-16 18:33:05 +02:00
|
|
|
'default' => _t('LeftAndMain.PERMDEFAULT'),
|
|
|
|
'alreadyLoggedIn' => _t('LeftAndMain.PERMALREADY'),
|
|
|
|
'logInAgain' => _t('LeftAndMain.PERMAGAIN'),
|
2007-07-19 12:40:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Security::permissionFailure($this, $messageSet);
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:26:56 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function Link($action = null) {
|
|
|
|
return "StaticExporter/$action";
|
|
|
|
}
|
2007-09-14 21:26:56 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function index() {
|
2007-09-16 18:33:05 +02:00
|
|
|
echo "<h1>"._t('StaticExporter.NAME','Static exporter')."</h1>";
|
2007-07-19 12:40:05 +02:00
|
|
|
echo $this->StaticExportForm()->forTemplate();
|
|
|
|
}
|
2007-09-14 21:26:56 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function StaticExportForm() {
|
|
|
|
return new Form($this, 'StaticExportForm', new FieldSet(
|
2007-09-16 18:33:05 +02:00
|
|
|
new TextField('folder', _t('StaticExporter.FOLDEREXPORT','Folder to export to')),
|
|
|
|
new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
|
2007-07-19 12:40:05 +02:00
|
|
|
), new FieldSet(
|
2007-09-16 18:33:05 +02:00
|
|
|
new FormAction('export', _t('StaticExporter.EXPORTTOTHAT','Export to that folder'))
|
2007-07-19 12:40:05 +02:00
|
|
|
));
|
|
|
|
}
|
2007-09-14 21:26:56 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
function export() {
|
2007-09-14 21:26:56 +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-14 21:26:56 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
$folder = $_REQUEST['folder'];
|
|
|
|
if($folder && file_exists($folder)) {
|
|
|
|
$pages = DataObject::get("SiteTree");
|
|
|
|
foreach($pages as $page) {
|
|
|
|
$subfolder = "$folder/$page->URLSegment";
|
|
|
|
$contentfile = "$folder/$page->URLSegment/index.html";
|
|
|
|
|
|
|
|
echo "<li>$page->URLSegment/index.html";
|
2007-09-14 21:26:56 +02:00
|
|
|
|
|
|
|
// Make the folder
|
2007-07-19 12:40:05 +02:00
|
|
|
if(!file_exists($subfolder)) {
|
|
|
|
mkdir($subfolder);
|
|
|
|
chmod($subfolder,02775);
|
|
|
|
}
|
2007-09-14 21:26:56 +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-14 21:26:56 +02:00
|
|
|
|
2007-08-17 05:09:46 +02:00
|
|
|
$response = $controller->run( array() );
|
|
|
|
$pageContent = $response->getBody();
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
// Write to file
|
|
|
|
if($fh = fopen($contentfile, 'w')) {
|
|
|
|
fwrite($fh, $pageContent);
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-09-16 18:33:05 +02:00
|
|
|
echo _t('StaticExporter.ONETHATEXISTS',"Please specify a folder that exists");
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-14 21:26:56 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-14 21:26:56 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|