Skip to content

Commit

Permalink
Merge pull request #111 from Xymph/111-switch-data-format
Browse files Browse the repository at this point in the history
Change API requests from deprecated PHP format to JSON format
  • Loading branch information
waldyrious authored Jul 7, 2021
2 parents e9b5d1c + d2be210 commit 8d540ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Since v0.10.0 this project adheres to [Semantic Versioning](http://semver.org/)

#### Changed

* Changed API requests from deprecated PHP format to JSON format ([#111])
* Grouped sections and added table of contents in USAGE.md ([#108])

### Version 0.13.0 - 2021-07-05
Expand Down Expand Up @@ -123,3 +124,4 @@ Since v0.10.0 this project adheres to [Semantic Versioning](http://semver.org/)
[#107]: https://github.com/hamstar/Wikimate/pull/107
[#108]: https://github.com/hamstar/Wikimate/pull/108
[#109]: https://github.com/hamstar/Wikimate/pull/109
[#111]: https://github.com/hamstar/Wikimate/pull/111
54 changes: 27 additions & 27 deletions Wikimate.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function token($type = self::TOKEN_DEFAULT)
return false;
}

$tokenResult = json_decode($response->body);
$tokenResult = json_decode($response->body, true);
// Check if we got a JSON result
if ($tokenResult === null) {
$this->error = array();
Expand All @@ -179,9 +179,9 @@ protected function token($type = self::TOKEN_DEFAULT)
}

if ($type == self::TOKEN_LOGIN) {
return $tokenResult->query->tokens->logintoken;
return $tokenResult['query']['tokens']['logintoken'];
} else {
return $tokenResult->query->tokens->csrftoken;
return $tokenResult['query']['tokens']['csrftoken'];
}
}

Expand Down Expand Up @@ -223,7 +223,7 @@ public function login($username, $password, $domain = null)
return false;
}

$loginResult = json_decode($response->body);
$loginResult = json_decode($response->body, true);
// Check if we got a JSON result
if ($loginResult === null) {
$this->error = array();
Expand All @@ -238,15 +238,15 @@ public function login($username, $password, $domain = null)
print_r($loginResult);
}

if (isset($loginResult->login->result) && $loginResult->login->result != 'Success') {
if (isset($loginResult['login']['result']) && $loginResult['login']['result'] != 'Success') {
// Some more comprehensive error checking
$this->error = array();
switch ($loginResult->login->result) {
switch ($loginResult['login']['result']) {
case 'Failed':
$this->error['login'] = 'Incorrect username or password';
break;
default:
$this->error['login'] = 'The API result was: ' . $loginResult->login->result;
$this->error['login'] = 'The API result was: ' . $loginResult['login']['result'];
break;
}
return false;
Expand Down Expand Up @@ -357,13 +357,13 @@ public function getFile($filename)
* Performs a query to the wiki API with the given details.
*
* @param array $array Array of details to be passed in the query
* @return array Unserialized php output from the wiki API
* @return array Decoded JSON output from the wiki API
* @link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Query
*/
public function query($array)
{
$array['action'] = 'query';
$array['format'] = 'php';
$array['format'] = 'json';

if ($this->debugMode) {
echo "query GET parameters:\n";
Expand All @@ -373,22 +373,22 @@ public function query($array)

if ($this->debugMode) {
echo "query GET response:\n";
print_r(unserialize($apiResult->body));
print_r(json_decode($apiResult->body, true));
}
return unserialize($apiResult->body);
return json_decode($apiResult->body, true);
}

/**
* Performs a parse query to the wiki API.
*
* @param array $array Array of details to be passed in the query
* @return array Unserialized php output from the wiki API
* @return array Decoded JSON output from the wiki API
* @link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext
*/
public function parse($array)
{
$array['action'] = 'parse';
$array['format'] = 'php';
$array['format'] = 'json';

if ($this->debugMode) {
echo "parse GET parameters:\n";
Expand All @@ -398,16 +398,16 @@ public function parse($array)

if ($this->debugMode) {
echo "parse GET response:\n";
print_r(unserialize($apiResult->body));
print_r(json_decode($apiResult->body, true));
}
return unserialize($apiResult->body);
return json_decode($apiResult->body, true);
}

/**
* Perfoms an edit query to the wiki API.
*
* @param array $array Array of details to be passed in the query
* @return array Unserialized php output from the wiki API
* @return array Decoded JSON output from the wiki API
* @link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Edit
*/
public function edit($array)
Expand All @@ -422,7 +422,7 @@ public function edit($array)
);

$array['action'] = 'edit';
$array['format'] = 'php';
$array['format'] = 'json';
$array['token'] = $edittoken;

if ($this->debugMode) {
Expand All @@ -433,16 +433,16 @@ public function edit($array)

if ($this->debugMode) {
echo "edit POST response:\n";
print_r(unserialize($apiResult->body));
print_r(json_decode($apiResult->body, true));
}
return unserialize($apiResult->body);
return json_decode($apiResult->body, true);
}

/**
* Perfoms a delete query to the wiki API.
*
* @param array $array Array of details to be passed in the query
* @return array Unserialized php output from the wiki API
* @return array Decoded JSON output from the wiki API
* @link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete
*/
public function delete($array)
Expand All @@ -457,7 +457,7 @@ public function delete($array)
);

$array['action'] = 'delete';
$array['format'] = 'php';
$array['format'] = 'json';
$array['token'] = $deletetoken;

if ($this->debugMode) {
Expand All @@ -468,9 +468,9 @@ public function delete($array)

if ($this->debugMode) {
echo "delete POST response:\n";
print_r(unserialize($apiResult->body));
print_r(json_decode($apiResult->body, true));
}
return unserialize($apiResult->body);
return json_decode($apiResult->body, true);
}

/**
Expand Down Expand Up @@ -500,7 +500,7 @@ public function download($url)
* Uploads a file to the wiki API.
*
* @param array $array Array of details to be used in the upload
* @return array Unserialized php output from the wiki API
* @return array Decoded JSON output from the wiki API
* @link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Upload
*/
public function upload($array)
Expand All @@ -511,7 +511,7 @@ public function upload($array)
}

$array['action'] = 'upload';
$array['format'] = 'php';
$array['format'] = 'json';
$array['token'] = $uploadtoken;

// Construct multipart body:
Expand Down Expand Up @@ -547,9 +547,9 @@ public function upload($array)

if ($this->debugMode) {
echo "upload POST response:\n";
print_r(unserialize($apiResult->body));
print_r(json_decode($apiResult->body, true));
}
return unserialize($apiResult->body);
return json_decode($apiResult->body, true);
}

/**
Expand Down

0 comments on commit 8d540ca

Please sign in to comment.