MediaWiki master
RepoGroup.php
Go to the documentation of this file.
1<?php
24use Wikimedia\Mime\MimeAnalyzer;
26
32class RepoGroup {
34 protected $localRepo;
35
37 protected $foreignRepos;
38
40 protected $wanCache;
41
43 protected $reposInitialised = false;
44
46 protected $localInfo;
47
49 protected $foreignInfo;
50
52 protected $cache;
53
55 private const MAX_CACHE_SIZE = 500;
56
58 private $mimeAnalyzer;
59
72 public function __construct(
73 $localInfo,
74 $foreignInfo,
75 WANObjectCache $wanCache,
76 MimeAnalyzer $mimeAnalyzer
77 ) {
78 $this->localInfo = $localInfo;
79 $this->foreignInfo = $foreignInfo;
80 $this->cache = new MapCacheLRU( self::MAX_CACHE_SIZE );
81 $this->wanCache = $wanCache;
82 $this->mimeAnalyzer = $mimeAnalyzer;
83 }
84
103 public function findFile( $title, $options = [] ) {
104 if ( !is_array( $options ) ) {
105 // MW 1.15 compat
106 $options = [ 'time' => $options ];
107 }
108 if ( isset( $options['bypassCache'] ) ) {
109 $options['latest'] = $options['bypassCache']; // b/c
110 }
111 if ( isset( $options['time'] ) && $options['time'] !== false ) {
112 $options['time'] = wfTimestamp( TS_MW, $options['time'] );
113 } else {
114 $options['time'] = false;
115 }
116
117 if ( !$this->reposInitialised ) {
118 $this->initialiseRepos();
119 }
120
121 $title = File::normalizeTitle( $title );
122 if ( !$title ) {
123 return false;
124 }
125
126 # Check the cache
127 $dbkey = $title->getDBkey();
128 $timeKey = is_string( $options['time'] ) ? $options['time'] : '';
129 if ( empty( $options['ignoreRedirect'] )
130 && empty( $options['private'] )
131 && empty( $options['latest'] )
132 ) {
133 if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
134 return $this->cache->getField( $dbkey, $timeKey );
135 }
136 $useCache = true;
137 } else {
138 $useCache = false;
139 }
140
141 # Check the local repo
142 $image = $this->localRepo->findFile( $title, $options );
143
144 # Check the foreign repos
145 if ( !$image ) {
146 foreach ( $this->foreignRepos as $repo ) {
147 $image = $repo->findFile( $title, $options );
148 if ( $image ) {
149 break;
150 }
151 }
152 }
153
154 $image = $image instanceof File ? $image : false; // type check
155 # Cache file existence or non-existence
156 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
157 $this->cache->setField( $dbkey, $timeKey, $image );
158 }
159
160 return $image;
161 }
162
180 public function findFiles( array $inputItems, $flags = 0 ) {
181 if ( !$this->reposInitialised ) {
182 $this->initialiseRepos();
183 }
184
185 $items = [];
186 foreach ( $inputItems as $item ) {
187 if ( !is_array( $item ) ) {
188 $item = [ 'title' => $item ];
189 }
190 $item['title'] = File::normalizeTitle( $item['title'] );
191 if ( $item['title'] ) {
192 $items[$item['title']->getDBkey()] = $item;
193 }
194 }
195
196 $images = $this->localRepo->findFiles( $items, $flags );
197
198 foreach ( $this->foreignRepos as $repo ) {
199 // Remove found files from $items
200 $items = array_diff_key( $items, $images );
201 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
202 }
203
204 return $images;
205 }
206
212 public function checkRedirect( $title ) {
213 if ( !$this->reposInitialised ) {
214 $this->initialiseRepos();
215 }
216
217 $title = File::normalizeTitle( $title );
218
219 $redir = $this->localRepo->checkRedirect( $title );
220 if ( $redir ) {
221 return $redir;
222 }
223
224 foreach ( $this->foreignRepos as $repo ) {
225 $redir = $repo->checkRedirect( $title );
226 if ( $redir ) {
227 return $redir;
228 }
229 }
230
231 return false;
232 }
233
242 public function findFileFromKey( $hash, $options = [] ) {
243 if ( !$this->reposInitialised ) {
244 $this->initialiseRepos();
245 }
246
247 $file = $this->localRepo->findFileFromKey( $hash, $options );
248 if ( !$file ) {
249 foreach ( $this->foreignRepos as $repo ) {
250 $file = $repo->findFileFromKey( $hash, $options );
251 if ( $file ) {
252 break;
253 }
254 }
255 }
256
257 return $file;
258 }
259
266 public function findBySha1( $hash ) {
267 if ( !$this->reposInitialised ) {
268 $this->initialiseRepos();
269 }
270
271 $result = $this->localRepo->findBySha1( $hash );
272 foreach ( $this->foreignRepos as $repo ) {
273 $result = array_merge( $result, $repo->findBySha1( $hash ) );
274 }
275 usort( $result, [ File::class, 'compare' ] );
276
277 return $result;
278 }
279
286 public function findBySha1s( array $hashes ) {
287 if ( !$this->reposInitialised ) {
288 $this->initialiseRepos();
289 }
290
291 $result = $this->localRepo->findBySha1s( $hashes );
292 foreach ( $this->foreignRepos as $repo ) {
293 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
294 }
295 // sort the merged (and presorted) sublist of each hash
296 foreach ( $result as $hash => $files ) {
297 usort( $result[$hash], [ File::class, 'compare' ] );
298 }
299
300 return $result;
301 }
302
308 public function getRepo( $index ) {
309 if ( !$this->reposInitialised ) {
310 $this->initialiseRepos();
311 }
312 if ( $index === 'local' ) {
313 return $this->localRepo;
314 }
315 return $this->foreignRepos[$index] ?? false;
316 }
317
323 public function getRepoByName( $name ) {
324 if ( !$this->reposInitialised ) {
325 $this->initialiseRepos();
326 }
327 foreach ( $this->foreignRepos as $repo ) {
328 if ( $repo->name == $name ) {
329 return $repo;
330 }
331 }
332
333 return false;
334 }
335
342 public function getLocalRepo() {
343 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
344 return $this->getRepo( 'local' );
345 }
346
355 public function forEachForeignRepo( $callback, $params = [] ) {
356 if ( !$this->reposInitialised ) {
357 $this->initialiseRepos();
358 }
359 foreach ( $this->foreignRepos as $repo ) {
360 if ( $callback( $repo, ...$params ) ) {
361 return true;
362 }
363 }
364
365 return false;
366 }
367
372 public function hasForeignRepos() {
373 if ( !$this->reposInitialised ) {
374 $this->initialiseRepos();
375 }
376 return (bool)$this->foreignRepos;
377 }
378
382 public function initialiseRepos() {
383 if ( $this->reposInitialised ) {
384 return;
385 }
386 $this->reposInitialised = true;
387
388 $this->localRepo = $this->newRepo( $this->localInfo );
389 $this->foreignRepos = [];
390 foreach ( $this->foreignInfo as $key => $info ) {
391 $this->foreignRepos[$key] = $this->newRepo( $info );
392 }
393 }
394
401 public function newCustomLocalRepo( $info = [] ) {
402 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
403 return $this->newRepo( $info + $this->localInfo );
404 }
405
411 protected function newRepo( $info ) {
412 $class = $info['class'];
413
414 $info['wanCache'] = $this->wanCache;
415
416 return new $class( $info );
417 }
418
424 private function splitVirtualUrl( $url ) {
425 if ( !str_starts_with( $url, 'mwrepo://' ) ) {
426 throw new InvalidArgumentException( __METHOD__ . ': unknown protocol' );
427 }
428
429 $bits = explode( '/', substr( $url, 9 ), 3 );
430 if ( count( $bits ) != 3 ) {
431 throw new InvalidArgumentException( __METHOD__ . ": invalid mwrepo URL: $url" );
432 }
433
434 return $bits;
435 }
436
441 public function getFileProps( $fileName ) {
442 if ( FileRepo::isVirtualUrl( $fileName ) ) {
443 [ $repoName, /* $zone */, /* $rel */ ] = $this->splitVirtualUrl( $fileName );
444 if ( $repoName === '' ) {
445 $repoName = 'local';
446 }
447 $repo = $this->getRepo( $repoName );
448
449 return $repo->getFileProps( $fileName );
450 } else {
451 $mwProps = new MWFileProps( $this->mimeAnalyzer );
452
453 return $mwProps->getPropsFromPath( $fileName, true );
454 }
455 }
456
461 public function clearCache( $title = null ) {
462 if ( $title == null ) {
463 $this->cache->clear();
464 } elseif ( is_string( $title ) ) {
465 $this->cache->clear( $title );
466 } else {
467 $this->cache->clear( $title->getDBkey() );
468 }
469 }
470}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
Base class for file repositories.
Definition FileRepo.php:57
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition FileRepo.php:293
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:49
MimeMagic helper wrapper.
Store key-value entries in a size-limited in-memory LRU cache.
Represents a title within MediaWiki.
Definition Title.php:78
Prioritized list of file repositories.
Definition RepoGroup.php:32
WANObjectCache $wanCache
Definition RepoGroup.php:40
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.
array $foreignInfo
Definition RepoGroup.php:49
__construct( $localInfo, $foreignInfo, WANObjectCache $wanCache, MimeAnalyzer $mimeAnalyzer)
Construct a group of file repositories.
Definition RepoGroup.php:72
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...
bool $reposInitialised
Definition RepoGroup.php:43
MapCacheLRU $cache
Definition RepoGroup.php:52
findFile( $title, $options=[])
Search repositories for an image.
getLocalRepo()
Get the local repository, i.e.
FileRepo[] $foreignRepos
Definition RepoGroup.php:37
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.
LocalRepo $localRepo
Definition RepoGroup.php:34
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.
array $localInfo
Definition RepoGroup.php:46
getFileProps( $fileName)
forEachForeignRepo( $callback, $params=[])
Call a function for each foreign repo, with the repo object as the first parameter.
Multi-datacenter aware caching interface.
Represents the target of a wiki link.
Interface for objects (potentially) representing an editable wiki page.