From e84c84ef625916de406b2acb1db9a19b58cc1d61 Mon Sep 17 00:00:00 2001 From: Xymph Date: Wed, 25 Aug 2021 15:06:50 +0200 Subject: [PATCH 1/2] Add method Wikimate::logout() --- Wikimate.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Wikimate.php b/Wikimate.php index 3aa4b75..59d00f0 100644 --- a/Wikimate.php +++ b/Wikimate.php @@ -241,6 +241,8 @@ private function request($data, $headers = array(), $post = false) * * If a CSRF (default) token is requested, it is stored and returned * upon further such requests, instead of making another API call. + * The stored token is discarded via {@see Wikimate::logout()}. + * * For now this method, in Wikimate tradition, is kept simple and supports * only the two token types needed elsewhere in the library. It also * doesn't support the option to request multiple tokens at once. @@ -374,6 +376,56 @@ public function login($username, $password, $domain = null) return true; } + /** + * Logs out of the wiki and discard CSRF token. + * + * @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. * From 286034048d69c8562ecd02cd0355cb8ad145b53a Mon Sep 17 00:00:00 2001 From: Xymph Date: Wed, 25 Aug 2021 15:07:28 +0200 Subject: [PATCH 2/2] Update USAGE.md; add changelog entry --- CHANGELOG.md | 2 ++ USAGE.md | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9269f6f..99dde6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Since v0.10.0 this project adheres to [Semantic Versioning](http://semver.org/) #### Added * New methods `WikiFile::revert()` and `Wikimate::filerevert()` ([#123]) +* New method `Wikimate::logout()` ([#124]) #### Changed @@ -151,3 +152,4 @@ Since v0.10.0 this project adheres to [Semantic Versioning](http://semver.org/) [#121]: https://github.com/hamstar/Wikimate/pull/121 [#122]: https://github.com/hamstar/Wikimate/pull/122 [#123]: https://github.com/hamstar/Wikimate/pull/123 +[#124]: https://github.com/hamstar/Wikimate/pull/124 diff --git a/USAGE.md b/USAGE.md index f2efa98..ba4dafe 100644 --- a/USAGE.md +++ b/USAGE.md @@ -341,7 +341,7 @@ Both methods return an array of the MediaWiki API result. API requests are made over HTTP with a user agent string to identify the client to the server. By default the user agent is formatted as: -`Wikimate (https://github.com/hamstar/Wikimate)` +`Wikimate/ (https://github.com/hamstar/Wikimate)` The string can be retrieved and customized via: @@ -363,7 +363,8 @@ print_r($page->getError()); ``` For MediaWiki API errors, the array contains the 'code' and 'info' key/value pairs [defined by the API](https://www.mediawiki.org/wiki/Special:MyLanguage/API:Errors_and_warnings#Errors). For other errors, the following key/value pairs are returned: -* 'login' for Wikimate authentication problems +* 'login' for Wikimate authentication problems +* 'auth' for Wikimate authentication problems * 'token' for Wikimate token problems * 'page' for WikiPage errors * 'file' for WikiFile errors