Merge "Revert "Made LCStoreDB try to use a separate DB connection""
[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 LoginForm::validErrorMessages. Otherwise,
29 * the user will just be shown the message rather than redirected.
30 *
31 * @par Example:
32 * @code
33 * if( $user->isAnon() ) {
34 * throw new UserNotLoggedIn();
35 * }
36 * @endcode
37 *
38 * Note the parameter order differs from ErrorPageError, this allows you to
39 * simply specify a reason without overriding the default title.
40 *
41 * @par Example:
42 * @code
43 * if( $user->isAnon() ) {
44 * throw new UserNotLoggedIn( 'action-require-loggedin' );
45 * }
46 * @endcode
47 *
48 * @see bug 37627
49 * @since 1.20
50 * @ingroup Exception
51 */
52 class UserNotLoggedIn extends ErrorPageError {
53
54 /**
55 * @note The value of the $reasonMsg parameter must be put into LoginForm::validErrorMessages
56 * if you want the user to be automatically redirected to the login form.
57 *
58 * @param string $reasonMsg A message key containing the reason for the error.
59 * Optional, default: 'exception-nologin-text'
60 * @param string $titleMsg A message key to set the page title.
61 * Optional, default: 'exception-nologin'
62 * @param array $params Parameters to wfMessage().
63 * Optional, default: array()
64 */
65 public function __construct(
66 $reasonMsg = 'exception-nologin-text',
67 $titleMsg = 'exception-nologin',
68 $params = array()
69 ) {
70 parent::__construct( $titleMsg, $reasonMsg, $params );
71 }
72
73 /**
74 * Redirect to Special:Userlogin if the specified message is compatible. Otherwise,
75 * show an error page as usual.
76 */
77 public function report() {
78 // If an unsupported message is used, don't try redirecting to Special:Userlogin,
79 // since the message may not be compatible.
80 if ( !in_array( $this->msg, LoginForm::$validErrorMessages ) ) {
81 parent::report();
82 }
83
84 // Message is valid. Redirec to Special:Userlogin
85
86 $context = RequestContext::getMain();
87
88 $output = $context->getOutput();
89 $query = $context->getRequest()->getValues();
90 // Title will be overridden by returnto
91 unset( $query['title'] );
92 // Redirect to Special:Userlogin
93 $output->redirect( SpecialPage::getTitleFor( 'Userlogin' )->getFullURL( array(
94 // Return to this page when the user logs in
95 'returnto' => $context->getTitle()->getFullText(),
96 'returntoquery' => wfArrayToCgi( $query ),
97 'warning' => $this->msg,
98 ) ) );
99
100 $output->output();
101 }
102 }