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
24 use MediaWiki\MediaWikiServices
;
27 * Prioritized list of file repositories
35 /** @var FileRepo[] */
36 protected $foreignRepos;
39 protected $reposInitialised = false;
45 protected $foreignInfo;
47 /** @var ProcessCacheLRU */
51 protected static $instance;
53 /** Maximum number of cache items */
54 const MAX_CACHE_SIZE
= 500;
57 * Get a RepoGroup instance. At present only one instance of RepoGroup is
58 * needed in a MediaWiki invocation, this may change in the future.
61 static function singleton() {
62 if ( self
::$instance ) {
63 return self
::$instance;
65 global $wgLocalFileRepo, $wgForeignFileRepos;
66 /** @var array $wgLocalFileRepo */
67 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
69 return self
::$instance;
73 * Destroy the singleton instance, so that a new one will be created next
74 * time singleton() is called.
76 static function destroySingleton() {
77 self
::$instance = null;
81 * Set the singleton instance to a given object
82 * Used by extensions which hook into the Repo chain.
83 * It's not enough to just create a superclass ... you have
84 * to get people to call into it even though all they know is RepoGroup::singleton()
86 * @param RepoGroup $instance
88 static function setSingleton( $instance ) {
89 self
::$instance = $instance;
93 * Construct a group of file repositories.
95 * @param array $localInfo Associative array for local repo's info
96 * @param array $foreignInfo Array of repository info arrays.
97 * Each info array is an associative array with the 'class' member
98 * giving the class name. The entire array is passed to the repository
99 * constructor as the first parameter.
101 function __construct( $localInfo, $foreignInfo ) {
102 $this->localInfo
= $localInfo;
103 $this->foreignInfo
= $foreignInfo;
104 $this->cache
= new MapCacheLRU( self
::MAX_CACHE_SIZE
);
108 * Search repositories for an image.
109 * You can also use wfFindFile() to do this.
111 * @param Title|string $title Title object or string
112 * @param array $options Associative array of options:
113 * time: requested time for an archived image, or false for the
114 * current version. An image object will be returned which was
115 * created at the specified time.
116 * ignoreRedirect: If true, do not follow file redirects
117 * private: If true, return restricted (deleted) files if the current
118 * user is allowed to view them. Otherwise, such files will not
120 * latest: If true, load from the latest available data into File objects
121 * @return File|bool False if title is not found
123 function findFile( $title, $options = [] ) {
124 if ( !is_array( $options ) ) {
126 $options = [ 'time' => $options ];
128 if ( isset( $options['bypassCache'] ) ) {
129 $options['latest'] = $options['bypassCache']; // b/c
131 $options +
= [ 'time' => false ];
133 if ( !$this->reposInitialised
) {
134 $this->initialiseRepos();
137 $title = File
::normalizeTitle( $title );
143 $dbkey = $title->getDBkey();
144 $timeKey = is_string( $options['time'] ) ?
$options['time'] : '';
145 if ( empty( $options['ignoreRedirect'] )
146 && empty( $options['private'] )
147 && empty( $options['latest'] )
149 if ( $this->cache
->hasField( $dbkey, $timeKey, 60 ) ) {
150 return $this->cache
->getField( $dbkey, $timeKey );
157 # Check the local repo
158 $image = $this->localRepo
->findFile( $title, $options );
160 # Check the foreign repos
162 foreach ( $this->foreignRepos
as $repo ) {
163 $image = $repo->findFile( $title, $options );
170 $image = $image instanceof File ?
$image : false; // type sanity
171 # Cache file existence or non-existence
172 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
173 $this->cache
->setField( $dbkey, $timeKey, $image );
180 * Search repositories for many files at once.
182 * @param array $inputItems An array of titles, or an array of findFile() options with
183 * the "title" option giving the title. Example:
185 * $findItem = [ 'title' => $title, 'private' => true ];
186 * $findBatch = [ $findItem ];
187 * $repo->findFiles( $findBatch );
189 * No title should appear in $items twice, as the result use titles as keys
190 * @param int $flags Supports:
191 * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
192 * The search title uses the input titles; the other is the final post-redirect title.
193 * All titles are returned as string DB keys and the inner array is associative.
194 * @return array Map of (file name => File objects) for matches
196 function findFiles( array $inputItems, $flags = 0 ) {
197 if ( !$this->reposInitialised
) {
198 $this->initialiseRepos();
202 foreach ( $inputItems as $item ) {
203 if ( !is_array( $item ) ) {
204 $item = [ 'title' => $item ];
206 $item['title'] = File
::normalizeTitle( $item['title'] );
207 if ( $item['title'] ) {
208 $items[$item['title']->getDBkey()] = $item;
212 $images = $this->localRepo
->findFiles( $items, $flags );
214 foreach ( $this->foreignRepos
as $repo ) {
215 // Remove found files from $items
216 foreach ( $images as $name => $image ) {
217 unset( $items[$name] );
220 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
227 * Interface for FileRepo::checkRedirect()
228 * @param Title $title
231 function checkRedirect( Title
$title ) {
232 if ( !$this->reposInitialised
) {
233 $this->initialiseRepos();
236 $redir = $this->localRepo
->checkRedirect( $title );
241 foreach ( $this->foreignRepos
as $repo ) {
242 $redir = $repo->checkRedirect( $title );
252 * Find an instance of the file with this key, created at the specified time
253 * Returns false if the file does not exist.
255 * @param string $hash Base 36 SHA-1 hash
256 * @param array $options Option array, same as findFile()
257 * @return File|bool File object or false if it is not found
259 function findFileFromKey( $hash, $options = [] ) {
260 if ( !$this->reposInitialised
) {
261 $this->initialiseRepos();
264 $file = $this->localRepo
->findFileFromKey( $hash, $options );
266 foreach ( $this->foreignRepos
as $repo ) {
267 $file = $repo->findFileFromKey( $hash, $options );
278 * Find all instances of files with this key
280 * @param string $hash Base 36 SHA-1 hash
283 function findBySha1( $hash ) {
284 if ( !$this->reposInitialised
) {
285 $this->initialiseRepos();
288 $result = $this->localRepo
->findBySha1( $hash );
289 foreach ( $this->foreignRepos
as $repo ) {
290 $result = array_merge( $result, $repo->findBySha1( $hash ) );
292 usort( $result, 'File::compare' );
298 * Find all instances of files with this keys
300 * @param array $hashes Base 36 SHA-1 hashes
301 * @return array Array of array of File objects
303 function findBySha1s( array $hashes ) {
304 if ( !$this->reposInitialised
) {
305 $this->initialiseRepos();
308 $result = $this->localRepo
->findBySha1s( $hashes );
309 foreach ( $this->foreignRepos
as $repo ) {
310 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
312 // sort the merged (and presorted) sublist of each hash
313 foreach ( $result as $hash => $files ) {
314 usort( $result[$hash], 'File::compare' );
321 * Get the repo instance with a given key.
322 * @param string|int $index
323 * @return bool|FileRepo
325 function getRepo( $index ) {
326 if ( !$this->reposInitialised
) {
327 $this->initialiseRepos();
329 if ( $index === 'local' ) {
330 return $this->localRepo
;
332 return $this->foreignRepos
[$index] ??
false;
336 * Get the repo instance by its name
337 * @param string $name
338 * @return FileRepo|bool
340 function getRepoByName( $name ) {
341 if ( !$this->reposInitialised
) {
342 $this->initialiseRepos();
344 foreach ( $this->foreignRepos
as $repo ) {
345 if ( $repo->name
== $name ) {
354 * Get the local repository, i.e. the one corresponding to the local image
355 * table. Files are typically uploaded to the local repository.
359 function getLocalRepo() {
360 /** @var LocalRepo $repo */
361 $repo = $this->getRepo( 'local' );
367 * Call a function for each foreign repo, with the repo object as the
370 * @param callable $callback The function to call
371 * @param array $params Optional additional parameters to pass to the function
374 function forEachForeignRepo( $callback, $params = [] ) {
375 if ( !$this->reposInitialised
) {
376 $this->initialiseRepos();
378 foreach ( $this->foreignRepos
as $repo ) {
379 if ( $callback( $repo, ...$params ) ) {
388 * Does the installation have any foreign repos set up?
391 function hasForeignRepos() {
392 if ( !$this->reposInitialised
) {
393 $this->initialiseRepos();
395 return (bool)$this->foreignRepos
;
399 * Initialise the $repos array
401 function initialiseRepos() {
402 if ( $this->reposInitialised
) {
405 $this->reposInitialised
= true;
407 $this->localRepo
= $this->newRepo( $this->localInfo
);
408 $this->foreignRepos
= [];
409 foreach ( $this->foreignInfo
as $key => $info ) {
410 $this->foreignRepos
[$key] = $this->newRepo( $info );
415 * Create a repo class based on an info structure
419 protected function newRepo( $info ) {
420 $class = $info['class'];
422 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
423 $info['wanCache'] = $cache;
425 return new $class( $info );
429 * Split a virtual URL into repo, zone and rel parts
431 * @throws MWException
432 * @return string[] Containing repo, zone and rel
434 function splitVirtualUrl( $url ) {
435 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
436 throw new MWException( __METHOD__
. ': unknown protocol' );
439 $bits = explode( '/', substr( $url, 9 ), 3 );
440 if ( count( $bits ) != 3 ) {
441 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
448 * @param string $fileName
451 function getFileProps( $fileName ) {
452 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
453 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
454 if ( $repoName === '' ) {
457 $repo = $this->getRepo( $repoName );
459 return $repo->getFileProps( $fileName );
461 $mwProps = new MWFileProps( MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer() );
463 return $mwProps->getPropsFromPath( $fileName, true );
468 * Clear RepoGroup process cache used for finding a file
469 * @param Title|null $title Title of the file or null to clear all files
471 public function clearCache( Title
$title = null ) {
472 if ( $title == null ) {
473 $this->cache
->clear();
475 $this->cache
->clear( $title->getDBkey() );