61 return MediaWikiServices::getInstance()->getRepoGroup();
69 MediaWikiServices::getInstance()->resetServiceForTesting(
'RepoGroup' );
78 $services = MediaWikiServices::getInstance();
79 $services->disableService(
'RepoGroup' );
80 $services->redefineService(
'RepoGroup',
81 function () use ( $instance ) {
101 $this->cache =
new MapCacheLRU( self::MAX_CACHE_SIZE );
122 if ( !is_array( $options ) ) {
124 $options = [
'time' => $options ];
126 if ( isset( $options[
'bypassCache'] ) ) {
127 $options[
'latest'] = $options[
'bypassCache'];
129 $options += [
'time' => false ];
131 if ( !$this->reposInitialised ) {
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;
169 # Cache file existence or non-existence
170 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
171 $this->cache->setField( $dbkey, $timeKey, $image );
195 if ( !$this->reposInitialised ) {
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 ) {
214 foreach ( $images as $name => $image ) {
215 unset( $items[$name] );
218 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
230 if ( !$this->reposInitialised ) {
234 $redir = $this->localRepo->checkRedirect(
$title );
239 foreach ( $this->foreignRepos as $repo ) {
240 $redir = $repo->checkRedirect(
$title );
258 if ( !$this->reposInitialised ) {
262 $file = $this->localRepo->findFileFromKey( $hash, $options );
264 foreach ( $this->foreignRepos as $repo ) {
265 $file = $repo->findFileFromKey( $hash, $options );
282 if ( !$this->reposInitialised ) {
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' );
302 if ( !$this->reposInitialised ) {
306 $result = $this->localRepo->findBySha1s(
$hashes );
307 foreach ( $this->foreignRepos as $repo ) {
308 $result = array_merge_recursive( $result, $repo->findBySha1s(
$hashes ) );
311 foreach ( $result as $hash => $files ) {
312 usort( $result[$hash],
'File::compare' );
324 if ( !$this->reposInitialised ) {
327 if ( $index ===
'local' ) {
328 return $this->localRepo;
330 return $this->foreignRepos[$index] ??
false;
339 if ( !$this->reposInitialised ) {
342 foreach ( $this->foreignRepos as $repo ) {
343 if ( $repo->name == $name ) {
359 $repo = $this->
getRepo(
'local' );
373 if ( !$this->reposInitialised ) {
376 foreach ( $this->foreignRepos as $repo ) {
377 if ( $callback( $repo, ...$params ) ) {
390 if ( !$this->reposInitialised ) {
393 return (
bool)$this->foreignRepos;
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 );
418 $class = $info[
'class'];
420 $info[
'wanCache'] = $this->wanCache;
422 return new $class( $info );
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" );
451 if ( $repoName ===
'' ) {
454 $repo = $this->
getRepo( $repoName );
456 return $repo->getFileProps( $fileName );
460 return $mwProps->getPropsFromPath( $fileName,
true );
470 $this->cache->clear();
472 $this->cache->clear(
$title->getDBkey() );
Base class for file repositories.
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Implements some public methods and some protected utility functions which are required by multiple ch...
A repository that stores files in the local filesystem and registers them in the wiki's own database.
MimeMagic helper wrapper.
Handles a simple LRU key/value map with a maximum number of entries.
Prioritized list of file repositories.
initialiseRepos()
Initialise the $repos array.
getRepoByName( $name)
Get the repo instance by its name.
__construct( $localInfo, $foreignInfo, $wanCache)
Construct a group of file repositories.
splitVirtualUrl( $url)
Split a virtual URL into repo, zone and rel parts.
const MAX_CACHE_SIZE
Maximum number of cache items.
hasForeignRepos()
Does the installation have any foreign repos set up?
findFileFromKey( $hash, $options=[])
Find an instance of the file with this key, created at the specified time Returns false if the file d...
findFile( $title, $options=[])
Search repositories for an image.
getLocalRepo()
Get the local repository, i.e.
findBySha1s(array $hashes)
Find all instances of files with this keys.
checkRedirect(Title $title)
Interface for FileRepo::checkRedirect()
getRepo( $index)
Get the repo instance with a given key.
clearCache(Title $title=null)
Clear RepoGroup process cache used for finding a file.
findBySha1( $hash)
Find all instances of files with this key.
findFiles(array $inputItems, $flags=0)
Search repositories for many files at once.
static setSingleton( $instance)
newRepo( $info)
Create a repo class based on an info structure.
static destroySingleton()
forEachForeignRepo( $callback, $params=[])
Call a function for each foreign repo, with the repo object as the first parameter.
Represents a title within MediaWiki.
Multi-datacenter aware caching interface.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.