* Use a proper factory for newFromRow
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Fri, 30 May 2008 13:16:08 +0000 (13:16 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Fri, 30 May 2008 13:16:08 +0000 (13:16 +0000)
* Don't remove found files from the $titles parameter in findFiles

includes/filerepo/FileRepo.php
includes/filerepo/ForeignDBRepo.php
includes/filerepo/ForeignDBViaLBRepo.php
includes/filerepo/LocalRepo.php
includes/filerepo/RepoGroup.php

index 44ad17d..3606292 100644 (file)
@@ -127,19 +127,16 @@ abstract class FileRepo {
        }
        
        /*
-        * Find many files at once. Removes the corresponding titles
-        * of files that are found from $titles.
+        * Find many files at once. 
         * @param array $titles, an array of titles
         * @param int $flags
         */
-       function findFiles( &$titles, $flags ) {
+       function findFiles( $titles, $flags ) {
                $result = array();
                foreach ( $titles as $index => $title ) {
-                       $file = $this->findFile( $title );
-                       if ( $file ) {
+                       $file = $this->findFile( $title, $flags );
+                       if ( $file )
                                $result[$file->getTitle()->getDBkey()] = $file;
-                               unset( $titles[$index] );
-                       }
                }
                return $result;
        }
index 8dc4ed5..294997f 100644 (file)
@@ -12,6 +12,7 @@ class ForeignDBRepo extends LocalRepo {
        # Other stuff
        var $dbConn;
        var $fileFactory = array( 'ForeignDBFile', 'newFromTitle' );
+       var $fileFromRowFactory = array( 'ForeignDBFile', 'newFromRow' );
 
        function newFileFromRow( $row ) {
                if ( isset( $row->img_name ) )
index e464f08..7648c23 100644 (file)
@@ -7,6 +7,7 @@
 class ForeignDBViaLBRepo extends LocalRepo {
        var $wiki, $dbName, $tablePrefix;
        var $fileFactory = array( 'ForeignDBFile', 'newFromTitle' );
+       var $fileFromRowFactory = array( 'ForeignDBFile', 'newFromRow' );
 
        function newFileFromRow( $row ) {
                if ( isset( $row->img_name ) )
index 0d7fbf0..90b198c 100644 (file)
@@ -7,6 +7,8 @@
 class LocalRepo extends FSRepo {
        var $fileFactory = array( 'LocalFile', 'newFromTitle' );
        var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
+       var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
+       var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
 
        function getSlaveDB() {
                return wfGetDB( DB_SLAVE );
@@ -22,9 +24,9 @@ class LocalRepo extends FSRepo {
 
        function newFileFromRow( $row ) {
                if ( isset( $row->img_name ) ) {
-                       return LocalFile::newFromRow( $row, $this );
+                       return call_user_func( $this->fileFromRowFactory, $row, $this );
                } elseif ( isset( $row->oi_name ) ) {
-                       return OldLocalFile::newFromRow( $row, $this );
+                       return call_user_func( $this->oldFileFromRowFactory, $row, $this );
                } else {
                        throw new MWException( __METHOD__.': invalid row' );
                }
@@ -159,33 +161,26 @@ class LocalRepo extends FSRepo {
                return $result;
        }
        
-       function findFiles( &$titles, $flags ) {
+       /*
+        * Find many files using one query
+        */
+       function findFiles( $titles, $flags ) {
+               // FIXME: Comply with $flags
+               // FIXME: Only accepts a $titles array where the keys are the sanitized
+               // file names.
+                
                if ( count( $titles ) == 0 ) return array();            
        
-               $dbKeys = array();
-               $indices = array();
-               
-               foreach ( $titles as $index => $title ) {
-                       if ( !( $title instanceof Title ) )
-                               $title = Title::makeTitleSafe( NS_IMAGE, $title );
-                       if ( is_object( $title ) ) {
-                               $key = $title->getDBkey();
-                               $indices[$key] = $index;
-                               $dbKeys[] = $key;
-                       }
-               }
-       
                $dbr = $this->getSlaveDB();
                $res = $dbr->select(
                        'image',
                        LocalFile::selectFields(),
-                       array( 'img_name' => $dbKeys )          
+                       array( 'img_name' => array_keys( $titles ) )            
                );
                
                $result = array();
                while ( $row = $res->fetchObject() ) {
                        $result[$row->img_name] = $this->newFileFromRow( $row );
-                       unset( $titles[$indices[$row->img_name]] );
                }
                $res->free();
                return $result;
index 938c67f..7cb837b 100644 (file)
@@ -87,10 +87,22 @@ class RepoGroup {
                        $this->initialiseRepos();
                }
 
-               $images = $this->localRepo->findFiles( $titles, $flags );
+               $titleObjs = array();
+               foreach ( $titles as $title ) {
+                       if ( !( $title instanceof Title ) )
+                               $title = Title::makeTitleSafe( NS_IMAGE, $title );
+                       $titleObjs[$title->getDBkey()] = $title;
+               }
+
+               $images = $this->localRepo->findFiles( $titleObjs, $flags );
 
                foreach ( $this->foreignRepos as $repo ) {
-                       $images = array_merge( $images, $repo->findFiles( $titles, $flags ) );
+                       // Remove found files from $titleObjs
+                       foreach ( $images as $name => $image )
+                               if ( isset( $titleObjs[$name] ) )
+                                       unset( $titleObjs[$name] );
+                       
+                       $images = array_merge( $images, $repo->findFiles( $titleObjs, $flags ) );
                }
                return $images;
        }