Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / exception / UserNotLoggedIn.php
blob768a902d79fd5694d242eca3ec5e622d891694a1
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Context\RequestContext;
22 use MediaWiki\SpecialPage\SpecialPage;
24 /**
25 * Redirect a user to the login page or account creation page
27 * This is essentially an ErrorPageError exception which by default uses the
28 * 'exception-nologin' as a title and 'exception-nologin-text' for the message.
30 * When the user is a temporary account, the redirect will point to the Special:CreateAccount page unless
31 * specifically set not to. In all other cases, the redirect is to the Special:UserLogin page.
33 * The message key for the reason will be modified to include '-for-temp-user' when the user is logged
34 * in to a temporary account and this message key exists (i.e. defined and not empty).
36 * @note In order for this exception to redirect, the error message passed to the
37 * constructor has to be explicitly added to LoginHelper::validErrorMessages or with
38 * the LoginFormValidErrorMessages hook. Otherwise, the user will just be shown the message
39 * rather than redirected.
41 * @par Example:
42 * @code
43 * if ( $user->isAnon() ) {
44 * throw new UserNotLoggedIn();
45 * }
46 * @endcode
48 * Note the parameter order differs from ErrorPageError, this allows you to
49 * simply specify a reason without overriding the default title.
51 * @par Example:
52 * @code
53 * if ( $user->isAnon() ) {
54 * throw new UserNotLoggedIn( 'action-require-loggedin' );
55 * }
56 * @endcode
58 * You can use {@link SpecialPage::requireLogin} and {@link SpecialPage::requireNamedUser} to throw this
59 * exception when the user is an anon user or not named respectively.
61 * @newable
62 * @see T39627
63 * @since 1.20
64 * @ingroup Exception
66 class UserNotLoggedIn extends ErrorPageError {
68 private bool $alwaysRedirectToLoginPage;
70 /**
71 * @stable to call
73 * @note The value of the $reasonMsg parameter must be set with the LoginFormValidErrorMessages
74 * hook if you want the user to be automatically redirected to the login form.
76 * @param string $reasonMsg A message key containing the reason for the error. '-for-temp-user' will be
77 * appended to the end of the message key if the user is a temporary account and the redirect is
78 * to the Special:CreateAccount page. The modification is skipped if the message key does not
79 * exist.
80 * Optional, default: 'exception-nologin-text'
81 * @param string $titleMsg A message key to set the page title.
82 * Optional, default: 'exception-nologin'
83 * @param array $params Parameters to wfMessage() for $reasonMsg and $tempUserReasonMsg
84 * Optional, default: []
85 * @param bool $alwaysRedirectToLoginPage Whether we should always redirect to the login page, even if the
86 * user is a temporary account. If false (the default), the redirect will be to Special:CreateAccount
87 * when the user is logged in to a temporary account.
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;
105 parent::__construct( $titleMsg, $reasonMsg, $params );
106 $this->alwaysRedirectToLoginPage = $alwaysRedirectToLoginPage;
110 * Redirect to Special:Userlogin or Special:CreateAccount if the specified message is compatible. Otherwise,
111 * show an error page as usual.
112 * @param int $action
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;
122 $context = RequestContext::getMain();
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';
131 $output = $context->getOutput();
132 $query = $context->getRequest()->getQueryValues();
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 ] ) );
145 if ( $action === self::SEND_OUTPUT ) {
146 $output->output();