MediaWiki master
LoginHelper.php
Go to the documentation of this file.
1<?php
2
6use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
10
17 use ProtectedHookAccessorTrait;
18
31 public static $validErrorMessages = [
32 'exception-nologin-text',
33 'exception-nologin-text-for-temp-user',
34 'watchlistanontext',
35 'watchlistanontext-for-temp-user',
36 'changeemail-no-info',
37 'confirmemail_needlogin',
38 'prefsnologintext2',
39 'prefsnologintext2-for-temp-user',
40 'specialmute-login-required',
41 'specialmute-login-required-for-temp-user',
42 ];
43
50 public static function getValidErrorMessages() {
51 static $messages = null;
52 if ( !$messages ) {
53 $messages = self::$validErrorMessages;
54 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
55 ->onLoginFormValidErrorMessages( $messages );
56 }
57
58 return $messages;
59 }
60
61 public function __construct( IContextSource $context ) {
62 $this->setContext( $context );
63 }
64
83 public function showReturnToPage(
84 $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
85 ) {
86 $config = $this->getConfig();
87 if ( $type !== 'error' && $config->get( MainConfigNames::RedirectOnLogin ) !== null ) {
88 $returnTo = $config->get( MainConfigNames::RedirectOnLogin );
89 $returnToQuery = [];
90 } elseif ( is_string( $returnToQuery ) ) {
91 $returnToQuery = wfCgiToArray( $returnToQuery );
92 }
93 if ( $returnToAnchor !== '' && $returnToAnchor[0] !== '#' ) {
94 $returnToAnchor = '';
95 }
96
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 = '';
105 }
106
107 $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
108
109 if ( $config->get( MainConfigNames::ForceHTTPS )
110 || ( $config->get( MainConfigNames::SecureLogin ) && $stickHTTPS )
111 ) {
112 $options = [ 'https' ];
113 $proto = PROTO_HTTPS;
114 } elseif ( $config->get( MainConfigNames::SecureLogin ) && !$stickHTTPS ) {
115 $options = [ 'http' ];
116 $proto = PROTO_HTTP;
117 } else {
118 $options = [];
119 $proto = PROTO_RELATIVE;
120 }
121
122 if ( $type === 'successredirect' ) {
123 $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
124 . $returnToAnchor;
125 $this->getOutput()->redirect( $redirectUrl );
126 } else {
127 $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
128 }
129 }
130}
const PROTO_HTTPS
Definition Defines.php:205
const PROTO_HTTP
Definition Defines.php:204
const PROTO_RELATIVE
Definition Defines.php:206
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
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 valid error messages.
__construct(IContextSource $context)
showReturnToPage( $type, $returnTo='', $returnToQuery='', $stickHTTPS=false, $returnToAnchor='')
Show a return link or redirect to it.
static string[] $validErrorMessages
Valid error and warning messages.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
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:78
Interface for objects which can provide a MediaWiki context on request.