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