Source of file StaticExporter.php
Size: 6,840 Bytes - Last Modified: 2021-12-23T10:34:47+00:00
/var/www/docs.ssmods.com/process/src/code/controllers/StaticExporter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 | <?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 * * @package staticpublisher */ class StaticExporter extends Controller { /** * @config * * @var array $export_objects */ private static $export_objects = array(); /** * @config * * @var bool */ private static $disable_sitetree_export = false; /** * @var array */ private static $allowed_actions = array( 'index', 'export', 'StaticExportForm' ); /** * */ public function __construct() { parent::__construct(); if (class_exists('SiteTree')) { if (!$this->config()->get('disable_sitetree_export')) { $objs = $this->config()->export_objects; if (!is_array($objs)) { $objs = array($objs); } if (!in_array('SiteTree', $objs)) { $objs[] = "SiteTree"; } $this->config()->export_objects = $objs; } } } /** * */ public function init() { parent::init(); $canAccess = (Director::isDev() || Director::is_cli()); if (!Permission::check("ADMIN") && !$canAccess) { return Security::permissionFailure($this); } } /** * @param string $action * * @return string */ public function Link($action = null) { return "dev/staticexporter/$action"; } /** * @param string $action * * @return string */ public function AbsoluteLink($action = null) { return Director::absoluteURL($this->Link($action)); } /** * @return array */ public function index() { return array( 'Title' => _t('StaticExporter.NAME', 'Static exporter'), 'Form' => $this->StaticExportForm()->forTemplate() ); } /** * @return Form */ 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']; if (substr($base, -1) != '/') { $base .= '/'; } Config::inst()->update('Director', 'alternate_base_url', $base); } 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); Config::inst()->update('Director', 'alternate_base_url', $base); 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 $urls = $this->getExportUrls(); if ($urls) { $total = count($urls); $i = 1; foreach ($urls as $url) { $subfolder = "$folder/" . trim($url, '/'); $contentfile = "$folder/" . trim($url, '/') . '/index.html'; // Make the folder if (!file_exists($subfolder)) { Filesystem::makeFolder($subfolder); } // Run the page Requirements::clear(); DataObject::flush_and_destroy_cache(); $response = Director::test($url); // Write to file if ($fh = fopen($contentfile, 'w')) { if (!$quiet) { printf("-- (%s/%s) Outputting page (%s)%s", $i, $total, $url, PHP_EOL ); } fwrite($fh, $response->getBody()); fclose($fh); } $i++; } } return $folder; } /** * Return an array of urls to publish * * @return array */ public function getExportUrls() { $classes = $this->config()->get('export_objects'); $urls = array(); foreach ($classes as $obj) { if (!class_exists($obj)) { continue; } foreach ($obj::get() as $objInstance) { $link = $objInstance->Link(); $urls[$link] = $link; } } $this->extend('alterExportUrls', $urls); // older api, keep around to ensure backwards compatibility $objs = new ArrayList(); $this->extend('alterObjectsToExport', $objs); if ($objs) { foreach ($objs as $obj) { $link = $obj->Link; $urls[$link] = $link; } } return $urls; } } |