From fe3ab4ade2e4fe4ae5551fc4d94c7e80ea63f2c0 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 20 May 2008 02:58:40 +0000 Subject: [PATCH] Add functions to get an image via sha-1 --- includes/filerepo/FileRepo.php | 57 +++++++++++++++++++++++++++++- includes/filerepo/LocalFile.php | 20 +++++++++++ includes/filerepo/OldLocalFile.php | 16 +++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index af2fc941d3..61f42f4a10 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -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 diff --git a/includes/filerepo/LocalFile.php b/includes/filerepo/LocalFile.php index 6258ef0ef4..828a320525 100644 --- a/includes/filerepo/LocalFile.php +++ b/includes/filerepo/LocalFile.php @@ -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 */ diff --git a/includes/filerepo/OldLocalFile.php b/includes/filerepo/OldLocalFile.php index 8090645848..121927cb04 100644 --- a/includes/filerepo/OldLocalFile.php +++ b/includes/filerepo/OldLocalFile.php @@ -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 -- 2.20.1