2012-09-21 07:47:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This class lets you export a static copy of your site either as an tar
|
|
|
|
* archive through the web browser or through the command line.
|
|
|
|
*
|
|
|
|
* The exporter will preserve the URL naming format of your pages by
|
|
|
|
* creating a number of subfolders folders each containing an index.html
|
|
|
|
* file.
|
|
|
|
*
|
|
|
|
* The bundled documentation in the 'docs' folder contains information
|
|
|
|
* and usage instructions.
|
|
|
|
*
|
|
|
|
* @see StaticPublisher
|
|
|
|
*
|
2012-09-25 10:02:26 +02:00
|
|
|
* @package staticpublisher
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
|
|
|
class StaticExporter extends Controller {
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var array $export_objects
|
|
|
|
*/
|
|
|
|
private static $export_objects = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $disable_sitetree_export = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-06 06:24:51 +02:00
|
|
|
private static $allowed_actions = array(
|
2012-09-21 07:47:26 +02:00
|
|
|
'index',
|
|
|
|
'export',
|
|
|
|
'StaticExportForm'
|
|
|
|
);
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
if(class_exists('SiteTree')) {
|
|
|
|
if(!$this->config()->get('disable_sitetree_export')) {
|
|
|
|
$objs[] = $this->config()->get('export_objects');
|
|
|
|
$objs[] = "SiteTree";
|
|
|
|
|
|
|
|
$this->config()->set('export_objects', $objs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2012-09-21 07:47:26 +02:00
|
|
|
public function init() {
|
|
|
|
parent::init();
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
$canAccess = (Director::isDev() || Director::is_cli());
|
|
|
|
|
|
|
|
if(!Permission::check("ADMIN") && !$canAccess) {
|
|
|
|
return Security::permissionFailure($this);
|
|
|
|
}
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
/**
|
|
|
|
* @param string $action
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-21 07:47:26 +02:00
|
|
|
public function Link($action = null) {
|
2014-01-14 08:36:46 +01:00
|
|
|
return "dev/staticexporter/$action";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $action
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function AbsoluteLink($action = null) {
|
|
|
|
return Director::absoluteURL($this->Link($action));
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-21 07:47:26 +02:00
|
|
|
public function index() {
|
|
|
|
return array(
|
|
|
|
'Title' => _t('StaticExporter.NAME','Static exporter'),
|
|
|
|
'Form' => $this->StaticExportForm()->forTemplate()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
2012-09-21 07:47:26 +02:00
|
|
|
public function StaticExportForm() {
|
|
|
|
$form = new Form($this, 'StaticExportForm', new FieldList(
|
|
|
|
new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
|
|
|
|
), new FieldList(
|
|
|
|
new FormAction('export', _t('StaticExporter.EXPORT','Export'))
|
|
|
|
));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function export() {
|
|
|
|
if(isset($_REQUEST['baseurl'])) {
|
|
|
|
$base = $_REQUEST['baseurl'];
|
2014-01-14 08:36:46 +01:00
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
if(substr($base,-1) != '/') $base .= '/';
|
2013-04-06 06:24:51 +02:00
|
|
|
|
|
|
|
Config::inst()->update('Director', 'alternate_base_url', $base);
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$base = Director::baseURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
$folder = TEMP_FOLDER . '/static-export';
|
|
|
|
$project = project();
|
|
|
|
|
|
|
|
$exported = $this->doExport($base, $folder .'/'. $project, false);
|
|
|
|
|
|
|
|
`cd $folder; tar -czhf $project-export.tar.gz $project`;
|
|
|
|
|
|
|
|
$archiveContent = file_get_contents("$folder/$project-export.tar.gz");
|
|
|
|
|
|
|
|
|
|
|
|
// return as download to the client
|
|
|
|
$response = SS_HTTPRequest::send_file(
|
|
|
|
$archiveContent,
|
|
|
|
"$project-export.tar.gz",
|
|
|
|
'application/x-tar-gz'
|
|
|
|
);
|
|
|
|
|
|
|
|
echo $response->output();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports the website with the given base url. Returns the path where the
|
|
|
|
* exported version of the website is located.
|
|
|
|
*
|
|
|
|
* @param string website base url
|
|
|
|
* @param string folder to export the site into
|
|
|
|
* @param bool symlink assets
|
|
|
|
* @param bool suppress output progress
|
|
|
|
*
|
|
|
|
* @return string path to export
|
|
|
|
*/
|
|
|
|
public function doExport($base, $folder, $symlink = true, $quiet = true) {
|
|
|
|
ini_set('max_execution_time', 0);
|
2013-04-06 06:24:51 +02:00
|
|
|
|
|
|
|
Config::inst()->update('Director', 'alternate_base_url', $base);
|
2012-09-21 07:47:26 +02:00
|
|
|
|
|
|
|
if(is_dir($folder)) {
|
|
|
|
Filesystem::removeFolder($folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
Filesystem::makeFolder($folder);
|
|
|
|
|
|
|
|
// symlink or copy /assets
|
|
|
|
$f1 = ASSETS_PATH;
|
|
|
|
$f2 = Director::baseFolder() . '/' . project();
|
|
|
|
|
|
|
|
if($symlink) {
|
|
|
|
`cd $folder; ln -s $f1; ln -s $f2`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
`cp -R $f1 $folder; cp -R $f2 $folder`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through items we need to export
|
2014-01-14 08:36:46 +01:00
|
|
|
$urls = $this->getExportUrls();
|
2012-09-21 07:47:26 +02:00
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
if($urls) {
|
|
|
|
$total = count($urls);
|
2012-09-21 07:47:26 +02:00
|
|
|
$i = 1;
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
foreach($urls as $url) {
|
|
|
|
$subfolder = "$folder/" . trim($url, '/');
|
|
|
|
$contentfile = "$folder/" . trim($url, '/') . '/index.html';
|
2012-09-21 07:47:26 +02:00
|
|
|
|
|
|
|
// Make the folder
|
|
|
|
if(!file_exists($subfolder)) {
|
|
|
|
Filesystem::makeFolder($subfolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the page
|
|
|
|
Requirements::clear();
|
2013-06-08 03:57:57 +02:00
|
|
|
DataObject::flush_and_destroy_cache();
|
2014-01-14 08:36:46 +01:00
|
|
|
|
|
|
|
$response = Director::test($url);
|
2012-09-21 07:47:26 +02:00
|
|
|
|
|
|
|
// Write to file
|
|
|
|
if($fh = fopen($contentfile, 'w')) {
|
2014-01-14 08:36:46 +01:00
|
|
|
if(!$quiet) {
|
|
|
|
printf("-- (%s/%s) Outputting page (%s)%s",
|
|
|
|
$i,
|
|
|
|
$total,
|
|
|
|
$url,
|
|
|
|
PHP_EOL
|
|
|
|
);
|
|
|
|
}
|
2012-09-21 07:47:26 +02:00
|
|
|
|
|
|
|
fwrite($fh, $response->getBody());
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-14 08:36:46 +01:00
|
|
|
* Return an array of urls to publish
|
2012-09-21 07:47:26 +02:00
|
|
|
*
|
2014-01-14 08:36:46 +01:00
|
|
|
* @return array
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
2014-01-14 08:36:46 +01:00
|
|
|
public function getExportUrls() {
|
|
|
|
$classes = $this->config()->get('export_objects');
|
|
|
|
$urls = array();
|
|
|
|
|
|
|
|
foreach($classes as $obj) {
|
|
|
|
$link = $obj->Link;
|
|
|
|
|
|
|
|
$urls[$link] = $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extend('alterExportUrls', $urls);
|
|
|
|
|
|
|
|
// older api, keep around to ensure backwards compatibility
|
|
|
|
$objs = new ArrayList();
|
2012-09-21 07:47:26 +02:00
|
|
|
$this->extend('alterObjectsToExport', $objs);
|
|
|
|
|
2014-01-14 08:36:46 +01:00
|
|
|
if($objs) {
|
|
|
|
foreach($objs as $obj) {
|
|
|
|
$link = $obj->Link;
|
|
|
|
|
|
|
|
$urls[$link] = $link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $urls;
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
}
|