From c9b5ae8b41e3a0dd3f250becba89b3913cfe9fb4 Mon Sep 17 00:00:00 2001 From: Brian Waters Date: Thu, 7 Apr 2011 02:40:24 -0400 Subject: [PATCH] API CHANGE: Added SiteTree::get_create_default_pages().MINOR: Error pages no longer created on /dev/build if SiteTree::get_create_default_pages() is false. Fixes #6587. --- code/model/ErrorPage.php | 108 ++++++++++++++++++++------------------- code/model/SiteTree.php | 11 +++- 2 files changed, 65 insertions(+), 54 deletions(-) diff --git a/code/model/ErrorPage.php b/code/model/ErrorPage.php index d2b04848..defbf3d2 100755 --- a/code/model/ErrorPage.php +++ b/code/model/ErrorPage.php @@ -63,62 +63,64 @@ class ErrorPage extends Page { function requireDefaultRecords() { parent::requireDefaultRecords(); - // Ensure that an assets path exists before we do any error page creation - 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'); + if ($this->class == 'ErrorPage' && SiteTree::get_create_default_pages()) { + // Ensure that an assets path exists before we do any error page creation + if(!file_exists(ASSETS_PATH)) { + mkdir(ASSETS_PATH); } - - // 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); + + $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())); + 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())); + if($fh = fopen($serverErrorPagePath, '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())); - 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'); + 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'); + } } } } diff --git a/code/model/SiteTree.php b/code/model/SiteTree.php index 9f1bcb7d..be0d8560 100755 --- a/code/model/SiteTree.php +++ b/code/model/SiteTree.php @@ -227,6 +227,15 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid public static function set_create_default_pages($option = true) { self::$create_default_pages = $option; } + + /** + * Return true if default pages should be created on /dev/build. + * + * @return bool + */ + public static function get_create_default_pages() { + return self::$create_default_pages; + } /** * Fetches the {@link SiteTree} object that maps to a link. @@ -1291,7 +1300,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid parent::requireDefaultRecords(); // default pages - if($this->class == 'SiteTree' && self::$create_default_pages) { + if($this->class == 'SiteTree' && self::get_create_default_pages()) { if(!SiteTree::get_by_link('home')) { $homepage = new Page(); $homepage->Title = _t('SiteTree.DEFAULTHOMETITLE', 'Home');