Skip to content

Commit

Permalink
Add method Wikimate::logout()
Browse files Browse the repository at this point in the history
  • Loading branch information
Xymph committed Aug 25, 2021
1 parent 4b8652b commit 5f6d4b7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Wikimate.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,56 @@ public function login($username, $password, $domain = null)
return true;
}

/**
* Logs out of the wiki.
*
* @return boolean True if logged out
* @link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logout
*/
public function logout()
{
// Obtain logout token first
if (($logouttoken = $this->token()) === null) {
return false;
}

// Token is needed in MediaWiki v1.34+, older versions produce an
// 'Unrecognized parameter' warning which can be ignored
$details = array(
'action' => 'logout',
'token' => $logouttoken,
);

// Send the logout request
$response = $this->request($details, array(), true);

// Check if we got an API result or the API doc page (invalid request)
if (strpos($response->body, "This is an auto-generated MediaWiki API documentation page") !== false) {
$this->error = array();
$this->error['auth'] = 'The API could not understand the logout request';
return false;
}

$logoutResult = json_decode($response->body, true);
// Check if we got a JSON result
if ($logoutResult === null) {
$this->error = array();
$this->error['auth'] = 'The API did not return the logout response';
return false;
}

if ($this->debugMode) {
echo "Logout request:\n";
print_r($details);
echo "Logout response:\n";
print_r($logoutResult);
}

// Discard CSRF token for this login session
$this->csrf_token = null;
return true;
}

/**
* Gets the current value of the maxlag parameter.
*
Expand Down

0 comments on commit 5f6d4b7

Please sign in to comment.