Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
28 / 42
58.33% covered (warning)
58.33%
7 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialUserLogout
68.29% covered (warning)
68.29%
28 / 41
58.33% covered (warning)
58.33%
7 / 12
22.17
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
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isListed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFormFields
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDisplayFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
2.31
 alterForm
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 onSubmit
41.67% covered (danger)
41.67%
5 / 12
0.00% covered (danger)
0.00%
0 / 1
2.79
 onSuccess
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 showSuccess
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 requiresUnblock
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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
21namespace MediaWiki\Specials;
22
23use ErrorPageError;
24use MediaWiki\HTMLForm\HTMLForm;
25use MediaWiki\Session\SessionManager;
26use MediaWiki\SpecialPage\FormSpecialPage;
27use MediaWiki\SpecialPage\SpecialPage;
28use MediaWiki\Status\Status;
29
30/**
31 * Implements Special:Userlogout
32 *
33 * @ingroup SpecialPage
34 * @ingroup Auth
35 */
36class SpecialUserLogout extends FormSpecialPage {
37    /**
38     * @var string
39     */
40    private $oldUserName;
41
42    public function __construct() {
43        parent::__construct( 'Userlogout' );
44    }
45
46    public function doesWrites() {
47        return true;
48    }
49
50    public function isListed() {
51        return $this->getAuthManager()->canAuthenticateNow();
52    }
53
54    protected function getGroupName() {
55        return 'login';
56    }
57
58    protected function getFormFields() {
59        return [];
60    }
61
62    protected function getDisplayFormat() {
63        return 'ooui';
64    }
65
66    public function execute( $par ) {
67        $user = $this->getUser();
68        if ( $user->isAnon() ) {
69            $this->setHeaders();
70            $this->showSuccess();
71            return;
72        }
73        $this->oldUserName = $user->getName();
74
75        parent::execute( $par );
76    }
77
78    public function alterForm( HTMLForm $form ) {
79        $form->setTokenSalt( 'logoutToken' );
80        $form->addHeaderHtml( $this->msg(
81            $this->getUser()->isTemp() ? 'userlogout-temp' : 'userlogout-continue'
82        ) );
83
84        $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
85    }
86
87    /**
88     * Process the form.  At this point we know that the user passes all the criteria in
89     * userCanExecute(), and if the data array contains 'Username', etc, then Username
90     * resets are allowed.
91     * @param array $data
92     * @return Status
93     */
94    public function onSubmit( array $data ) {
95        // Make sure it's possible to log out
96        $session = SessionManager::getGlobalSession();
97        if ( !$session->canSetUser() ) {
98            throw new ErrorPageError(
99                'cannotlogoutnow-title',
100                'cannotlogoutnow-text',
101                [
102                    $session->getProvider()->describe( $this->getLanguage() )
103                ]
104            );
105        }
106
107        $user = $this->getUser();
108
109        $user->logout();
110        return new Status();
111    }
112
113    public function onSuccess() {
114        $this->showSuccess();
115
116        $out = $this->getOutput();
117        // Hook.
118        $injected_html = '';
119        $this->getHookRunner()->onUserLogoutComplete( $this->getUser(), $injected_html, $this->oldUserName );
120        $out->addHTML( $injected_html );
121    }
122
123    private function showSuccess() {
124        $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
125            $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
126
127        $out = $this->getOutput();
128        $out->addWikiMsg( 'logouttext', $loginURL );
129
130        $out->returnToMain();
131    }
132
133    /**
134     * Let blocked users to log out and come back with their sockpuppets
135     * @return bool
136     */
137    public function requiresUnblock() {
138        return false;
139    }
140}
141
142/**
143 * Retain the old class name for backwards compatibility.
144 * @deprecated since 1.41
145 */
146class_alias( SpecialUserLogout::class, 'SpecialUserLogout' );