MediaWiki master
LoginHelper.php
Go to the documentation of this file.
1<?php
2
4
8use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
12
19 use ProtectedHookAccessorTrait;
20
24 private static $validErrorMessages = [
25 'exception-nologin-text',
26 'exception-nologin-text-for-temp-user',
27 'watchlistanontext',
28 'watchlistanontext-for-temp-user',
29 'watchlistlabels-not-logged-in',
30 'watchlistlabels-not-logged-in-for-temp-user',
31 'changeemail-no-info',
32 'confirmemail_needlogin',
33 'prefsnologintext2',
34 'prefsnologintext2-for-temp-user',
35 'specialmute-login-required',
36 'specialmute-login-required-for-temp-user',
37 'mailnologintext',
38 ];
39
41 private static ?array $validErrorMessagesCache = null;
42
56 public static function getValidErrorMessages(): array {
57 if ( !static::$validErrorMessagesCache ) {
58 static::$validErrorMessagesCache = self::$validErrorMessages;
59 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
60 ->onLoginFormValidErrorMessages( static::$validErrorMessagesCache );
61 }
62
63 return static::$validErrorMessagesCache;
64 }
65
66 public function __construct( IContextSource $context ) {
67 $this->setContext( $context );
68 }
69
88 public function showReturnToPage(
89 $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
90 ) {
91 $config = $this->getConfig();
92 if ( $type !== 'error' && $config->get( MainConfigNames::RedirectOnLogin ) !== null ) {
93 $returnTo = $config->get( MainConfigNames::RedirectOnLogin );
94 $returnToQuery = [];
95 } elseif ( is_string( $returnToQuery ) ) {
96 $returnToQuery = wfCgiToArray( $returnToQuery );
97 }
98 if ( $returnToAnchor !== '' && $returnToAnchor[0] !== '#' ) {
99 $returnToAnchor = '';
100 }
101
102 // Allow modification of redirect behavior
103 $oldReturnTo = $returnTo;
104 $oldReturnToQuery = $returnToQuery;
105 $this->getHookRunner()->onPostLoginRedirect( $returnTo, $returnToQuery, $type );
106 if ( $returnTo !== $oldReturnTo || $returnToQuery !== $oldReturnToQuery ) {
107 // PostLoginRedirect does not handle $returnToAnchor, and changing hooks is hard.
108 // At least don't add the anchor if the hook changed the URL.
109 $returnToAnchor = '';
110 }
111
112 $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
113
114 if ( $config->get( MainConfigNames::ForceHTTPS )
115 || ( $config->get( MainConfigNames::SecureLogin ) && $stickHTTPS )
116 ) {
117 $options = [ 'https' ];
118 $proto = PROTO_HTTPS;
119 } elseif ( $config->get( MainConfigNames::SecureLogin ) && !$stickHTTPS ) {
120 $options = [ 'http' ];
121 $proto = PROTO_HTTP;
122 } else {
123 $options = [];
124 $proto = PROTO_RELATIVE;
125 }
126
127 if ( $type === 'successredirect' ) {
128 $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
129 . $returnToAnchor;
130 $this->getOutput()->redirect( $redirectUrl );
131 } else {
132 $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
133 }
134 }
135}
136
138class_alias( LoginHelper::class, 'LoginHelper' );
const PROTO_HTTPS
Definition Defines.php:218
const PROTO_HTTP
Definition Defines.php:217
const PROTO_RELATIVE
Definition Defines.php:219
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
const ForceHTTPS
Name constant for the ForceHTTPS setting, for use with Config::get()
const RedirectOnLogin
Name constant for the RedirectOnLogin setting, for use with Config::get()
const SecureLogin
Name constant for the SecureLogin setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Helper functions for the login form that need to be shared with other special pages (such as CentralA...
static getValidErrorMessages()
Returns an array of all error and warning messages that can be displayed on Special:UserLogin or Spec...
__construct(IContextSource $context)
showReturnToPage( $type, $returnTo='', $returnToQuery='', $stickHTTPS=false, $returnToAnchor='')
Show a return link or redirect to it.
Represents a title within MediaWiki.
Definition Title.php:69
Interface for objects which can provide a MediaWiki context on request.