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
22 * Redirect a user to the login page
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.
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.
34 * if( $user->isAnon() ) {
35 * throw new UserNotLoggedIn();
39 * Note the parameter order differs from ErrorPageError, this allows you to
40 * simply specify a reason without overriding the default title.
44 * if( $user->isAnon() ) {
45 * throw new UserNotLoggedIn( 'action-require-loggedin' );
53 class UserNotLoggedIn
extends ErrorPageError
{
56 * @note The value of the $reasonMsg parameter must be put into LoginForm::validErrorMessages or
57 * set with the LoginFormValidErrorMessages Hook.
58 * if you want the user to be automatically redirected to the login form.
60 * @param string $reasonMsg A message key containing the reason for the error.
61 * Optional, default: 'exception-nologin-text'
62 * @param string $titleMsg A message key to set the page title.
63 * Optional, default: 'exception-nologin'
64 * @param array $params Parameters to wfMessage().
65 * Optional, default: []
67 public function __construct(
68 $reasonMsg = 'exception-nologin-text',
69 $titleMsg = 'exception-nologin',
72 parent
::__construct( $titleMsg, $reasonMsg, $params );
76 * Redirect to Special:Userlogin if the specified message is compatible. Otherwise,
77 * show an error page as usual.
79 public function report() {
80 // If an unsupported message is used, don't try redirecting to Special:Userlogin,
81 // since the message may not be compatible.
82 if ( !in_array( $this->msg
, LoginHelper
::getValidErrorMessages() ) ) {
86 // Message is valid. Redirec to Special:Userlogin
88 $context = RequestContext
::getMain();
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
,