From b1c1448d98d5ed41ac9f650d9a569e22173e27fd Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Fri, 22 Jun 2012 12:37:47 +0200 Subject: [PATCH] (bug 37627) generic exception for not logged in users We have various place in MediaWiki core and in extensions which are showing anonymous user a very standard error page about them not being logged in. Each developer ends up writing its own because we do not provide a generic error, that is what this patch does. This UserNotLoggedIn exception, when called, will show the usual ErrorPage with a default title and default reason text. That makes it as easy to use as doing: if( $user->isAnon() ) { throw new UserNotLoggedIn(); } One can override the default reason by passing a message key as the first parameter: if( $user->isAnon() ) { throw new UserNotLoggedIn( 'nologin-reason-text' ); } In that case, the page title will still be the default 'Not Logged In.' Change-Id: Id81272995627bf0f5bbef785230a8e6e4e8582ca --- RELEASE-NOTES-1.20 | 2 ++ includes/Exception.php | 43 ++++++++++++++++++++++++++++++ languages/messages/MessagesEn.php | 2 ++ languages/messages/MessagesQqq.php | 2 ++ maintenance/language/messages.inc | 2 ++ 5 files changed, 51 insertions(+) diff --git a/RELEASE-NOTES-1.20 b/RELEASE-NOTES-1.20 index 0745cb7537..5900e614a1 100644 --- a/RELEASE-NOTES-1.20 +++ b/RELEASE-NOTES-1.20 @@ -75,6 +75,8 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki. * (bug 23427) New magic word {{PAGEID}} which gives the current page ID. Will be null on previewing a page being created. * Added ContribsPager::reallyDoQuery hook allowing extensions to data to MyContribs +* (bug 37627) UserNotLoggedIn() exception to show a generic error page whenever + a user is not logged in. === Bug fixes in 1.20 === * (bug 30245) Use the correct way to construct a log page title. diff --git a/includes/Exception.php b/includes/Exception.php index 4fc66db42d..502f2ade16 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -463,6 +463,49 @@ class UserBlockedError extends ErrorPageError { } } +/** + * Shows a generic "user is not logged in" error page. + * + * This is essentially an ErrorPageError exception which by default use the + * 'exception-nologin' as a title and 'exception-nologin-text' for the message. + * @see bug 37627 + * + * @par Example: + * @code + * if( $user->isAnon ) { + * throw new UserNotLoggedIn(); + * } + * @endcode + * + * Please note the parameters are mixed up compared to ErrorPageError, this + * is done to be able to simply specify a reason whitout overriding the default + * title. + * + * @par Example: + * @code + * if( $user->isAnon ) { + * throw new UserNotLoggedIn( 'action-require-loggedin' ); + * } + * @endcode + * + * @param $reasonMsg A message key containing the reason for the error. + * Optional, default: 'exception-nologin-text' + * @param $titleMsg A message key to set the page title. + * Optional, default: 'exception-nologin' + * @param $params Parameters to wfMsg(). + * Optiona, default: null + */ +class UserNotLoggedIn extends ErrorPageError { + + public function __construct( + $reasonMsg = 'exception-nologin-text', + $titleMsg = 'exception-nologin', + $params = null + ) { + parent::__construct( $titleMsg, $reasonMsg, $params ); + } +} + /** * Show an error that looks like an HTTP server error. * Replacement for wfHttpError(). diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 181f44ac8c..2e1f2fad17 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -1087,6 +1087,8 @@ Do not forget to change your [[Special:Preferences|{{SITENAME}} preferences]].', Please choose a different name.', 'loginerror' => 'Login error', 'createaccounterror' => 'Could not create account: $1', +'exception-nologin' => 'Not logged in.', +'exception-nologin-text' => 'This page or action requires you to be logged in on this wiki.', 'nocookiesnew' => 'The user account was created, but you are not logged in. {{SITENAME}} uses cookies to log in users. You have cookies disabled. diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 91f5572aa1..9866eb712e 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -744,6 +744,8 @@ It is also used on the top of the page for logged out users, where it appears ne 'createaccountreason' => '{{Identical|Reason}}', 'createaccounterror' => 'Parameters: * $1 is an error message', +'exception-nologin' => 'Generic page title used on error page when a user is not logged in. Message used by the UserNotLoggedIn exception.', +'exception-nologin-text' => 'Generic reason displayed on error page when a user is not logged in. Message used by the UserNotLoggedIn exception.', 'nocookiesnew' => "This message is displayed when a new account was successfully created, but the browser doesn't accept cookies.", 'nocookieslogin' => "This message is displayed when someone tried to login, but the browser doesn't accept cookies.", 'nocookiesfornew' => "This message is displayed when the user tried to create a new account, but it failed the cross-site request forgery (CSRF) check. It could be blocking an attack, but most likely, the browser isn't accepting cookies.", diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 30c3d9ab31..befff60b33 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -412,6 +412,8 @@ $wgMessageStructure = array( 'filereadonlyerror', 'invalidtitle-knownnamespace', 'invalidtitle-unknownnamespace', + 'exception-nologin', + 'exception-nologin-text', ), 'virus' => array( 'virus-badscanner', -- 2.20.1