(bug 37627) generic exception for not logged in users
authorAntoine Musso <hashar@free.fr>
Fri, 22 Jun 2012 10:37:47 +0000 (12:37 +0200)
committerAntoine Musso <hashar@free.fr>
Tue, 26 Jun 2012 14:03:42 +0000 (16:03 +0200)
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
includes/Exception.php
languages/messages/MessagesEn.php
languages/messages/MessagesQqq.php
maintenance/language/messages.inc

index 0745cb7..5900e61 100644 (file)
@@ -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.
index 4fc66db..502f2ad 100644 (file)
@@ -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().
index 181f44a..2e1f2fa 100644 (file)
@@ -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.
index 91f5572..9866eb7 100644 (file)
@@ -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.",
index 30c3d9a..befff60 100644 (file)
@@ -412,6 +412,8 @@ $wgMessageStructure = array(
                'filereadonlyerror',
                'invalidtitle-knownnamespace',
                'invalidtitle-unknownnamespace',
+               'exception-nologin',
+               'exception-nologin-text',
        ),
        'virus' => array(
                'virus-badscanner',