3 use MediaWiki\Context\ContextSource
;
4 use MediaWiki\Context\IContextSource
;
5 use MediaWiki\HookContainer\HookRunner
;
6 use MediaWiki\HookContainer\ProtectedHookAccessorTrait
;
7 use MediaWiki\MainConfigNames
;
8 use MediaWiki\MediaWikiServices
;
9 use MediaWiki\Title\Title
;
12 * Helper functions for the login form that need to be shared with other special pages
13 * (such as CentralAuth's SpecialCentralLogin).
16 class LoginHelper
extends ContextSource
{
17 use ProtectedHookAccessorTrait
;
20 * Valid error and warning messages
22 * Special:Userlogin can show an error or warning message on the form when
23 * coming from another page. This is done via the ?error= or ?warning= GET
26 * This array is the list of valid message keys. Further keys can be added by the
27 * LoginFormValidErrorMessages hook. All other values will be ignored.
31 public static $validErrorMessages = [
32 'exception-nologin-text',
33 'exception-nologin-text-for-temp-user',
35 'watchlistanontext-for-temp-user',
36 'changeemail-no-info',
37 'confirmemail_needlogin',
39 'prefsnologintext2-for-temp-user',
40 'specialmute-login-required',
41 'specialmute-login-required-for-temp-user',
45 * Returns an array of all valid error messages.
48 * @see LoginHelper::$validErrorMessages
50 public static function getValidErrorMessages() {
51 static $messages = null;
53 $messages = self
::$validErrorMessages;
54 ( new HookRunner( MediaWikiServices
::getInstance()->getHookContainer() ) )
55 ->onLoginFormValidErrorMessages( $messages );
61 public function __construct( IContextSource
$context ) {
62 $this->setContext( $context );
66 * Show a return link or redirect to it.
67 * Extensions can change where the link should point or inject content into the page
68 * (which will change it from redirect to link mode).
70 * @param string $type One of the following:
71 * - error: display a return to link ignoring $wgRedirectOnLogin
72 * - success: display a return to link using $wgRedirectOnLogin if needed
73 * - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
74 * - signup: used during signup, functionally identical to 'success'
75 * @param string $returnTo Title of page to return to. Overriden by $wgRedirectOnLogin
76 * when that is set (and $type is not 'error').
77 * @param array|string $returnToQuery Query parameters to return to.
78 * @param bool $stickHTTPS Keep redirect link on HTTPS. Ignored (treated as
79 * true) if $wgForceHTTPS is true.
80 * @param string $returnToAnchor A string to append to the URL, presumed to
81 * be either a fragment including the leading hash or an empty string.
83 public function showReturnToPage(
84 $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
86 $config = $this->getConfig();
87 if ( $type !== 'error' && $config->get( MainConfigNames
::RedirectOnLogin
) !== null ) {
88 $returnTo = $config->get( MainConfigNames
::RedirectOnLogin
);
90 } elseif ( is_string( $returnToQuery ) ) {
91 $returnToQuery = wfCgiToArray( $returnToQuery );
93 if ( $returnToAnchor !== '' && $returnToAnchor[0] !== '#' ) {
97 // Allow modification of redirect behavior
98 $oldReturnTo = $returnTo;
99 $oldReturnToQuery = $returnToQuery;
100 $this->getHookRunner()->onPostLoginRedirect( $returnTo, $returnToQuery, $type );
101 if ( $returnTo !== $oldReturnTo ||
$returnToQuery !== $oldReturnToQuery ) {
102 // PostLoginRedirect does not handle $returnToAnchor, and changing hooks is hard.
103 // At least don't add the anchor if the hook changed the URL.
104 $returnToAnchor = '';
107 $returnToTitle = Title
::newFromText( $returnTo ) ?
: Title
::newMainPage();
109 if ( $config->get( MainConfigNames
::ForceHTTPS
)
110 ||
( $config->get( MainConfigNames
::SecureLogin
) && $stickHTTPS )
112 $options = [ 'https' ];
113 $proto = PROTO_HTTPS
;
114 } elseif ( $config->get( MainConfigNames
::SecureLogin
) && !$stickHTTPS ) {
115 $options = [ 'http' ];
119 $proto = PROTO_RELATIVE
;
122 if ( $type === 'successredirect' ) {
123 $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
125 $this->getOutput()->redirect( $redirectUrl );
127 $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );