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;
38 /** @var WANObjectCache */
42 protected $reposInitialised = false;
48 protected $foreignInfo;
50 /** @var MapCacheLRU */
53 /** Maximum number of cache items */
54 const MAX_CACHE_SIZE
= 500;
57 * @deprecated since 1.34, use MediaWikiServices::getRepoGroup
60 static function singleton() {
61 return MediaWikiServices
::getInstance()->getRepoGroup();
65 * @deprecated since 1.34, use MediaWikiTestCase::overrideMwServices() or similar. This will
66 * cause bugs if you don't reset all other services that depend on this one at the same time.
68 static function destroySingleton() {
69 MediaWikiServices
::getInstance()->resetServiceForTesting( 'RepoGroup' );
73 * @deprecated since 1.34, use MediaWikiTestCase::setService, this can mess up state of other
75 * @param RepoGroup $instance
77 static function setSingleton( $instance ) {
78 $services = MediaWikiServices
::getInstance();
79 $services->disableService( 'RepoGroup' );
80 $services->redefineService( 'RepoGroup',
81 function () use ( $instance ) {
88 * Construct a group of file repositories. Do not call this -- use
89 * MediaWikiServices::getRepoGroup.
91 * @param array $localInfo Associative array for local repo's info
92 * @param array $foreignInfo Array of repository info arrays.
93 * Each info array is an associative array with the 'class' member
94 * giving the class name. The entire array is passed to the repository
95 * constructor as the first parameter.
96 * @param WANObjectCache $wanCache
98 function __construct( $localInfo, $foreignInfo, $wanCache ) {
99 $this->localInfo
= $localInfo;
100 $this->foreignInfo
= $foreignInfo;
101 $this->cache
= new MapCacheLRU( self
::MAX_CACHE_SIZE
);
102 $this->wanCache
= $wanCache;
106 * Search repositories for an image.
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 * latest: If true, load from the latest available data into File objects
118 * @phan-param array{time?:mixed,ignoreRedirect?:bool,private?:bool,latest?:bool} $options
119 * @return File|bool False if title is not found
121 function findFile( $title, $options = [] ) {
122 if ( !is_array( $options ) ) {
124 $options = [ 'time' => $options ];
126 if ( isset( $options['bypassCache'] ) ) {
127 $options['latest'] = $options['bypassCache']; // b/c
129 $options +
= [ 'time' => false ];
131 if ( !$this->reposInitialised
) {
132 $this->initialiseRepos();
135 $title = File
::normalizeTitle( $title );
141 $dbkey = $title->getDBkey();
142 $timeKey = is_string( $options['time'] ) ?
$options['time'] : '';
143 if ( empty( $options['ignoreRedirect'] )
144 && empty( $options['private'] )
145 && empty( $options['latest'] )
147 if ( $this->cache
->hasField( $dbkey, $timeKey, 60 ) ) {
148 return $this->cache
->getField( $dbkey, $timeKey );
155 # Check the local repo
156 $image = $this->localRepo
->findFile( $title, $options );
158 # Check the foreign repos
160 foreach ( $this->foreignRepos
as $repo ) {
161 $image = $repo->findFile( $title, $options );
168 $image = $image instanceof File ?
$image : false; // type sanity
169 # Cache file existence or non-existence
170 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
171 $this->cache
->setField( $dbkey, $timeKey, $image );
178 * Search repositories for many files at once.
180 * @param array $inputItems An array of titles, or an array of findFile() options with
181 * the "title" option giving the title. Example:
183 * $findItem = [ 'title' => $title, 'private' => true ];
184 * $findBatch = [ $findItem ];
185 * $repo->findFiles( $findBatch );
187 * No title should appear in $items twice, as the result use titles as keys
188 * @param int $flags Supports:
189 * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
190 * The search title uses the input titles; the other is the final post-redirect title.
191 * All titles are returned as string DB keys and the inner array is associative.
192 * @return array Map of (file name => File objects) for matches
194 function findFiles( array $inputItems, $flags = 0 ) {
195 if ( !$this->reposInitialised
) {
196 $this->initialiseRepos();
200 foreach ( $inputItems as $item ) {
201 if ( !is_array( $item ) ) {
202 $item = [ 'title' => $item ];
204 $item['title'] = File
::normalizeTitle( $item['title'] );
205 if ( $item['title'] ) {
206 $items[$item['title']->getDBkey()] = $item;
210 $images = $this->localRepo
->findFiles( $items, $flags );
212 foreach ( $this->foreignRepos
as $repo ) {
213 // Remove found files from $items
214 foreach ( $images as $name => $image ) {
215 unset( $items[$name] );
218 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
225 * Interface for FileRepo::checkRedirect()
226 * @param Title $title
229 function checkRedirect( Title
$title ) {
230 if ( !$this->reposInitialised
) {
231 $this->initialiseRepos();
234 $redir = $this->localRepo
->checkRedirect( $title );
239 foreach ( $this->foreignRepos
as $repo ) {
240 $redir = $repo->checkRedirect( $title );
250 * Find an instance of the file with this key, created at the specified time
251 * Returns false if the file does not exist.
253 * @param string $hash Base 36 SHA-1 hash
254 * @param array $options Option array, same as findFile()
255 * @return File|bool File object or false if it is not found
257 function findFileFromKey( $hash, $options = [] ) {
258 if ( !$this->reposInitialised
) {
259 $this->initialiseRepos();
262 $file = $this->localRepo
->findFileFromKey( $hash, $options );
264 foreach ( $this->foreignRepos
as $repo ) {
265 $file = $repo->findFileFromKey( $hash, $options );
276 * Find all instances of files with this key
278 * @param string $hash Base 36 SHA-1 hash
281 function findBySha1( $hash ) {
282 if ( !$this->reposInitialised
) {
283 $this->initialiseRepos();
286 $result = $this->localRepo
->findBySha1( $hash );
287 foreach ( $this->foreignRepos
as $repo ) {
288 $result = array_merge( $result, $repo->findBySha1( $hash ) );
290 usort( $result, 'File::compare' );
296 * Find all instances of files with this keys
298 * @param array $hashes Base 36 SHA-1 hashes
299 * @return array Array of array of File objects
301 function findBySha1s( array $hashes ) {
302 if ( !$this->reposInitialised
) {
303 $this->initialiseRepos();
306 $result = $this->localRepo
->findBySha1s( $hashes );
307 foreach ( $this->foreignRepos
as $repo ) {
308 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
310 // sort the merged (and presorted) sublist of each hash
311 foreach ( $result as $hash => $files ) {
312 usort( $result[$hash], 'File::compare' );
319 * Get the repo instance with a given key.
320 * @param string|int $index
321 * @return bool|FileRepo
323 function getRepo( $index ) {
324 if ( !$this->reposInitialised
) {
325 $this->initialiseRepos();
327 if ( $index === 'local' ) {
328 return $this->localRepo
;
330 return $this->foreignRepos
[$index] ??
false;
334 * Get the repo instance by its name
335 * @param string $name
336 * @return FileRepo|bool
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 /** @var LocalRepo $repo */
359 $repo = $this->getRepo( 'local' );
365 * Call a function for each foreign repo, with the repo object as the
368 * @param callable $callback The function to call
369 * @param array $params Optional additional parameters to pass to the function
372 function forEachForeignRepo( $callback, $params = [] ) {
373 if ( !$this->reposInitialised
) {
374 $this->initialiseRepos();
376 foreach ( $this->foreignRepos
as $repo ) {
377 if ( $callback( $repo, ...$params ) ) {
386 * Does the installation have any foreign repos set up?
389 function hasForeignRepos() {
390 if ( !$this->reposInitialised
) {
391 $this->initialiseRepos();
393 return (bool)$this->foreignRepos
;
397 * Initialise the $repos array
399 function initialiseRepos() {
400 if ( $this->reposInitialised
) {
403 $this->reposInitialised
= true;
405 $this->localRepo
= $this->newRepo( $this->localInfo
);
406 $this->foreignRepos
= [];
407 foreach ( $this->foreignInfo
as $key => $info ) {
408 $this->foreignRepos
[$key] = $this->newRepo( $info );
413 * Create a repo class based on an info structure
417 protected function newRepo( $info ) {
418 $class = $info['class'];
420 $info['wanCache'] = $this->wanCache
;
422 return new $class( $info );
426 * Split a virtual URL into repo, zone and rel parts
428 * @throws MWException
429 * @return string[] Containing repo, zone and rel
431 function splitVirtualUrl( $url ) {
432 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
433 throw new MWException( __METHOD__
. ': unknown protocol' );
436 $bits = explode( '/', substr( $url, 9 ), 3 );
437 if ( count( $bits ) != 3 ) {
438 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
445 * @param string $fileName
448 function getFileProps( $fileName ) {
449 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
450 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
451 if ( $repoName === '' ) {
454 $repo = $this->getRepo( $repoName );
456 return $repo->getFileProps( $fileName );
458 $mwProps = new MWFileProps( MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer() );
460 return $mwProps->getPropsFromPath( $fileName, true );
465 * Clear RepoGroup process cache used for finding a file
466 * @param Title|null $title Title of the file or null to clear all files
468 public function clearCache( Title
$title = null ) {
469 if ( $title == null ) {
470 $this->cache
->clear();
472 $this->cache
->clear( $title->getDBkey() );