2007-07-19 12:40:05 +02:00
< ? php
2008-02-25 03:10:37 +01:00
/**
* @ package cms
* @ subpackage export
*/
2007-07-19 12:40:05 +02:00
/**
* 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 .
2008-02-25 03:10:37 +01:00
* @ package cms
* @ subpackage export
2007-07-19 12:40:05 +02:00
*/
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 (
2008-02-25 03:10:37 +01:00
'default' => _t ( 'LeftAndMain.PERMDEFAULT' , 'Enter your email address and password to access the CMS.' ),
'alreadyLoggedIn' => _t ( 'LeftAndMain.PERMALREADY' , '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' , '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-10-21 01:49:14 +02:00
echo " <h1> " . _t ( 'StaticExporter.NAME' , '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-10-21 01:49:14 +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-10-21 01:49:14 +02:00
new FormAction ( 'export' , _t ( 'StaticExporter.EXPORTTO' , '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' ;
2007-10-26 01:07:24 +02:00
if ( ! file_exists ( $folder )) mkdir ( $folder , Filesystem :: $folder_create_mask , true );
2007-09-25 05:44:07 +02:00
$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-10-26 01:07:24 +02:00
mkdir ( $subfolder , Filesystem :: $folder_create_mask );
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-10-21 01:49:14 +02:00
echo _t ( 'StaticExporter.ONETHATEXISTS' , " 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
}
?>