MediaWiki master
LoginHelper.php
Go to the documentation of this file.
1<?php
2
6use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
10
17 use ProtectedHookAccessorTrait;
18
24 public static $validErrorMessages = [
25 'exception-nologin-text',
26 'exception-nologin-text-for-temp-user',
27 'watchlistanontext',
28 'watchlistanontext-for-temp-user',
29 'changeemail-no-info',
30 'confirmemail_needlogin',
31 'prefsnologintext2',
32 'prefsnologintext2-for-temp-user',
33 'specialmute-login-required',
34 'specialmute-login-required-for-temp-user',
35 'mailnologintext',
36 ];
37
39 private static ?array $validErrorMessagesCache = null;
40
54 public static function getValidErrorMessages(): array {
55 if ( !static::$validErrorMessagesCache ) {
56 static::$validErrorMessagesCache = self::$validErrorMessages;
57 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
58 ->onLoginFormValidErrorMessages( static::$validErrorMessagesCache );
59 }
60
61 return static::$validErrorMessagesCache;
62 }
63
64 public function __construct( IContextSource $context ) {
65 $this->setContext( $context );
66 }
67
86 public function showReturnToPage(
87 $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
88 ) {
89 $config = $this->getConfig();
90 if ( $type !== 'error' && $config->get( MainConfigNames::RedirectOnLogin ) !== null ) {
91 $returnTo = $config->get( MainConfigNames::RedirectOnLogin );
92 $returnToQuery = [];
93 } elseif ( is_string( $returnToQuery ) ) {
94 $returnToQuery = wfCgiToArray( $returnToQuery );
95 }
96 if ( $returnToAnchor !== '' && $returnToAnchor[0] !== '#' ) {
97 $returnToAnchor = '';
98 }
99
100 // Allow modification of redirect behavior
101 $oldReturnTo = $returnTo;
102 $oldReturnToQuery = $returnToQuery;
103 $this->getHookRunner()->onPostLoginRedirect( $returnTo, $returnToQuery, $type );
104 if ( $returnTo !== $oldReturnTo || $returnToQuery !== $oldReturnToQuery ) {
105 // PostLoginRedirect does not handle $returnToAnchor, and changing hooks is hard.
106 // At least don't add the anchor if the hook changed the URL.
107 $returnToAnchor = '';
108 }
109
110 $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
111
112 if ( $config->get( MainConfigNames::ForceHTTPS )
113 || ( $config->get( MainConfigNames::SecureLogin ) && $stickHTTPS )
114 ) {
115 $options = [ 'https' ];
116 $proto = PROTO_HTTPS;
117 } elseif ( $config->get( MainConfigNames::SecureLogin ) && !$stickHTTPS ) {
118 $options = [ 'http' ];
119 $proto = PROTO_HTTP;
120 } else {
121 $options = [];
122 $proto = PROTO_RELATIVE;
123 }
124
125 if ( $type === 'successredirect' ) {
126 $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
127 . $returnToAnchor;
128 $this->getOutput()->redirect( $redirectUrl );
129 } else {
130 $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
131 }
132 }
133}
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:68
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.
static string[] $validErrorMessages
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.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:69
Interface for objects which can provide a MediaWiki context on request.