silverstripe-reports/code/staticpublisher/FilesystemPublisher.php
Sam Minnee 387b5edd21 Merged changes from branches/2.2.2-assets:
Static publishing
Ability to lock down comments to logged-in members only

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@60470 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-08-12 02:59:27 +00:00

102 lines
2.9 KiB
PHP

<?php
/**
* Usage: Object::add_extension("SiteTree", "FilesystemPublisher('../static-folder/')")
*/
class FilesystemPublisher extends StaticPublisher {
protected $destFolder;
protected $fileExtension;
/**
* @param $destFolder The folder to save the cached site into
* @param $fileExtension The file extension to use, for example, 'html'. If omitted, then each page will be placed
* in its own directory, with the filename 'index.html'
*/
function __construct($destFolder, $fileExtension = null) {
if(substr($destFolder, -1) == '/') $destFolder = substr($destFolder, 0, -1);
$this->destFolder = $destFolder;
$this->fileExtension = $fileExtension;
}
function publishPages($urls) {
//$base = Director::absoluteURL($this->destFolder);
//$base = preg_replace('/\/[^\/]+\/\.\./','',$base) . '/';
//Director::setBaseURL($base);
$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;
}
}*/
}
Director::setBaseURL(null);
//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]");
}
}
}
}