From 1c0e593bc23d700e1fb9152fc60afe78d39d1cb2 Mon Sep 17 00:00:00 2001 From: Florianschmidtwelzow Date: Sat, 1 Nov 2014 17:09:58 +0100 Subject: [PATCH] UserLogin: Allow extensions to add valid error messages 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 | 5 +++++ includes/exception/UserNotLoggedIn.php | 10 ++++++---- includes/specials/SpecialUserlogin.php | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/hooks.txt b/docs/hooks.txt index 52eeab8356..7afbf1c1b7 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -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 diff --git a/includes/exception/UserNotLoggedIn.php b/includes/exception/UserNotLoggedIn.php index 03ba0b2039..02fca3d8d0 100644 --- a/includes/exception/UserNotLoggedIn.php +++ b/includes/exception/UserNotLoggedIn.php @@ -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(); } diff --git a/includes/specials/SpecialUserlogin.php b/includes/specials/SpecialUserlogin.php index bdd6751ea8..b6a3be2904 100644 --- a/includes/specials/SpecialUserlogin.php +++ b/includes/specials/SpecialUserlogin.php @@ -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(); -- 2.20.1