}
/*
- * 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;
}
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 );
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' );
}
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;
$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;
}