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
|
|
|
*
|
2010-10-15 05:57:11 +02:00
|
|
|
* Requirements: Unix Filesystem supporting symlinking. Doesn't work on Windows.
|
|
|
|
*
|
|
|
|
* <b>Usage</b>
|
|
|
|
*
|
|
|
|
* The exporter can only be invoked through a URL. Usage on commandline (through [sake](sake)) is not possible at the moment, as we're sending a file to the browser for download.
|
|
|
|
*
|
|
|
|
* <pre>http://localhost/StaticExporter/export</pre>
|
|
|
|
*
|
|
|
|
* Specify a custom baseurl in case you want to deploy the static HTML pages on a different host:
|
|
|
|
* <pre>http://localhost/StaticExporter/export?baseurl=http://example.com</pre>
|
2008-12-04 23:38:58 +01:00
|
|
|
*
|
|
|
|
* @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 {
|
2011-12-18 18:19:55 +01:00
|
|
|
|
|
|
|
static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'export',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function init() {
|
2007-07-19 12:40:05 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Link($action = null) {
|
2007-07-19 12:40:05 +02:00
|
|
|
return "StaticExporter/$action";
|
|
|
|
}
|
2007-09-25 05:44:07 +02:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public 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
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function StaticExportForm() {
|
2011-10-26 07:35:51 +02:00
|
|
|
return new Form($this, 'StaticExportForm', new FieldList(
|
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'))
|
2011-10-26 07:35:51 +02:00
|
|
|
), new FieldList(
|
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
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public 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) {
|
2009-10-11 02:08:04 +02:00
|
|
|
$subfolder = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/');
|
|
|
|
$contentfile = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/') . '/index.html';
|
2008-12-04 23:38:58 +01:00
|
|
|
|
|
|
|
// 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
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Sam Minnee <sam@silverstripe.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@90076 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:42 +01:00
|
|
|
$response = SS_HTTPRequest::send_file($archiveContent, "$baseFolderName.tar.gz", 'application/x-tar-gz');
|
2008-12-04 23:38:58 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|