MediaWiki master
UserNotLoggedIn.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Exception;
8
11
55
56 private bool $alwaysRedirectToLoginPage;
57
77 public function __construct(
78 $reasonMsg = 'exception-nologin-text',
79 $titleMsg = 'exception-nologin',
80 $params = [],
81 bool $alwaysRedirectToLoginPage = false
82 ) {
83 $context = RequestContext::getMain();
84 // Replace the reason message for one that describes creating account when the user is a temporary account
85 // when such a custom message exists (T358586).
86 if ( $context->getUser()->isTemp() && !$alwaysRedirectToLoginPage ) {
87 // For grep to find usages: exception-nologin-text-for-temp-user
88 $tempUserReasonMsg = $reasonMsg . '-for-temp-user';
89 if ( $context->msg( $tempUserReasonMsg )->exists() ) {
90 $reasonMsg = $tempUserReasonMsg;
91 }
92 }
93 parent::__construct( $titleMsg, $reasonMsg, $params );
94 $this->alwaysRedirectToLoginPage = $alwaysRedirectToLoginPage;
95 }
96
102 public function report( $action = self::SEND_OUTPUT ) {
103 // If an unsupported message is used, don't try redirecting to Special:Userlogin,
104 // since the message may not be compatible.
105 if ( !in_array( $this->msg, LoginErrorHelper::getValidErrorMessages() ) ) {
106 parent::report( $action );
107 return;
108 }
109
110 $context = RequestContext::getMain();
111
112 // Message is valid. Redirect to Special:Userlogin, unless the user is a temporary account in which case
113 // redirect to Special:CreateAccount (T358586).
114 $specialPageName = 'Userlogin';
115 if ( $context->getUser()->isTemp() && !$this->alwaysRedirectToLoginPage ) {
116 $specialPageName = 'CreateAccount';
117 }
118
119 $output = $context->getOutput();
120 $query = $context->getRequest()->getQueryValues();
121 // Title will be overridden by returnto
122 unset( $query['title'] );
123 // Redirect to Special:Userlogin
124 $output->redirect( SpecialPage::getTitleFor( $specialPageName )->getFullURL( [
125 // Return to this page when the user logs in
126 'returnto' => $context->getTitle()->getFullText(),
127 'returntoquery' => wfArrayToCgi( $query ),
128 'warning' => $this->msg,
129 // Forward the 'display' parameter if provided
130 'display' => $query['display'] ?? null,
131 ] ) );
132
133 if ( $action === self::SEND_OUTPUT ) {
134 $output->output();
135 }
136 }
137}
138
140class_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....
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.
static getValidErrorMessages()
Returns an array of all error and warning messages that can be displayed on Special:UserLogin or Spec...
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,...