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