mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
Results returned from FSP->publishPages()
Aids further processing and verbose display, e.g. when the publication is triggered by a queuing mechanism.
This commit is contained in:
parent
8dae8a3076
commit
8c3f87bb8a
@ -142,7 +142,19 @@ class FilesystemPublisher extends StaticPublisher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses {@link Director::test()} to perform in-memory HTTP requests
|
||||
* on the passed-in URLs.
|
||||
*
|
||||
* @param array $urls Relative URLs
|
||||
* @return array Result, keyed by URL. Keys:
|
||||
* - "statuscode": The HTTP status code
|
||||
* - "redirect": A redirect location (if applicable)
|
||||
* - "path": The filesystem path where the cache has been written
|
||||
*/
|
||||
public function publishPages($urls) {
|
||||
$result = array();
|
||||
|
||||
// Do we need to map these?
|
||||
// Detect a numerically indexed arrays
|
||||
if (is_numeric(join('', array_keys($urls)))) $urls = $this->urlsToPaths($urls);
|
||||
@ -167,8 +179,9 @@ class FilesystemPublisher extends StaticPublisher {
|
||||
$files = array();
|
||||
$i = 0;
|
||||
$totalURLs = sizeof($urls);
|
||||
|
||||
foreach($urls as $url => $path) {
|
||||
$origUrl = $url;
|
||||
$result[$origUrl] = array('statuscode' => null, 'redirect' => null, 'path' => null);
|
||||
|
||||
if(self::$static_base_url) Director::setBaseURL(self::$static_base_url);
|
||||
$i++;
|
||||
@ -189,6 +202,10 @@ class FilesystemPublisher extends StaticPublisher {
|
||||
if(Director::is_relative_url($url)) $url = Director::absoluteURL($url);
|
||||
$response = Director::test(str_replace('+', ' ', $url));
|
||||
|
||||
if($response) {
|
||||
$result[$origUrl]['statuscode'] = $response->getStatusCode();
|
||||
}
|
||||
|
||||
Requirements::clear();
|
||||
|
||||
singleton('DataObject')->flushCache();
|
||||
@ -214,6 +231,7 @@ class FilesystemPublisher extends StaticPublisher {
|
||||
if(is_object($response)) {
|
||||
if($response->getStatusCode() == '301' || $response->getStatusCode() == '302') {
|
||||
$absoluteURL = Director::absoluteURL($response->getHeader('Location'));
|
||||
$result[$origUrl]['redirect'] = $response->getHeader('Location');
|
||||
$content = "<meta http-equiv=\"refresh\" content=\"2; URL=$absoluteURL\">";
|
||||
} else {
|
||||
$content = $response->getBody();
|
||||
@ -231,7 +249,7 @@ class FilesystemPublisher extends StaticPublisher {
|
||||
);
|
||||
}
|
||||
|
||||
$files[] = array(
|
||||
$files[$origUrl] = array(
|
||||
'Content' => $content,
|
||||
'Folder' => dirname($path).'/',
|
||||
'Filename' => basename($path),
|
||||
@ -268,17 +286,22 @@ class FilesystemPublisher extends StaticPublisher {
|
||||
if($this->fileExtension == 'php') SSViewer::setOption('rewriteHashlinks', true);
|
||||
|
||||
$base = BASE_PATH . "/$this->destFolder";
|
||||
foreach($files as $file) {
|
||||
foreach($files as $origUrl => $file) {
|
||||
Filesystem::makeFolder("$base/$file[Folder]");
|
||||
|
||||
$path = "$base/$file[Folder]$file[Filename]";
|
||||
$result[$origUrl]['path'] = $path;
|
||||
|
||||
if(isset($file['Content'])) {
|
||||
$fh = fopen("$base/$file[Folder]$file[Filename]", "w");
|
||||
$fh = fopen($path, "w");
|
||||
fwrite($fh, $file['Content']);
|
||||
fclose($fh);
|
||||
} else if(isset($file['Copy'])) {
|
||||
copy($file['Copy'], "$base/$file[Folder]$file[Filename]");
|
||||
copy($file['Copy'], $path);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,8 @@ class FilesystemPublisherTest extends SapphireTest {
|
||||
protected $usesDatabase = true;
|
||||
|
||||
protected $orig = array();
|
||||
|
||||
static $fixture_file = 'cms/tests/staticpublisher/FilesystemPublisherTest.yml';
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
@ -156,5 +158,41 @@ class FilesystemPublisherTest extends SapphireTest {
|
||||
|
||||
|
||||
}
|
||||
|
||||
function testPublishPages() {
|
||||
$cacheFolder = '/assets/FilesystemPublisherTest-static-folder/';
|
||||
$cachePath = Director::baseFolder() . $cacheFolder;
|
||||
$publisher = new FilesystemPublisher($cacheFolder, 'html');
|
||||
$page1 = $this->objFromFixture('Page', 'page1');
|
||||
$page1->publish('Stage', 'Live');
|
||||
$redirector1 = $this->objFromFixture('RedirectorPage', 'redirector1');
|
||||
$redirector1->publish('Stage', 'Live');
|
||||
|
||||
$results = $publisher->publishPages(array(
|
||||
$page1->Link(),
|
||||
$redirector1->regularLink(),
|
||||
'/notfound'
|
||||
));
|
||||
|
||||
$this->assertArrayHasKey($page1->Link(), $results);
|
||||
$this->assertEquals(200, $results[$page1->Link()]['statuscode']);
|
||||
$this->assertEquals(
|
||||
realpath($results[$page1->Link()]['path']),
|
||||
realpath($cachePath . './page1.html')
|
||||
);
|
||||
|
||||
$this->assertArrayHasKey($redirector1->regularLink(), $results);
|
||||
$this->assertEquals(301, $results[$redirector1->regularLink()]['statuscode']);
|
||||
$this->assertEquals(Director::baseURL() . 'page1/', $results[$redirector1->regularLink()]['redirect']);
|
||||
$this->assertEquals(
|
||||
realpath($results[$redirector1->regularLink()]['path']),
|
||||
realpath($cachePath . './redirect-to-page1.html')
|
||||
);
|
||||
|
||||
$this->assertArrayHasKey('/notfound', $results);
|
||||
$this->assertEquals(404, $results['/notfound']['statuscode']);
|
||||
$this->assertNull($results['/notfound']['redirect']);
|
||||
$this->assertNull($results['/notfound']['path']);
|
||||
}
|
||||
|
||||
}
|
||||
|
12
tests/staticpublisher/FilesystemPublisherTest.yml
Normal file
12
tests/staticpublisher/FilesystemPublisherTest.yml
Normal file
@ -0,0 +1,12 @@
|
||||
Page:
|
||||
page1:
|
||||
URLSegment: page1
|
||||
page1a:
|
||||
URLSegment: page1a
|
||||
Parent: =>Page.page1
|
||||
page2:
|
||||
URLSegment: page2
|
||||
RedirectorPage:
|
||||
redirector1:
|
||||
URLSegment: redirect-to-page1
|
||||
LinkTo: =>Page.page1
|
Loading…
Reference in New Issue
Block a user