MediaWiki master
ApiLogout.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
27
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
108class_alias( ApiLogout::class, 'ApiLogout' );
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:795
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1495
API module to allow users to log out of the wiki.
Definition ApiLogout.php:34
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiLogout.php:36
getExamplesMessages()
Returns usage examples for this module.
Definition ApiLogout.php:95
getHelpUrls()
Return links to more detailed help pages about the module.
needsToken()
Returns the token type this module requires in order to execute.
Definition ApiLogout.php:76
getWebUITokenSalt(array $params)
Fetch the salt used in the Web UI corresponding to this module.
Definition ApiLogout.php:87
isReadMode()
Indicates whether this module requires read rights.
Definition ApiLogout.php:91
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiLogout.php:72
isWriteMode()
Indicates whether this module requires write access to the wiki.
Definition ApiLogout.php:80
This serves as the entry point to the MediaWiki session handling system.