52 private const MAX_CACHE_SIZE = 500;
55 private $mimeAnalyzer;
77 $this->cache =
new MapCacheLRU( self::MAX_CACHE_SIZE );
79 $this->mimeAnalyzer = $mimeAnalyzer;
101 if ( !is_array( $options ) ) {
103 $options = [
'time' => $options ];
105 if ( isset( $options[
'bypassCache'] ) ) {
106 $options[
'latest'] = $options[
'bypassCache'];
108 if ( isset( $options[
'time'] ) && $options[
'time'] !==
false ) {
109 $options[
'time'] =
wfTimestamp( TS_MW, $options[
'time'] );
111 $options[
'time'] =
false;
114 if ( !$this->reposInitialised ) {
124 $dbkey =
$title->getDBkey();
125 $timeKey = is_string( $options[
'time'] ) ? $options[
'time'] :
'';
126 if ( empty( $options[
'ignoreRedirect'] )
127 && empty( $options[
'private'] )
128 && empty( $options[
'latest'] )
130 if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
131 return $this->cache->getField( $dbkey, $timeKey );
138 # Check the local repo
139 $image = $this->localRepo->findFile(
$title, $options );
141 # Check the foreign repos
143 foreach ( $this->foreignRepos as $repo ) {
144 $image = $repo->findFile(
$title, $options );
151 $image = $image instanceof
File ? $image :
false;
152 # Cache file existence or non-existence
153 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
154 $this->cache->setField( $dbkey, $timeKey, $image );
177 public function findFiles( array $inputItems, $flags = 0 ) {
178 if ( !$this->reposInitialised ) {
183 foreach ( $inputItems as $item ) {
184 if ( !is_array( $item ) ) {
185 $item = [
'title' => $item ];
188 if ( $item[
'title'] ) {
189 $items[$item[
'title']->getDBkey()] = $item;
193 $images = $this->localRepo->findFiles( $items, $flags );
195 foreach ( $this->foreignRepos as $repo ) {
197 foreach ( $images as $name => $image ) {
198 unset( $items[$name] );
201 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
213 if ( !$this->reposInitialised ) {
219 $redir = $this->localRepo->checkRedirect(
$title );
224 foreach ( $this->foreignRepos as $repo ) {
225 $redir = $repo->checkRedirect(
$title );
243 if ( !$this->reposInitialised ) {
247 $file = $this->localRepo->findFileFromKey( $hash, $options );
249 foreach ( $this->foreignRepos as $repo ) {
250 $file = $repo->findFileFromKey( $hash, $options );
267 if ( !$this->reposInitialised ) {
271 $result = $this->localRepo->findBySha1( $hash );
272 foreach ( $this->foreignRepos as $repo ) {
273 $result = array_merge( $result, $repo->findBySha1( $hash ) );
275 usort( $result, [ File::class,
'compare' ] );
287 if ( !$this->reposInitialised ) {
291 $result = $this->localRepo->findBySha1s(
$hashes );
292 foreach ( $this->foreignRepos as $repo ) {
293 $result = array_merge_recursive( $result, $repo->findBySha1s(
$hashes ) );
296 foreach ( $result as $hash => $files ) {
297 usort( $result[$hash], [ File::class,
'compare' ] );
309 if ( !$this->reposInitialised ) {
312 if ( $index ===
'local' ) {
315 return $this->foreignRepos[$index] ??
false;
324 if ( !$this->reposInitialised ) {
327 foreach ( $this->foreignRepos as $repo ) {
328 if ( $repo->name == $name ) {
344 return $this->
getRepo(
'local' );
356 if ( !$this->reposInitialised ) {
359 foreach ( $this->foreignRepos as $repo ) {
360 if ( $callback( $repo, ...$params ) ) {
373 if ( !$this->reposInitialised ) {
383 if ( $this->reposInitialised ) {
386 $this->reposInitialised =
true;
388 $this->localRepo = $this->
newRepo( $this->localInfo );
389 $this->foreignRepos = [];
390 foreach ( $this->foreignInfo as $key => $info ) {
391 $this->foreignRepos[$key] = $this->
newRepo( $info );
403 return $this->
newRepo( $info + $this->localInfo );
412 $class = $info[
'class'];
416 return new $class( $info );
425 private function splitVirtualUrl( $url ) {
426 if ( !str_starts_with( $url,
'mwrepo://' ) ) {
427 throw new MWException( __METHOD__ .
': unknown protocol' );
430 $bits = explode(
'/', substr( $url, 9 ), 3 );
431 if ( count( $bits ) != 3 ) {
432 throw new MWException( __METHOD__ .
": invalid mwrepo URL: $url" );
444 [ $repoName, , ] = $this->splitVirtualUrl( $fileName );
445 if ( $repoName ===
'' ) {
448 $repo = $this->
getRepo( $repoName );
450 return $repo->getFileProps( $fileName );
454 return $mwProps->getPropsFromPath( $fileName,
true );
464 $this->cache->clear();
465 } elseif ( is_string(
$title ) ) {
466 $this->cache->clear(
$title );
468 $this->cache->clear(
$title->getDBkey() );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
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...
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
MimeMagic helper wrapper.
Handles a simple LRU key/value map with a maximum number of entries.
Implements functions related to MIME types such as detection and mapping to file extension.
Prioritized list of file repositories.
newCustomLocalRepo( $info=[])
Create a local repo with the specified option overrides.
initialiseRepos()
Initialise the $repos array.
checkRedirect( $title)
Interface for FileRepo::checkRedirect()
getRepoByName( $name)
Get the repo instance by its name.
__construct( $localInfo, $foreignInfo, WANObjectCache $wanCache, MimeAnalyzer $mimeAnalyzer)
Construct a group of file repositories.
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.
clearCache( $title=null)
Clear RepoGroup process cache used for finding a file.
getRepo( $index)
Get the repo instance with a given key.
findBySha1( $hash)
Find all instances of files with this key.
findFiles(array $inputItems, $flags=0)
Search repositories for many files at once.
newRepo( $info)
Create a repo class based on an info structure.
forEachForeignRepo( $callback, $params=[])
Call a function for each foreign repo, with the repo object as the first parameter.
Multi-datacenter aware caching interface.
Interface for objects (potentially) representing an editable wiki page.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.