Changed TIME_ONLY to NAME_AND_TIME_ONLY in FileRepo
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 9 Dec 2013 20:24:32 +0000 (12:24 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 11 Dec 2013 19:54:31 +0000 (11:54 -0800)
* This now returns a (final DB key, timestamp) map for each file.
  This makes proper redirect handling easier for callers.
* Also fixed the case where ucfirst normalization is different in the
  repo versus the local wiki.
* Also added $flags to RepoGroup::findFiles() as expected.

Change-Id: I978a1cc0a0589179c46e477cfd06c8bff4c08fa9

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

index f1d3499..cab5690 100644 (file)
@@ -40,7 +40,7 @@ class FileRepo {
        const OVERWRITE_SAME = 4;
        const SKIP_LOCKING = 8;
 
-       const TIME_ONLY = 1;
+       const NAME_AND_TIME_ONLY = 1;
 
        /** @var bool Whether to fetch commons image description pages and display
         *    them on the local wiki */
@@ -477,7 +477,9 @@ class FileRepo {
         *
         *    No title should appear in $items twice, as the result use titles as keys
         * @param int $flags Supports:
-        *     - FileRepo::TIME_ONLY : return a (file name => timestamp) map instead
+        *     - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
+        *       The search title uses the input titles; the other is the final post-redirect title.
+        *       All titles are returned as string DB keys and the inner array is associative.
         * @return array Map of (file name => File objects) for matches
         */
        public function findFiles( array $items, $flags = 0 ) {
@@ -493,8 +495,15 @@ class FileRepo {
                        }
                        $file = $this->findFile( $title, $options );
                        if ( $file ) {
-                               $result[$title->getDBkey()] =
-                                       ( $flags & self::TIME_ONLY ) ? $file->getTimestamp() : $file;
+                               $searchName = File::normalizeTitle( $title )->getDBkey(); // must be valid
+                               if ( $flags & self::NAME_AND_TIME_ONLY ) {
+                                       $result[$searchName] = array(
+                                               'title'     => $file->getTitle()->getDBkey(),
+                                               'timestamp' => $file->getTimestamp()
+                                       );
+                               } else {
+                                       $result[$searchName] = $file;
+                               }
                        }
                }
 
index e38f71d..2df28bc 100644 (file)
@@ -243,13 +243,18 @@ class LocalRepo extends FileRepo {
        public function findFiles( array $items, $flags = 0 ) {
                $finalFiles = array(); // map of (DB key => corresponding File) for matches
 
-               $searchSet = array(); // map of (DB key => normalized search params)
+               $searchSet = array(); // map of (normalized DB key => search params)
                foreach ( $items as $item ) {
-                       $title = is_array( $item )
-                               ? File::normalizeTitle( $item['title'] )
-                               : File::normalizeTitle( $item );
-                       if ( $title ) { // valid title
-                               $searchSet[$title->getDbKey()] = ( is_array( $item ) ? $item : array() );
+                       if ( is_array( $item ) ) {
+                               $title = File::normalizeTitle( $item['title'] );
+                               if ( $title ) {
+                                       $searchSet[$title->getDBkey()] = $item;
+                               }
+                       } else {
+                               $title = File::normalizeTitle( $item );
+                               if ( $title ) {
+                                       $searchSet[$title->getDBkey()] = array();
+                               }
                        }
                }
 
@@ -272,11 +277,12 @@ class LocalRepo extends FileRepo {
                {
                        foreach ( $res as $row ) {
                                $file = $repo->newFileFromRow( $row );
-                               $dbKey = $file->getName();
-                               // There must have been a search for this DB Key
+                               $dbKey = $file->getTitle()->getDBkey();
+                               // There must have been a search for this exact DB Key
                                if ( $fileMatchesSearch( $file, $searchSet[$dbKey] ) ) {
-                                       $finalFiles[$dbKey] =
-                                               ( $flags & FileRepo::TIME_ONLY ) ? $file->getTimestamp() : $file;
+                                       $finalFiles[$dbKey] = ( $flags & FileRepo::NAME_AND_TIME_ONLY )
+                                               ? array( 'title' => $dbKey, 'timestamp' => $file->getTimestamp() )
+                                               : $file;
                                        unset( $searchSet[$dbKey] );
                                }
                        }
@@ -285,7 +291,10 @@ class LocalRepo extends FileRepo {
                $dbr = $this->getSlaveDB();
 
                // Query image table
-               $imgNames = array_keys( $searchSet );
+               $imgNames = array();
+               foreach ( array_keys( $searchSet ) as $dbKey ) {
+                       $imgNames[] = $this->getNameFromTitle( File::normalizeTitle( $dbKey ) );
+               }
                if ( count( $imgNames ) ) {
                        $res = $dbr->select( 'image',
                                LocalFile::selectFields(), array( 'img_name' => $imgNames ), __METHOD__ );
@@ -296,8 +305,13 @@ class LocalRepo extends FileRepo {
                $oiConds = array(); // WHERE clause array for each file
                foreach ( $searchSet as $dbKey => $search ) {
                        if ( isset( $search['params']['time'] ) ) {
-                               $oiConds[] = $dbr->makeList( array( 'oi_name' => $dbKey,
-                                       'oi_timestamp' => $dbr->timestamp( $search['params']['time'] ) ), LIST_AND );
+                               $oiConds[] = $dbr->makeList(
+                                       array(
+                                               'oi_name' => $this->getNameFromTitle( File::normalizeTitle( $dbKey ) ),
+                                               'oi_timestamp' => $dbr->timestamp( $search['params']['time'] )
+                                       ),
+                                       LIST_AND
+                               );
                        }
                }
                if ( count( $oiConds ) ) {
@@ -317,8 +331,14 @@ class LocalRepo extends FileRepo {
                                $file = $this->newFile( $redir );
                                if ( $file && $fileMatchesSearch( $file, $search ) ) {
                                        $file->redirectedFrom( $title->getDBkey() );
-                                       $finalFiles[$dbKey] =
-                                               ( $flags & FileRepo::TIME_ONLY ) ? $file->getTimestamp() : $file;
+                                       if ( $flags & FileRepo::NAME_AND_TIME_ONLY ) {
+                                               $finalFiles[$dbKey] = array(
+                                                       'title'     => $file->getTitle()->getDBkey(),
+                                                       'timestamp' => $file->getTimestamp()
+                                               );
+                                       } else {
+                                               $finalFiles[$dbKey] = $file;
+                                       }
                                }
                        }
                }
index 100a11b..c6e93b6 100644 (file)
@@ -174,10 +174,27 @@ class RepoGroup {
        }
 
        /**
+        * Search repositories for many files at once.
+        *
+        * @param array $items An array of titles, or an array of findFile() options with
+        *    the "title" option giving the title. Example:
+        *
+        *     $findItem = array( 'title' => $title, 'private' => true );
+        *     $findBatch = array( $findItem );
+        *     $repo->findFiles( $findBatch );
+        *
+        *    No title should appear in $items twice, as the result use titles as keys
+        * @param int $flags Supports:
+        *     - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
+        *       The search title uses the input titles; the other is the final post-redirect title.
+        *       All titles are returned as string DB keys and the inner array is associative.
+        * @return array Map of (file name => File objects) for matches
+        *
         * @param array $inputItems
+        * @param integer $flags
         * @return array
         */
-       function findFiles( $inputItems ) {
+       function findFiles( array $inputItems, $flags = 0 ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
@@ -193,7 +210,7 @@ class RepoGroup {
                        }
                }
 
-               $images = $this->localRepo->findFiles( $items );
+               $images = $this->localRepo->findFiles( $items, $flags );
 
                foreach ( $this->foreignRepos as $repo ) {
                        // Remove found files from $items
@@ -201,7 +218,7 @@ class RepoGroup {
                                unset( $items[$name] );
                        }
 
-                       $images = array_merge( $images, $repo->findFiles( $items ) );
+                       $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
                }
 
                return $images;