Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.85% covered (warning)
78.85%
41 / 52
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialUserLogout
80.39% covered (warning)
80.39%
41 / 51
69.23% covered (warning)
69.23%
9 / 13
26.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
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
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 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%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 requiresUnblock
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
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;
29use MediaWiki\User\TempUser\TempUserConfig;
30
31/**
32 * Implements Special:Userlogout
33 *
34 * @ingroup SpecialPage
35 * @ingroup Auth
36 */
37class SpecialUserLogout extends FormSpecialPage {
38    /**
39     * @var string
40     */
41    private $oldUserName;
42
43    private TempUserConfig $tempUserConfig;
44
45    public function __construct( TempUserConfig $tempUserConfig ) {
46        parent::__construct( 'Userlogout' );
47        $this->tempUserConfig = $tempUserConfig;
48    }
49
50    public function doesWrites() {
51        return true;
52    }
53
54    public function isListed() {
55        return $this->getAuthManager()->canAuthenticateNow();
56    }
57
58    protected function getGroupName() {
59        return 'login';
60    }
61
62    protected function getFormFields() {
63        return [];
64    }
65
66    protected function getDisplayFormat() {
67        return 'ooui';
68    }
69
70    public function execute( $par ) {
71        $user = $this->getUser();
72        if ( $user->isAnon() ) {
73            $this->setHeaders();
74            $this->showSuccess();
75            return;
76        }
77        $this->oldUserName = $user->getName();
78
79        parent::execute( $par );
80    }
81
82    public function alterForm( HTMLForm $form ) {
83        $form->setTokenSalt( 'logoutToken' );
84        $form->addHeaderHtml( $this->msg(
85            $this->getUser()->isTemp() ? 'userlogout-temp' : 'userlogout-continue'
86        ) );
87
88        $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
89    }
90
91    /**
92     * Process the form.  At this point we know that the user passes all the criteria in
93     * userCanExecute(), and if the data array contains 'Username', etc, then Username
94     * resets are allowed.
95     * @param array $data
96     * @return Status
97     */
98    public function onSubmit( array $data ) {
99        // Make sure it's possible to log out
100        $session = SessionManager::getGlobalSession();
101        if ( !$session->canSetUser() ) {
102            throw new ErrorPageError(
103                'cannotlogoutnow-title',
104                'cannotlogoutnow-text',
105                [
106                    $session->getProvider()->describe( $this->getLanguage() )
107                ]
108            );
109        }
110
111        $user = $this->getUser();
112
113        $user->logout();
114        return new Status();
115    }
116
117    public function onSuccess() {
118        $this->showSuccess();
119
120        $out = $this->getOutput();
121        // Hook.
122        $injected_html = '';
123        $this->getHookRunner()->onUserLogoutComplete( $this->getUser(), $injected_html, $this->oldUserName );
124        $out->addHTML( $injected_html );
125    }
126
127    private function showSuccess() {
128        $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
129            $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
130
131        $out = $this->getOutput();
132
133        $messageKey = 'logouttext';
134        if (
135            ( isset( $this->oldUserName ) && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
136            $this->getRequest()->getCheck( 'wasTempUser' )
137        ) {
138            // Generates the message key logouttext-for-temporary-account which is used to customise the success
139            // message for a temporary account.
140            $messageKey .= '-for-temporary-account';
141        }
142        $out->addWikiMsg( $messageKey, $loginURL );
143
144        $out->returnToMain();
145    }
146
147    /**
148     * Let blocked users to log out and come back with their sockpuppets
149     * @return bool
150     */
151    public function requiresUnblock() {
152        return false;
153    }
154
155    public function getDescription() {
156        // Set the page title as "templogout" if the user is (or just was) logged in to a temporary account
157        if (
158            $this->getUser()->isTemp() ||
159            ( isset( $this->oldUserName ) && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
160            $this->getRequest()->getCheck( 'wasTempUser' )
161        ) {
162            return $this->msg( 'templogout' );
163        }
164        return parent::getDescription();
165    }
166}
167
168/**
169 * Retain the old class name for backwards compatibility.
170 * @deprecated since 1.41
171 */
172class_alias( SpecialUserLogout::class, 'SpecialUserLogout' );