Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Results returned from FSP->publishPages()

Aids further processing and verbose display,
e.g. when the publication is triggered by a queuing mechanism.
  • Loading branch information...
commit 8c3f87bb8acd92dc8feabacc3bd47077e03775b2 1 parent 8dae8a3
@chillu chillu authored
View
33 code/staticpublisher/FilesystemPublisher.php
@@ -142,7 +142,19 @@ public function unpublishPages($urls) {
}
}
+ /**
+ * 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 @@ public function publishPages($urls) {
$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 @@ public function publishPages($urls) {
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 @@ public function publishPages($urls) {
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 @@ public function publishPages($urls) {
);
}
- $files[] = array(
+ $files[$origUrl] = array(
'Content' => $content,
'Folder' => dirname($path).'/',
'Filename' => basename($path),
@@ -268,17 +286,22 @@ public function publishPages($urls) {
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;
}
/**
View
38 tests/staticpublisher/FilesystemPublisherTest.php
@@ -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 @@ public function testStaticPublisherTheme(){
}
+
+ 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']);
+ }
}
View
12 tests/staticpublisher/FilesystemPublisherTest.yml
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.