Reverted r84918 per CR
authorAaron Schulz <aaron@users.mediawiki.org>
Mon, 28 Mar 2011 21:40:50 +0000 (21:40 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Mon, 28 Mar 2011 21:40:50 +0000 (21:40 +0000)
includes/filerepo/FileRepo.php
includes/filerepo/RepoGroup.php

index 8d862b0..6e4d12f 100644 (file)
@@ -30,6 +30,7 @@ abstract class FileRepo {
         * Override these in the base class
         */
        var $fileFactory = false, $oldFileFactory = false;
+       var $fileFactoryKey = false, $oldFileFactoryKey = false;
 
        function __construct( $info ) {
                // Required settings
@@ -177,6 +178,63 @@ abstract class FileRepo {
                return $result;
        }
 
+       /**
+        * Create a new File object from the local repository
+        * @param $sha1 Mixed: base 36 SHA-1 hash
+        * @param $time Mixed: time at which the image was uploaded.
+        *              If this is specified, the returned object will be an
+        *              of the repository's old file class instead of a current
+        *              file. Repositories not supporting version control should
+        *              return false if this parameter is set.
+        *
+        * @return File
+        */
+       function newFileFromKey( $sha1, $time = false ) {
+               if ( $time ) {
+                       if ( $this->oldFileFactoryKey ) {
+                               return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
+                       }
+               } else {
+                       if ( $this->fileFactoryKey ) {
+                               return call_user_func( $this->fileFactoryKey, $sha1, $this );
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * Find an instance of the file with this key, created at the specified time
+        * Returns false if the file does not exist. Repositories not supporting
+        * version control should return false if the time is specified.
+        *
+        * @param $sha1 String base 36 SHA-1 hash
+        * @param $options Option array, same as findFile().
+        */
+       function findFileFromKey( $sha1, $options = array() ) {
+               $time = isset( $options['time'] ) ? $options['time'] : false;
+
+               # First try the current version of the file to see if it precedes the timestamp
+               $img = $this->newFileFromKey( $sha1 );
+               if ( !$img ) {
+                       return false;
+               }
+               if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
+                       return $img;
+               }
+               # Now try an old version of the file
+               if ( $time !== false ) {
+                       $img = $this->newFileFromKey( $sha1, $time );
+                       if ( $img && $img->exists() ) {
+                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
+                                       return $img;
+                               } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
+                                       return $img;
+                               }
+                       }
+               }
+               return false;
+       }
+
        /**
         * Get the URL of thumb.php
         */
index 49e5023..2d0b1ac 100644 (file)
@@ -211,6 +211,29 @@ class RepoGroup {
                return false;
        }
 
+       /**
+        * Find an instance of the file with this key, created at the specified time
+        * Returns false if the file does not exist.
+        *
+        * @param $hash String base 36 SHA-1 hash
+        * @param $options Option array, same as findFile()
+        * @return File object or false if it is not found
+        */
+       function findFileFromKey( $hash, $options = array() ) {
+               if ( !$this->reposInitialised ) {
+                       $this->initialiseRepos();
+               }
+
+               $file = $this->localRepo->findFileFromKey( $hash, $options );
+               if ( !$file ) {
+                       foreach ( $this->foreignRepos as $repo ) {
+                               $file = $repo->findFileFromKey( $hash, $options );
+                               if ( $file ) break;
+                       }
+               }
+               return $file;
+       }
+
        /**
         * Find all instances of files with this key
         *