Fill GenderCache inside ApiPageSet
authorumherirrender <umherirrender_de.wp@web.de>
Wed, 16 May 2012 14:46:22 +0000 (16:46 +0200)
committerumherirrender <umherirrender_de.wp@web.de>
Wed, 16 May 2012 14:46:22 +0000 (16:46 +0200)
the queries for title and pageids are own queries created by the api,
which does not use the GenderCache, that can produce a query per row

Change-Id: I932f8d9d1cfa751dbb6f5237e2de325527d3ff53

includes/api/ApiPageSet.php
includes/cache/GenderCache.php

index cc86c76..598f9ad 100644 (file)
@@ -481,6 +481,7 @@ class ApiPageSet extends ApiQueryBase {
                        ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
                }
 
+               $usernames = array();
                if ( $res ) {
                        foreach ( $res as $row ) {
                                $pageId = intval( $row->page_id );
@@ -496,6 +497,11 @@ class ApiPageSet extends ApiQueryBase {
 
                                // Store any extra fields requested by modules
                                $this->processDbRow( $row );
+
+                               // Need gender information
+                               if( MWNamespace::hasGenderDistinction( $row->page_namespace ) ) {
+                                       $usernames[] = $row->page_title;
+                               }
                        }
                }
 
@@ -510,6 +516,11 @@ class ApiPageSet extends ApiQueryBase {
                                                $this->mMissingTitles[$this->mFakePageId] = $title;
                                                $this->mFakePageId--;
                                                $this->mTitles[] = $title;
+
+                                               // need gender information
+                                               if( MWNamespace::hasGenderDistinction( $ns ) ) {
+                                                       $usernames[] = $dbkey;
+                                               }
                                        }
                                }
                        } else {
@@ -521,6 +532,10 @@ class ApiPageSet extends ApiQueryBase {
                                }
                        }
                }
+
+               // Get gender information
+               $genderCache = GenderCache::singleton();
+               $genderCache->doQuery( $usernames, __METHOD__ );
        }
 
        /**
@@ -664,6 +679,9 @@ class ApiPageSet extends ApiQueryBase {
         * @return LinkBatch
         */
        private function processTitlesArray( $titles ) {
+               $genderCache = GenderCache::singleton();
+               $genderCache->doTitlesArray( $titles, __METHOD__ );
+
                $linkBatch = new LinkBatch();
 
                foreach ( $titles as $title ) {
index 6a9ecd2..2a169bb 100644 (file)
@@ -111,6 +111,29 @@ class GenderCache {
                $this->doQuery( array_keys( $users ), $caller );
        }
 
+       /**
+        * Wrapper for doQuery that processes a title or string array.
+        *
+        * @since 1.20
+        * @param $titles List: array of Title objects or strings
+        * @param $caller String: the calling method
+        */
+       public function doTitlesArray( $titles, $caller = '' ) {
+               $users = array();
+               foreach ( $titles as $title ) {
+                       $titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
+                       if ( !$titleObj ) {
+                               continue;
+                       }
+                       if ( !MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
+                               continue;
+                       }
+                       $users[] = $titleObj->getText();
+               }
+
+               $this->doQuery( $users, $caller );
+       }
+
        /**
         * Preloads genders for given list of users.
         * @param $users List|String: usernames