From e1f1a19b7e8bbbb3a99bbf4ffed5a66804f9d7a6 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Thu, 31 May 2007 01:43:41 +0000 Subject: [PATCH] Fixed a few filerepo bugs, added some documentation --- RELEASE-NOTES | 1 + includes/DefaultSettings.php | 7 +++++ includes/filerepo/FSRepo.php | 48 +++++++++++++++++++++++++++++++-- includes/filerepo/File.php | 14 ++++++---- includes/filerepo/LocalFile.php | 23 +++++++++++++++- includes/filerepo/LocalRepo.php | 1 + includes/filerepo/RepoGroup.php | 19 +++++++++++-- 7 files changed, 103 insertions(+), 10 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 49e4204f7e..25e03854af 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -47,6 +47,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN file repositories. * Added a Content-Disposition header to thumb.php output * Improved thumb.php error handling +* Display file history on local image description pages of shared images == Bugfixes since 1.10 == diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index f0bbe3e6bc..32488d16cd 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1405,6 +1405,13 @@ $wgEnableUploads = false; /** * Show EXIF data, on by default if available. * Requires PHP's EXIF extension: http://www.php.net/manual/en/ref.exif.php + * + * NOTE FOR WINDOWS USERS: + * To enable EXIF functions, add the folloing lines to the + * "Windows extensions" section of php.ini: + * + * extension=extensions/php_mbstring.dll + * extension=extensions/php_exif.dll */ $wgShowEXIF = function_exists( 'exif_read_data' ); diff --git a/includes/filerepo/FSRepo.php b/includes/filerepo/FSRepo.php index f1eaf29e5f..af5aa63558 100644 --- a/includes/filerepo/FSRepo.php +++ b/includes/filerepo/FSRepo.php @@ -3,6 +3,8 @@ /** * A repository for files accessible via the local filesystem. Does not support * database access or registration. + * + * TODO: split off abstract base FileRepo */ class FSRepo { @@ -11,6 +13,7 @@ class FSRepo { var $directory, $url, $hashLevels, $thumbScriptUrl, $transformVia404; var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription; var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' ); + var $oldFileFactory = false; function __construct( $info ) { // Required settings @@ -47,7 +50,11 @@ class FSRepo { } } if ( $time ) { - return call_user_func( $this->oldFileFactor, $title, $this, $time ); + if ( $this->oldFileFactory ) { + return call_user_func( $this->oldFileFactory, $title, $this, $time ); + } else { + return false; + } } else { return call_user_func( $this->fileFactory, $title, $this ); } @@ -61,39 +68,59 @@ class FSRepo { * @param mixed $time 14-character timestamp, or false for the current version */ function findFile( $title, $time = false ) { + # First try the current version of the file to see if it precedes the timestamp $img = $this->newFile( $title ); if ( !$img ) { return false; } - if ( $img->exists() && $img->getTimestamp() <= $time ) { + if ( $img->exists() && ( !$time || $img->getTimestamp() <= $time ) ) { return $img; } + # Now try an old version of the file $img = $this->newFile( $title, $time ); if ( $img->exists() ) { return $img; } } + /** + * Get the public root directory of the repository. + */ function getRootDirectory() { return $this->directory; } + /** + * Get the public root URL of the repository + */ function getRootUrl() { return $this->url; } + /** + * Returns true if the repository uses a multi-level directory structure + */ function isHashed() { return (bool)$this->hashLevels; } + /** + * Get the URL of thumb.php + */ function getThumbScriptUrl() { return $this->thumbScriptUrl; } + /** + * Returns true if the repository can transform files via a 404 handler + */ function canTransformVia404() { return $this->transformVia404; } + /** + * Get the local directory corresponding to one of the three basic zones + */ function getZonePath( $zone ) { switch ( $zone ) { case 'public': @@ -107,6 +134,9 @@ class FSRepo { } } + /** + * Get the URL corresponding to one of the three basic zones + */ function getZoneUrl( $zone ) { switch ( $zone ) { case 'public': @@ -205,6 +235,17 @@ class FSRepo { } } + /** + * Copy or move a file either from the local filesystem or from an mwrepo:// + * virtual URL, into this repository at the specified destination location. + * + * @param string $srcPath The source path or URL + * @param string $dstPath The destination relative path + * @param string $archivePath The relative path where the existing file is to + * be archived, if there is one. + * @param integer $flags Bitfield, may be FSRepo::DELETE_SOURCE to indicate + * that the source file should be deleted if possible + */ function publish( $srcPath, $dstPath, $archivePath, $flags = 0 ) { if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) { $srcPath = $this->resolveVirtualUrl( $srcPath ); @@ -272,6 +313,9 @@ class FSRepo { } } + /** + * Get the name of this repository, as specified by $info['name]' to the constructor + */ function getName() { return $this->name; } diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index 1e59d2f565..84de8e9935 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -10,13 +10,14 @@ * Stub functions which should be overridden are marked with STUB. Some more * concrete functions are also typically overridden by child classes. * + * Note that only the repo object knows what its file class is called. You should + * never name a file class explictly outside of the repo class. Instead use the + * repo's factory functions to generate file objects, for example: * - * NOTE FOR WINDOWS USERS: - * To enable EXIF functions, add the folloing lines to the - * "Windows extensions" section of php.ini: + * RepoGroup::singleton()->getLocalRepo()->newFile($title); * - * extension=extensions/php_mbstring.dll - * extension=extensions/php_exif.dll + * The convenience functions wfLocalFile() and wfFindFile() should be sufficient + * in most cases. * * @addtogroup FileRepo */ @@ -49,6 +50,9 @@ class File { */ var $repo, $title, $lastError; + /** + * Call this constructor from child classes + */ function __construct( $title, $repo ) { $this->title = $title; $this->repo = $repo; diff --git a/includes/filerepo/LocalFile.php b/includes/filerepo/LocalFile.php index 3a70fd7603..501fa42560 100644 --- a/includes/filerepo/LocalFile.php +++ b/includes/filerepo/LocalFile.php @@ -13,6 +13,15 @@ define( 'MW_FILE_VERSION', 4 ); * Provides methods to retrieve paths (physical, logical, URL), * to generate image thumbnails or for uploading. * + * Note that only the repo object knows what its file class is called. You should + * never name a file class explictly outside of the repo class. Instead use the + * repo's factory functions to generate file objects, for example: + * + * RepoGroup::singleton()->getLocalRepo()->newFile($title); + * + * The convenience functions wfLocalFile() and wfFindFile() should be sufficient + * in most cases. + * * @addtogroup FileRepo */ class LocalFile extends File @@ -39,10 +48,18 @@ class LocalFile extends File /**#@-*/ + /** + * Create a LocalFile from a title + * Do not call this except from inside a repo class. + */ function newFromTitle( $title, $repo ) { return new self( $title, $repo ); } + /** + * Create a LocalFile from a title + * Do not call this except from inside a repo class. + */ function newFromRow( $row, $repo ) { $title = Title::makeTitle( NS_IMAGE, $row->img_name ); $file = new self( $title, $repo ); @@ -50,6 +67,10 @@ class LocalFile extends File return $file; } + /** + * Constructor. + * Do not call this except from inside a repo class. + */ function __construct( $title, $repo ) { if( !is_object( $title ) ) { throw new MWException( __CLASS__.' constructor given bogus title.' ); @@ -1134,7 +1155,7 @@ class LocalFile extends File continue; } - $restoredImage = new self( $row->fa_name, $this->repo ); + $restoredImage = new self( Title::makeTitle( NS_IMAGE, $row->fa_name ), $this->repo ); if( $revisions == 1 && !$exists ) { $destPath = $restoredImage->getFullPath(); diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index 4903015e6c..be32c02ac3 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -5,6 +5,7 @@ */ class LocalRepo extends FSRepo { var $fileFactory = array( 'LocalFile', 'newFromTitle' ); + var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' ); function getSlaveDB() { return wfGetDB( DB_SLAVE ); diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index 3055382624..021d3ba737 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -1,11 +1,19 @@ foreignRepos as $repo ) { - $image = $repo->findFile( $image, $time ); + $image = $repo->findFile( $title, $time ); if ( $image ) { return $image; } @@ -69,6 +77,10 @@ class RepoGroup { } } + /** + * Get the local repository, i.e. the one corresponding to the local image + * table. Files are typically uploaded to the local repository. + */ function getLocalRepo() { return $this->getRepo( 'local' ); } @@ -89,7 +101,10 @@ class RepoGroup { } } - function newRepo( $info ) { + /** + * Create a repo class based on an info structure + */ + protected function newRepo( $info ) { $class = $info['class']; return new $class( $info ); } -- 2.20.1