3 * Prioritized list of file repositories
10 * Prioritized list of file repositories
20 var $foreignRepos, $reposInitialised = false;
21 var $localInfo, $foreignInfo;
27 protected static $instance;
28 const MAX_CACHE_SIZE
= 500;
31 * Get a RepoGroup instance. At present only one instance of RepoGroup is
32 * needed in a MediaWiki invocation, this may change in the future.
35 static function singleton() {
36 if ( self
::$instance ) {
37 return self
::$instance;
39 global $wgLocalFileRepo, $wgForeignFileRepos;
40 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
41 return self
::$instance;
45 * Destroy the singleton instance, so that a new one will be created next
46 * time singleton() is called.
48 static function destroySingleton() {
49 self
::$instance = null;
53 * Set the singleton instance to a given object
54 * Used by extensions which hook into the Repo chain.
55 * It's not enough to just create a superclass ... you have
56 * to get people to call into it even though all they know is RepoGroup::singleton()
58 * @param $instance RepoGroup
60 static function setSingleton( $instance ) {
61 self
::$instance = $instance;
65 * Construct a group of file repositories.
67 * @param $localInfo array Associative array for local repo's info
68 * @param $foreignInfo Array of repository info arrays.
69 * Each info array is an associative array with the 'class' member
70 * giving the class name. The entire array is passed to the repository
71 * constructor as the first parameter.
73 function __construct( $localInfo, $foreignInfo ) {
74 $this->localInfo
= $localInfo;
75 $this->foreignInfo
= $foreignInfo;
76 $this->cache
= array();
80 * Search repositories for an image.
81 * You can also use wfFindFile() to do this.
83 * @param $title Title|string Title object or string
84 * @param $options array Associative array of options:
85 * time: requested time for an archived image, or false for the
86 * current version. An image object will be returned which was
87 * created at the specified time.
89 * ignoreRedirect: If true, do not follow file redirects
91 * private: If true, return restricted (deleted) files if the current
92 * user is allowed to view them. Otherwise, such files will not
95 * bypassCache: If true, do not use the process-local cache of File objects
96 * @return File object or false if it is not found
98 function findFile( $title, $options = array() ) {
99 if ( !is_array( $options ) ) {
101 $options = array( 'time' => $options );
103 if ( !$this->reposInitialised
) {
104 $this->initialiseRepos();
106 $title = File
::normalizeTitle( $title );
112 if ( empty( $options['ignoreRedirect'] )
113 && empty( $options['private'] )
114 && empty( $options['bypassCache'] ) )
116 $time = isset( $options['time'] ) ?
$options['time'] : '';
117 $dbkey = $title->getDBkey();
118 if ( isset( $this->cache
[$dbkey][$time] ) ) {
119 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
120 # Move it to the end of the list so that we can delete the LRU entry later
121 $this->pingCache( $dbkey );
123 return $this->cache
[$dbkey][$time];
130 # Check the local repo
131 $image = $this->localRepo
->findFile( $title, $options );
133 # Check the foreign repos
135 foreach ( $this->foreignRepos
as $repo ) {
136 $image = $repo->findFile( $title, $options );
143 $image = $image ?
$image : false; // type sanity
144 # Cache file existence or non-existence
145 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
147 $this->cache
[$dbkey][$time] = $image;
153 function findFiles( $inputItems ) {
154 if ( !$this->reposInitialised
) {
155 $this->initialiseRepos();
159 foreach ( $inputItems as $item ) {
160 if ( !is_array( $item ) ) {
161 $item = array( 'title' => $item );
163 $item['title'] = File
::normalizeTitle( $item['title'] );
164 if ( $item['title'] ) {
165 $items[$item['title']->getDBkey()] = $item;
169 $images = $this->localRepo
->findFiles( $items );
171 foreach ( $this->foreignRepos
as $repo ) {
172 // Remove found files from $items
173 foreach ( $images as $name => $image ) {
174 unset( $items[$name] );
177 $images = array_merge( $images, $repo->findFiles( $items ) );
183 * Interface for FileRepo::checkRedirect()
186 function checkRedirect( Title
$title ) {
187 if ( !$this->reposInitialised
) {
188 $this->initialiseRepos();
191 $redir = $this->localRepo
->checkRedirect( $title );
195 foreach ( $this->foreignRepos
as $repo ) {
196 $redir = $repo->checkRedirect( $title );
205 * Find an instance of the file with this key, created at the specified time
206 * Returns false if the file does not exist.
208 * @param $hash String base 36 SHA-1 hash
209 * @param $options array Option array, same as findFile()
210 * @return File object or false if it is not found
212 function findFileFromKey( $hash, $options = array() ) {
213 if ( !$this->reposInitialised
) {
214 $this->initialiseRepos();
217 $file = $this->localRepo
->findFileFromKey( $hash, $options );
219 foreach ( $this->foreignRepos
as $repo ) {
220 $file = $repo->findFileFromKey( $hash, $options );
228 * Find all instances of files with this key
230 * @param $hash String base 36 SHA-1 hash
231 * @return Array of File objects
233 function findBySha1( $hash ) {
234 if ( !$this->reposInitialised
) {
235 $this->initialiseRepos();
238 $result = $this->localRepo
->findBySha1( $hash );
239 foreach ( $this->foreignRepos
as $repo ) {
240 $result = array_merge( $result, $repo->findBySha1( $hash ) );
246 * Get the repo instance with a given key.
247 * @return bool|LocalRepo
249 function getRepo( $index ) {
250 if ( !$this->reposInitialised
) {
251 $this->initialiseRepos();
253 if ( $index === 'local' ) {
254 return $this->localRepo
;
255 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
256 return $this->foreignRepos
[$index];
262 * Get the repo instance by its name
265 function getRepoByName( $name ) {
266 if ( !$this->reposInitialised
) {
267 $this->initialiseRepos();
269 foreach ( $this->foreignRepos
as $repo ) {
270 if ( $repo->name
== $name)
277 * Get the local repository, i.e. the one corresponding to the local image
278 * table. Files are typically uploaded to the local repository.
282 function getLocalRepo() {
283 return $this->getRepo( 'local' );
287 * Call a function for each foreign repo, with the repo object as the
290 * @param $callback Callback: the function to call
291 * @param $params Array: optional additional parameters to pass to the function
294 function forEachForeignRepo( $callback, $params = array() ) {
295 foreach( $this->foreignRepos
as $repo ) {
296 $args = array_merge( array( $repo ), $params );
297 if( call_user_func_array( $callback, $args ) ) {
305 * Does the installation have any foreign repos set up?
308 function hasForeignRepos() {
309 return (bool)$this->foreignRepos
;
313 * Initialise the $repos array
315 function initialiseRepos() {
316 if ( $this->reposInitialised
) {
319 $this->reposInitialised
= true;
321 $this->localRepo
= $this->newRepo( $this->localInfo
);
322 $this->foreignRepos
= array();
323 foreach ( $this->foreignInfo
as $key => $info ) {
324 $this->foreignRepos
[$key] = $this->newRepo( $info );
329 * Create a repo class based on an info structure
331 protected function newRepo( $info ) {
332 $class = $info['class'];
333 return new $class( $info );
337 * Split a virtual URL into repo, zone and rel parts
339 * @return array containing repo, zone and rel
341 function splitVirtualUrl( $url ) {
342 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
343 throw new MWException( __METHOD__
.': unknown protocol' );
346 $bits = explode( '/', substr( $url, 9 ), 3 );
347 if ( count( $bits ) != 3 ) {
348 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
353 function getFileProps( $fileName ) {
354 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
355 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
356 if ( $repoName === '' ) {
359 $repo = $this->getRepo( $repoName );
360 return $repo->getFileProps( $fileName );
362 return FSFile
::getPropsFromPath( $fileName );
367 * Move a cache entry to the top (such as when accessed)
369 protected function pingCache( $key ) {
370 if ( isset( $this->cache
[$key] ) ) {
371 $tmp = $this->cache
[$key];
372 unset( $this->cache
[$key] );
373 $this->cache
[$key] = $tmp;
380 protected function trimCache() {
381 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
382 reset( $this->cache
);
383 $key = key( $this->cache
);
384 wfDebug( __METHOD__
.": evicting $key\n" );
385 unset( $this->cache
[$key] );
390 * Clear RepoGroup process cache used for finding a file
391 * @param $title Title|null Title of the file or null to clear all files
393 public function clearCache( Title
$title = null ) {
394 if ( $title == null ) {
395 $this->cache
= array();
397 $dbKey = $title->getDBkey();
398 if ( isset( $this->cache
[$dbKey] ) ) {
399 unset( $this->cache
[$dbKey] );