Add functions to get an image via sha-1
authorAaron Schulz <aaron@users.mediawiki.org>
Tue, 20 May 2008 02:58:40 +0000 (02:58 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Tue, 20 May 2008 02:58:40 +0000 (02:58 +0000)
includes/filerepo/FileRepo.php
includes/filerepo/LocalFile.php
includes/filerepo/OldLocalFile.php

index af2fc94..61f42f4 100644 (file)
@@ -20,6 +20,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
@@ -107,7 +108,9 @@ abstract class FileRepo {
                }
                                
                # Now try redirects
-               if ( $flags & FileRepo::FIND_IGNORE_REDIRECT ) return false;
+               if ( $flags & FileRepo::FIND_IGNORE_REDIRECT ) {
+                       return false;
+               }
                $redir = $this->checkRedirect( $title );                
                if( $redir && $redir->getNamespace() == NS_IMAGE) {
                        $img = $this->newFile( $redir );
@@ -121,6 +124,58 @@ abstract class FileRepo {
                }
                return false;
        }
+       
+       /**
+        * Create a new File object from the local repository
+        * @param mixed $sha1 SHA-1 key
+        * @param mixed $time Time at which the image was uploaded.
+        *                    If this is specified, the returned object will be an
+        *                    instance 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.
+        */
+       function newFileFromKey( $sha1, $time = false ) {
+               if ( $time ) {
+                       if ( $this->oldFileFactoryKey ) {
+                               return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
+                       } else {
+                               return false;
+                       }
+               } else {
+                       return call_user_func( $this->fileFactoryKey, $sha1, $this );
+               }
+       }
+       
+       /**
+        * 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 string $sha1 string
+        * @param mixed $time 14-character timestamp, or false for the current version
+        */
+       function findFileFromKey( $sha1, $time = false, $flags = 0 ) {
+               # 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->exists() ) {
+                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
+                                       return $img;
+                               } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
+                                       return $img;
+                               }
+                       }
+               }
+               return false;
+       }
 
        /**
         * Get the URL of thumb.php
index 6258ef0..828a320 100644 (file)
@@ -74,6 +74,26 @@ class LocalFile extends File
                return $file;
        }
        
+       /**
+        * Create a LocalFile from a SHA-1 key
+        * Do not call this except from inside a repo class.
+        */
+       static function newFromKey( $sha1, $repo, $timestamp = false ) {
+               # Polymorphic function name to distinguish foreign and local fetches
+               $fname = get_class( $this ) . '::' . __FUNCTION__;
+
+               $conds = array( 'img_sha1' => $sha1 );
+               if( $timestamp ) {
+                       $conds['img_timestamp'] = $timestamp;
+               }
+               $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ), $conds, $fname );
+               if( $row ) {
+                       return self::newFromRow( $row, $repo );
+               } else {
+                       return false;
+               }
+       }
+       
        /**
         * Fields in the image table
         */
index 8090645..121927c 100644 (file)
@@ -28,6 +28,22 @@ class OldLocalFile extends LocalFile {
                $file->loadFromRow( $row, 'oi_' );
                return $file;
        }
+       
+       static function newFromKey( $sha1, $repo, $timestamp = false ) {
+               # Polymorphic function name to distinguish foreign and local fetches
+               $fname = get_class( $this ) . '::' . __FUNCTION__;
+
+               $conds = array( 'oi_sha1' => $sha1 );
+               if( $timestamp ) {
+                       $conds['oi_timestamp'] = $timestamp;
+               }
+               $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ), $conds, $fname );
+               if( $row ) {
+                       return self::newFromRow( $row, $repo );
+               } else {
+                       return false;
+               }
+       }
 
        /**
         * @param Title $title