Skip to content

Commit

Permalink
Change token() error value to null; add changelog entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Xymph committed Aug 25, 2021
1 parent d782d4a commit 2a8a803
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Since v0.10.0 this project adheres to [Semantic Versioning](http://semver.org/)

### Upcoming version

#### Changed

* Updated `Wikimate::token()` to remember CSRF token and reduce API calls ([#122])

#### Fixed

* Fixed format of user agent string ([#121])
Expand Down Expand Up @@ -141,3 +145,4 @@ Since v0.10.0 this project adheres to [Semantic Versioning](http://semver.org/)
[#114]: https://github.com/hamstar/Wikimate/pull/114
[#118]: https://github.com/hamstar/Wikimate/pull/118
[#121]: https://github.com/hamstar/Wikimate/pull/121
[#122]: https://github.com/hamstar/Wikimate/pull/122
16 changes: 8 additions & 8 deletions Wikimate.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ private function request($data, $headers = array(), $post = false)
* for more information.
*
* @param string $type The token type
* @return string The requested token
* @return mixed The requested token (string), or null if error
*/
protected function token($type = self::TOKEN_DEFAULT)
{
// Check for supported token types
if ($type != self::TOKEN_DEFAULT && $type != self::TOKEN_LOGIN) {
$this->error = array();
$this->error['token'] = 'The API does not support the token type';
return false;
return null;
}

// Check for existing CSRF token for this login session
Expand All @@ -277,15 +277,15 @@ protected function token($type = self::TOKEN_DEFAULT)
if (strpos($response->body, "This is an auto-generated MediaWiki API documentation page") !== false) {
$this->error = array();
$this->error['token'] = 'The API could not understand the token request';
return false;
return null;
}

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

if ($this->debugMode) {
Expand Down Expand Up @@ -316,7 +316,7 @@ protected function token($type = self::TOKEN_DEFAULT)
public function login($username, $password, $domain = null)
{
// Obtain login token first
if (($logintoken = $this->token(self::TOKEN_LOGIN)) === false) {
if (($logintoken = $this->token(self::TOKEN_LOGIN)) === null) {
return false;
}

Expand Down Expand Up @@ -576,7 +576,7 @@ public function parse($array)
public function edit($array)
{
// Obtain default token first
if (($edittoken = $this->token()) === false) {
if (($edittoken = $this->token()) === null) {
return false;
}

Expand Down Expand Up @@ -610,7 +610,7 @@ public function edit($array)
public function delete($array)
{
// Obtain default token first
if (($deletetoken = $this->token()) === false) {
if (($deletetoken = $this->token()) === null) {
return false;
}

Expand Down Expand Up @@ -667,7 +667,7 @@ public function download($url)
public function upload($array)
{
// Obtain default token first
if (($uploadtoken = $this->token()) === false) {
if (($uploadtoken = $this->token()) === null) {
return false;
}

Expand Down

0 comments on commit 2a8a803

Please sign in to comment.