3 * Prioritized list of file repositories.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Prioritized list of file repositories
33 protected $foreignRepos;
36 protected $reposInitialised = false;
39 protected $foreignInfo;
45 protected static $instance;
47 /** Maximum number of cache items */
48 const MAX_CACHE_SIZE
= 500;
51 * Get a RepoGroup instance. At present only one instance of RepoGroup is
52 * needed in a MediaWiki invocation, this may change in the future.
55 static function singleton() {
56 if ( self
::$instance ) {
57 return self
::$instance;
59 global $wgLocalFileRepo, $wgForeignFileRepos;
60 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
62 return self
::$instance;
66 * Destroy the singleton instance, so that a new one will be created next
67 * time singleton() is called.
69 static function destroySingleton() {
70 self
::$instance = null;
74 * Set the singleton instance to a given object
75 * Used by extensions which hook into the Repo chain.
76 * It's not enough to just create a superclass ... you have
77 * to get people to call into it even though all they know is RepoGroup::singleton()
79 * @param $instance RepoGroup
81 static function setSingleton( $instance ) {
82 self
::$instance = $instance;
86 * Construct a group of file repositories.
88 * @param array $localInfo Associative array for local repo's info
89 * @param array $foreignInfo of repository info arrays.
90 * Each info array is an associative array with the 'class' member
91 * giving the class name. The entire array is passed to the repository
92 * constructor as the first parameter.
94 function __construct( $localInfo, $foreignInfo ) {
95 $this->localInfo
= $localInfo;
96 $this->foreignInfo
= $foreignInfo;
97 $this->cache
= array();
101 * Search repositories for an image.
102 * You can also use wfFindFile() to do this.
104 * @param $title Title|string Title object or string
105 * @param array $options Associative array of options:
106 * time: requested time for an archived image, or false for the
107 * current version. An image object will be returned which was
108 * created at the specified time.
110 * ignoreRedirect: If true, do not follow file redirects
112 * private: If true, return restricted (deleted) files if the current
113 * user is allowed to view them. Otherwise, such files will not
116 * bypassCache: If true, do not use the process-local cache of File objects
117 * @return File object or false if it is not found
119 function findFile( $title, $options = array() ) {
120 if ( !is_array( $options ) ) {
122 $options = array( 'time' => $options );
124 if ( !$this->reposInitialised
) {
125 $this->initialiseRepos();
127 $title = File
::normalizeTitle( $title );
133 if ( empty( $options['ignoreRedirect'] )
134 && empty( $options['private'] )
135 && empty( $options['bypassCache'] )
137 $time = isset( $options['time'] ) ?
$options['time'] : '';
138 $dbkey = $title->getDBkey();
139 if ( isset( $this->cache
[$dbkey][$time] ) ) {
140 wfDebug( __METHOD__
. ": got File:$dbkey from process cache\n" );
141 # Move it to the end of the list so that we can delete the LRU entry later
142 $this->pingCache( $dbkey );
145 return $this->cache
[$dbkey][$time];
152 # Check the local repo
153 $image = $this->localRepo
->findFile( $title, $options );
155 # Check the foreign repos
157 foreach ( $this->foreignRepos
as $repo ) {
158 $image = $repo->findFile( $title, $options );
165 $image = $image ?
$image : false; // type sanity
166 # Cache file existence or non-existence
167 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
169 $this->cache
[$dbkey][$time] = $image;
176 * @param $inputItems array
179 function findFiles( $inputItems ) {
180 if ( !$this->reposInitialised
) {
181 $this->initialiseRepos();
185 foreach ( $inputItems as $item ) {
186 if ( !is_array( $item ) ) {
187 $item = array( 'title' => $item );
189 $item['title'] = File
::normalizeTitle( $item['title'] );
190 if ( $item['title'] ) {
191 $items[$item['title']->getDBkey()] = $item;
195 $images = $this->localRepo
->findFiles( $items );
197 foreach ( $this->foreignRepos
as $repo ) {
198 // Remove found files from $items
199 foreach ( $images as $name => $image ) {
200 unset( $items[$name] );
203 $images = array_merge( $images, $repo->findFiles( $items ) );
210 * Interface for FileRepo::checkRedirect()
211 * @param $title Title
214 function checkRedirect( Title
$title ) {
215 if ( !$this->reposInitialised
) {
216 $this->initialiseRepos();
219 $redir = $this->localRepo
->checkRedirect( $title );
223 foreach ( $this->foreignRepos
as $repo ) {
224 $redir = $repo->checkRedirect( $title );
234 * Find an instance of the file with this key, created at the specified time
235 * Returns false if the file does not exist.
237 * @param string $hash base 36 SHA-1 hash
238 * @param array $options Option array, same as findFile()
239 * @return File object or false if it is not found
241 function findFileFromKey( $hash, $options = array() ) {
242 if ( !$this->reposInitialised
) {
243 $this->initialiseRepos();
246 $file = $this->localRepo
->findFileFromKey( $hash, $options );
248 foreach ( $this->foreignRepos
as $repo ) {
249 $file = $repo->findFileFromKey( $hash, $options );
260 * Find all instances of files with this key
262 * @param string $hash base 36 SHA-1 hash
263 * @return Array of File objects
265 function findBySha1( $hash ) {
266 if ( !$this->reposInitialised
) {
267 $this->initialiseRepos();
270 $result = $this->localRepo
->findBySha1( $hash );
271 foreach ( $this->foreignRepos
as $repo ) {
272 $result = array_merge( $result, $repo->findBySha1( $hash ) );
274 usort( $result, 'File::compare' );
280 * Find all instances of files with this keys
282 * @param array $hashes base 36 SHA-1 hashes
283 * @return Array of array of File objects
285 function findBySha1s( array $hashes ) {
286 if ( !$this->reposInitialised
) {
287 $this->initialiseRepos();
290 $result = $this->localRepo
->findBySha1s( $hashes );
291 foreach ( $this->foreignRepos
as $repo ) {
292 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
294 //sort the merged (and presorted) sublist of each hash
295 foreach ( $result as $hash => $files ) {
296 usort( $result[$hash], 'File::compare' );
303 * Get the repo instance with a given key.
304 * @param $index string|int
305 * @return bool|LocalRepo
307 function getRepo( $index ) {
308 if ( !$this->reposInitialised
) {
309 $this->initialiseRepos();
311 if ( $index === 'local' ) {
312 return $this->localRepo
;
313 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
314 return $this->foreignRepos
[$index];
321 * Get the repo instance by its name
322 * @param $name string
325 function getRepoByName( $name ) {
326 if ( !$this->reposInitialised
) {
327 $this->initialiseRepos();
329 foreach ( $this->foreignRepos
as $repo ) {
330 if ( $repo->name
== $name ) {
339 * Get the local repository, i.e. the one corresponding to the local image
340 * table. Files are typically uploaded to the local repository.
344 function getLocalRepo() {
345 return $this->getRepo( 'local' );
349 * Call a function for each foreign repo, with the repo object as the
352 * @param $callback Callback: the function to call
353 * @param array $params optional additional parameters to pass to the function
356 function forEachForeignRepo( $callback, $params = array() ) {
357 foreach ( $this->foreignRepos
as $repo ) {
358 $args = array_merge( array( $repo ), $params );
359 if ( call_user_func_array( $callback, $args ) ) {
368 * Does the installation have any foreign repos set up?
371 function hasForeignRepos() {
372 return (bool)$this->foreignRepos
;
376 * Initialise the $repos array
378 function initialiseRepos() {
379 if ( $this->reposInitialised
) {
382 $this->reposInitialised
= true;
384 $this->localRepo
= $this->newRepo( $this->localInfo
);
385 $this->foreignRepos
= array();
386 foreach ( $this->foreignInfo
as $key => $info ) {
387 $this->foreignRepos
[$key] = $this->newRepo( $info );
392 * Create a repo class based on an info structure
394 protected function newRepo( $info ) {
395 $class = $info['class'];
397 return new $class( $info );
401 * Split a virtual URL into repo, zone and rel parts
403 * @throws MWException
404 * @return array containing repo, zone and rel
406 function splitVirtualUrl( $url ) {
407 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
408 throw new MWException( __METHOD__
. ': unknown protocol' );
411 $bits = explode( '/', substr( $url, 9 ), 3 );
412 if ( count( $bits ) != 3 ) {
413 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
420 * @param $fileName string
423 function getFileProps( $fileName ) {
424 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
425 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
426 if ( $repoName === '' ) {
429 $repo = $this->getRepo( $repoName );
431 return $repo->getFileProps( $fileName );
433 return FSFile
::getPropsFromPath( $fileName );
438 * Move a cache entry to the top (such as when accessed)
440 protected function pingCache( $key ) {
441 if ( isset( $this->cache
[$key] ) ) {
442 $tmp = $this->cache
[$key];
443 unset( $this->cache
[$key] );
444 $this->cache
[$key] = $tmp;
451 protected function trimCache() {
452 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
453 reset( $this->cache
);
454 $key = key( $this->cache
);
455 wfDebug( __METHOD__
. ": evicting $key\n" );
456 unset( $this->cache
[$key] );
461 * Clear RepoGroup process cache used for finding a file
462 * @param $title Title|null Title of the file or null to clear all files
464 public function clearCache( Title
$title = null ) {
465 if ( $title == null ) {
466 $this->cache
= array();
468 $dbKey = $title->getDBkey();
469 if ( isset( $this->cache
[$dbKey] ) ) {
470 unset( $this->cache
[$dbKey] );