Cast various things that are supposed to be ints
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 6 May 2015 15:33:08 +0000 (11:33 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 6 May 2015 15:40:41 +0000 (11:40 -0400)
Mysqli is returning SELECTed ints as strings rather than as ints, I'm
guessing to avoid problems with 64-bit int types on 32-bit systems. PHP
mostly doesn't care, but it causes API JSON output to have strings
instead of ints all over the place.

This also fixes ForeignAPIFile::getUser( 'id' ) returning the user
*name*.

Bug: T98276
Change-Id: Ie6591d72b3ac40172f8176a8ca8b6fad8e9275a5

16 files changed:
includes/Block.php
includes/api/ApiQueryAllUsers.php
includes/api/ApiQueryBacklinksprop.php
includes/api/ApiQueryBlocks.php
includes/api/ApiQueryContributors.php
includes/api/ApiQueryDeletedrevs.php
includes/api/ApiQueryFilearchive.php
includes/api/ApiQueryProtectedTitles.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryUserContributions.php
includes/api/ApiQueryUsers.php
includes/api/ApiQueryWatchlist.php
includes/db/DatabaseMysqli.php
includes/filerepo/file/ArchivedFile.php
includes/filerepo/file/ForeignAPIFile.php
includes/filerepo/file/LocalFile.php

index 7666751..d582201 100644 (file)
@@ -371,7 +371,7 @@ class Block {
                $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
                $this->mAuto = $row->ipb_auto;
                $this->mHideName = $row->ipb_deleted;
-               $this->mId = $row->ipb_id;
+               $this->mId = (int)$row->ipb_id;
                $this->mParentBlockId = $row->ipb_parent_block_id;
 
                // I wish I didn't have to do this
index 5168859..faaf1fd 100644 (file)
@@ -235,14 +235,14 @@ class ApiQueryAllUsers extends ApiQueryBase {
                        }
 
                        $data = array(
-                               'userid' => $row->user_id,
+                               'userid' => (int)$row->user_id,
                                'name' => $row->user_name,
                        );
 
                        if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
-                               $data['blockid'] = $row->ipb_id;
+                               $data['blockid'] = (int)$row->ipb_id;
                                $data['blockedby'] = $row->ipb_by_text;
-                               $data['blockedbyid'] = $row->ipb_by;
+                               $data['blockedbyid'] = (int)$row->ipb_by;
                                $data['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
                                $data['blockreason'] = $row->ipb_reason;
                                $data['blockexpiry'] = $row->ipb_expiry;
index fe77882..dbed36c 100644 (file)
@@ -277,7 +277,7 @@ class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
 
                                $vals = array();
                                if ( $fld_pageid ) {
-                                       $vals['pageid'] = $row->page_id;
+                                       $vals['pageid'] = (int)$row->page_id;
                                }
                                if ( $fld_title ) {
                                        ApiQueryBase::addTitleInfo( $vals,
index 4a7023b..25f0bf7 100644 (file)
@@ -191,19 +191,19 @@ class ApiQueryBlocks extends ApiQueryBase {
                                ApiResult::META_TYPE => 'assoc',
                        );
                        if ( $fld_id ) {
-                               $block['id'] = $row->ipb_id;
+                               $block['id'] = (int)$row->ipb_id;
                        }
                        if ( $fld_user && !$row->ipb_auto ) {
                                $block['user'] = $row->ipb_address;
                        }
                        if ( $fld_userid && !$row->ipb_auto ) {
-                               $block['userid'] = $row->ipb_user;
+                               $block['userid'] = (int)$row->ipb_user;
                        }
                        if ( $fld_by ) {
                                $block['by'] = $row->ipb_by_text;
                        }
                        if ( $fld_byid ) {
-                               $block['byid'] = $row->ipb_by;
+                               $block['byid'] = (int)$row->ipb_by;
                        }
                        if ( $fld_timestamp ) {
                                $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
index 4514fa9..524bffd 100644 (file)
@@ -89,7 +89,7 @@ class ApiQueryContributors extends ApiQueryBase {
                $res = $this->select( __METHOD__ );
                foreach ( $res as $row ) {
                        $fit = $result->addValue( array( 'query', 'pages', $row->page ),
-                               'anoncontributors', $row->anons
+                               'anoncontributors', (int)$row->anons
                        );
                        if ( !$fit ) {
                                // This not fitting isn't reasonable, so it probably means that
@@ -189,7 +189,7 @@ class ApiQueryContributors extends ApiQueryBase {
                        }
 
                        $fit = $this->addPageSubItem( $row->page,
-                               array( 'userid' => $row->user, 'name' => $row->username ),
+                               array( 'userid' => (int)$row->user, 'name' => $row->username ),
                                'user'
                        );
                        if ( !$fit ) {
index 72a331f..76f594e 100644 (file)
@@ -328,7 +328,7 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                                                $rev['user'] = $row->ar_user_text;
                                        }
                                        if ( $fld_userid ) {
-                                               $rev['userid'] = $row->ar_user;
+                                               $rev['userid'] = (int)$row->ar_user;
                                        }
                                }
                        }
index 4d357a7..5488984 100644 (file)
@@ -162,7 +162,7 @@ class ApiQueryFilearchive extends ApiQueryBase {
                        }
 
                        $file = array();
-                       $file['id'] = $row->fa_id;
+                       $file['id'] = (int)$row->fa_id;
                        $file['name'] = $row->fa_name;
                        $title = Title::makeTitle( NS_FILE, $row->fa_name );
                        self::addTitleInfo( $file, $title );
@@ -179,7 +179,7 @@ class ApiQueryFilearchive extends ApiQueryBase {
                        if ( $fld_user &&
                                Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
                        ) {
-                               $file['userid'] = $row->fa_user;
+                               $file['userid'] = (int)$row->fa_user;
                                $file['user'] = $row->fa_user_text;
                        }
                        if ( $fld_sha1 ) {
index fb65e5e..033310d 100644 (file)
@@ -123,7 +123,7 @@ class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
                                }
 
                                if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
-                                       $vals['userid'] = $row->pt_user;
+                                       $vals['userid'] = (int)$row->pt_user;
                                }
 
                                if ( isset( $prop['comment'] ) ) {
index f6a6478..74bccc2 100644 (file)
@@ -458,7 +458,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
                                }
 
                                if ( $this->fld_userid ) {
-                                       $vals['userid'] = $row->rc_user;
+                                       $vals['userid'] = (int)$row->rc_user;
                                }
 
                                if ( !$row->rc_user ) {
index e5ec67d..480a1ab 100644 (file)
@@ -340,7 +340,7 @@ class ApiQueryContributions extends ApiQueryBase {
                }
 
                // Any rows where we can't view the user were filtered out in the query.
-               $vals['userid'] = $row->rev_user;
+               $vals['userid'] = (int)$row->rev_user;
                $vals['user'] = $row->rev_user_text;
                if ( $row->rev_deleted & Revision::DELETED_USER ) {
                        $vals['userhidden'] = true;
index f22c213..8b1a075 100644 (file)
@@ -193,9 +193,9 @@ class ApiQueryUsers extends ApiQueryBase {
                                        $data[$name]['hidden'] = true;
                                }
                                if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
-                                       $data[$name]['blockid'] = $row->ipb_id;
+                                       $data[$name]['blockid'] = (int)$row->ipb_id;
                                        $data[$name]['blockedby'] = $row->ipb_by_text;
-                                       $data[$name]['blockedbyid'] = $row->ipb_by;
+                                       $data[$name]['blockedbyid'] = (int)$row->ipb_by;
                                        $data[$name]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
                                        $data[$name]['blockreason'] = $row->ipb_reason;
                                        $data[$name]['blockexpiry'] = $row->ipb_expiry;
index 9f7387c..3eb57fd 100644 (file)
@@ -332,9 +332,9 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                        }
                        if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_USER, $user ) ) {
                                if ( $this->fld_userid ) {
-                                       $vals['userid'] = $row->rc_user;
+                                       $vals['userid'] = (int)$row->rc_user;
                                        // for backwards compatibility
-                                       $vals['user'] = $row->rc_user;
+                                       $vals['user'] = (int)$row->rc_user;
                                }
 
                                if ( $this->fld_user ) {
index ad12e19..d2b5ecb 100644 (file)
@@ -134,7 +134,7 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return int
         */
        function insertId() {
-               return $this->mConn->insert_id;
+               return (int)$this->mConn->insert_id;
        }
 
        /**
index 1d45428..0713a92 100644 (file)
@@ -485,7 +485,7 @@ class ArchivedFile {
                if ( $type == 'text' ) {
                        return $this->user_text;
                } elseif ( $type == 'id' ) {
-                       return $this->user;
+                       return (int)$this->user;
                }
 
                throw new MWException( "Unknown type '$type'." );
index 3d5d5d6..e51f381 100644 (file)
@@ -219,11 +219,15 @@ class ForeignAPIFile extends File {
        }
 
        /**
-        * @param string $method
+        * @param string $type
         * @return int|null|string
         */
-       public function getUser( $method = 'text' ) {
-               return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
+       public function getUser( $type = 'text' ) {
+               if ( $type == 'text' ) {
+                       return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
+               } elseif ( $type == 'id' ) {
+                       return 0; // What makes sense here, for a remote user?
+               }
        }
 
        /**
index d368d90..ba437f0 100644 (file)
@@ -734,7 +734,7 @@ class LocalFile extends File {
                if ( $type == 'text' ) {
                        return $this->user_text;
                } elseif ( $type == 'id' ) {
-                       return $this->user;
+                       return (int)$this->user;
                }
        }
 
@@ -753,7 +753,7 @@ class LocalFile extends File {
        function getBitDepth() {
                $this->load();
 
-               return $this->bits;
+               return (int)$this->bits;
        }
 
        /**