BUGFIX #6055 ErrorPage should always create static error page files when dev/build is called if they don't exist

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@111842 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2010-10-10 06:03:10 +00:00 committed by Sam Minnee
parent 9dc1306439
commit 9d78fbb983

View File

@ -61,7 +61,10 @@ class ErrorPage extends Page {
parent::requireDefaultRecords();
$pageNotFoundErrorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '404'");
if(!($pageNotFoundErrorPage && $pageNotFoundErrorPage->exists())) {
$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');
@ -69,12 +72,27 @@ class ErrorPage extends Page {
$pageNotFoundErrorPage->Status = 'New page';
$pageNotFoundErrorPage->write();
$pageNotFoundErrorPage->publish('Stage', 'Live');
}
DB::alteration_message('404 page created', 'created');
// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($pageNotFoundErrorPage->Link()));
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'");
if(!($serverErrorPage && $serverErrorPage->exists())) {
$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');
@ -82,8 +100,20 @@ class ErrorPage extends Page {
$serverErrorPage->Status = 'New page';
$serverErrorPage->write();
$serverErrorPage->publish('Stage', 'Live');
}
DB::alteration_message('500 page created', 'created');
// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($serverErrorPage->Link()));
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');
}
}
}