Version: current

      1 <?php
      2 /**
      3  * This class lets you export a static copy of your site.
      4  * It creates a huge number of folders each containing an index.html file.
      5  * This preserves the URL naming format.
      6  *
      7  * Requirements: Unix Filesystem supporting symlinking.
      8  * Doesn't work on Windows.
      9  *
     10  * @see StaticPublisher
     11  *
     12  * @package cms
     13  * @subpackage export
     14  */
     15 class StaticExporter extends Controller {
     16     function init() {
     17         parent::init();
     18
     19         $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
     20         if(!$canAccess) return Security::permissionFailure($this);
     21     }
     22
     23
     24     function Link($action = null) {
     25         return "StaticExporter/$action";
     26     }
     27
     28     function index() {
     29         echo "<h1>"._t('StaticExporter.NAME','Static exporter')."</h1>";
     30         echo $this->StaticExportForm()->forTemplate();
     31     }
     32
     33     function StaticExportForm() {
     34         return new Form($this, 'StaticExportForm', new FieldSet(
     35             // new TextField('folder', _t('StaticExporter.FOLDEREXPORT','Folder to export to')),
     36             new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
     37         ), new FieldSet(
     38             new FormAction('export', _t('StaticExporter.EXPORTTO','Export to that folder'))
     39         ));
     40     }
     41
     42     function export() {
     43         // specify custom baseurl for publishing to other webroot
     44         if(isset($_REQUEST['baseurl'])) {
     45             $base = $_REQUEST['baseurl'];
     46             if(substr($base,-1) != '/') $base .= '/';
     47             Director::setBaseURL($base);
     48         }
     49
     50         // setup temporary folders
     51         $tmpBaseFolder = TEMP_FOLDER . '/static-export';
     52         $tmpFolder = (project()) ? "$tmpBaseFolder/" . project() : "$tmpBaseFolder/site";
     53         if(!file_exists($tmpFolder)) Filesystem::makeFolder($tmpFolder);
     54         $baseFolderName = basename($tmpFolder);
     55
     56         // symlink /assets
     57         $f1 = ASSETS_PATH;
     58         $f2 = Director::baseFolder() . '/' . project();
     59         `cd $tmpFolder; ln -s $f1; ln -s $f2`;
     60
     61         // iterate through all instances of SiteTree
     62         $pages = DataObject::get("SiteTree");
     63         foreach($pages as $page) {
     64             $subfolder   = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/');
     65             $contentfile = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/') . '/index.html';
     66
     67             // Make the folder
     68             if(!file_exists($subfolder)) {
     69                 Filesystem::makeFolder($subfolder);
     70             }
     71
     72             // Run the page
     73             Requirements::clear();
     74             $link = Director::makeRelative($page->Link());
     75             $response = Director::test($link);
     76
     77             // Write to file
     78             if($fh = fopen($contentfile, 'w')) {
     79                 fwrite($fh, $response->getBody());
     80                 fclose($fh);
     81             }
     82         }
     83
     84         // copy homepage (URLSegment: "home") to webroot
     85         copy("$tmpFolder/home/index.html", "$tmpFolder/index.html");
     86
     87         // archive all generated files
     88         `cd $tmpBaseFolder; tar -czhf $baseFolderName.tar.gz $baseFolderName`;
     89         $archiveContent = file_get_contents("$tmpBaseFolder/$baseFolderName.tar.gz");
     90
     91         // remove temporary files and folder
     92         Filesystem::removeFolder($tmpBaseFolder);
     93
     94         // return as download to the client
     95         $response = SS_HTTPRequest::send_file($archiveContent, "$baseFolderName.tar.gz", 'application/x-tar-gz');
     96         echo $response->output();
     97     }
     98
     99 }
    100
    101 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -