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 * Implements Special:Userlogout
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24namespace MediaWiki\Specials;
25
26use ErrorPageError;
27use MediaWiki\HTMLForm\HTMLForm;
28use MediaWiki\Session\SessionManager;
29use MediaWiki\SpecialPage\FormSpecialPage;
30use MediaWiki\SpecialPage\SpecialPage;
31use MediaWiki\Status\Status;
32
33/**
34 * Implements Special:Userlogout
35 *
36 * @ingroup SpecialPage
37 */
38class SpecialUserLogout extends FormSpecialPage {
39    /**
40     * @var string
41     */
42    private $oldUserName;
43
44    public function __construct() {
45        parent::__construct( 'Userlogout' );
46    }
47
48    public function doesWrites() {
49        return true;
50    }
51
52    public function isListed() {
53        return $this->getAuthManager()->canAuthenticateNow();
54    }
55
56    protected function getGroupName() {
57        return 'login';
58    }
59
60    protected function getFormFields() {
61        return [];
62    }
63
64    protected function getDisplayFormat() {
65        return 'ooui';
66    }
67
68    public function execute( $par ) {
69        $user = $this->getUser();
70        if ( $user->isAnon() ) {
71            $this->setHeaders();
72            $this->showSuccess();
73            return;
74        }
75        $this->oldUserName = $user->getName();
76
77        parent::execute( $par );
78    }
79
80    public function alterForm( HTMLForm $form ) {
81        $form->setTokenSalt( 'logoutToken' );
82        $form->addHeaderHtml( $this->msg(
83            $this->getUser()->isTemp() ? 'userlogout-temp' : 'userlogout-continue'
84        ) );
85
86        $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
87    }
88
89    /**
90     * Process the form.  At this point we know that the user passes all the criteria in
91     * userCanExecute(), and if the data array contains 'Username', etc, then Username
92     * resets are allowed.
93     * @param array $data
94     * @return Status
95     */
96    public function onSubmit( array $data ) {
97        // Make sure it's possible to log out
98        $session = SessionManager::getGlobalSession();
99        if ( !$session->canSetUser() ) {
100            throw new ErrorPageError(
101                'cannotlogoutnow-title',
102                'cannotlogoutnow-text',
103                [
104                    $session->getProvider()->describe( $this->getLanguage() )
105                ]
106            );
107        }
108
109        $user = $this->getUser();
110
111        $user->logout();
112        return new Status();
113    }
114
115    public function onSuccess() {
116        $this->showSuccess();
117
118        $out = $this->getOutput();
119        // Hook.
120        $injected_html = '';
121        $this->getHookRunner()->onUserLogoutComplete( $this->getUser(), $injected_html, $this->oldUserName );
122        $out->addHTML( $injected_html );
123    }
124
125    private function showSuccess() {
126        $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
127            $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
128
129        $out = $this->getOutput();
130        $out->addWikiMsg( 'logouttext', $loginURL );
131
132        $out->returnToMain();
133    }
134
135    /**
136     * Let blocked users to log out and come back with their sockpuppets
137     * @return bool
138     */
139    public function requiresUnblock() {
140        return false;
141    }
142}
143
144/**
145 * Retain the old class name for backwards compatibility.
146 * @deprecated since 1.41
147 */
148class_alias( SpecialUserLogout::class, 'SpecialUserLogout' );