From 416178b6682878c5b094d8d21fa2a1660ebe4ce8 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 29 Jan 2013 19:34:05 +0100 Subject: [PATCH 1/3] Reduced code duplication in ErrorPage (no functional changes) --- code/model/ErrorPage.php | 115 +++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/code/model/ErrorPage.php b/code/model/ErrorPage.php index a8e9d04e..a2345935 100644 --- a/code/model/ErrorPage.php +++ b/code/model/ErrorPage.php @@ -72,62 +72,71 @@ class ErrorPage extends Page { if(!file_exists(ASSETS_PATH)) { mkdir(ASSETS_PATH); } - - $pageNotFoundErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '404'"); - $pageNotFoundErrorPageExists = ($pageNotFoundErrorPage && $pageNotFoundErrorPage->exists()) ? true : false; - $pageNotFoundErrorPagePath = self::get_filepath_for_errorcode(404); - if(!($pageNotFoundErrorPageExists && file_exists($pageNotFoundErrorPagePath))) { - if(!$pageNotFoundErrorPageExists) { - $pageNotFoundErrorPage = new ErrorPage(); - $pageNotFoundErrorPage->ErrorCode = 404; - $pageNotFoundErrorPage->Title = _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'); - $pageNotFoundErrorPage->Content = _t('ErrorPage.DEFAULTERRORPAGECONTENT', '

Sorry, it seems you were trying to access a page that doesn\'t exist.

Please check the spelling of the URL you were trying to access and try again.

'); - $pageNotFoundErrorPage->write(); - $pageNotFoundErrorPage->publish('Stage', 'Live'); - } - - // Ensure a static error page is created from latest error page content - $response = Director::test(Director::makeRelative($pageNotFoundErrorPage->Link())); - $written = null; - if($fh = fopen($pageNotFoundErrorPagePath, 'w')) { - $written = fwrite($fh, $response->getBody()); - fclose($fh); - } - - if($written) { - DB::alteration_message('404 error page created', 'created'); - } else { - DB::alteration_message(sprintf('404 error page could not be created at %s. Please check permissions', $pageNotFoundErrorPagePath), 'error'); - } - } - - $serverErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '500'"); - $serverErrorPageExists = ($serverErrorPage && $serverErrorPage->exists()) ? true : false; - $serverErrorPagePath = self::get_filepath_for_errorcode(500); - if(!($serverErrorPageExists && file_exists($serverErrorPagePath))) { - if(!$serverErrorPageExists) { - $serverErrorPage = new ErrorPage(); - $serverErrorPage->ErrorCode = 500; - $serverErrorPage->Title = _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'); - $serverErrorPage->Content = _t('ErrorPage.DEFAULTSERVERERRORPAGECONTENT', '

Sorry, there was a problem with handling your request.

'); - $serverErrorPage->write(); - $serverErrorPage->publish('Stage', 'Live'); - } - - // Ensure a static error page is created from latest error page content - $response = Director::test(Director::makeRelative($serverErrorPage->Link())); - $written = null; - if($fh = fopen($serverErrorPagePath, 'w')) { - $written = fwrite($fh, $response->getBody()); - fclose($fh); - } - if($written) { - DB::alteration_message('500 error page created', 'created'); - } else { - DB::alteration_message(sprintf('500 error page could not be created at %s. Please check permissions', $serverErrorPagePath), 'error'); + $defaultPages = array( + array( + 'ErrorCode' => 404, + 'Title' => _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'), + 'Content' => _t( + 'ErrorPage.DEFAULTERRORPAGECONTENT', + '

Sorry, it seems you were trying to access a page that doesn\'t exist.

' + . '

Please check the spelling of the URL you were trying to access and try again.

' + ) + ), + array( + 'ErrorCode' => 500, + 'Title' => _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'), + 'Content' => _t( + 'ErrorPage.DEFAULTSERVERERRORPAGECONTENT', + '

Sorry, there was a problem with handling your request.

' + ) + ) + ); + + foreach($defaultPages as $defaultData) { + $code = $defaultData['ErrorCode']; + $page = DataObject::get_one( + 'ErrorPage', + sprintf("\"ErrorCode\" = '%s'", $code) + ); + $pageExists = ($page && $page->exists()); + $pagePath = self::get_filepath_for_errorcode($code); + if(!($pageExists && file_exists($pagePath))) { + if(!$pageExists) { + $page = new ErrorPage(); + $page->ErrorCode = $code; + $page->Title = $defaultData['Title']; + $page->Content = $defaultData['Title']; + $page->write(); + $page->publish('Stage', 'Live'); + } + + // Ensure a static error page is created from latest error page content + $response = Director::test(Director::makeRelative($page->Link())); + $written = null; + if($fh = fopen($pagePath, 'w')) { + $written = fwrite($fh, $response->getBody()); + fclose($fh); + } + + if($written) { + DB::alteration_message( + sprintf('%s error page created', $code), + 'created' + ); + } else { + DB::alteration_message( + sprintf( + '%s error page could not be created at %s. Please check permissions', + $code, + $pagePath + ), + 'error' + ); + } } } + } } From b25063b09ca3cfcd26ba0cb18d1a74d341e24ce0 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 30 Jan 2013 13:07:11 +0100 Subject: [PATCH 2/3] Extracted ErrorPage default data to getter Allows for less verbose overloading, while still keeping i18n capabilities in place, which isn't possible with DataObject::$default_records --- code/model/ErrorPage.php | 60 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/code/model/ErrorPage.php b/code/model/ErrorPage.php index a2345935..35e04123 100644 --- a/code/model/ErrorPage.php +++ b/code/model/ErrorPage.php @@ -21,7 +21,7 @@ class ErrorPage extends Page { "ShowInMenus" => 0, "ShowInSearch" => 0 ); - + static $allowed_children = array(); static $description = 'Custom content for different error cases (e.g. "Page not found")'; @@ -57,7 +57,7 @@ class ErrorPage extends Page { return $response; } } - + /** * Ensures that there is always a 404 page * by checking if there's an instance of @@ -73,25 +73,7 @@ class ErrorPage extends Page { mkdir(ASSETS_PATH); } - $defaultPages = array( - array( - 'ErrorCode' => 404, - 'Title' => _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'), - 'Content' => _t( - 'ErrorPage.DEFAULTERRORPAGECONTENT', - '

Sorry, it seems you were trying to access a page that doesn\'t exist.

' - . '

Please check the spelling of the URL you were trying to access and try again.

' - ) - ), - array( - 'ErrorCode' => 500, - 'Title' => _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'), - 'Content' => _t( - 'ErrorPage.DEFAULTSERVERERRORPAGECONTENT', - '

Sorry, there was a problem with handling your request.

' - ) - ) - ); + $defaultPages = $this->getDefaultRecords(); foreach($defaultPages as $defaultData) { $code = $defaultData['ErrorCode']; @@ -103,10 +85,7 @@ class ErrorPage extends Page { $pagePath = self::get_filepath_for_errorcode($code); if(!($pageExists && file_exists($pagePath))) { if(!$pageExists) { - $page = new ErrorPage(); - $page->ErrorCode = $code; - $page->Title = $defaultData['Title']; - $page->Content = $defaultData['Title']; + $page = new ErrorPage($defaultData); $page->write(); $page->publish('Stage', 'Live'); } @@ -140,6 +119,37 @@ class ErrorPage extends Page { } } + /** + * Returns an array of arrays, each of which defines + * properties for a new ErrorPage record. + * + * @return Array + */ + protected function getDefaultRecords() { + $data = array( + array( + 'ErrorCode' => 404, + 'Title' => _t('ErrorPage.DEFAULTERRORPAGETITLE', 'Page not found'), + 'Content' => _t( + 'ErrorPage.DEFAULTERRORPAGECONTENT', + '

Sorry, it seems you were trying to access a page that doesn\'t exist.

' + . '

Please check the spelling of the URL you were trying to access and try again.

' + ) + ), + array( + 'ErrorCode' => 500, + 'Title' => _t('ErrorPage.DEFAULTSERVERERRORPAGETITLE', 'Server error'), + 'Content' => _t( + 'ErrorPage.DEFAULTSERVERERRORPAGECONTENT', + '

Sorry, there was a problem with handling your request.

' + ) + ) + ); + $this->extend('getDefaultRecords', $data); + + return $data; + } + public function getCMSFields() { $fields = parent::getCMSFields(); From 74a44933b2e85cc787c3abfca93502648f945b3a Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 30 Jan 2013 13:07:38 +0100 Subject: [PATCH 3/3] Excluded Postgres from Travis (breaks build due to internal errors) We'll need to fix the "no space left on device" issue, most likely caused by Postgres keeping too much of a query log, or somehow creating a history of past data. For now, having a Postgres build breaking the whole build process (incl. MySQL builds) does more harm than good. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6bba86bf..5a2c56c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ php: env: - TESTDB=MYSQL - - TESTDB=PGSQL matrix: exclude: