Replace more uses of "SELECT *"
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 12 Oct 2017 18:34:15 +0000 (14:34 -0400)
committerAnomie <bjorsch@wikimedia.org>
Fri, 13 Oct 2017 19:02:56 +0000 (19:02 +0000)
With the introduction of CommentStore, selects from various table
require certain joins or column aliases for proper operation. The
upcoming actor table change, and the suggested title table change, will
add more such requirements.

Change-Id: Ic8213bff74b8350b15cd271d0ef252e63e7e79bd

15 files changed:
includes/CategoryFinder.php
includes/MovePage.php
includes/installer/DatabaseInstaller.php
includes/libs/objectcache/WANObjectCache.php
includes/upload/UploadStash.php
maintenance/checkImages.php
maintenance/cleanupBlocks.php
maintenance/compareParserCache.php
maintenance/eraseArchivedFile.php
maintenance/populateLogSearch.php
maintenance/populateRevisionSha1.php
maintenance/rebuildImages.php
maintenance/refreshFileHeaders.php
maintenance/refreshImageMetadata.php
maintenance/storage/testCompression.php

index 89bf5c7..2a70f5f 100644 (file)
@@ -190,7 +190,7 @@ class CategoryFinder {
                $layer = [];
                $res = $this->dbr->select(
                        /* FROM   */ 'categorylinks',
-                       /* SELECT */ '*',
+                       /* SELECT */ [ 'cl_to', 'cl_from' ],
                        /* WHERE  */ [ 'cl_from' => $this->next ],
                        __METHOD__ . '-1'
                );
index 2f8255b..fe82465 100644 (file)
@@ -310,7 +310,7 @@ class MovePage {
                        # Protect the redirect title as the title used to be...
                        $res = $dbw->select(
                                'page_restrictions',
-                               '*',
+                               [ 'pr_type', 'pr_level', 'pr_cascade', 'pr_user', 'pr_expiry' ],
                                [ 'pr_page' => $pageid ],
                                __METHOD__,
                                'FOR UPDATE'
index 6c56b3d..d4150c8 100644 (file)
@@ -697,7 +697,7 @@ abstract class DatabaseInstaller {
                }
                $this->db->selectDB( $this->getVar( 'wgDBname' ) );
 
-               if ( $this->db->selectRow( 'interwiki', '*', [], __METHOD__ ) ) {
+               if ( $this->db->selectRow( 'interwiki', '1', [], __METHOD__ ) ) {
                        $status->warning( 'config-install-interwiki-exists' );
 
                        return $status;
index 1f757a4..cab5782 100644 (file)
@@ -1083,7 +1083,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
         *             $setOpts += Database::getCacheSetOptions( $dbr );
         *
         *             // Load the row for this file
-        *             $row = $dbr->selectRow( 'file', '*', [ 'id' => $id ], __METHOD__ );
+        *             $row = $dbr->selectRow( 'file', File::selectFields(), [ 'id' => $id ], __METHOD__ );
         *
         *             return $row ? (array)$row : false;
         *         },
@@ -1169,7 +1169,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
         *
         *             // Load the rows for these files
         *             $rows = [];
-        *             $res = $dbr->select( 'file', '*', [ 'id' => $ids ], __METHOD__ );
+        *             $res = $dbr->select( 'file', File::selectFields(), [ 'id' => $ids ], __METHOD__ );
         *             foreach ( $res as $row ) {
         *                 $rows[$row->id] = $row;
         *                 $mtime = wfTimestamp( TS_UNIX, $row->timestamp );
index 755f9fd..da896f9 100644 (file)
@@ -515,7 +515,12 @@ class UploadStash {
 
                $row = $dbr->selectRow(
                        'uploadstash',
-                       '*',
+                       [
+                               'us_user', 'us_key', 'us_orig_path', 'us_path', 'us_props',
+                               'us_size', 'us_sha1', 'us_mime', 'us_media_type',
+                               'us_image_width', 'us_image_height', 'us_image_bits',
+                               'us_source_type', 'us_timestamp', 'us_status',
+                       ],
                        [ 'us_key' => $key ],
                        __METHOD__
                );
index 3e57393..2df0a09 100644 (file)
@@ -44,7 +44,8 @@ class CheckImages extends Maintenance {
 
                $repo = RepoGroup::singleton()->getLocalRepo();
                do {
-                       $res = $dbr->select( 'image', '*', [ 'img_name > ' . $dbr->addQuotes( $start ) ],
+                       $res = $dbr->select( 'image', LocalFile::selectFields(),
+                               [ 'img_name > ' . $dbr->addQuotes( $start ) ],
                                __METHOD__, [ 'LIMIT' => $this->mBatchSize ] );
                        foreach ( $res as $row ) {
                                $numImages++;
index 7a3034f..f489333 100644 (file)
@@ -66,7 +66,7 @@ class CleanupBlocks extends Maintenance {
                                $bestBlock = null;
                                $res2 = $db->select(
                                        'ipblocks',
-                                       '*',
+                                       Block::selectFields(),
                                        [
                                                'ipb_user' => $row->ipb_user,
                                        ]
index 504c7d7..afbb268 100644 (file)
@@ -45,7 +45,12 @@ class CompareParserCache extends Maintenance {
                $withdiff = 0;
                $parserCache = MediaWikiServices::getInstance()->getParserCache();
                while ( $pages-- > 0 ) {
-                       $row = $dbr->selectRow( 'page', '*',
+                       $row = $dbr->selectRow( 'page',
+                               // @todo Title::selectFields() or Title::getQueryInfo() or something
+                               [
+                                       'page_namespace', 'page_title', 'page_id',
+                                       'page_len', 'page_is_redirect', 'page_latest',
+                               ],
                                [
                                        'page_namespace' => $this->getOption( 'namespace' ),
                                        'page_is_redirect' => 0,
index c90056d..05fbbbc 100644 (file)
@@ -55,7 +55,7 @@ class EraseArchivedFile extends Maintenance {
                        $afile = false;
                } else { // specified version
                        $dbw = $this->getDB( DB_MASTER );
-                       $row = $dbw->selectRow( 'filearchive', '*',
+                       $row = $dbw->selectRow( 'filearchive', ArchivedFile::selectFields(),
                                [ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $filekey ],
                                __METHOD__ );
                        if ( !$row ) {
@@ -85,7 +85,7 @@ class EraseArchivedFile extends Maintenance {
 
        protected function scrubAllVersions( $name ) {
                $dbw = $this->getDB( DB_MASTER );
-               $res = $dbw->select( 'filearchive', '*',
+               $res = $dbw->select( 'filearchive', ArchivedFile::selectFields(),
                        [ 'fa_name' => $name, 'fa_storage_group' => 'deleted' ],
                        __METHOD__ );
                foreach ( $res as $row ) {
index 1b07407..e75b84d 100644 (file)
@@ -76,7 +76,9 @@ class PopulateLogSearch extends LoggedUpdateMaintenance {
                while ( $blockEnd <= $end ) {
                        $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
                        $cond = "log_id BETWEEN $blockStart AND $blockEnd";
-                       $res = $db->select( 'logging', '*', $cond, __FUNCTION__ );
+                       $res = $db->select(
+                               'logging', [ 'log_id', 'log_type', 'log_action', 'log_params' ], $cond, __FUNCTION__
+                       );
                        foreach ( $res as $row ) {
                                // RevisionDelete logs - revisions
                                if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
index fb97e91..c06f1e8 100644 (file)
@@ -55,10 +55,10 @@ class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
                }
 
                $this->output( "Populating rev_sha1 column\n" );
-               $rc = $this->doSha1Updates( 'revision', 'rev_id', 'rev' );
+               $rc = $this->doSha1Updates( 'revision', 'rev_id', Revision::selectFields(), 'rev' );
 
                $this->output( "Populating ar_sha1 column\n" );
-               $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', 'ar' );
+               $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', Revision::selectArchiveFields(), 'ar' );
                $this->output( "Populating ar_sha1 column legacy rows\n" );
                $ac += $this->doSha1LegacyUpdates();
 
@@ -74,7 +74,7 @@ class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
         * @param string $prefix
         * @return int Rows changed
         */
-       protected function doSha1Updates( $table, $idCol, $prefix ) {
+       protected function doSha1Updates( $table, $idCol, $fields, $prefix ) {
                $db = $this->getDB( DB_MASTER );
                $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
                $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
@@ -93,7 +93,7 @@ class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
                        $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
                        $cond = "$idCol BETWEEN $blockStart AND $blockEnd
                                AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
-                       $res = $db->select( $table, '*', $cond, __METHOD__ );
+                       $res = $db->select( $table, $fields, $cond, __METHOD__ );
 
                        $this->beginTransaction( $db, __METHOD__ );
                        foreach ( $res as $row ) {
@@ -117,7 +117,7 @@ class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
        protected function doSha1LegacyUpdates() {
                $count = 0;
                $db = $this->getDB( DB_MASTER );
-               $res = $db->select( 'archive', '*',
+               $res = $db->select( 'archive', Revision::selectArchiveFields(),
                        [ 'ar_rev_id IS NULL', 'ar_sha1' => '' ], __METHOD__ );
 
                $updateSize = 0;
index 109350c..a8fb9a3 100644 (file)
@@ -125,12 +125,12 @@ class ImageBuilder extends Maintenance {
                flush();
        }
 
-       function buildTable( $table, $key, $callback ) {
+       function buildTable( $table, $key, $fields, $callback ) {
                $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
                $this->init( $count, $table );
                $this->output( "Processing $table...\n" );
 
-               $result = $this->getDB( DB_REPLICA )->select( $table, '*', [], __METHOD__ );
+               $result = $this->getDB( DB_REPLICA )->select( $table, $fields, [], __METHOD__ );
 
                foreach ( $result as $row ) {
                        $update = call_user_func( $callback, $row, null );
@@ -145,7 +145,7 @@ class ImageBuilder extends Maintenance {
 
        function buildImage() {
                $callback = [ $this, 'imageCallback' ];
-               $this->buildTable( 'image', 'img_name', $callback );
+               $this->buildTable( 'image', 'img_name', LocalFile::selectFields(), $callback );
        }
 
        function imageCallback( $row, $copy ) {
@@ -157,7 +157,8 @@ class ImageBuilder extends Maintenance {
        }
 
        function buildOldImage() {
-               $this->buildTable( 'oldimage', 'oi_archive_name', [ $this, 'oldimageCallback' ] );
+               $this->buildTable( 'oldimage', 'oi_archive_name', OldLocalFile::selectFields(),
+                       [ $this, 'oldimageCallback' ] );
        }
 
        function oldimageCallback( $row, $copy ) {
index fd3faeb..1670235 100644 (file)
@@ -76,7 +76,7 @@ class RefreshFileHeaders extends Maintenance {
                                $conds[] = "img_minor_mime = {$dbr->addQuotes( $minor_mime )}";
                        }
 
-                       $res = $dbr->select( 'image', '*', $conds,
+                       $res = $dbr->select( 'image', LocalFile::selectFields(), $conds,
                                __METHOD__, [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name ASC' ] );
 
                        if ( $res->numRows() > 0 ) {
index f6e9e9c..41da6b4 100644 (file)
@@ -127,7 +127,7 @@ class RefreshImageMetadata extends Maintenance {
                do {
                        $res = $dbw->select(
                                'image',
-                               '*',
+                               LocalFile::selectFields(),
                                array_merge( $conds, $conds2 ),
                                __METHOD__,
                                $options
index 90d8d03..deb2ca6 100644 (file)
@@ -50,7 +50,11 @@ $type = isset( $options['type'] ) ? $options['type'] : 'ConcatenatedGzipHistoryB
 $dbr = $this->getDB( DB_REPLICA );
 $res = $dbr->select(
        [ 'page', 'revision', 'text' ],
-       '*',
+       array_merge(
+               Revision::selectFields(),
+               Revision::selectPageFields(),
+               Revision::selectTextFields()
+       ),
        [
                'page_namespace' => $title->getNamespace(),
                'page_title' => $title->getDBkey(),