MediaWiki  master
RepoGroup.php
Go to the documentation of this file.
1 <?php
27 
33 class RepoGroup {
35  protected $localRepo;
36 
38  protected $foreignRepos;
39 
41  protected $wanCache;
42 
44  protected $reposInitialised = false;
45 
47  protected $localInfo;
48 
50  protected $foreignInfo;
51 
53  protected $cache;
54 
56  private const MAX_CACHE_SIZE = 500;
57 
64  public static function singleton() {
65  wfDeprecated( __METHOD__, '1.34' );
66  return MediaWikiServices::getInstance()->getRepoGroup();
67  }
68 
74  public static function destroySingleton() {
75  wfDeprecated( __METHOD__, '1.34' );
76  MediaWikiServices::getInstance()->resetServiceForTesting( 'RepoGroup' );
77  }
78 
85  public static function setSingleton( $instance ) {
86  wfDeprecated( __METHOD__, '1.34' );
87  $services = MediaWikiServices::getInstance();
88  $services->disableService( 'RepoGroup' );
89  $services->redefineService( 'RepoGroup',
90  static function () use ( $instance ) {
91  return $instance;
92  }
93  );
94  }
95 
108  $this->localInfo = $localInfo;
109  $this->foreignInfo = $foreignInfo;
110  $this->cache = new MapCacheLRU( self::MAX_CACHE_SIZE );
111  $this->wanCache = $wanCache;
112  }
113 
132  public function findFile( $title, $options = [] ) {
133  if ( !is_array( $options ) ) {
134  // MW 1.15 compat
135  $options = [ 'time' => $options ];
136  }
137  if ( isset( $options['bypassCache'] ) ) {
138  $options['latest'] = $options['bypassCache']; // b/c
139  }
140  $options += [ 'time' => false ];
141 
142  if ( !$this->reposInitialised ) {
143  $this->initialiseRepos();
144  }
145 
147  if ( !$title ) {
148  return false;
149  }
150 
151  # Check the cache
152  $dbkey = $title->getDBkey();
153  $timeKey = is_string( $options['time'] ) ? $options['time'] : '';
154  if ( empty( $options['ignoreRedirect'] )
155  && empty( $options['private'] )
156  && empty( $options['latest'] )
157  ) {
158  if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
159  return $this->cache->getField( $dbkey, $timeKey );
160  }
161  $useCache = true;
162  } else {
163  $useCache = false;
164  }
165 
166  # Check the local repo
167  $image = $this->localRepo->findFile( $title, $options );
168 
169  # Check the foreign repos
170  if ( !$image ) {
171  foreach ( $this->foreignRepos as $repo ) {
172  $image = $repo->findFile( $title, $options );
173  if ( $image ) {
174  break;
175  }
176  }
177  }
178 
179  $image = $image instanceof File ? $image : false; // type sanity
180  # Cache file existence or non-existence
181  if ( $useCache && ( !$image || $image->isCacheable() ) ) {
182  $this->cache->setField( $dbkey, $timeKey, $image );
183  }
184 
185  return $image;
186  }
187 
205  public function findFiles( array $inputItems, $flags = 0 ) {
206  if ( !$this->reposInitialised ) {
207  $this->initialiseRepos();
208  }
209 
210  $items = [];
211  foreach ( $inputItems as $item ) {
212  if ( !is_array( $item ) ) {
213  $item = [ 'title' => $item ];
214  }
215  $item['title'] = File::normalizeTitle( $item['title'] );
216  if ( $item['title'] ) {
217  $items[$item['title']->getDBkey()] = $item;
218  }
219  }
220 
221  $images = $this->localRepo->findFiles( $items, $flags );
222 
223  foreach ( $this->foreignRepos as $repo ) {
224  // Remove found files from $items
225  foreach ( $images as $name => $image ) {
226  unset( $items[$name] );
227  }
228 
229  $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
230  }
231 
232  return $images;
233  }
234 
240  public function checkRedirect( $title ) {
241  if ( !$this->reposInitialised ) {
242  $this->initialiseRepos();
243  }
244 
246 
247  $redir = $this->localRepo->checkRedirect( $title );
248  if ( $redir ) {
249  return $redir;
250  }
251 
252  foreach ( $this->foreignRepos as $repo ) {
253  $redir = $repo->checkRedirect( $title );
254  if ( $redir ) {
255  return $redir;
256  }
257  }
258 
259  return false;
260  }
261 
270  public function findFileFromKey( $hash, $options = [] ) {
271  if ( !$this->reposInitialised ) {
272  $this->initialiseRepos();
273  }
274 
275  $file = $this->localRepo->findFileFromKey( $hash, $options );
276  if ( !$file ) {
277  foreach ( $this->foreignRepos as $repo ) {
278  $file = $repo->findFileFromKey( $hash, $options );
279  if ( $file ) {
280  break;
281  }
282  }
283  }
284 
285  return $file;
286  }
287 
294  public function findBySha1( $hash ) {
295  if ( !$this->reposInitialised ) {
296  $this->initialiseRepos();
297  }
298 
299  $result = $this->localRepo->findBySha1( $hash );
300  foreach ( $this->foreignRepos as $repo ) {
301  $result = array_merge( $result, $repo->findBySha1( $hash ) );
302  }
303  usort( $result, [ File::class, 'compare' ] );
304 
305  return $result;
306  }
307 
314  public function findBySha1s( array $hashes ) {
315  if ( !$this->reposInitialised ) {
316  $this->initialiseRepos();
317  }
318 
319  $result = $this->localRepo->findBySha1s( $hashes );
320  foreach ( $this->foreignRepos as $repo ) {
321  $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
322  }
323  // sort the merged (and presorted) sublist of each hash
324  foreach ( $result as $hash => $files ) {
325  usort( $result[$hash], [ File::class, 'compare' ] );
326  }
327 
328  return $result;
329  }
330 
336  public function getRepo( $index ) {
337  if ( !$this->reposInitialised ) {
338  $this->initialiseRepos();
339  }
340  if ( $index === 'local' ) {
341  return $this->localRepo;
342  }
343  return $this->foreignRepos[$index] ?? false;
344  }
345 
351  public function getRepoByName( $name ) {
352  if ( !$this->reposInitialised ) {
353  $this->initialiseRepos();
354  }
355  foreach ( $this->foreignRepos as $repo ) {
356  if ( $repo->name == $name ) {
357  return $repo;
358  }
359  }
360 
361  return false;
362  }
363 
370  public function getLocalRepo() {
372  $repo = $this->getRepo( 'local' );
373 
374  return $repo;
375  }
376 
385  public function forEachForeignRepo( $callback, $params = [] ) {
386  if ( !$this->reposInitialised ) {
387  $this->initialiseRepos();
388  }
389  foreach ( $this->foreignRepos as $repo ) {
390  if ( $callback( $repo, ...$params ) ) {
391  return true;
392  }
393  }
394 
395  return false;
396  }
397 
402  public function hasForeignRepos() {
403  if ( !$this->reposInitialised ) {
404  $this->initialiseRepos();
405  }
406  return (bool)$this->foreignRepos;
407  }
408 
412  public function initialiseRepos() {
413  if ( $this->reposInitialised ) {
414  return;
415  }
416  $this->reposInitialised = true;
417 
418  $this->localRepo = $this->newRepo( $this->localInfo );
419  $this->foreignRepos = [];
420  foreach ( $this->foreignInfo as $key => $info ) {
421  $this->foreignRepos[$key] = $this->newRepo( $info );
422  }
423  }
424 
430  protected function newRepo( $info ) {
431  $class = $info['class'];
432 
433  $info['wanCache'] = $this->wanCache;
434 
435  return new $class( $info );
436  }
437 
444  private function splitVirtualUrl( $url ) {
445  if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
446  throw new MWException( __METHOD__ . ': unknown protocol' );
447  }
448 
449  $bits = explode( '/', substr( $url, 9 ), 3 );
450  if ( count( $bits ) != 3 ) {
451  throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
452  }
453 
454  return $bits;
455  }
456 
461  public function getFileProps( $fileName ) {
462  if ( FileRepo::isVirtualUrl( $fileName ) ) {
463  list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
464  if ( $repoName === '' ) {
465  $repoName = 'local';
466  }
467  $repo = $this->getRepo( $repoName );
468 
469  return $repo->getFileProps( $fileName );
470  } else {
471  $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
472 
473  return $mwProps->getPropsFromPath( $fileName, true );
474  }
475  }
476 
481  public function clearCache( $title = null ) {
482  if ( $title == null ) {
483  $this->cache->clear();
484  } elseif ( is_string( $title ) ) {
485  $this->cache->clear( $title );
486  } else {
487  $this->cache->clear( $title->getDBkey() );
488  }
489  }
490 }
RepoGroup\findFiles
findFiles(array $inputItems, $flags=0)
Search repositories for many files at once.
Definition: RepoGroup.php:205
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
RepoGroup\hasForeignRepos
hasForeignRepos()
Does the installation have any foreign repos set up?
Definition: RepoGroup.php:402
RepoGroup\$cache
MapCacheLRU $cache
Definition: RepoGroup.php:53
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:64
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:180
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
RepoGroup\$localRepo
LocalRepo $localRepo
Definition: RepoGroup.php:35
File\normalizeTitle
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
Definition: File.php:205
RepoGroup\findBySha1s
findBySha1s(array $hashes)
Find all instances of files with this keys.
Definition: RepoGroup.php:314
RepoGroup\findFile
findFile( $title, $options=[])
Search repositories for an image.
Definition: RepoGroup.php:132
FileRepo
Base class for file repositories.
Definition: FileRepo.php:45
RepoGroup\$reposInitialised
bool $reposInitialised
Definition: RepoGroup.php:44
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:66
MWException
MediaWiki exception.
Definition: MWException.php:29
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:996
RepoGroup\forEachForeignRepo
forEachForeignRepo( $callback, $params=[])
Call a function for each foreign repo, with the repo object as the first parameter.
Definition: RepoGroup.php:385
RepoGroup\clearCache
clearCache( $title=null)
Clear RepoGroup process cache used for finding a file.
Definition: RepoGroup.php:481
RepoGroup\$foreignInfo
array $foreignInfo
Definition: RepoGroup.php:50
RepoGroup\MAX_CACHE_SIZE
const MAX_CACHE_SIZE
Maximum number of cache items.
Definition: RepoGroup.php:56
MediaWiki
A helper class for throttling authentication attempts.
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:37
$title
$title
Definition: testCompression.php:38
MWFileProps
MimeMagic helper wrapper.
Definition: MWFileProps.php:28
RepoGroup\splitVirtualUrl
splitVirtualUrl( $url)
Split a virtual URL into repo, zone and rel parts.
Definition: RepoGroup.php:444
RepoGroup\initialiseRepos
initialiseRepos()
Initialise the $repos array.
Definition: RepoGroup.php:412
RepoGroup\$wanCache
WANObjectCache $wanCache
Definition: RepoGroup.php:41
RepoGroup\newRepo
newRepo( $info)
Create a repo class based on an info structure.
Definition: RepoGroup.php:430
RepoGroup\__construct
__construct( $localInfo, $foreignInfo, $wanCache)
Construct a group of file repositories.
Definition: RepoGroup.php:107
RepoGroup\destroySingleton
static destroySingleton()
Definition: RepoGroup.php:74
RepoGroup\checkRedirect
checkRedirect( $title)
Interface for FileRepo::checkRedirect()
Definition: RepoGroup.php:240
RepoGroup\getLocalRepo
getLocalRepo()
Get the local repository, i.e.
Definition: RepoGroup.php:370
RepoGroup\$localInfo
array $localInfo
Definition: RepoGroup.php:47
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:129
RepoGroup\findFileFromKey
findFileFromKey( $hash, $options=[])
Find an instance of the file with this key, created at the specified time Returns false if the file d...
Definition: RepoGroup.php:270
RepoGroup\findBySha1
findBySha1( $hash)
Find all instances of files with this key.
Definition: RepoGroup.php:294
RepoGroup\$foreignRepos
FileRepo[] $foreignRepos
Definition: RepoGroup.php:38
FileRepo\isVirtualUrl
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:290
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:33
RepoGroup\getRepoByName
getRepoByName( $name)
Get the repo instance by its name.
Definition: RepoGroup.php:351
RepoGroup\getFileProps
getFileProps( $fileName)
Definition: RepoGroup.php:461
$hashes
$hashes
Definition: testCompression.php:71
RepoGroup\getRepo
getRepo( $index)
Get the repo instance with a given key.
Definition: RepoGroup.php:336
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:40
RepoGroup\setSingleton
static setSingleton( $instance)
Definition: RepoGroup.php:85