From: Bryan Tong Minh Date: Fri, 30 May 2008 13:16:08 +0000 (+0000) Subject: * Use a proper factory for newFromRow X-Git-Tag: 1.31.0-rc.0~47288 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=57af2aecb53a4ee5cececbd322266f253e5f7ec6;p=lhc%2Fweb%2Fwiklou.git * Use a proper factory for newFromRow * Don't remove found files from the $titles parameter in findFiles --- diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 44ad17de31..3606292f5e 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -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; } diff --git a/includes/filerepo/ForeignDBRepo.php b/includes/filerepo/ForeignDBRepo.php index 8dc4ed52b9..294997f895 100644 --- a/includes/filerepo/ForeignDBRepo.php +++ b/includes/filerepo/ForeignDBRepo.php @@ -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 ) ) diff --git a/includes/filerepo/ForeignDBViaLBRepo.php b/includes/filerepo/ForeignDBViaLBRepo.php index e464f080b5..7648c23468 100644 --- a/includes/filerepo/ForeignDBViaLBRepo.php +++ b/includes/filerepo/ForeignDBViaLBRepo.php @@ -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 ) ) diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index 0d7fbf00f2..90b198c806 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -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; diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index 938c67fa75..7cb837b373 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -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; }