MediaWiki master
ApiLogout.php
Go to the documentation of this file.
1<?php
24
31class ApiLogout extends ApiBase {
32
33 public function execute() {
34 $session = MediaWiki\Session\SessionManager::getGlobalSession();
35
36 // Handle bot password logout specially
37 if ( $session->getProvider() instanceof BotPasswordSessionProvider ) {
38 $session->unpersist();
39 return;
40 }
41
42 // Make sure it's possible to log out
43 if ( !$session->canSetUser() ) {
44 $this->dieWithError(
45 [
46 'cannotlogoutnow-text',
47 $session->getProvider()->describe( $this->getErrorFormatter()->getLanguage() )
48 ],
49 'cannotlogout'
50 );
51 }
52
53 $user = $this->getUser();
54
55 if ( $user->isAnon() ) {
56 // Cannot logout a anon user, so add a warning and return early.
57 $this->addWarning( 'apierror-mustbeloggedin-generic', 'notloggedin' );
58 return;
59 }
60
61 $oldName = $user->getName();
62 $user->logout();
63
64 // Give extensions to do something after user logout
65 $injected_html = '';
66 $this->getHookRunner()->onUserLogoutComplete( $user, $injected_html, $oldName );
67 }
68
69 public function mustBePosted() {
70 return true;
71 }
72
73 public function needsToken() {
74 return 'csrf';
75 }
76
77 public function isWriteMode() {
78 // While core is optimized by default to not require DB writes on log out,
79 // these are authenticated POST requests and extensions (eg. CheckUser) are
80 // allowed to perform DB writes here without warnings.
81 return true;
82 }
83
84 protected function getWebUITokenSalt( array $params ) {
85 return 'logoutToken';
86 }
87
88 public function isReadMode() {
89 return false;
90 }
91
92 protected function getExamplesMessages() {
93 return [
94 'action=logout&token=123ABC'
95 => 'apihelp-logout-example-logout',
96 ];
97 }
98
99 public function getHelpUrls() {
100 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logout';
101 }
102}
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:67
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1567
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1485
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:785
API module to allow users to log out of the wiki.
Definition ApiLogout.php:31
getWebUITokenSalt(array $params)
Fetch the salt used in the Web UI corresponding to this module.
Definition ApiLogout.php:84
isWriteMode()
Indicates whether this module requires write access to the wiki.
Definition ApiLogout.php:77
needsToken()
Returns the token type this module requires in order to execute.
Definition ApiLogout.php:73
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiLogout.php:99
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiLogout.php:69
isReadMode()
Indicates whether this module requires read rights.
Definition ApiLogout.php:88
getExamplesMessages()
Returns usage examples for this module.
Definition ApiLogout.php:92
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiLogout.php:33