Skip to content

Commit

Permalink
Use format=json, because serialized PHP is dumb.
Browse files Browse the repository at this point in the history
  • Loading branch information
legoktm committed Nov 13, 2015
1 parent 932e909 commit 100ec4b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
66 changes: 33 additions & 33 deletions botclasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function query ($query,$post=null,$repeat=0) {
throw new Exception("HTTP Error.");
}
}
return unserialize($ret);
return json_decode($ret, true);
}

/**
Expand All @@ -240,7 +240,7 @@ function getpage ($page,$revid=null,$detectEditConflict=false) {
$append = '';
if ($revid!=null)
$append = '&rvstartid='.$revid;
$x = $this->query('?action=query&format=php&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content|timestamp'.$append);
$x = $this->query('?action=query&format=json&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content|timestamp'.$append);
foreach ($x['query']['pages'] as $ret) {
if (isset($ret['revisions'][0]['*'])) {
if ($detectEditConflict)
Expand All @@ -257,7 +257,7 @@ function getpage ($page,$revid=null,$detectEditConflict=false) {
* @return The page id of the page.
**/
function getpageid ($page) {
$x = $this->query('?action=query&format=php&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content');
$x = $this->query('?action=query&format=json&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content');
foreach ($x['query']['pages'] as $ret) {
return $ret['pageid'];
}
Expand All @@ -269,7 +269,7 @@ function getpageid ($page) {
* @return The number of contributions the user has.
**/
function contribcount ($user) {
$x = $this->query('?action=query&list=allusers&format=php&auprop=editcount&aulimit=1&aufrom='.urlencode($user));
$x = $this->query('?action=query&list=allusers&format=json&auprop=editcount&aulimit=1&aufrom='.urlencode($user));
return $x['query']['allusers'][0]['editcount'];
}

Expand All @@ -283,7 +283,7 @@ function categorymembers ($category,$subcat=false) {
$continue = '&rawcontinue=';
$pages = array();
while (true) {
$res = $this->query('?action=query&list=categorymembers&cmtitle='.urlencode($category).'&format=php&cmlimit=500'.$continue);
$res = $this->query('?action=query&list=categorymembers&cmtitle='.urlencode($category).'&format=json&cmlimit=500'.$continue);
if (isset($x['error'])) {
return false;
}
Expand Down Expand Up @@ -312,7 +312,7 @@ function categorymembers ($category,$subcat=false) {
* @return integer
**/
function categorypagecount ($category) {
$res = $this->query('?action=query&format=php&titles='.urlencode($category).'&prop=categoryinfo&formatversion=2');
$res = $this->query('?action=query&format=json&titles='.urlencode($category).'&prop=categoryinfo&formatversion=2');
return $res['query']['pages'][0]['categoryinfo']['pages'];
}

Expand All @@ -326,7 +326,7 @@ function whatlinkshere ($page,$extra=null) {
$continue = '&rawcontinue=';
$pages = array();
while (true) {
$res = $this->query('?action=query&list=backlinks&bltitle='.urlencode($page).'&bllimit=500&format=php'.$continue.$extra);
$res = $this->query('?action=query&list=backlinks&bltitle='.urlencode($page).'&bllimit=500&format=json'.$continue.$extra);
if (isset($res['error'])) {
return false;
}
Expand All @@ -351,7 +351,7 @@ function whereisincluded ($image,$extre=null) {
$continue = '&rawcontinue=';
$pages = array();
while (true) {
$res = $this->query('?action=query&list=imageusage&iutitle='.urlencode($image).'&iulimit=500&format=php'.$continue.$extra);
$res = $this->query('?action=query&list=imageusage&iutitle='.urlencode($image).'&iulimit=500&format=json'.$continue.$extra);
if (isset($res['error']))
return false;
foreach ($res['query']['imageusage'] as $x) {
Expand All @@ -374,7 +374,7 @@ function whatusethetemplate ($template,$extra=null) {
$continue = '&rawcontinue=';
$pages = array();
while (true) {
$res = $this->query('?action=query&list=embeddedin&eititle=Template:'.urlencode($template).'&eilimit=500&format=php'.$continue.$extra);
$res = $this->query('?action=query&list=embeddedin&eititle=Template:'.urlencode($template).'&eilimit=500&format=json'.$continue.$extra);
if (isset($res['error'])) {
return false;
}
Expand All @@ -396,7 +396,7 @@ function whatusethetemplate ($template,$extra=null) {
**/
function subpages ($page) {
/* Calculate all the namespace codes */
$ret = $this->query('?action=query&meta=siteinfo&siprop=namespaces&format=php');
$ret = $this->query('?action=query&meta=siteinfo&siprop=namespaces&format=json');
foreach ($ret['query']['namespaces'] as $x) {
$namespaces[$x['*']] = $x['id'];
}
Expand All @@ -406,7 +406,7 @@ function subpages ($page) {
$continue = '&rawcontinue=';
$subpages = array();
while (true) {
$res = $this->query('?action=query&format=php&list=allpages&apprefix='.urlencode($title).'&aplimit=500&apnamespace='.$namespace.$continue);
$res = $this->query('?action=query&format=json&list=allpages&apprefix='.urlencode($title).'&aplimit=500&apnamespace='.$namespace.$continue);
if (isset($x['error'])) {
return false;
}
Expand All @@ -429,11 +429,11 @@ function subpages ($page) {
**/
function login ($user,$pass) {
$post = array('lgname' => $user, 'lgpassword' => $pass);
$ret = $this->query('?action=login&format=php',$post);
$ret = $this->query('?action=login&format=json',$post);
/* This is now required - see https://bugzilla.wikimedia.org/show_bug.cgi?id=23076 */
if ($ret['login']['result'] == 'NeedToken') {
$post['lgtoken'] = $ret['login']['token'];
$ret = $this->query( '?action=login&format=php', $post );
$ret = $this->query( '?action=login&format=json', $post );
}
if ($ret['login']['result'] != 'Success') {
echo "Login error: \n";
Expand Down Expand Up @@ -484,7 +484,7 @@ function nobots ($page,$user=null,$text=null) {
* @return string edit token.
**/
function getedittoken() {
$x = $this->query('?action=query&prop=info&intoken=edit&titles=Main%20Page&format=php');
$x = $this->query('?action=query&prop=info&intoken=edit&titles=Main%20Page&format=json');
foreach ($x['query']['pages'] as $ret) {
return $ret['edittoken'];
}
Expand All @@ -496,7 +496,7 @@ function getedittoken() {
* @return Api result.
**/
function purgeCache($page) {
return $this->query('?action=purge&titles='.urlencode($page).'&format=php');
return $this->query('?action=purge&titles='.urlencode($page).'&format=json');
}

/**
Expand All @@ -506,7 +506,7 @@ function purgeCache($page) {
* @return bool.
**/
function checkEmail($user) {
$x = $this->query('?action=query&meta=allmessages&ammessages=noemailtext|notargettext&amlang=en&format=php');
$x = $this->query('?action=query&meta=allmessages&ammessages=noemailtext|notargettext&amlang=en&format=json');
$messages[0] = $x['query']['allmessages'][0]['*'];
$messages[1] = $x['query']['allmessages'][1]['*'];
$page = $this->http->get(str_replace('api.php','index.php',$this->url).'?title=Special:EmailUser&target='.urlencode($user));
Expand All @@ -527,7 +527,7 @@ function getTransclusions($page,$sleep=null,$extra=null) {
$continue = '&rawcontinue=';
$pages = array();
while (true) {
$ret = $this->query('?action=query&list=embeddedin&eititle='.urlencode($page).$continue.$extra.'&eilimit=500&format=php');
$ret = $this->query('?action=query&list=embeddedin&eititle='.urlencode($page).$continue.$extra.'&eilimit=500&format=json');
if ($sleep != null) {
sleep($sleep);
}
Expand Down Expand Up @@ -573,7 +573,7 @@ function edit ($page,$data,$summary = '',$minor = false,$bot = true,$section = n
if ($maxlag!='') {
$maxlag='&maxlag='.$maxlag;
}
return $this->query('?action=edit&format=php'.$maxlag,$params);
return $this->query('?action=edit&format=json'.$maxlag,$params);
}

/**
Expand Down Expand Up @@ -616,7 +616,7 @@ function move ($old,$new,$reason,$options=null) {
$params[$o] = true;
}
}
return $this->query('?action=move&format=php',$params);
return $this->query('?action=move&format=json',$params);
}

/**
Expand All @@ -628,7 +628,7 @@ function move ($old,$new,$reason,$options=null) {
* @return api result
**/
function rollback ($title,$user,$reason=null,$bot=false) {
$ret = $this->query('?action=query&prop=revisions&rvtoken=rollback&titles='.urlencode($title).'&format=php');
$ret = $this->query('?action=query&prop=revisions&rvtoken=rollback&titles='.urlencode($title).'&format=json');
foreach ($ret['query']['pages'] as $x) {
$token = $x['revisions'][0]['rollbacktoken'];
break;
Expand All @@ -642,7 +642,7 @@ function rollback ($title,$user,$reason=null,$bot=false) {
$params['markbot'] = true;
}
if ($reason != null) { $params['summary'] = $reason; }
return $this->query('?action=rollback&format=php',$params);
return $this->query('?action=rollback&format=json',$params);
}

/**
Expand All @@ -669,7 +669,7 @@ function block ($user,$reason='vand',$expiry='infinite',$options=null,$retry=tru
$params[$o] = true;
}
}
$ret = $this->query('?action=block&format=php',$params);
$ret = $this->query('?action=block&format=json',$params);
/* Retry on a failed token. */
if ($retry and $ret['error']['code']=='badtoken') {
$this->token = $this->getedittoken();
Expand All @@ -693,7 +693,7 @@ function unblock ($user,$reason) {
'reason' => $reason,
'token' => $this->token
);
return $this->query('?action=unblock&format=php',$params);
return $this->query('?action=unblock&format=json',$params);
}

/**
Expand All @@ -717,7 +717,7 @@ function email ($target,$subject,$text,$ccme=false) {
if ($ccme) {
$params['ccme'] = true;
}
return $this->query('?action=emailuser&format=php',$params);
return $this->query('?action=emailuser&format=json',$params);
}

/**
Expand All @@ -735,7 +735,7 @@ function delete ($title,$reason) {
'reason' => $reason,
'token' => $this->token
);
return $this->query('?action=delete&format=php',$params);
return $this->query('?action=delete&format=json',$params);
}

/**
Expand All @@ -753,7 +753,7 @@ function undelete ($title,$reason) {
'reason' => $reason,
'token' => $this->token
);
return $this->query('?action=undelete&format=php',$params);
return $this->query('?action=undelete&format=json',$params);
}

/**
Expand All @@ -779,7 +779,7 @@ function protect ($title,$protections,$expiry,$reason,$cascade=false) {
if ($cascade) {
$params['cascade'] = true;
}
return $this->query('?action=protect&format=php',$params);
return $this->query('?action=protect&format=json',$params);
}

/**
Expand All @@ -800,7 +800,7 @@ function upload ($page,$file,$desc='') {
'ignorewarnings' => '1',
'file' => '@'.$file
);
return $this->query('?action=upload&format=php',$params);
return $this->query('?action=upload&format=json',$params);
}

/*
Expand Down Expand Up @@ -858,7 +858,7 @@ function createaccount ($username,$password,$email=null) {
**/
function userrights ($user,$add,$remove,$reason='') {
// get the userrights token
$token = $this->query('?action=query&list=users&ususers='.urlencode($user).'&ustoken=userrights&format=php');
$token = $this->query('?action=query&list=users&ususers='.urlencode($user).'&ustoken=userrights&format=json');
$token = $token['query']['users'][0]['userrightstoken'];
$params = array(
'user' => $user,
Expand All @@ -867,7 +867,7 @@ function userrights ($user,$add,$remove,$reason='') {
'remove' => $remove,
'reason' => $reason
);
return $this->query('?action=userrights&format=php',$params);
return $this->query('?action=userrights&format=json',$params);
}

/**
Expand All @@ -876,7 +876,7 @@ function userrights ($user,$add,$remove,$reason='') {
* @return The number of images with the same sha1 hash.
**/
function imagematches ($hash) {
$x = $this->query('?action=query&list=allimages&format=php&aisha1='.$hash);
$x = $this->query('?action=query&list=allimages&format=json&aisha1='.$hash);
return count($x['query']['allimages']);
}

Expand All @@ -886,7 +886,7 @@ function imagematches ($hash) {
* @return The URL pointing directly to the media file (Eg http://upload.mediawiki.org/wikipedia/en/1/1/Example.jpg)
**/
function getfilelocation ($page) {
$x = $this->query('?action=query&format=php&prop=imageinfo&titles='.urlencode($page).'&iilimit=1&iiprop=url');
$x = $this->query('?action=query&format=json&prop=imageinfo&titles='.urlencode($page).'&iilimit=1&iiprop=url');
foreach ($x['query']['pages'] as $ret ) {
if (isset($ret['imageinfo'][0]['url'])) {
return $ret['imageinfo'][0]['url'];
Expand All @@ -901,7 +901,7 @@ function getfilelocation ($page) {
* @return The user who uploaded the topmost version of the file.
**/
function getfileuploader ($page) {
$x = $this->query('?action=query&format=php&prop=imageinfo&titles='.urlencode($page).'&iilimit=1&iiprop=user');
$x = $this->query('?action=query&format=json&prop=imageinfo&titles='.urlencode($page).'&iilimit=1&iiprop=user');
foreach ($x['query']['pages'] as $ret ) {
if (isset($ret['imageinfo'][0]['user'])) {
return $ret['imageinfo'][0]['user'];
Expand Down
2 changes: 1 addition & 1 deletion goodarticles.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getpage ($page,$revid=null,$detectEditConflict=false) {
$append = '&rvstartid='.$revid;

for ($i=0;$i<5;$i++) {
$x = parent::query('?action=query&format=php&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content|timestamp'.$append);
$x = parent::query('?action=query&format=json&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content|timestamp'.$append);
if (isset($x['query']['pages'])) {
foreach ($x['query']['pages'] as $ret) {
if (isset($ret['revisions'][0]['*'])) {
Expand Down
4 changes: 2 additions & 2 deletions mediawiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public function query ($query, $post = null) {
$data = $this->http->get($this->url . $query);
else
$data = $this->http->post($this->url . $query, $post);
return unserialize($data);
return json_decode($data, true);
}

protected function queryString ($query) {
$return = "?format=php";
$return = "?format=json";
foreach ($query as $key => $value) {
$return .= "&" . urlencode($key) . "=" . urlencode($value);
}
Expand Down
6 changes: 3 additions & 3 deletions new_mediawiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ public function query( $query, $post = null ) {
if ( is_array($query) ) {
$query = $this->queryString($query);
} else {
$query .= '&format=php';
$query .= '&format=json';
}
if ( $post === null || $post === false ) {
$data = $this->http->get($this->url . $query);
} else {
$data = $this->http->post($this->url . $query, $post);
}
return unserialize($data);
return json_decode($data, true);
}

/**
* @param $query array
* @return string
*/
protected function queryString( $query ) {
$return = "?format=php";
$return = "?format=json";
foreach ($query as $key => $value) {
$return .= "&" . urlencode($key) . "=" . urlencode($value);
}
Expand Down
2 changes: 1 addition & 1 deletion noticeboards.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function nbprocess ($pagearray, $color, $submissionpage) {

foreach ($pagearray as $topic => $page) {

$raw = $objwiki->query("?action=parse&page=". urlencode($page) . "&prop=sections&format=php");
$raw = $objwiki->query("?action=parse&page=". urlencode($page) . "&prop=sections&format=json");

$sections = array();
$seccount = 0;
Expand Down

0 comments on commit 100ec4b

Please sign in to comment.