API: Some cleanup
authorRoan Kattouw <catrope@users.mediawiki.org>
Fri, 27 Feb 2009 16:03:01 +0000 (16:03 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Fri, 27 Feb 2009 16:03:01 +0000 (16:03 +0000)
* Aggressively intval() numeric things; addresses the concerns raised on bug 11633
* Use ApiQueryBase::addTitleInfo() everywhere
* Remove a drug-induced-looking line from ApiQueryWatchlist.php

26 files changed:
includes/api/ApiBlock.php
includes/api/ApiEditPage.php
includes/api/ApiImport.php
includes/api/ApiLogin.php
includes/api/ApiParse.php
includes/api/ApiPatrol.php
includes/api/ApiQueryAllCategories.php
includes/api/ApiQueryAllLinks.php
includes/api/ApiQueryBacklinks.php
includes/api/ApiQueryCategoryInfo.php
includes/api/ApiQueryCategoryMembers.php
includes/api/ApiQueryDeletedrevs.php
includes/api/ApiQueryExtLinksUsage.php
includes/api/ApiQueryImageInfo.php
includes/api/ApiQueryInfo.php
includes/api/ApiQueryRandom.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryRevisions.php
includes/api/ApiQuerySearch.php
includes/api/ApiQuerySiteinfo.php
includes/api/ApiQueryUserInfo.php
includes/api/ApiQueryUsers.php
includes/api/ApiQueryWatchlist.php
includes/api/ApiRollback.php
includes/api/ApiUnblock.php
includes/api/ApiUndelete.php

index e01b64d..7d8b087 100644 (file)
@@ -94,7 +94,7 @@ class ApiBlock extends ApiBase {
                        $this->dieUsageMsg($retval);
 
                $res['user'] = $params['user'];
-               $res['userID'] = $userID;
+               $res['userID'] = intval($userID);
                $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : wfTimestamp(TS_ISO_8601, $expiry));
                $res['reason'] = $params['reason'];
                if($params['anononly'])
index d092755..8fc3f15 100644 (file)
@@ -249,7 +249,7 @@ class ApiEditPage extends ApiBase {
                                $r['new'] = '';
                        case EditPage::AS_SUCCESS_UPDATE:
                                $r['result'] = "Success";
-                               $r['pageid'] = $titleObj->getArticleID();
+                               $r['pageid'] = intval($titleObj->getArticleID());
                                $r['title'] = $titleObj->getPrefixedText();
                                # HACK: We create a new Article object here because getRevIdFetched()
                                # refuses to be run twice, and because Title::getLatestRevId()
@@ -261,8 +261,8 @@ class ApiEditPage extends ApiBase {
                                        $r['nochange'] = '';
                                else
                                {
-                                       $r['oldrevid'] = $oldRevId;
-                                       $r['newrevid'] = $newRevId;
+                                       $r['oldrevid'] = intval($oldRevId);
+                                       $r['newrevid'] = intval($newRevId);
                                }
                                break;
                        default:
index 53fdf17..ef2f51e 100644 (file)
@@ -162,7 +162,7 @@ class ApiImportReporter extends ImportReporter {
                // Add a result entry
                $r = array();
                ApiQueryBase::addTitleInfo($r, $title);
-               $r['revisions'] = $successCount;
+               $r['revisions'] = intval($successCount);
                $this->mResultArr[] = $r;
 
                // Piggyback on the parent to do the logging
index 24a1314..b197c20 100644 (file)
@@ -82,7 +82,7 @@ class ApiLogin extends ApiBase {
                                wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
 
                                $result['result'] = 'Success';
-                               $result['lguserid'] = $wgUser->getId();
+                               $result['lguserid'] = intval($wgUser->getId());
                                $result['lgusername'] = $wgUser->getName();
                                $result['lgtoken'] = $wgUser->getToken();
                                $result['cookieprefix'] = $wgCookiePrefix;
@@ -114,7 +114,7 @@ class ApiLogin extends ApiBase {
                        case LoginForm :: THROTTLED :
                                global $wgPasswordAttemptThrottle;
                                $result['result'] = 'Throttled';
-                               $result['wait'] = $wgPasswordAttemptThrottle['seconds'];
+                               $result['wait'] = intval($wgPasswordAttemptThrottle['seconds']);
                                break;
                        default :
                                ApiBase :: dieDebug(__METHOD__, "Unhandled case value: {$authRes}");
index 050587f..e5d1fb9 100644 (file)
@@ -156,7 +156,7 @@ class ApiParse extends ApiBase {
                                                        $p_result->getDisplayTitle() :
                                                        $titleObj->getPrefixedText();
                if(!is_null($oldid))
-                       $result_array['revid'] = $oldid;
+                       $result_array['revid'] = intval($oldid);
 
                $result_mapping = array(
                        'redirects' => 'r',
index 872a22b..5949f00 100644 (file)
@@ -60,7 +60,7 @@ class ApiPatrol extends ApiBase {
                if($retval)
                        $this->dieUsageMsg(current($retval));
                
-               $result = array('rcid' => $rc->getAttribute('rc_id'));
+               $result = array('rcid' => intval($rc->getAttribute('rc_id')));
                ApiQueryBase::addTitleInfo($result, $rc->getTitle());
                $this->getResult()->addValue(null, $this->getModuleName(), $result);
        }
index ea38742..b8a9380 100644 (file)
@@ -103,10 +103,10 @@ class ApiQueryAllCategories extends ApiQueryGeneratorBase {
                                $item = array();
                                $result->setContent( $item, $titleObj->getText() );
                                if( isset( $prop['size'] ) ) {
-                                       $item['size'] = $row->cat_pages;
+                                       $item['size'] = intval($row->cat_pages);
                                        $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
-                                       $item['files'] = $row->cat_files;
-                                       $item['subcats'] = $row->cat_subcats;
+                                       $item['files'] = intval($row->cat_files);
+                                       $item['subcats'] = intval($row->cat_subcats);
                                }
                                if( isset( $prop['hidden'] ) && $row->cat_hidden )
                                        $item['hidden'] = '';
index 03262a7..0f6e6bc 100644 (file)
@@ -121,8 +121,7 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                                        $vals['fromid'] = intval($row->pl_from);
                                if ($fld_title) {
                                        $title = Title :: makeTitle($params['namespace'], $row->pl_title);
-                                       $vals['ns'] = intval($title->getNamespace());
-                                       $vals['title'] = $title->getPrefixedText();
+                                       ApiQueryBase::addTitleInfo($vals, $title);
                                }
                                $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
                                if(!$fit)
index a1a6f61..4b75c10 100644 (file)
@@ -292,7 +292,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
        private function extractRowInfo($row) {
                $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
                $t = Title::makeTitle($row->page_namespace, $row->page_title);
-               $a = array('pageid' => $row->page_id);
+               $a = array('pageid' => intval($row->page_id));
                ApiQueryBase::addTitleInfo($a, $t);
                if($row->page_is_redirect)
                {
@@ -305,7 +305,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
        private function extractRedirRowInfo($row)
        {
-               $a['pageid'] = $row->page_id;
+               $a['pageid'] = intval($row->page_id);
                ApiQueryBase::addTitleInfo($a, Title::makeTitle($row->page_namespace, $row->page_title));
                if($row->page_is_redirect)
                        $a['redirect'] = '';
index a7c4fc4..9e36d93 100644 (file)
@@ -81,10 +81,10 @@ class ApiQueryCategoryInfo extends ApiQueryBase {
                while($row = $db->fetchObject($res))
                {
                        $vals = array();
-                       $vals['size'] = $row->cat_pages;
+                       $vals['size'] = intval($row->cat_pages);
                        $vals['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
-                       $vals['files'] = $row->cat_files;
-                       $vals['subcats'] = $row->cat_subcats;
+                       $vals['files'] = intval($row->cat_files);
+                       $vals['subcats'] = intval($row->cat_subcats);
                        if($row->cat_hidden)
                                $vals['hidden'] = '';
                        $fit = $this->addPageSubItems($catids[$row->cat_title], $vals);
index 6e88012..8b6bd1d 100644 (file)
@@ -118,8 +118,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
                                        $vals['pageid'] = intval($row->page_id);
                                if ($fld_title) {
                                        $title = Title :: makeTitle($row->page_namespace, $row->page_title);
-                                       $vals['ns'] = intval($title->getNamespace());
-                                       $vals['title'] = $title->getPrefixedText();
+                                       ApiQueryBase::addTitleInfo($vals, $title);
                                }
                                if ($fld_sortkey)
                                        $vals['sortkey'] = $row->cl_sortkey;
index d4a6294..adda49f 100644 (file)
@@ -192,7 +192,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                        $rev = array();
                        $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);
                        if($fld_revid)
-                               $rev['revid'] = $row->ar_rev_id;
+                               $rev['revid'] = intval($row->ar_rev_id);
                        if($fld_user)
                                $rev['user'] = $row->ar_user_text;
                        if($fld_comment)
@@ -210,12 +210,9 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                                $pageID = $newPageID++;
                                $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
                                $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
-                               $a = array(
-                                       'title' => $t->getPrefixedText(),
-                                       'ns' => intval($row->ar_namespace),
-                                       'revisions' => array($rev)
-                               );
+                               $a['revisions'] = array($rev);
                                $result->setIndexedTagName($a['revisions'], 'rev');
+                               ApiQueryBase::addTitleInfo($a, $t);
                                if($fld_token)
                                        $a['token'] = $token;
                                $fit = $result->addValue(array('query', $this->getModuleName()), $pageID, $a);
index d4dcb83..8d76e8b 100644 (file)
@@ -125,8 +125,7 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
                                        $vals['pageid'] = intval($row->page_id);
                                if ($fld_title) {
                                        $title = Title :: makeTitle($row->page_namespace, $row->page_title);
-                                       $vals['ns'] = intval($title->getNamespace());
-                                       $vals['title'] = $title->getPrefixedText();
+                                       ApiQueryBase::addTitleInfo($vals, $title);
                                }
                                if ($fld_url)
                                        $vals['url'] = $row->el_to;
index b98d2ad..291a8fe 100644 (file)
@@ -194,8 +194,8 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                if( $mto && !$mto->isError() )
                                {
                                        $vals['thumburl'] = $mto->getUrl();
-                                       $vals['thumbwidth'] = $mto->getWidth();
-                                       $vals['thumbheight'] = $mto->getHeight();
+                                       $vals['thumbwidth'] = intval( $mto->getWidth() );
+                                       $vals['thumbheight'] = intval( $mto->getHeight() );
                                }
                        }
                        $vals['url'] = $file->getFullURL();
index a1d1533..e6b89ae 100644 (file)
@@ -492,10 +492,10 @@ class ApiQueryInfo extends ApiQueryBase {
                {
                        if(MWNamespace::isTalk($row->page_namespace))
                                $this->talkids[MWNamespace::getSubject($row->page_namespace)][$row->page_title] =
-                                               $row->page_id;
+                                               intval($row->page_id);
                        else
                                $this->subjectids[MWNamespace::getTalk($row->page_namespace)][$row->page_title] =
-                                               $row->page_id;
+                                               intval($row->page_id);
                }
        }
 
index a8fc147..73c4a81 100644 (file)
@@ -116,9 +116,8 @@ if (!defined('MEDIAWIKI')) {
        private function extractRowInfo($row) {
                $title = Title::makeTitle($row->page_namespace, $row->page_title);
                $vals = array();
-               $vals['title'] = $title->getPrefixedText();
-               $vals['ns'] = $row->page_namespace;
-               $vals['id'] = $row->page_id;
+               $vals['id'] = intval($row->page_id);
+               ApiQueryBase::addTitleInfo($vals, $title);
                return $vals;
        }
 
index c900989..9dacb86 100644 (file)
@@ -311,7 +311,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
                        $vals['patrolled'] = '';
                        
                if ($this->fld_loginfo && $row->rc_type == RC_LOG) {
-                       $vals['logid'] = $row->rc_logid;
+                       $vals['logid'] = intval($row->rc_logid);
                        $vals['logtype'] = $row->rc_log_type;
                        $vals['logaction'] = $row->rc_log_action;
                        ApiQueryLogEvents::addLogParams($this->getResult(),
index d3e2125..9984986 100644 (file)
@@ -292,7 +292,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                $vals = array ();
 
                if ($this->fld_ids) {
-                       $vals['revid'] = $revision->getId();
+                       $vals['revid'] = intval($revision->getId());
                        // $vals['oldid'] = intval($row->rev_text_id);  // todo: should this be exposed?
                }
 
@@ -314,7 +314,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                }
 
                if ($this->fld_size && !is_null($revision->getSize())) {
-                       $vals['size'] = $revision->getSize();
+                       $vals['size'] = intval($revision->getSize());
                }
 
                if ($this->fld_comment) {
index a34c2f6..80ae14d 100644 (file)
@@ -102,9 +102,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        
                        $title = $result->getTitle();
                        if (is_null($resultPageSet)) {
-                               $vals = array(
-                                       'ns' => intval($title->getNamespace()),
-                                       'title' => $title->getPrefixedText());
+                               $vals = array();
+                               ApiQueryBase::addTitleInfo($vals, $title);
                                $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
                                if(!$fit)
                                {
index 27965aa..348b02b 100644 (file)
@@ -137,7 +137,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                        $offset = 0;
                }
                $data['timezone'] = $tz;
-               $data['timeoffset'] = $offset;
+               $data['timeoffset'] = intval($offset);
 
                return $this->getResult()->addValue( 'query', $property, $data );
        }
@@ -148,7 +148,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
                {
                        $data[$ns] = array(
-                               'id' => $ns
+                               'id' => intval($ns)
                        );
                        ApiResult :: setContent( $data[$ns], $title );
                        $canonical = MWNamespace::getCanonicalName( $ns );
@@ -176,7 +176,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                                continue;
                        }
                        $item = array(
-                               'id' => $ns
+                               'id' => intval($ns)
                        );
                        ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
                        $data[] = $item;
@@ -272,7 +272,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                        list( $host, $lag ) = wfGetLB()->getMaxLag();
                        $data[] = array(
                                'host' => $wgShowHostnames ? $host : '',
-                               'lag' => $lag
+                               'lag' => intval( $lag )
                        );
                }
 
index 9ab8874..18feb70 100644 (file)
@@ -57,7 +57,7 @@ class ApiQueryUserInfo extends ApiQueryBase {
                global $wgUser;
                $result = $this->getResult();
                $vals = array();
-               $vals['id'] = $wgUser->getId();
+               $vals['id'] = intval($wgUser->getId());
                $vals['name'] = $wgUser->getName();
 
                if($wgUser->isAnon())
@@ -87,7 +87,7 @@ class ApiQueryUserInfo extends ApiQueryBase {
                        $vals['preferencestoken'] = $wgUser->editToken();
                }
                if (isset($this->prop['editcount'])) {
-                       $vals['editcount'] = $wgUser->getEditCount();
+                       $vals['editcount'] = intval($wgUser->getEditCount());
                }
                if (isset($this->prop['ratelimits'])) {
                        $vals['ratelimits'] = $this->getRateLimits();
@@ -128,8 +128,8 @@ class ApiQueryUserInfo extends ApiQueryBase {
                        foreach($categories as $cat)
                                if(isset($limits[$cat]) && !is_null($limits[$cat]))
                                {
-                                       $retval[$action][$cat]['hits'] = $limits[$cat][0];
-                                       $retval[$action][$cat]['seconds'] = $limits[$cat][1];
+                                       $retval[$action][$cat]['hits'] = intval($limits[$cat][0]);
+                                       $retval[$action][$cat]['seconds'] = intval($limits[$cat][1]);
                                }
                return $retval;
        }
index 3efd4e0..51fa44e 100644 (file)
@@ -103,11 +103,9 @@ if (!defined('MEDIAWIKI')) {
                                $name = $user->getName();
                                $data[$name]['name'] = $name;
                                if(isset($this->prop['editcount']))
-                                       // No proper member function in the User class for this
-                                       $data[$name]['editcount'] = $r->user_editcount;
+                                       $data[$name]['editcount'] = intval($user->getEditCount());
                                if(isset($this->prop['registration']))
-                                       // Nor for this one
-                                       $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $r->user_registration);
+                                       $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $user->getRegistration());
                                if(isset($this->prop['groups']) && !is_null($r->ug_group))
                                        // This row contains only one group, others will be added from other rows
                                        $data[$name]['groups'][] = $r->ug_group;
index 8865a71..fa126f8 100644 (file)
@@ -244,8 +244,6 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                if ($this->fld_timestamp)
                        $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
 
-                       $this->addFieldsIf('rc_new_len', $this->fld_sizes);
-
                if ($this->fld_sizes) {
                        $vals['oldlen'] = intval($row->rc_old_len);
                        $vals['newlen'] = intval($row->rc_new_len);
index 13d8583..cb0486f 100644 (file)
@@ -72,11 +72,11 @@ class ApiRollback extends ApiBase {
 
                $info = array(
                        'title' => $titleObj->getPrefixedText(),
-                       'pageid' => $details['current']->getPage(),
+                       'pageid' => intval($details['current']->getPage()),
                        'summary' => $details['summary'],
-                       'revid' => $titleObj->getLatestRevID(),
-                       'old_revid' => $details['current']->getID(),
-                       'last_revid' => $details['target']->getID()
+                       'revid' => intval($titleObj->getLatestRevID()),
+                       'old_revid' => intval($details['current']->getID()),
+                       'last_revid' => intval($details['target']->getID())
                );
 
                $this->getResult()->addValue(null, $this->getModuleName(), $info);
index b262df2..b8568bc 100644 (file)
@@ -72,7 +72,7 @@ class ApiUnblock extends ApiBase {
                if($retval)
                        $this->dieUsageMsg($retval);
 
-               $res['id'] = $id;
+               $res['id'] = intval($id);
                $res['user'] = $user;
                $res['reason'] = $reason;
                $this->getResult()->addValue(null, $this->getModuleName(), $res);
index 63fbc99..9bb2efb 100644 (file)
@@ -78,9 +78,9 @@ class ApiUndelete extends ApiBase {
                                array($titleObj, array(), $wgUser, $params['reason']) );
 
                $info['title'] = $titleObj->getPrefixedText();
-               $info['revisions'] = $retval[0];
-               $info['fileversions'] = $retval[1];
-               $info['reason'] = $retval[2];
+               $info['revisions'] = intval($retval[0]);
+               $info['fileversions'] = intval($retval[1]);
+               $info['reason'] = intval($retval[2]);
                $this->getResult()->addValue(null, $this->getModuleName(), $info);
        }