Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / exception / UserNotLoggedIn.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 /**
22 * Redirect a user to the login page
23 *
24 * This is essentially an ErrorPageError exception which by default uses the
25 * 'exception-nologin' as a title and 'exception-nologin-text' for the message.
26 *
27 * @note In order for this exception to redirect, the error message passed to the
28 * constructor has to be explicitly added to LoginHelper::validErrorMessages or with
29 * the LoginFormValidErrorMessages hook. Otherwise, the user will just be shown the message
30 * rather than redirected.
31 *
32 * @par Example:
33 * @code
34 * if( $user->isAnon() ) {
35 * throw new UserNotLoggedIn();
36 * }
37 * @endcode
38 *
39 * Note the parameter order differs from ErrorPageError, this allows you to
40 * simply specify a reason without overriding the default title.
41 *
42 * @par Example:
43 * @code
44 * if( $user->isAnon() ) {
45 * throw new UserNotLoggedIn( 'action-require-loggedin' );
46 * }
47 * @endcode
48 *
49 * @see T39627
50 * @since 1.20
51 * @ingroup Exception
52 */
53 class UserNotLoggedIn extends ErrorPageError {
54
55 /**
56 * @note The value of the $reasonMsg parameter must be set with the LoginFormValidErrorMessages
57 * hook if you want the user to be automatically redirected to the login form.
58 *
59 * @param string $reasonMsg A message key containing the reason for the error.
60 * Optional, default: 'exception-nologin-text'
61 * @param string $titleMsg A message key to set the page title.
62 * Optional, default: 'exception-nologin'
63 * @param array $params Parameters to wfMessage().
64 * Optional, default: []
65 */
66 public function __construct(
67 $reasonMsg = 'exception-nologin-text',
68 $titleMsg = 'exception-nologin',
69 $params = []
70 ) {
71 parent::__construct( $titleMsg, $reasonMsg, $params );
72 }
73
74 /**
75 * Redirect to Special:Userlogin if the specified message is compatible. Otherwise,
76 * show an error page as usual.
77 */
78 public function report( $action = self::SEND_OUTPUT ) {
79 // If an unsupported message is used, don't try redirecting to Special:Userlogin,
80 // since the message may not be compatible.
81 if ( !in_array( $this->msg, LoginHelper::getValidErrorMessages() ) ) {
82 parent::report( $action );
83 return;
84 }
85
86 // Message is valid. Redirect to Special:Userlogin
87
88 $context = RequestContext::getMain();
89
90 $output = $context->getOutput();
91 $query = $context->getRequest()->getValues();
92 // Title will be overridden by returnto
93 unset( $query['title'] );
94 // Redirect to Special:Userlogin
95 $output->redirect( SpecialPage::getTitleFor( 'Userlogin' )->getFullURL( [
96 // Return to this page when the user logs in
97 'returnto' => $context->getTitle()->getFullText(),
98 'returntoquery' => wfArrayToCgi( $query ),
99 'warning' => $this->msg,
100 ] ) );
101
102 if ( $action === self::SEND_OUTPUT ) {
103 $output->output();
104 }
105 }
106 }