MediaWiki master
UserNotLoggedIn.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Exception;
22
23use LoginHelper;
26
70
71 private bool $alwaysRedirectToLoginPage;
72
92 public function __construct(
93 $reasonMsg = 'exception-nologin-text',
94 $titleMsg = 'exception-nologin',
95 $params = [],
96 bool $alwaysRedirectToLoginPage = false
97 ) {
98 $context = RequestContext::getMain();
99 // Replace the reason message for one that describes creating account when the user is a temporary account
100 // when such a custom message exists (T358586).
101 if ( $context->getUser()->isTemp() && !$alwaysRedirectToLoginPage ) {
102 // For grep to find usages: exception-nologin-text-for-temp-user
103 $tempUserReasonMsg = $reasonMsg . '-for-temp-user';
104 if ( $context->msg( $tempUserReasonMsg )->exists() ) {
105 $reasonMsg = $tempUserReasonMsg;
106 }
107 }
108 parent::__construct( $titleMsg, $reasonMsg, $params );
109 $this->alwaysRedirectToLoginPage = $alwaysRedirectToLoginPage;
110 }
111
117 public function report( $action = self::SEND_OUTPUT ) {
118 // If an unsupported message is used, don't try redirecting to Special:Userlogin,
119 // since the message may not be compatible.
120 if ( !in_array( $this->msg, LoginHelper::getValidErrorMessages() ) ) {
121 parent::report( $action );
122 return;
123 }
124
125 $context = RequestContext::getMain();
126
127 // Message is valid. Redirect to Special:Userlogin, unless the user is a temporary account in which case
128 // redirect to Special:CreateAccount (T358586).
129 $specialPageName = 'Userlogin';
130 if ( $context->getUser()->isTemp() && !$this->alwaysRedirectToLoginPage ) {
131 $specialPageName = 'CreateAccount';
132 }
133
134 $output = $context->getOutput();
135 $query = $context->getRequest()->getQueryValues();
136 // Title will be overridden by returnto
137 unset( $query['title'] );
138 // Redirect to Special:Userlogin
139 $output->redirect( SpecialPage::getTitleFor( $specialPageName )->getFullURL( [
140 // Return to this page when the user logs in
141 'returnto' => $context->getTitle()->getFullText(),
142 'returntoquery' => wfArrayToCgi( $query ),
143 'warning' => $this->msg,
144 // Forward the 'display' parameter if provided
145 'display' => $query['display'] ?? null,
146 ] ) );
147
148 if ( $action === self::SEND_OUTPUT ) {
149 $output->output();
150 }
151 }
152}
153
155class_alias( UserNotLoggedIn::class, 'UserNotLoggedIn' );
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
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.
Group all the pieces relevant to the context of a request into one instance.
An error page which can definitely be safely rendered using the OutputPage.
msg( $key, $fallback,... $params)
Get a message from i18n.
Redirect a user to the login page or account creation page.
__construct( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin', $params=[], bool $alwaysRedirectToLoginPage=false)
report( $action=self::SEND_OUTPUT)
Redirect to Special:Userlogin or Special:CreateAccount if the specified message is compatible.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...