UserLogin: Allow extensions to add valid error messages
authorFlorianschmidtwelzow <florian.schmidt.welzow@t-online.de>
Sat, 1 Nov 2014 16:09:58 +0000 (17:09 +0100)
committerFlorianschmidtwelzow <florian.schmidt.welzow@t-online.de>
Mon, 3 Nov 2014 20:28:18 +0000 (21:28 +0100)
Add hook LoginFormValidErrorMessages to allow extensions, to add own valid
error messages to redirect to the login form.

Bug: 71769
Change-Id: I9e996a88e3972f09946726060916a21124de049c

docs/hooks.txt
includes/exception/UserNotLoggedIn.php
includes/specials/SpecialUserlogin.php

index 52eeab8..7afbf1c 100644 (file)
@@ -3005,6 +3005,11 @@ when UserMailer sends an email, with a bounce handling extension.
 $to: Array of MailAddress objects for the recipients
 &$returnPath: The return address string
 
+'LoginFormValidErrorMessages': Called in LoginForm when a function gets valid error
+messages. Allows to add additional error messages (except messages already in
+LoginForm::$validErrorMessages).
+&$messages Already added messages (inclusive messages from LoginForm::$validErrorMessages)
+
 'WantedPages::getQueryInfo': Called in WantedPagesPage::getQueryInfo(), can be
 used to alter the SQL query which gets the list of wanted pages.
 &$wantedPages: WantedPagesPage object
index 03ba0b2..02fca3d 100644 (file)
@@ -25,8 +25,9 @@
  * 'exception-nologin' as a title and 'exception-nologin-text' for the message.
  *
  * @note In order for this exception to redirect, the error message passed to the
- * constructor has to be explicitly added to LoginForm::validErrorMessages. Otherwise,
- * the user will just be shown the message rather than redirected.
+ * constructor has to be explicitly added to LoginForm::validErrorMessages or with
+ * the LoginFormValidErrorMessages hook. Otherwise, the user will just be shown the message
+ * rather than redirected.
  *
  * @par Example:
  * @code
@@ -52,7 +53,8 @@
 class UserNotLoggedIn extends ErrorPageError {
 
        /**
-        * @note The value of the $reasonMsg parameter must be put into LoginForm::validErrorMessages
+        * @note The value of the $reasonMsg parameter must be put into LoginForm::validErrorMessages or
+        * set with the LoginFormValidErrorMessages Hook.
         * if you want the user to be automatically redirected to the login form.
         *
         * @param string $reasonMsg A message key containing the reason for the error.
@@ -77,7 +79,7 @@ class UserNotLoggedIn extends ErrorPageError {
        public function report() {
                // If an unsupported message is used, don't try redirecting to Special:Userlogin,
                // since the message may not be compatible.
-               if ( !in_array( $this->msg, LoginForm::$validErrorMessages ) ) {
+               if ( !in_array( $this->msg, LoginForm::getValidErrorMessages() ) ) {
                        parent::report();
                }
 
index bdd6751..b6a3be2 100644 (file)
@@ -113,6 +113,21 @@ class LoginForm extends SpecialPage {
                $wgUseMediaWikiUIEverywhere = true;
        }
 
+       /**
+        * Returns an array of all valid error messages.
+        *
+        * @return array
+        */
+       public static function getValidErrorMessages() {
+               static $messages = null;
+               if ( !$messages ) {
+                       $messages = self::$validErrorMessages;
+                       wfRunHooks( 'LoginFormValidErrorMessages', array( &$messages ) );
+               }
+
+               return $messages;
+       }
+
        /**
         * Loader
         */
@@ -175,13 +190,13 @@ class LoginForm extends SpecialPage {
 
                // Only show valid error or warning messages.
                if ( $entryError->exists()
-                       && in_array( $entryError->getKey(), self::$validErrorMessages )
+                       && in_array( $entryError->getKey(), self::getValidErrorMessages() )
                ) {
                        $this->mEntryErrorType = 'error';
                        $this->mEntryError = $entryError->rawParams( $loginreqlink )->escaped();
 
                } elseif ( $entryWarning->exists()
-                       && in_array( $entryWarning->getKey(), self::$validErrorMessages )
+                       && in_array( $entryWarning->getKey(), self::getValidErrorMessages() )
                ) {
                        $this->mEntryErrorType = 'warning';
                        $this->mEntryError = $entryWarning->rawParams( $loginreqlink )->escaped();