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 if ( !($title instanceof Title
) ) {
112 $title = Title
::makeTitleSafe( NS_FILE
, $title );
113 if ( !is_object( $title ) ) {
118 if ( $title->getNamespace() != NS_MEDIA
&& $title->getNamespace() != NS_FILE
) {
119 throw new MWException( __METHOD__
. ' received an Title object with incorrect namespace' );
123 if ( empty( $options['ignoreRedirect'] )
124 && empty( $options['private'] )
125 && empty( $options['bypassCache'] )
126 && $title->getNamespace() == NS_FILE
)
129 $time = isset( $options['time'] ) ?
$options['time'] : '';
130 $dbkey = $title->getDBkey();
131 if ( isset( $this->cache
[$dbkey][$time] ) ) {
132 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
133 # Move it to the end of the list so that we can delete the LRU entry later
134 $tmp = $this->cache
[$dbkey];
135 unset( $this->cache
[$dbkey] );
136 $this->cache
[$dbkey] = $tmp;
138 return $this->cache
[$dbkey][$time];
140 # Add a negative cache entry, may be overridden
142 $this->cache
[$dbkey][$time] = false;
143 $cacheEntry =& $this->cache
[$dbkey][$time];
149 # Check the local repo
150 $image = $this->localRepo
->findFile( $title, $options );
153 $cacheEntry = $image;
158 # Check the foreign repos
159 foreach ( $this->foreignRepos
as $repo ) {
160 $image = $repo->findFile( $title, $options );
163 $cacheEntry = $image;
168 # Not found, do not override negative cache
172 function findFiles( $inputItems ) {
173 if ( !$this->reposInitialised
) {
174 $this->initialiseRepos();
178 foreach ( $inputItems as $item ) {
179 if ( !is_array( $item ) ) {
180 $item = array( 'title' => $item );
182 if ( !( $item['title'] instanceof Title
) )
183 $item['title'] = Title
::makeTitleSafe( NS_FILE
, $item['title'] );
184 if ( $item['title'] )
185 $items[$item['title']->getDBkey()] = $item;
188 $images = $this->localRepo
->findFiles( $items );
190 foreach ( $this->foreignRepos
as $repo ) {
191 // Remove found files from $items
192 foreach ( $images as $name => $image ) {
193 unset( $items[$name] );
196 $images = array_merge( $images, $repo->findFiles( $items ) );
202 * Interface for FileRepo::checkRedirect()
204 function checkRedirect( $title ) {
205 if ( !$this->reposInitialised
) {
206 $this->initialiseRepos();
209 $redir = $this->localRepo
->checkRedirect( $title );
213 foreach ( $this->foreignRepos
as $repo ) {
214 $redir = $repo->checkRedirect( $title );
223 * Find an instance of the file with this key, created at the specified time
224 * Returns false if the file does not exist.
226 * @param $hash String base 36 SHA-1 hash
227 * @param $options Option array, same as findFile()
228 * @return File object or false if it is not found
230 function findFileFromKey( $hash, $options = array() ) {
231 if ( !$this->reposInitialised
) {
232 $this->initialiseRepos();
235 $file = $this->localRepo
->findFileFromKey( $hash, $options );
237 foreach ( $this->foreignRepos
as $repo ) {
238 $file = $repo->findFileFromKey( $hash, $options );
246 * Find all instances of files with this key
248 * @param $hash String base 36 SHA-1 hash
249 * @return Array of File objects
251 function findBySha1( $hash ) {
252 if ( !$this->reposInitialised
) {
253 $this->initialiseRepos();
256 $result = $this->localRepo
->findBySha1( $hash );
257 foreach ( $this->foreignRepos
as $repo ) {
258 $result = array_merge( $result, $repo->findBySha1( $hash ) );
264 * Get the repo instance with a given key.
266 function getRepo( $index ) {
267 if ( !$this->reposInitialised
) {
268 $this->initialiseRepos();
270 if ( $index === 'local' ) {
271 return $this->localRepo
;
272 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
273 return $this->foreignRepos
[$index];
279 * Get the repo instance by its name
281 function getRepoByName( $name ) {
282 if ( !$this->reposInitialised
) {
283 $this->initialiseRepos();
285 foreach ( $this->foreignRepos
as $repo ) {
286 if ( $repo->name
== $name)
293 * Get the local repository, i.e. the one corresponding to the local image
294 * table. Files are typically uploaded to the local repository.
298 function getLocalRepo() {
299 return $this->getRepo( 'local' );
303 * Call a function for each foreign repo, with the repo object as the
306 * @param $callback Callback: the function to call
307 * @param $params Array: optional additional parameters to pass to the function
309 function forEachForeignRepo( $callback, $params = array() ) {
310 foreach( $this->foreignRepos
as $repo ) {
311 $args = array_merge( array( $repo ), $params );
312 if( call_user_func_array( $callback, $args ) ) {
320 * Does the installation have any foreign repos set up?
323 function hasForeignRepos() {
324 return (bool)$this->foreignRepos
;
328 * Initialise the $repos array
330 function initialiseRepos() {
331 if ( $this->reposInitialised
) {
334 $this->reposInitialised
= true;
336 $this->localRepo
= $this->newRepo( $this->localInfo
);
337 $this->foreignRepos
= array();
338 foreach ( $this->foreignInfo
as $key => $info ) {
339 $this->foreignRepos
[$key] = $this->newRepo( $info );
344 * Create a repo class based on an info structure
346 protected function newRepo( $info ) {
347 $class = $info['class'];
348 return new $class( $info );
352 * Split a virtual URL into repo, zone and rel parts
353 * @return an array containing repo, zone and rel
355 function splitVirtualUrl( $url ) {
356 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
357 throw new MWException( __METHOD__
.': unknown protocol' );
360 $bits = explode( '/', substr( $url, 9 ), 3 );
361 if ( count( $bits ) != 3 ) {
362 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
367 function getFileProps( $fileName ) {
368 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
369 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
370 if ( $repoName === '' ) {
373 $repo = $this->getRepo( $repoName );
374 return $repo->getFileProps( $fileName );
376 return File
::getPropsFromPath( $fileName );
383 protected function trimCache() {
384 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
385 reset( $this->cache
);
386 $key = key( $this->cache
);
387 wfDebug( __METHOD__
.": evicting $key\n" );
388 unset( $this->cache
[$key] );
393 * Clear RepoGroup process cache used for finding a file
394 * @param $title Title|null Title of the file or null to clear all files
396 public function clearCache( Title
$title = null ) {
397 if ( $title == null ) {
398 $this->cache
= array();
400 $dbKey = $title->getDBkey();
401 if ( isset( $this->cache
[$dbKey] ) ) {
402 unset( $this->cache
[$dbKey] );