* @ingroup FileRepo
*/
class RepoGroup {
- var $localRepo, $foreignRepos, $reposInitialised = false;
+
+ /**
+ * @var LocalRepo
+ */
+ var $localRepo;
+
+ var $foreignRepos, $reposInitialised = false;
var $localInfo, $foreignInfo;
var $cache;
+ /**
+ * @var RepoGroup
+ */
protected static $instance;
const MAX_CACHE_SIZE = 1000;
/**
* Get a RepoGroup instance. At present only one instance of RepoGroup is
* needed in a MediaWiki invocation, this may change in the future.
+ * @return RepoGroup
*/
static function singleton() {
if ( self::$instance ) {
/**
* Set the singleton instance to a given object
+ * Used by extensions which hook into the Repo chain.
+ * It's not enough to just create a superclass ... you have
+ * to get people to call into it even though all they know is RepoGroup::singleton()
+ *
+ * @param $instance RepoGroup
*/
static function setSingleton( $instance ) {
self::$instance = $instance;
* Search repositories for an image.
* You can also use wfFindFile() to do this.
*
- * @param $title Mixed: Title object or string
- * @param $options Associative array of options:
+ * @param $title Title|string Title object or string
+ * @param $options array Associative array of options:
* time: requested time for an archived image, or false for the
* current version. An image object will be returned which was
* created at the specified time.
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
- if ( !($title instanceof Title) ) {
- $title = Title::makeTitleSafe( NS_FILE, $title );
- if ( !is_object( $title ) ) {
- return false;
- }
+ $title = File::normalizeTitle( $title );
+ if ( !$title ) {
+ return false;
}
# Check the cache
if ( !is_array( $item ) ) {
$item = array( 'title' => $item );
}
- if ( !( $item['title'] instanceof Title ) )
- $item['title'] = Title::makeTitleSafe( NS_FILE, $item['title'] );
- if ( $item['title'] )
+ $item['title'] = File::normalizeTitle( $item['title'] );
+ if ( $item['title'] ) {
$items[$item['title']->getDBkey()] = $item;
+ }
}
$images = $this->localRepo->findFiles( $items );
/**
* Interface for FileRepo::checkRedirect()
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
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
+ *
+ * @param $hash String base 36 SHA-1 hash
+ * @return Array of File objects
+ */
function findBySha1( $hash ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
$result = $this->localRepo->findBySha1( $hash );
- foreach ( $this->foreignRepos as $repo )
+ foreach ( $this->foreignRepos as $repo ) {
$result = array_merge( $result, $repo->findBySha1( $hash ) );
+ }
return $result;
}
/**
* Get the local repository, i.e. the one corresponding to the local image
* table. Files are typically uploaded to the local repository.
+ *
+ * @return LocalRepo
*/
function getLocalRepo() {
return $this->getRepo( 'local' );
*/
function splitVirtualUrl( $url ) {
if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
- throw new MWException( __METHOD__.': unknown protoocl' );
+ throw new MWException( __METHOD__.': unknown protocol' );
}
$bits = explode( '/', substr( $url, 9 ), 3 );
/**
* Limit cache memory
*/
- function trimCache() {
+ protected function trimCache() {
while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
reset( $this->cache );
$key = key( $this->cache );
unset( $this->cache[$key] );
}
}
+
+ /**
+ * Clear RepoGroup process cache used for finding a file
+ * @param $title Title|null Title of the file or null to clear all files
+ */
+ public function clearCache( Title $title = null ) {
+ if ( $title == null ) {
+ $this->cache = array();
+ } else {
+ $dbKey = $title->getDBkey();
+ if ( isset( $this->cache[$dbKey] ) ) {
+ unset( $this->cache[$dbKey] );
+ }
+ }
+ }
}