Add some documentation
authorSam Reed <reedy@users.mediawiki.org>
Wed, 1 Jun 2011 16:40:59 +0000 (16:40 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Wed, 1 Jun 2011 16:40:59 +0000 (16:40 +0000)
Swap if ( $foo ) { $this->addFields( 'foo' ); } for $this->addFieldsIf( 'foo', $foo ); etc

Also swap $this->addFieldsIf( 'foo', $foo ); $this->addFieldsIf( 'foo2', $foo ); for $this->addFieldsIf( array( 'foo', 'foo2' ), $foo );

Micro-optimisation and readability!

includes/api/ApiQueryBase.php
includes/api/ApiQueryBlocks.php
includes/api/ApiQueryDeletedrevs.php
includes/api/ApiQueryFilearchive.php
includes/api/ApiQueryLogEvents.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryTags.php
includes/api/ApiQueryUserContributions.php
includes/api/ApiQueryWatchlist.php

index 8d43479..74e8ae6 100644 (file)
@@ -111,7 +111,7 @@ abstract class ApiQueryBase extends ApiBase {
 
        /**
         * Add a set of fields to select to the internal array
-        * @param $value mixed Field name or array of field names
+        * @param $value array|string Field name or array of field names
         */
        protected function addFields( $value ) {
                if ( is_array( $value ) ) {
@@ -123,7 +123,7 @@ abstract class ApiQueryBase extends ApiBase {
 
        /**
         * Same as addFields(), but add the fields only if a condition is met
-        * @param $value mixed See addFields()
+        * @param $value array|string See addFields()
         * @param $condition bool If false, do nothing
         * @return bool $condition
         */
index 9cea45f..3eed956 100644 (file)
@@ -70,33 +70,17 @@ class ApiQueryBlocks extends ApiQueryBase {
                $this->addTables( 'ipblocks' );
                $this->addFields( 'ipb_auto' );
 
-               if ( $fld_id ) {
-                       $this->addFields( 'ipb_id' );
-               }
-               if ( $fld_user || $fld_userid ) {
-                       $this->addFields( array( 'ipb_address', 'ipb_user' ) );
-               }
-               if ( $fld_by ) {
-                       $this->addFields( 'ipb_by_text' );
-               }
-               if ( $fld_byid ) {
-                       $this->addFields( 'ipb_by' );
-               }
-               if ( $fld_timestamp ) {
-                       $this->addFields( 'ipb_timestamp' );
-               }
-               if ( $fld_expiry ) {
-                       $this->addFields( 'ipb_expiry' );
-               }
-               if ( $fld_reason ) {
-                       $this->addFields( 'ipb_reason' );
-               }
-               if ( $fld_range ) {
-                       $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
-               }
-               if ( $fld_flags ) {
-                       $this->addFields( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
-               }
+               $this->addFieldsIf ( 'ipb_id', $fld_id );
+               $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user || $fld_userid );
+               $this->addFieldsIf( 'ipb_by_text', $fld_by );
+               $this->addFieldsIf( 'ipb_by', $fld_byid );
+               $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp );
+               $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
+               $this->addFieldsIf( 'ipb_reason', $fld_reason );
+               $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range );
+               $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
+                                                                       'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ),
+                                                       $fld_flags );
 
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
                $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
index d774733..486b9a9 100644 (file)
@@ -99,27 +99,14 @@ class ApiQueryDeletedrevs extends ApiQueryBase {
                $this->addWhere( 'ar_deleted = 0' );
                $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
 
-               if ( $fld_parentid ) {
-                       $this->addFields( 'ar_parent_id' );
-               }
-               if ( $fld_revid ) {
-                       $this->addFields( 'ar_rev_id' );
-               }
-               if ( $fld_user ) {
-                       $this->addFields( 'ar_user_text' );
-               }
-               if ( $fld_userid ) {
-                       $this->addFields( 'ar_user' );
-               }
-               if ( $fld_comment || $fld_parsedcomment ) {
-                       $this->addFields( 'ar_comment' );
-               }
-               if ( $fld_minor ) {
-                       $this->addFields( 'ar_minor_edit' );
-               }
-               if ( $fld_len ) {
-                       $this->addFields( 'ar_len' );
-               }
+               $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
+               $this->addFieldsIf( 'ar_rev_id', $fld_revid );
+               $this->addFieldsIf( 'ar_user_text', $fld_user );
+               $this->addFieldsIf( 'ar_user', $fld_userid );
+               $this->addFieldsIf( 'ar_comment', $fld_comment || $fld_parsedcomment );
+               $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
+               $this->addFieldsIf( 'ar_len', $fld_len );
+
                if ( $fld_content ) {
                        $this->addTables( 'text' );
                        $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
index 5cde9b7..0f53141 100644 (file)
@@ -69,21 +69,10 @@ class ApiQueryFilearchive extends ApiQueryBase {
                $this->addFields( array( 'fa_name', 'fa_deleted' ) );
                $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
                $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
-
-               if ( $fld_user ) {
-                       $this->addFields( array( 'fa_user', 'fa_user_text' ) );
-               }
-
-               if ( $fld_dimensions || $fld_size ) {
-                       $this->addFields( array( 'fa_height', 'fa_width', 'fa_size' ) );
-               }
-
+               $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
+               $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
                $this->addFieldsIf( 'fa_description', $fld_description );
-
-               if ( $fld_mime ) {
-                       $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
-               }
-
+               $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
                $this->addFieldsIf( 'fa_metadata', $fld_metadata );
                $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
 
index 234d47c..8370244 100644 (file)
@@ -86,13 +86,10 @@ class ApiQueryLogEvents extends ApiQueryBase {
                        'log_deleted',
                ) );
 
-               $this->addFieldsIf( 'log_id', $this->fld_ids );
-               $this->addFieldsIf( 'page_id', $this->fld_ids );
-               $this->addFieldsIf( 'log_user', $this->fld_user );
-               $this->addFieldsIf( 'user_name', $this->fld_user );
+               $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids );
+               $this->addFieldsIf( array( 'log_user', 'user_name' ), $this->fld_user );
                $this->addFieldsIf( 'user_id', $this->fld_userid );
-               $this->addFieldsIf( 'log_namespace', $this->fld_title || $this->fld_parsedcomment );
-               $this->addFieldsIf( 'log_title', $this->fld_title || $this->fld_parsedcomment );
+               $this->addFieldsIf( array( 'log_namespace', 'log_title' ), $this->fld_title || $this->fld_parsedcomment );
                $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
                $this->addFieldsIf( 'log_params', $this->fld_details );
 
index 0260212..2712bea 100644 (file)
@@ -224,22 +224,14 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
                        }
 
                        /* Add fields to our query if they are specified as a needed parameter. */
-                       $this->addFieldsIf( 'rc_id', $this->fld_ids );
-                       $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids );
-                       $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids );
+                       $this->addFieldsIf( array( 'rc_id', 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids );
                        $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
                        $this->addFieldsIf( 'rc_user', $this->fld_user );
                        $this->addFieldsIf( 'rc_user_text', $this->fld_user || $this->fld_userid );
-                       $this->addFieldsIf( 'rc_minor', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_bot', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_new', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
-                       $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
+                       $this->addFieldsIf( array( 'rc_minor', 'rc_new', 'rc_bot' ) , $this->fld_flags );
+                       $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
                        $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
-                       $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
+                       $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo );
                        $showRedirects = $this->fld_redirect || isset( $show['redirect'] ) || isset( $show['!redirect'] );
                }
 
index 61b3466..12f3eed 100644 (file)
@@ -64,9 +64,7 @@ class ApiQueryTags extends ApiQueryBase {
                $this->addTables( 'change_tag' );
                $this->addFields( 'ct_tag' );
 
-               if ( $this->fld_hitcount ) {
-                       $this->addFields( 'count(*) AS hitcount' );
-               }
+               $this->addFieldsIf( 'count(*) AS hitcount', $this->fld_hitcount );
 
                $this->addOption( 'LIMIT', $this->limit + 1 );
                $this->addOption( 'GROUP BY', 'ct_tag' );
index b294589..0b7a5f9 100644 (file)
@@ -249,8 +249,7 @@ class ApiQueryContributions extends ApiQueryBase {
                // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
                $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
                $this->addFieldsIf( 'rev_len', $this->fld_size );
-               $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
-               $this->addFieldsIf( 'rev_parent_id', $this->fld_flags );
+               $this->addFieldsIf( array( 'rev_minor_edit', 'rev_parent_id' ), $this->fld_flags );
                $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
 
                if ( $this->fld_tags ) {
index 0443eeb..e5507f6 100644 (file)
@@ -101,20 +101,14 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                                'rc_last_oldid',
                        ) );
 
-                       $this->addFieldsIf( 'rc_new', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_minor', $this->fld_flags );
-                       $this->addFieldsIf( 'rc_bot', $this->fld_flags );
+                       $this->addFieldsIf( array( 'rc_new', 'rc_minor', 'rc_bot' ), $this->fld_flags );
                        $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
                        $this->addFieldsIf( 'rc_user_text', $this->fld_user );
                        $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
                        $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
-                       $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
-                       $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
+                       $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
                        $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
-                       $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
-                       $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
+                       $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo );
                } elseif ( $params['allrev'] ) {
                        $this->addFields( 'rc_this_oldid' );
                } else {