3 * Prioritized list of file repositories
10 * @defgroup FileRepo FileRepo
14 * Prioritized list of file repositories
25 var $foreignRepos, $reposInitialised = false;
26 var $localInfo, $foreignInfo;
32 protected static $instance;
33 const MAX_CACHE_SIZE
= 1000;
36 * Get a RepoGroup instance. At present only one instance of RepoGroup is
37 * needed in a MediaWiki invocation, this may change in the future.
40 static function singleton() {
41 if ( self
::$instance ) {
42 return self
::$instance;
44 global $wgLocalFileRepo, $wgForeignFileRepos;
45 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
46 return self
::$instance;
50 * Destroy the singleton instance, so that a new one will be created next
51 * time singleton() is called.
53 static function destroySingleton() {
54 self
::$instance = null;
58 * Set the singleton instance to a given object
59 * Used by extensions which hook into the Repo chain.
60 * It's not enough to just create a superclass ... you have
61 * to get people to call into it even though all they know is RepoGroup::singleton()
63 * @param $instance RepoGroup
65 static function setSingleton( $instance ) {
66 self
::$instance = $instance;
70 * Construct a group of file repositories.
72 * @param $localInfo Associative array for local repo's info
73 * @param $foreignInfo Array of repository info arrays.
74 * Each info array is an associative array with the 'class' member
75 * giving the class name. The entire array is passed to the repository
76 * constructor as the first parameter.
78 function __construct( $localInfo, $foreignInfo ) {
79 $this->localInfo
= $localInfo;
80 $this->foreignInfo
= $foreignInfo;
81 $this->cache
= array();
85 * Search repositories for an image.
86 * You can also use wfFindFile() to do this.
88 * @param $title Title|string Title object or string
89 * @param $options array Associative array of options:
90 * time: requested time for an archived image, or false for the
91 * current version. An image object will be returned which was
92 * created at the specified time.
94 * ignoreRedirect: If true, do not follow file redirects
96 * private: If true, return restricted (deleted) files if the current
97 * user is allowed to view them. Otherwise, such files will not
100 * bypassCache: If true, do not use the process-local cache of File objects
101 * @return File object or false if it is not found
103 function findFile( $title, $options = array() ) {
104 if ( !is_array( $options ) ) {
106 $options = array( 'time' => $options );
108 if ( !$this->reposInitialised
) {
109 $this->initialiseRepos();
111 $title = File
::normalizeTitle( $title );
117 if ( empty( $options['ignoreRedirect'] )
118 && empty( $options['private'] )
119 && empty( $options['bypassCache'] ) )
122 $time = isset( $options['time'] ) ?
$options['time'] : '';
123 $dbkey = $title->getDBkey();
124 if ( isset( $this->cache
[$dbkey][$time] ) ) {
125 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
126 # Move it to the end of the list so that we can delete the LRU entry later
127 $tmp = $this->cache
[$dbkey];
128 unset( $this->cache
[$dbkey] );
129 $this->cache
[$dbkey] = $tmp;
131 return $this->cache
[$dbkey][$time];
133 # Add a negative cache entry, may be overridden
135 $this->cache
[$dbkey][$time] = false;
136 $cacheEntry =& $this->cache
[$dbkey][$time];
142 # Check the local repo
143 $image = $this->localRepo
->findFile( $title, $options );
146 $cacheEntry = $image;
151 # Check the foreign repos
152 foreach ( $this->foreignRepos
as $repo ) {
153 $image = $repo->findFile( $title, $options );
156 $cacheEntry = $image;
161 # Not found, do not override negative cache
165 function findFiles( $inputItems ) {
166 if ( !$this->reposInitialised
) {
167 $this->initialiseRepos();
171 foreach ( $inputItems as $item ) {
172 if ( !is_array( $item ) ) {
173 $item = array( 'title' => $item );
175 $item['title'] = File
::normalizeTitle( $item['title'] );
176 if ( $item['title'] ) {
177 $items[$item['title']->getDBkey()] = $item;
181 $images = $this->localRepo
->findFiles( $items );
183 foreach ( $this->foreignRepos
as $repo ) {
184 // Remove found files from $items
185 foreach ( $images as $name => $image ) {
186 unset( $items[$name] );
189 $images = array_merge( $images, $repo->findFiles( $items ) );
195 * Interface for FileRepo::checkRedirect()
197 function checkRedirect( Title
$title ) {
198 if ( !$this->reposInitialised
) {
199 $this->initialiseRepos();
202 $redir = $this->localRepo
->checkRedirect( $title );
206 foreach ( $this->foreignRepos
as $repo ) {
207 $redir = $repo->checkRedirect( $title );
216 * Find an instance of the file with this key, created at the specified time
217 * Returns false if the file does not exist.
219 * @param $hash String base 36 SHA-1 hash
220 * @param $options Option array, same as findFile()
221 * @return File object or false if it is not found
223 function findFileFromKey( $hash, $options = array() ) {
224 if ( !$this->reposInitialised
) {
225 $this->initialiseRepos();
228 $file = $this->localRepo
->findFileFromKey( $hash, $options );
230 foreach ( $this->foreignRepos
as $repo ) {
231 $file = $repo->findFileFromKey( $hash, $options );
239 * Find all instances of files with this key
241 * @param $hash String base 36 SHA-1 hash
242 * @return Array of File objects
244 function findBySha1( $hash ) {
245 if ( !$this->reposInitialised
) {
246 $this->initialiseRepos();
249 $result = $this->localRepo
->findBySha1( $hash );
250 foreach ( $this->foreignRepos
as $repo ) {
251 $result = array_merge( $result, $repo->findBySha1( $hash ) );
257 * Get the repo instance with a given key.
259 function getRepo( $index ) {
260 if ( !$this->reposInitialised
) {
261 $this->initialiseRepos();
263 if ( $index === 'local' ) {
264 return $this->localRepo
;
265 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
266 return $this->foreignRepos
[$index];
272 * Get the repo instance by its name
274 function getRepoByName( $name ) {
275 if ( !$this->reposInitialised
) {
276 $this->initialiseRepos();
278 foreach ( $this->foreignRepos
as $repo ) {
279 if ( $repo->name
== $name)
286 * Get the local repository, i.e. the one corresponding to the local image
287 * table. Files are typically uploaded to the local repository.
291 function getLocalRepo() {
292 return $this->getRepo( 'local' );
296 * Call a function for each foreign repo, with the repo object as the
299 * @param $callback Callback: the function to call
300 * @param $params Array: optional additional parameters to pass to the function
302 function forEachForeignRepo( $callback, $params = array() ) {
303 foreach( $this->foreignRepos
as $repo ) {
304 $args = array_merge( array( $repo ), $params );
305 if( call_user_func_array( $callback, $args ) ) {
313 * Does the installation have any foreign repos set up?
316 function hasForeignRepos() {
317 return (bool)$this->foreignRepos
;
321 * Initialise the $repos array
323 function initialiseRepos() {
324 if ( $this->reposInitialised
) {
327 $this->reposInitialised
= true;
329 $this->localRepo
= $this->newRepo( $this->localInfo
);
330 $this->foreignRepos
= array();
331 foreach ( $this->foreignInfo
as $key => $info ) {
332 $this->foreignRepos
[$key] = $this->newRepo( $info );
337 * Create a repo class based on an info structure
339 protected function newRepo( $info ) {
340 $class = $info['class'];
341 return new $class( $info );
345 * Split a virtual URL into repo, zone and rel parts
346 * @return an array containing repo, zone and rel
348 function splitVirtualUrl( $url ) {
349 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
350 throw new MWException( __METHOD__
.': unknown protocol' );
353 $bits = explode( '/', substr( $url, 9 ), 3 );
354 if ( count( $bits ) != 3 ) {
355 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
360 function getFileProps( $fileName ) {
361 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
362 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
363 if ( $repoName === '' ) {
366 $repo = $this->getRepo( $repoName );
367 return $repo->getFileProps( $fileName );
369 return File
::getPropsFromPath( $fileName );
376 protected function trimCache() {
377 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
378 reset( $this->cache
);
379 $key = key( $this->cache
);
380 wfDebug( __METHOD__
.": evicting $key\n" );
381 unset( $this->cache
[$key] );
386 * Clear RepoGroup process cache used for finding a file
387 * @param $title Title|null Title of the file or null to clear all files
389 public function clearCache( Title
$title = null ) {
390 if ( $title == null ) {
391 $this->cache
= array();
393 $dbKey = $title->getDBkey();
394 if ( isset( $this->cache
[$dbKey] ) ) {
395 unset( $this->cache
[$dbKey] );