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 /** @var FileRepo[] */
34 protected $foreignRepos;
37 protected $reposInitialised = false;
43 protected $foreignInfo;
45 /** @var ProcessCacheLRU */
49 protected static $instance;
51 /** Maximum number of cache items */
52 const MAX_CACHE_SIZE
= 500;
55 * Get a RepoGroup instance. At present only one instance of RepoGroup is
56 * needed in a MediaWiki invocation, this may change in the future.
59 static function singleton() {
60 if ( self
::$instance ) {
61 return self
::$instance;
63 global $wgLocalFileRepo, $wgForeignFileRepos;
64 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
66 return self
::$instance;
70 * Destroy the singleton instance, so that a new one will be created next
71 * time singleton() is called.
73 static function destroySingleton() {
74 self
::$instance = null;
78 * Set the singleton instance to a given object
79 * Used by extensions which hook into the Repo chain.
80 * It's not enough to just create a superclass ... you have
81 * to get people to call into it even though all they know is RepoGroup::singleton()
83 * @param RepoGroup $instance
85 static function setSingleton( $instance ) {
86 self
::$instance = $instance;
90 * Construct a group of file repositories.
92 * @param array $localInfo Associative array for local repo's info
93 * @param array $foreignInfo Array of repository info arrays.
94 * Each info array is an associative array with the 'class' member
95 * giving the class name. The entire array is passed to the repository
96 * constructor as the first parameter.
98 function __construct( $localInfo, $foreignInfo ) {
99 $this->localInfo
= $localInfo;
100 $this->foreignInfo
= $foreignInfo;
101 $this->cache
= new ProcessCacheLRU( self
::MAX_CACHE_SIZE
);
105 * Search repositories for an image.
106 * You can also use wfFindFile() to do this.
108 * @param Title|string $title Title object or string
109 * @param array $options Associative array of options:
110 * time: requested time for an archived image, or false for the
111 * current version. An image object will be returned which was
112 * created at the specified time.
113 * ignoreRedirect: If true, do not follow file redirects
114 * private: If true, return restricted (deleted) files if the current
115 * user is allowed to view them. Otherwise, such files will not
117 * bypassCache: If true, do not use the process-local cache of File objects
118 * @return File|bool False if title is not found
120 function findFile( $title, $options = array() ) {
121 if ( !is_array( $options ) ) {
123 $options = array( 'time' => $options );
125 if ( !$this->reposInitialised
) {
126 $this->initialiseRepos();
128 $title = File
::normalizeTitle( $title );
134 if ( empty( $options['ignoreRedirect'] )
135 && empty( $options['private'] )
136 && empty( $options['bypassCache'] )
138 $time = isset( $options['time'] ) ?
$options['time'] : '';
139 $dbkey = $title->getDBkey();
140 if ( $this->cache
->has( $dbkey, $time, 60 ) ) {
141 return $this->cache
->get( $dbkey, $time );
148 # Check the local repo
149 $image = $this->localRepo
->findFile( $title, $options );
151 # Check the foreign repos
153 foreach ( $this->foreignRepos
as $repo ) {
154 $image = $repo->findFile( $title, $options );
161 $image = $image ?
$image : false; // type sanity
162 # Cache file existence or non-existence
163 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
164 $this->cache
->set( $dbkey, $time, $image );
171 * Search repositories for many files at once.
173 * @param array $items An array of titles, or an array of findFile() options with
174 * the "title" option giving the title. Example:
176 * $findItem = array( 'title' => $title, 'private' => true );
177 * $findBatch = array( $findItem );
178 * $repo->findFiles( $findBatch );
180 * No title should appear in $items twice, as the result use titles as keys
181 * @param int $flags Supports:
182 * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
183 * The search title uses the input titles; the other is the final post-redirect title.
184 * All titles are returned as string DB keys and the inner array is associative.
185 * @return array Map of (file name => File objects) for matches
187 * @param array $inputItems
191 function findFiles( array $inputItems, $flags = 0 ) {
192 if ( !$this->reposInitialised
) {
193 $this->initialiseRepos();
197 foreach ( $inputItems as $item ) {
198 if ( !is_array( $item ) ) {
199 $item = array( 'title' => $item );
201 $item['title'] = File
::normalizeTitle( $item['title'] );
202 if ( $item['title'] ) {
203 $items[$item['title']->getDBkey()] = $item;
207 $images = $this->localRepo
->findFiles( $items, $flags );
209 foreach ( $this->foreignRepos
as $repo ) {
210 // Remove found files from $items
211 foreach ( $images as $name => $image ) {
212 unset( $items[$name] );
215 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
222 * Interface for FileRepo::checkRedirect()
223 * @param Title $title
226 function checkRedirect( Title
$title ) {
227 if ( !$this->reposInitialised
) {
228 $this->initialiseRepos();
231 $redir = $this->localRepo
->checkRedirect( $title );
236 foreach ( $this->foreignRepos
as $repo ) {
237 $redir = $repo->checkRedirect( $title );
247 * Find an instance of the file with this key, created at the specified time
248 * Returns false if the file does not exist.
250 * @param string $hash Base 36 SHA-1 hash
251 * @param array $options Option array, same as findFile()
252 * @return File|bool File object or false if it is not found
254 function findFileFromKey( $hash, $options = array() ) {
255 if ( !$this->reposInitialised
) {
256 $this->initialiseRepos();
259 $file = $this->localRepo
->findFileFromKey( $hash, $options );
261 foreach ( $this->foreignRepos
as $repo ) {
262 $file = $repo->findFileFromKey( $hash, $options );
273 * Find all instances of files with this key
275 * @param string $hash base 36 SHA-1 hash
276 * @return array Array of File objects
278 function findBySha1( $hash ) {
279 if ( !$this->reposInitialised
) {
280 $this->initialiseRepos();
283 $result = $this->localRepo
->findBySha1( $hash );
284 foreach ( $this->foreignRepos
as $repo ) {
285 $result = array_merge( $result, $repo->findBySha1( $hash ) );
287 usort( $result, 'File::compare' );
293 * Find all instances of files with this keys
295 * @param array $hashes base 36 SHA-1 hashes
296 * @return array Array of array of File objects
298 function findBySha1s( array $hashes ) {
299 if ( !$this->reposInitialised
) {
300 $this->initialiseRepos();
303 $result = $this->localRepo
->findBySha1s( $hashes );
304 foreach ( $this->foreignRepos
as $repo ) {
305 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
307 //sort the merged (and presorted) sublist of each hash
308 foreach ( $result as $hash => $files ) {
309 usort( $result[$hash], 'File::compare' );
316 * Get the repo instance with a given key.
317 * @param string|int $index
318 * @return bool|LocalRepo
320 function getRepo( $index ) {
321 if ( !$this->reposInitialised
) {
322 $this->initialiseRepos();
324 if ( $index === 'local' ) {
325 return $this->localRepo
;
326 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
327 return $this->foreignRepos
[$index];
334 * Get the repo instance by its name
335 * @param string $name
338 function getRepoByName( $name ) {
339 if ( !$this->reposInitialised
) {
340 $this->initialiseRepos();
342 foreach ( $this->foreignRepos
as $repo ) {
343 if ( $repo->name
== $name ) {
352 * Get the local repository, i.e. the one corresponding to the local image
353 * table. Files are typically uploaded to the local repository.
357 function getLocalRepo() {
358 return $this->getRepo( 'local' );
362 * Call a function for each foreign repo, with the repo object as the
365 * @param callable $callback The function to call
366 * @param array $params Optional additional parameters to pass to the function
369 function forEachForeignRepo( $callback, $params = array() ) {
370 foreach ( $this->foreignRepos
as $repo ) {
371 $args = array_merge( array( $repo ), $params );
372 if ( call_user_func_array( $callback, $args ) ) {
381 * Does the installation have any foreign repos set up?
384 function hasForeignRepos() {
385 return (bool)$this->foreignRepos
;
389 * Initialise the $repos array
391 function initialiseRepos() {
392 if ( $this->reposInitialised
) {
395 $this->reposInitialised
= true;
397 $this->localRepo
= $this->newRepo( $this->localInfo
);
398 $this->foreignRepos
= array();
399 foreach ( $this->foreignInfo
as $key => $info ) {
400 $this->foreignRepos
[$key] = $this->newRepo( $info );
405 * Create a repo class based on an info structure
407 protected function newRepo( $info ) {
408 $class = $info['class'];
410 return new $class( $info );
414 * Split a virtual URL into repo, zone and rel parts
416 * @throws MWException
417 * @return array containing repo, zone and rel
419 function splitVirtualUrl( $url ) {
420 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
421 throw new MWException( __METHOD__
. ': unknown protocol' );
424 $bits = explode( '/', substr( $url, 9 ), 3 );
425 if ( count( $bits ) != 3 ) {
426 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
433 * @param string $fileName
436 function getFileProps( $fileName ) {
437 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
438 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
439 if ( $repoName === '' ) {
442 $repo = $this->getRepo( $repoName );
444 return $repo->getFileProps( $fileName );
446 return FSFile
::getPropsFromPath( $fileName );
451 * Clear RepoGroup process cache used for finding a file
452 * @param Title|null $title Title of the file or null to clear all files
454 public function clearCache( Title
$title = null ) {
455 if ( $title == null ) {
456 $this->cache
->clear();
458 $this->cache
->clear( $title->getDBkey() );