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