function publishPages($urls) { parent::publishPages($urls); $base = Director::baseFolder(); $framework = FRAMEWORK_DIR; // Get variable that can turn off the rsync component of publication if (isset($_GET['norsync']) && $_GET['norsync']) { return; } $extraArg = ""; if (self::$excluded_folders) { foreach (self::$excluded_folders as $folder) { $extraArg .= " --exclude " . escapeshellarg($folder); } } foreach (self::$targets as $target) { // Transfer non-PHP content from everything to the target; that will ensure that we have all the JS/CSS/etc $rsyncOutput = `cd {$base}; rsync -av -e ssh --exclude /.htaccess --exclude /web.config --exclude '*.php' --exclude '*.svn' --exclude '*.git' --exclude '*~' {$extraArg} --delete . {$target}`; // Then transfer "safe" PHP from the cache/ directory $rsyncOutput .= `cd {$base}; rsync -av -e ssh --exclude '*.svn' --exclude '*~' {$extraArg} --delete cache {$target}`; // Transfer framework/static-main.php to the target $rsyncOutput .= `cd {$base}; rsync -av -e ssh --delete {$framework}/static-main.php {$target}/{$framework}`; if (StaticPublisher::echo_progress()) { echo $rsyncOutput; } } }
function tearDown() { parent::tearDown(); // Static publishing will just confuse things StaticPublisher::$disable_realtime = false; i18n::set_locale($this->origLocale); }
/** * @param $destFolder The folder to save the cached site into. * This needs to be set in framework/static-main.php as well through the {@link $cacheBaseDir} variable. * @param $fileExtension The file extension to use, e.g 'html'. * If omitted, then each page will be placed in its own directory, * with the filename 'index.html'. If you set the extension to PHP, then a simple PHP script will * be generated that can do appropriate cache & redirect header negotation. */ public function __construct($destFolder = 'cache', $fileExtension = null) { // Remove trailing slash from folder if (substr($destFolder, -1) == '/') { $destFolder = substr($destFolder, 0, -1); } $this->destFolder = $destFolder; if ($fileExtension) { $this->fileExtension = $fileExtension; } parent::__construct(); }
function index() { StaticPublisher::set_echo_progress(true); $page = singleton('Page'); if (!$page->hasMethod('allPagesToCache')) { user_error('RebuildStaticCacheTask::index(): Please define a method "allPagesToCache()" on your Page class to return all pages affected by a cache refresh.', E_USER_ERROR); } if (!empty($_GET['urls'])) { $urls = $_GET['urls']; } else { $urls = $page->allPagesToCache(); } $this->rebuildCache($urls, true); }
function publishPages($urls) { parent::publishPages($urls); $base = Director::baseFolder(); // Get variable that can turn off the rsync component of publication if (isset($_GET['norsync']) && $_GET['norsync']) { return; } foreach (self::$targets as $target) { // Transfer non-PHP content from everything to the target; that will ensure that we have all the JS/CSS/etc $rsyncOutput = `cd {$base}; rsync -av -e ssh --exclude /.htaccess --exclude '*.php' --exclude '*.svn' --exclude '*~' --delete . {$target}`; // Then transfer "safe" PHP from the cache/ directory $rsyncOutput .= `cd {$base}; rsync -av -e ssh --exclude '*.svn' --exclude '*~' --delete cache {$target}`; if (StaticPublisher::echo_progress()) { echo $rsyncOutput; } } }
function testStaticPublisherTheme() { //This will be the name of the default theme of this particular project $default_theme = SSViewer::current_theme(); $p1 = new Page(); $p1->URLSegment = strtolower(__CLASS__) . '-page-1'; $p1->HomepageForDomain = ''; $p1->write(); $p1->doPublish(); $current_theme = SSViewer::current_custom_theme(); $this->assertEquals($current_theme, $default_theme, 'After a standard publication, the theme is correct'); //The CMS sometimes sets the theme to null. Check that the $current_custom_theme is still the default SSViewer::set_theme(null); $current_theme = SSViewer::current_custom_theme(); $this->assertEquals($current_theme, $default_theme, 'After a setting the theme to null, the default theme is correct'); //We can set the static_publishing theme to something completely different: //Static publishing will use this one instead of the current_custom_theme if it is not false StaticPublisher::set_static_publisher_theme('otherTheme'); $current_theme = StaticPublisher::static_publisher_theme(); $this->assertNotEquals($current_theme, $default_theme, 'The static publisher theme overrides the custom theme'); }
function set_static_publisher_theme($theme) { self::$static_publisher_theme = $theme; }
function testUnpublishingParentPageUnpublishesSubsiteVirtualPages() { StaticPublisher::$disable_realtime = true; // Go to main site, get parent page $subsite = $this->objFromFixture('Subsite_Template', 'main'); Subsite::changeSubsite($subsite->ID); $page = $this->objFromFixture('SiteTree', 'importantpage'); // Create two SVPs on other subsites $subsite = $this->objFromFixture('Subsite_Template', 'subsite1'); Subsite::changeSubsite($subsite->ID); $vp1 = new SubsitesVirtualPage(); $vp1->CopyContentFromID = $page->ID; $vp1->write(); $vp1->doPublish(); $subsite = $this->objFromFixture('Subsite_Template', 'subsite2'); Subsite::changeSubsite($subsite->ID); $vp2 = new SubsitesVirtualPage(); $vp2->CopyContentFromID = $page->ID; $vp2->write(); $vp2->doPublish(); // Switch back to main site, unpublish source $subsite = $this->objFromFixture('Subsite_Template', 'main'); Subsite::changeSubsite($subsite->ID); $page = $this->objFromFixture('SiteTree', 'importantpage'); $page->doUnpublish(); Subsite::changeSubsite($vp1->SubsiteID); $onLive = Versioned::get_one_by_stage('SubsitesVirtualPage', 'Live', "\"SiteTree_Live\".\"ID\" = " . $vp1->ID); $this->assertFalse($onLive, 'SVP has been removed from live'); $subsite = $this->objFromFixture('Subsite_Template', 'subsite2'); Subsite::changeSubsite($vp2->SubsiteID); $onLive = Versioned::get_one_by_stage('SubsitesVirtualPage', 'Live', "\"SiteTree_Live\".\"ID\" = " . $vp2->ID); $this->assertFalse($onLive, 'SVP has been removed from live'); }
function tearDown() { parent::tearDown(); // Static publishing will just confuse things StaticPublisher::$disable_realtime = false; }
/** * Either turns on (boolean true) or off (boolean false) the progress indicators. * @see StaticPublisher::$echo_progress */ static function set_echo_progress($progress) { self::$echo_progress = (boolean)$progress; }
function publishPages($urls) { // Do we need to map these? // Detect a numerically indexed arrays if (is_numeric(join('', array_keys($urls)))) { $urls = $this->urlsToPaths($urls); } // This can be quite memory hungry and time-consuming // @todo - Make a more memory efficient publisher increase_time_limit_to(); increase_memory_limit_to(); // Set the appropriate theme for this publication batch. // This may have been set explicitly via StaticPublisher::static_publisher_theme, // or we can use the last non-null theme. if (!StaticPublisher::static_publisher_theme()) { SSViewer::set_theme(SSViewer::current_custom_theme()); } else { SSViewer::set_theme(StaticPublisher::static_publisher_theme()); } $currentBaseURL = Director::baseURL(); if (self::$static_base_url) { Director::setBaseURL(self::$static_base_url); } if ($this->fileExtension == 'php') { SSViewer::setOption('rewriteHashlinks', 'php'); } if (StaticPublisher::echo_progress()) { echo $this->class . ": Publishing to " . self::$static_base_url . "\n"; } $files = array(); $i = 0; $totalURLs = sizeof($urls); foreach ($urls as $url => $path) { if (self::$static_base_url) { Director::setBaseURL(self::$static_base_url); } $i++; if ($url && !is_string($url)) { user_error("Bad url:" . var_export($url, true), E_USER_WARNING); continue; } if (StaticPublisher::echo_progress()) { echo " * Publishing page {$i}/{$totalURLs}: {$url}\n"; flush(); } Requirements::clear(); if ($url == "") { $url = "/"; } if (Director::is_relative_url($url)) { $url = Director::absoluteURL($url); } $response = Director::test(str_replace('+', ' ', $url)); Requirements::clear(); singleton('DataObject')->flushCache(); //skip any responses with a 404 status code. We don't want to turn those into statically cached pages if (!$response || $response->getStatusCode() == '404') { continue; } // Generate file content // PHP file caching will generate a simple script from a template if ($this->fileExtension == 'php') { if (is_object($response)) { if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') { $content = $this->generatePHPCacheRedirection($response->getHeader('Location')); } else { $content = $this->generatePHPCacheFile($response->getBody(), HTTP::get_cache_age(), date('Y-m-d H:i:s')); } } else { $content = $this->generatePHPCacheFile($response . '', HTTP::get_cache_age(), date('Y-m-d H:i:s')); } // HTML file caching generally just creates a simple file } else { if (is_object($response)) { if ($response->getStatusCode() == '301' || $response->getStatusCode() == '302') { $absoluteURL = Director::absoluteURL($response->getHeader('Location')); $content = "<meta http-equiv=\"refresh\" content=\"2; URL={$absoluteURL}\">"; } else { $content = $response->getBody(); } } else { $content = $response . ''; } } $files[] = array('Content' => $content, 'Folder' => dirname($path) . '/', 'Filename' => basename($path)); // Add externals /* $externals = $this->externalReferencesFor($content); if($externals) foreach($externals as $external) { // Skip absolute URLs if(preg_match('/^[a-zA-Z]+:\/\//', $external)) continue; // Drop querystring parameters $external = strtok($external, '?'); if(file_exists("../" . $external)) { // Break into folder and filename if(preg_match('/^(.*\/)([^\/]+)$/', $external, $matches)) { $files[$external] = array( "Copy" => "../$external", "Folder" => $matches[1], "Filename" => $matches[2], ); } else { user_error("Can't parse external: $external", E_USER_WARNING); } } else { $missingFiles[$external] = true; } }*/ } if (self::$static_base_url) { Director::setBaseURL($currentBaseURL); } if ($this->fileExtension == 'php') { SSViewer::setOption('rewriteHashlinks', true); } $base = BASE_PATH . "/{$this->destFolder}"; foreach ($files as $file) { Filesystem::makeFolder("{$base}/{$file['Folder']}"); if (isset($file['Content'])) { $fh = fopen("{$base}/{$file['Folder']}{$file['Filename']}", "w"); fwrite($fh, $file['Content']); fclose($fh); } else { if (isset($file['Copy'])) { copy($file['Copy'], "{$base}/{$file['Folder']}{$file['Filename']}"); } } } }
function tearDown() { SiteTreeFutureState::set_future_datetime(null); Versioned::reading_stage('Stage'); parent::tearDown(); // Static publishing will just confuse things StaticPublisher::$disable_realtime = false; }
function publishPages($urls) { set_time_limit(0); ini_set("memory_limit", -1); //$base = Director::absoluteURL($this->destFolder); //$base = preg_replace('/\/[^\/]+\/\.\./','',$base) . '/'; if (self::$static_base_url) { Director::setBaseURL(self::$static_base_url); } if ($this->fileExtension == 'php') { SSViewer::setOption('rewriteHashlinks', 'php'); } $files = array(); $i = 0; $totalURLs = sizeof($urls); foreach ($urls as $url) { $i++; if (StaticPublisher::echo_progress()) { echo " * Publishing page {$i}/{$totalURLs}: {$url}\n"; flush(); } Requirements::clear(); $response = Director::test($url); Requirements::clear(); /* if(!is_object($response)) { echo "String response for url '$url'\n"; print_r($response); }*/ if (is_object($response)) { $content = $response->getBody(); } else { $content = $response . ''; } if ($this->fileExtension) { $filename = $url ? "{$url}.{$this->fileExtension}" : "index.{$this->fileExtension}"; } else { $filename = $url ? "{$url}/index.html" : "index.html"; } $files[$filename] = array('Content' => $content, 'Folder' => dirname($filename) == '/' ? '' : dirname($filename) . '/', 'Filename' => basename($filename)); // Add externals /* $externals = $this->externalReferencesFor($content); if($externals) foreach($externals as $external) { // Skip absolute URLs if(preg_match('/^[a-zA-Z]+:\/\//', $external)) continue; // Drop querystring parameters $external = strtok($external, '?'); if(file_exists("../" . $external)) { // Break into folder and filename if(preg_match('/^(.*\/)([^\/]+)$/', $external, $matches)) { $files[$external] = array( "Copy" => "../$external", "Folder" => $matches[1], "Filename" => $matches[2], ); } else { user_error("Can't parse external: $external", E_USER_WARNING); } } else { $missingFiles[$external] = true; } }*/ } if (self::$static_base_url) { Director::setBaseURL(null); } if ($this->fileExtension == 'php') { SSViewer::setOption('rewriteHashlinks', true); } //Debug::show(array_keys($files)); //Debug::show(array_keys($missingFiles)); $base = "../{$this->destFolder}"; foreach ($files as $file) { Filesystem::makeFolder("{$base}/{$file['Folder']}"); if (isset($file['Content'])) { $fh = fopen("{$base}/{$file['Folder']}{$file['Filename']}", "w"); fwrite($fh, $file['Content']); fclose($fh); } else { if (isset($file['Copy'])) { copy($file['Copy'], "{$base}/{$file['Folder']}{$file['Filename']}"); } } } }