Added a TIME_ONLY option to FileRepo::findFiles to use less RAM
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 5 Dec 2013 21:40:14 +0000 (13:40 -0800)
committerAnomie <bjorsch@wikimedia.org>
Mon, 9 Dec 2013 16:30:14 +0000 (16:30 +0000)
* Also renamed $possFile to just $file in method

Change-Id: Idb497901b9a7bd96c828267769a66efa282c5918

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

index f62db89..f834be5 100644 (file)
@@ -40,6 +40,8 @@ class FileRepo {
        const OVERWRITE_SAME = 4;
        const SKIP_LOCKING = 8;
 
+       const TIME_ONLY = 1;
+
        /** @var bool Whether to fetch commons image description pages and display
         *    them on the local wiki */
        public $fetchDescription;
@@ -474,9 +476,11 @@ class FileRepo {
         *     $repo->findFiles( $findBatch );
         *
         *    No title should appear in $items twice, as the result use titles as keys
-        * @return array (Map of file names => File objects) for matches
+        * @param int $flags Supports:
+        *     - FileRepo::TIME_ONLY : return a (file name => timestamp) map instead
+        * @return array Map of (file name => File objects) for matches
         */
-       public function findFiles( array $items ) {
+       public function findFiles( array $items, $flags = 0 ) {
                $result = array();
                foreach ( $items as $item ) {
                        if ( is_array( $item ) ) {
@@ -489,7 +493,8 @@ class FileRepo {
                        }
                        $file = $this->findFile( $title, $options );
                        if ( $file ) {
-                               $result[$file->getTitle()->getDBkey()] = $file;
+                               $result[$file->getTitle()->getDBkey()] =
+                                       ( $flags & self::TIME_ONLY ) ? $file->getTimestamp() : $file;
                        }
                }
 
index aa851ff..e38f71d 100644 (file)
@@ -240,7 +240,7 @@ class LocalRepo extends FileRepo {
                return $id;
        }
 
-       public function findFiles( array $items ) {
+       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)
@@ -268,14 +268,15 @@ class LocalRepo extends FileRepo {
 
                $repo = $this;
                $applyMatchingFiles = function( ResultWrapper $res, &$searchSet, &$finalFiles )
-                       use ( $repo, $fileMatchesSearch )
+                       use ( $repo, $fileMatchesSearch, $flags )
                {
                        foreach ( $res as $row ) {
-                               $possFile = $repo->newFileFromRow( $row );
-                               $dbKey = $possFile->getName();
+                               $file = $repo->newFileFromRow( $row );
+                               $dbKey = $file->getName();
                                // There must have been a search for this DB Key
-                               if ( $fileMatchesSearch( $possFile, $searchSet[$dbKey] ) ) {
-                                       $finalFiles[$dbKey] = $possFile;
+                               if ( $fileMatchesSearch( $file, $searchSet[$dbKey] ) ) {
+                                       $finalFiles[$dbKey] =
+                                               ( $flags & FileRepo::TIME_ONLY ) ? $file->getTimestamp() : $file;
                                        unset( $searchSet[$dbKey] );
                                }
                        }
@@ -313,10 +314,11 @@ class LocalRepo extends FileRepo {
                        $title = File::normalizeTitle( $dbKey );
                        $redir = $this->checkRedirect( $title ); // hopefully hits memcached
                        if ( $redir && $redir->getNamespace() == NS_FILE ) {
-                               $possFile = $this->newFile( $redir );
-                               if ( $possFile && $fileMatchesSearch( $possFile, $search ) ) {
-                                       $possFile->redirectedFrom( $title->getDBkey() );
-                                       $finalFiles[$dbKey] = $possFile;
+                               $file = $this->newFile( $redir );
+                               if ( $file && $fileMatchesSearch( $file, $search ) ) {
+                                       $file->redirectedFrom( $title->getDBkey() );
+                                       $finalFiles[$dbKey] =
+                                               ( $flags & FileRepo::TIME_ONLY ) ? $file->getTimestamp() : $file;
                                }
                        }
                }