277364e8c2347f1a508f102e90cecc25173c1395
3 * Prioritized list of file repositories
10 * Prioritized list of file repositories
21 var $foreignRepos, $reposInitialised = false;
22 var $localInfo, $foreignInfo;
28 protected static $instance;
29 const MAX_CACHE_SIZE
= 1000;
32 * Get a RepoGroup instance. At present only one instance of RepoGroup is
33 * needed in a MediaWiki invocation, this may change in the future.
36 static function singleton() {
37 if ( self
::$instance ) {
38 return self
::$instance;
40 global $wgLocalFileRepo, $wgForeignFileRepos;
41 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
42 return self
::$instance;
46 * Destroy the singleton instance, so that a new one will be created next
47 * time singleton() is called.
49 static function destroySingleton() {
50 self
::$instance = null;
54 * Set the singleton instance to a given object
55 * Used by extensions which hook into the Repo chain.
56 * It's not enough to just create a superclass ... you have
57 * to get people to call into it even though all they know is RepoGroup::singleton()
59 * @param $instance RepoGroup
61 static function setSingleton( $instance ) {
62 self
::$instance = $instance;
66 * Construct a group of file repositories.
68 * @param $localInfo array Associative array for local repo's info
69 * @param $foreignInfo Array of repository info arrays.
70 * Each info array is an associative array with the 'class' member
71 * giving the class name. The entire array is passed to the repository
72 * constructor as the first parameter.
74 function __construct( $localInfo, $foreignInfo ) {
75 $this->localInfo
= $localInfo;
76 $this->foreignInfo
= $foreignInfo;
77 $this->cache
= array();
81 * Search repositories for an image.
82 * You can also use wfFindFile() to do this.
84 * @param $title Title|string Title object or string
85 * @param $options array Associative array of options:
86 * time: requested time for an archived image, or false for the
87 * current version. An image object will be returned which was
88 * created at the specified time.
90 * ignoreRedirect: If true, do not follow file redirects
92 * private: If true, return restricted (deleted) files if the current
93 * user is allowed to view them. Otherwise, such files will not
96 * bypassCache: If true, do not use the process-local cache of File objects
97 * @return File object or false if it is not found
99 function findFile( $title, $options = array() ) {
100 if ( !is_array( $options ) ) {
102 $options = array( 'time' => $options );
104 if ( !$this->reposInitialised
) {
105 $this->initialiseRepos();
107 $title = File
::normalizeTitle( $title );
113 if ( empty( $options['ignoreRedirect'] )
114 && empty( $options['private'] )
115 && empty( $options['bypassCache'] ) )
118 $time = isset( $options['time'] ) ?
$options['time'] : '';
119 $dbkey = $title->getDBkey();
120 if ( isset( $this->cache
[$dbkey][$time] ) ) {
121 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
122 # Move it to the end of the list so that we can delete the LRU entry later
123 $tmp = $this->cache
[$dbkey];
124 unset( $this->cache
[$dbkey] );
125 $this->cache
[$dbkey] = $tmp;
127 return $this->cache
[$dbkey][$time];
129 # Add a negative cache entry, may be overridden
131 $this->cache
[$dbkey][$time] = false;
132 $cacheEntry =& $this->cache
[$dbkey][$time];
138 # Check the local repo
139 $image = $this->localRepo
->findFile( $title, $options );
142 $cacheEntry = $image;
147 # Check the foreign repos
148 foreach ( $this->foreignRepos
as $repo ) {
149 $image = $repo->findFile( $title, $options );
152 $cacheEntry = $image;
157 # Not found, do not override negative cache
161 function findFiles( $inputItems ) {
162 if ( !$this->reposInitialised
) {
163 $this->initialiseRepos();
167 foreach ( $inputItems as $item ) {
168 if ( !is_array( $item ) ) {
169 $item = array( 'title' => $item );
171 $item['title'] = File
::normalizeTitle( $item['title'] );
172 if ( $item['title'] ) {
173 $items[$item['title']->getDBkey()] = $item;
177 $images = $this->localRepo
->findFiles( $items );
179 foreach ( $this->foreignRepos
as $repo ) {
180 // Remove found files from $items
181 foreach ( $images as $name => $image ) {
182 unset( $items[$name] );
185 $images = array_merge( $images, $repo->findFiles( $items ) );
191 * Interface for FileRepo::checkRedirect()
193 function checkRedirect( Title
$title ) {
194 if ( !$this->reposInitialised
) {
195 $this->initialiseRepos();
198 $redir = $this->localRepo
->checkRedirect( $title );
202 foreach ( $this->foreignRepos
as $repo ) {
203 $redir = $repo->checkRedirect( $title );
212 * Find an instance of the file with this key, created at the specified time
213 * Returns false if the file does not exist.
215 * @param $hash String base 36 SHA-1 hash
216 * @param $options array Option array, same as findFile()
217 * @return File object or false if it is not found
219 function findFileFromKey( $hash, $options = array() ) {
220 if ( !$this->reposInitialised
) {
221 $this->initialiseRepos();
224 $file = $this->localRepo
->findFileFromKey( $hash, $options );
226 foreach ( $this->foreignRepos
as $repo ) {
227 $file = $repo->findFileFromKey( $hash, $options );
235 * Find all instances of files with this key
237 * @param $hash String base 36 SHA-1 hash
238 * @return Array of File objects
240 function findBySha1( $hash ) {
241 if ( !$this->reposInitialised
) {
242 $this->initialiseRepos();
245 $result = $this->localRepo
->findBySha1( $hash );
246 foreach ( $this->foreignRepos
as $repo ) {
247 $result = array_merge( $result, $repo->findBySha1( $hash ) );
253 * Get the repo instance with a given key.
255 function getRepo( $index ) {
256 if ( !$this->reposInitialised
) {
257 $this->initialiseRepos();
259 if ( $index === 'local' ) {
260 return $this->localRepo
;
261 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
262 return $this->foreignRepos
[$index];
268 * Get the repo instance by its name
270 function getRepoByName( $name ) {
271 if ( !$this->reposInitialised
) {
272 $this->initialiseRepos();
274 foreach ( $this->foreignRepos
as $repo ) {
275 if ( $repo->name
== $name)
282 * Get the local repository, i.e. the one corresponding to the local image
283 * table. Files are typically uploaded to the local repository.
287 function getLocalRepo() {
288 return $this->getRepo( 'local' );
292 * Call a function for each foreign repo, with the repo object as the
295 * @param $callback Callback: the function to call
296 * @param $params Array: optional additional parameters to pass to the function
298 function forEachForeignRepo( $callback, $params = array() ) {
299 foreach( $this->foreignRepos
as $repo ) {
300 $args = array_merge( array( $repo ), $params );
301 if( call_user_func_array( $callback, $args ) ) {
309 * Does the installation have any foreign repos set up?
312 function hasForeignRepos() {
313 return (bool)$this->foreignRepos
;
317 * Initialise the $repos array
319 function initialiseRepos() {
320 if ( $this->reposInitialised
) {
323 $this->reposInitialised
= true;
325 $this->localRepo
= $this->newRepo( $this->localInfo
);
326 $this->foreignRepos
= array();
327 foreach ( $this->foreignInfo
as $key => $info ) {
328 $this->foreignRepos
[$key] = $this->newRepo( $info );
333 * Create a repo class based on an info structure
335 protected function newRepo( $info ) {
336 $class = $info['class'];
337 return new $class( $info );
341 * Split a virtual URL into repo, zone and rel parts
343 * @return array containing repo, zone and rel
345 function splitVirtualUrl( $url ) {
346 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
347 throw new MWException( __METHOD__
.': unknown protocol' );
350 $bits = explode( '/', substr( $url, 9 ), 3 );
351 if ( count( $bits ) != 3 ) {
352 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
357 function getFileProps( $fileName ) {
358 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
359 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
360 if ( $repoName === '' ) {
363 $repo = $this->getRepo( $repoName );
364 return $repo->getFileProps( $fileName );
366 return FSFile
::getPropsFromPath( $fileName );
373 protected function trimCache() {
374 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
375 reset( $this->cache
);
376 $key = key( $this->cache
);
377 wfDebug( __METHOD__
.": evicting $key\n" );
378 unset( $this->cache
[$key] );
383 * Clear RepoGroup process cache used for finding a file
384 * @param $title Title|null Title of the file or null to clear all files
386 public function clearCache( Title
$title = null ) {
387 if ( $title == null ) {
388 $this->cache
= array();
390 $dbKey = $title->getDBkey();
391 if ( isset( $this->cache
[$dbKey] ) ) {
392 unset( $this->cache
[$dbKey] );