MediaWiki  master
RepoGroup.php
Go to the documentation of this file.
1 <?php
23 
29 class RepoGroup {
31  protected $localRepo;
32 
34  protected $foreignRepos;
35 
37  protected $wanCache;
38 
40  protected $reposInitialised = false;
41 
43  protected $localInfo;
44 
46  protected $foreignInfo;
47 
49  protected $cache;
50 
52  private const MAX_CACHE_SIZE = 500;
53 
55  private $mimeAnalyzer;
56 
69  public function __construct(
70  $localInfo,
74  ) {
75  $this->localInfo = $localInfo;
76  $this->foreignInfo = $foreignInfo;
77  $this->cache = new MapCacheLRU( self::MAX_CACHE_SIZE );
78  $this->wanCache = $wanCache;
79  $this->mimeAnalyzer = $mimeAnalyzer;
80  }
81 
100  public function findFile( $title, $options = [] ) {
101  if ( !is_array( $options ) ) {
102  // MW 1.15 compat
103  $options = [ 'time' => $options ];
104  }
105  if ( isset( $options['bypassCache'] ) ) {
106  $options['latest'] = $options['bypassCache']; // b/c
107  }
108  $options += [ 'time' => false ];
109 
110  if ( !$this->reposInitialised ) {
111  $this->initialiseRepos();
112  }
113 
115  if ( !$title ) {
116  return false;
117  }
118 
119  # Check the cache
120  $dbkey = $title->getDBkey();
121  // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
122  $timeKey = is_string( $options['time'] ) ? $options['time'] : '';
123  if ( empty( $options['ignoreRedirect'] )
124  && empty( $options['private'] )
125  && empty( $options['latest'] )
126  ) {
127  if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
128  return $this->cache->getField( $dbkey, $timeKey );
129  }
130  $useCache = true;
131  } else {
132  $useCache = false;
133  }
134 
135  # Check the local repo
136  $image = $this->localRepo->findFile( $title, $options );
137 
138  # Check the foreign repos
139  if ( !$image ) {
140  foreach ( $this->foreignRepos as $repo ) {
141  $image = $repo->findFile( $title, $options );
142  if ( $image ) {
143  break;
144  }
145  }
146  }
147 
148  $image = $image instanceof File ? $image : false; // type check
149  # Cache file existence or non-existence
150  if ( $useCache && ( !$image || $image->isCacheable() ) ) {
151  $this->cache->setField( $dbkey, $timeKey, $image );
152  }
153 
154  return $image;
155  }
156 
174  public function findFiles( array $inputItems, $flags = 0 ) {
175  if ( !$this->reposInitialised ) {
176  $this->initialiseRepos();
177  }
178 
179  $items = [];
180  foreach ( $inputItems as $item ) {
181  if ( !is_array( $item ) ) {
182  $item = [ 'title' => $item ];
183  }
184  $item['title'] = File::normalizeTitle( $item['title'] );
185  if ( $item['title'] ) {
186  $items[$item['title']->getDBkey()] = $item;
187  }
188  }
189 
190  $images = $this->localRepo->findFiles( $items, $flags );
191 
192  foreach ( $this->foreignRepos as $repo ) {
193  // Remove found files from $items
194  foreach ( $images as $name => $image ) {
195  unset( $items[$name] );
196  }
197 
198  $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
199  }
200 
201  return $images;
202  }
203 
209  public function checkRedirect( $title ) {
210  if ( !$this->reposInitialised ) {
211  $this->initialiseRepos();
212  }
213 
215 
216  $redir = $this->localRepo->checkRedirect( $title );
217  if ( $redir ) {
218  return $redir;
219  }
220 
221  foreach ( $this->foreignRepos as $repo ) {
222  $redir = $repo->checkRedirect( $title );
223  if ( $redir ) {
224  return $redir;
225  }
226  }
227 
228  return false;
229  }
230 
239  public function findFileFromKey( $hash, $options = [] ) {
240  if ( !$this->reposInitialised ) {
241  $this->initialiseRepos();
242  }
243 
244  $file = $this->localRepo->findFileFromKey( $hash, $options );
245  if ( !$file ) {
246  foreach ( $this->foreignRepos as $repo ) {
247  $file = $repo->findFileFromKey( $hash, $options );
248  if ( $file ) {
249  break;
250  }
251  }
252  }
253 
254  return $file;
255  }
256 
263  public function findBySha1( $hash ) {
264  if ( !$this->reposInitialised ) {
265  $this->initialiseRepos();
266  }
267 
268  $result = $this->localRepo->findBySha1( $hash );
269  foreach ( $this->foreignRepos as $repo ) {
270  $result = array_merge( $result, $repo->findBySha1( $hash ) );
271  }
272  usort( $result, [ File::class, 'compare' ] );
273 
274  return $result;
275  }
276 
283  public function findBySha1s( array $hashes ) {
284  if ( !$this->reposInitialised ) {
285  $this->initialiseRepos();
286  }
287 
288  $result = $this->localRepo->findBySha1s( $hashes );
289  foreach ( $this->foreignRepos as $repo ) {
290  $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
291  }
292  // sort the merged (and presorted) sublist of each hash
293  foreach ( $result as $hash => $files ) {
294  usort( $result[$hash], [ File::class, 'compare' ] );
295  }
296 
297  return $result;
298  }
299 
305  public function getRepo( $index ) {
306  if ( !$this->reposInitialised ) {
307  $this->initialiseRepos();
308  }
309  if ( $index === 'local' ) {
310  return $this->localRepo;
311  }
312  return $this->foreignRepos[$index] ?? false;
313  }
314 
320  public function getRepoByName( $name ) {
321  if ( !$this->reposInitialised ) {
322  $this->initialiseRepos();
323  }
324  foreach ( $this->foreignRepos as $repo ) {
325  if ( $repo->name == $name ) {
326  return $repo;
327  }
328  }
329 
330  return false;
331  }
332 
339  public function getLocalRepo() {
340  // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
341  return $this->getRepo( 'local' );
342  }
343 
352  public function forEachForeignRepo( $callback, $params = [] ) {
353  if ( !$this->reposInitialised ) {
354  $this->initialiseRepos();
355  }
356  foreach ( $this->foreignRepos as $repo ) {
357  if ( $callback( $repo, ...$params ) ) {
358  return true;
359  }
360  }
361 
362  return false;
363  }
364 
369  public function hasForeignRepos() {
370  if ( !$this->reposInitialised ) {
371  $this->initialiseRepos();
372  }
373  return (bool)$this->foreignRepos;
374  }
375 
379  public function initialiseRepos() {
380  if ( $this->reposInitialised ) {
381  return;
382  }
383  $this->reposInitialised = true;
384 
385  $this->localRepo = $this->newRepo( $this->localInfo );
386  $this->foreignRepos = [];
387  foreach ( $this->foreignInfo as $key => $info ) {
388  $this->foreignRepos[$key] = $this->newRepo( $info );
389  }
390  }
391 
398  public function newCustomLocalRepo( $info = [] ) {
399  // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
400  return $this->newRepo( $info + $this->localInfo );
401  }
402 
408  protected function newRepo( $info ) {
409  $class = $info['class'];
410 
411  $info['wanCache'] = $this->wanCache;
412 
413  return new $class( $info );
414  }
415 
422  private function splitVirtualUrl( $url ) {
423  if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
424  throw new MWException( __METHOD__ . ': unknown protocol' );
425  }
426 
427  $bits = explode( '/', substr( $url, 9 ), 3 );
428  if ( count( $bits ) != 3 ) {
429  throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
430  }
431 
432  return $bits;
433  }
434 
439  public function getFileProps( $fileName ) {
440  if ( FileRepo::isVirtualUrl( $fileName ) ) {
441  list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
442  if ( $repoName === '' ) {
443  $repoName = 'local';
444  }
445  $repo = $this->getRepo( $repoName );
446 
447  return $repo->getFileProps( $fileName );
448  } else {
449  $mwProps = new MWFileProps( $this->mimeAnalyzer );
450 
451  return $mwProps->getPropsFromPath( $fileName, true );
452  }
453  }
454 
459  public function clearCache( $title = null ) {
460  if ( $title == null ) {
461  $this->cache->clear();
462  } elseif ( is_string( $title ) ) {
463  $this->cache->clear( $title );
464  } else {
465  $this->cache->clear( $title->getDBkey() );
466  }
467  }
468 }
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:288
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:68
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:207
MediaWiki exception.
Definition: MWException.php:29
MimeMagic helper wrapper.
Definition: MWFileProps.php:28
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:36
Implements functions related to MIME types such as detection and mapping to file extension.
Prioritized list of file repositories.
Definition: RepoGroup.php:29
WANObjectCache $wanCache
Definition: RepoGroup.php:37
newCustomLocalRepo( $info=[])
Create a local repo with the specified option overrides.
Definition: RepoGroup.php:398
initialiseRepos()
Initialise the $repos array.
Definition: RepoGroup.php:379
checkRedirect( $title)
Interface for FileRepo::checkRedirect()
Definition: RepoGroup.php:209
getRepoByName( $name)
Get the repo instance by its name.
Definition: RepoGroup.php:320
array $foreignInfo
Definition: RepoGroup.php:46
__construct( $localInfo, $foreignInfo, WANObjectCache $wanCache, MimeAnalyzer $mimeAnalyzer)
Construct a group of file repositories.
Definition: RepoGroup.php:69
splitVirtualUrl( $url)
Split a virtual URL into repo, zone and rel parts.
Definition: RepoGroup.php:422
const MAX_CACHE_SIZE
Maximum number of cache items.
Definition: RepoGroup.php:52
hasForeignRepos()
Does the installation have any foreign repos set up?
Definition: RepoGroup.php:369
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:239
bool $reposInitialised
Definition: RepoGroup.php:40
MapCacheLRU $cache
Definition: RepoGroup.php:49
findFile( $title, $options=[])
Search repositories for an image.
Definition: RepoGroup.php:100
getLocalRepo()
Get the local repository, i.e.
Definition: RepoGroup.php:339
FileRepo[] $foreignRepos
Definition: RepoGroup.php:34
findBySha1s(array $hashes)
Find all instances of files with this keys.
Definition: RepoGroup.php:283
clearCache( $title=null)
Clear RepoGroup process cache used for finding a file.
Definition: RepoGroup.php:459
getRepo( $index)
Get the repo instance with a given key.
Definition: RepoGroup.php:305
LocalRepo $localRepo
Definition: RepoGroup.php:31
findBySha1( $hash)
Find all instances of files with this key.
Definition: RepoGroup.php:263
findFiles(array $inputItems, $flags=0)
Search repositories for many files at once.
Definition: RepoGroup.php:174
newRepo( $info)
Create a repo class based on an info structure.
Definition: RepoGroup.php:408
MimeAnalyzer $mimeAnalyzer
Definition: RepoGroup.php:55
array $localInfo
Definition: RepoGroup.php:43
getFileProps( $fileName)
Definition: RepoGroup.php:439
forEachForeignRepo( $callback, $params=[])
Call a function for each foreign repo, with the repo object as the first parameter.
Definition: RepoGroup.php:352
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.
Definition: router.php:42