Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.61% covered (warning)
51.61%
16 / 31
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiLogout
53.33% covered (warning)
53.33%
16 / 30
62.50% covered (warning)
62.50%
5 / 8
23.30
0.00% covered (danger)
0.00%
0 / 1
 execute
55.00% covered (warning)
55.00%
11 / 20
0.00% covered (danger)
0.00%
0 / 1
5.46
 mustBePosted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isWriteMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWebUITokenSalt
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isReadMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
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 */
22
23namespace MediaWiki\Api;
24
25use MediaWiki\Session\BotPasswordSessionProvider;
26use MediaWiki\Session\SessionManager;
27
28/**
29 * API module to allow users to log out of the wiki. API equivalent of
30 * Special:Userlogout.
31 *
32 * @ingroup API
33 */
34class ApiLogout extends ApiBase {
35
36    public function execute() {
37        $session = SessionManager::getGlobalSession();
38
39        // Handle bot password logout specially
40        if ( $session->getProvider() instanceof BotPasswordSessionProvider ) {
41            $session->unpersist();
42            return;
43        }
44
45        // Make sure it's possible to log out
46        if ( !$session->canSetUser() ) {
47            $this->dieWithError(
48                [
49                    'cannotlogoutnow-text',
50                    $session->getProvider()->describe( $this->getErrorFormatter()->getLanguage() )
51                ],
52                'cannotlogout'
53            );
54        }
55
56        $user = $this->getUser();
57
58        if ( $user->isAnon() ) {
59            // Cannot logout a anon user, so add a warning and return early.
60            $this->addWarning( 'apierror-mustbeloggedin-generic', 'notloggedin' );
61            return;
62        }
63
64        $oldName = $user->getName();
65        $user->logout();
66
67        // Give extensions to do something after user logout
68        $injected_html = '';
69        $this->getHookRunner()->onUserLogoutComplete( $user, $injected_html, $oldName );
70    }
71
72    public function mustBePosted() {
73        return true;
74    }
75
76    public function needsToken() {
77        return 'csrf';
78    }
79
80    public function isWriteMode() {
81        // While core is optimized by default to not require DB writes on log out,
82        // these are authenticated POST requests and extensions (eg. CheckUser) are
83        // allowed to perform DB writes here without warnings.
84        return true;
85    }
86
87    protected function getWebUITokenSalt( array $params ) {
88        return 'logoutToken';
89    }
90
91    public function isReadMode() {
92        return false;
93    }
94
95    protected function getExamplesMessages() {
96        return [
97            'action=logout&token=123ABC'
98                => 'apihelp-logout-example-logout',
99        ];
100    }
101
102    public function getHelpUrls() {
103        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logout';
104    }
105}
106
107/** @deprecated class alias since 1.43 */
108class_alias( ApiLogout::class, 'ApiLogout' );