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.
|
2008-12-04 23:38:58 +01:00
|
|
|
*
|
|
|
|
* Requirements: Unix Filesystem supporting symlinking.
|
|
|
|
* Doesn't work on Windows.
|
|
|
|
*
|
|
|
|
* @see StaticPublisher
|
|
|
|
*
|
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();
|
2009-09-10 03:49:07 +02:00
|
|
|
|
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
|
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
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() {
|
2008-12-04 23:38:58 +01:00
|
|
|
// specify custom baseurl for publishing to other webroot
|
|
|
|
if(isset($_REQUEST['baseurl'])) {
|
2007-07-19 12:40:05 +02:00
|
|
|
$base = $_REQUEST['baseurl'];
|
|
|
|
if(substr($base,-1) != '/') $base .= '/';
|
|
|
|
Director::setBaseURL($base);
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
// setup temporary folders
|
|
|
|
$tmpBaseFolder = TEMP_FOLDER . '/static-export';
|
|
|
|
$tmpFolder = (project()) ? "$tmpBaseFolder/" . project() : "$tmpBaseFolder/site";
|
|
|
|
if(!file_exists($tmpFolder)) Filesystem::makeFolder($tmpFolder);
|
|
|
|
$baseFolderName = basename($tmpFolder);
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
// symlink /assets
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
$f1 = ASSETS_PATH;
|
2007-09-25 05:44:07 +02:00
|
|
|
$f2 = Director::baseFolder() . '/' . project();
|
2008-12-04 23:38:58 +01:00
|
|
|
`cd $tmpFolder; ln -s $f1; ln -s $f2`;
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
// iterate through all instances of SiteTree
|
|
|
|
$pages = DataObject::get("SiteTree");
|
|
|
|
foreach($pages as $page) {
|
|
|
|
$subfolder = "$tmpFolder/$page->URLSegment";
|
|
|
|
$contentfile = "$tmpFolder/$page->URLSegment/index.html";
|
|
|
|
|
|
|
|
// Make the folder
|
|
|
|
if(!file_exists($subfolder)) {
|
|
|
|
Filesystem::makeFolder($subfolder);
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
// Run the page
|
|
|
|
Requirements::clear();
|
|
|
|
$link = Director::makeRelative($page->Link());
|
|
|
|
$response = Director::test($link);
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
// Write to file
|
|
|
|
if($fh = fopen($contentfile, 'w')) {
|
|
|
|
fwrite($fh, $response->getBody());
|
|
|
|
fclose($fh);
|
|
|
|
}
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2008-12-04 23:38:58 +01:00
|
|
|
|
|
|
|
// copy homepage (URLSegment: "home") to webroot
|
|
|
|
copy("$tmpFolder/home/index.html", "$tmpFolder/index.html");
|
|
|
|
|
|
|
|
// archive all generated files
|
|
|
|
`cd $tmpBaseFolder; tar -czhf $baseFolderName.tar.gz $baseFolderName`;
|
|
|
|
$archiveContent = file_get_contents("$tmpBaseFolder/$baseFolderName.tar.gz");
|
|
|
|
|
|
|
|
// remove temporary files and folder
|
|
|
|
Filesystem::removeFolder($tmpBaseFolder);
|
|
|
|
|
|
|
|
// return as download to the client
|
|
|
|
$response = HTTPRequest::send_file($archiveContent, "$baseFolderName.tar.gz", 'application/x-tar-gz');
|
|
|
|
echo $response->output();
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|