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