MediaWiki  1.23.12
RepoGroup.php
Go to the documentation of this file.
1 <?php
29 class RepoGroup {
31  protected $localRepo;
32 
34  protected $foreignRepos;
35 
37  protected $reposInitialised = false;
38 
40  protected $localInfo;
41 
43  protected $foreignInfo;
44 
46  protected $cache;
47 
49  protected static $instance;
50 
52  const MAX_CACHE_SIZE = 500;
53 
59  static function singleton() {
60  if ( self::$instance ) {
61  return self::$instance;
62  }
63  global $wgLocalFileRepo, $wgForeignFileRepos;
64  self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
65 
66  return self::$instance;
67  }
68 
73  static function destroySingleton() {
74  self::$instance = null;
75  }
76 
85  static function setSingleton( $instance ) {
86  self::$instance = $instance;
87  }
88 
98  function __construct( $localInfo, $foreignInfo ) {
99  $this->localInfo = $localInfo;
100  $this->foreignInfo = $foreignInfo;
101  $this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
102  }
103 
120  function findFile( $title, $options = array() ) {
121  if ( !is_array( $options ) ) {
122  // MW 1.15 compat
123  $options = array( 'time' => $options );
124  }
125  if ( !$this->reposInitialised ) {
126  $this->initialiseRepos();
127  }
129  if ( !$title ) {
130  return false;
131  }
132 
133  # Check the cache
134  if ( empty( $options['ignoreRedirect'] )
135  && empty( $options['private'] )
136  && empty( $options['bypassCache'] )
137  ) {
138  $time = isset( $options['time'] ) ? $options['time'] : '';
139  $dbkey = $title->getDBkey();
140  if ( $this->cache->has( $dbkey, $time, 60 ) ) {
141  return $this->cache->get( $dbkey, $time );
142  }
143  $useCache = true;
144  } else {
145  $useCache = false;
146  }
147 
148  # Check the local repo
149  $image = $this->localRepo->findFile( $title, $options );
150 
151  # Check the foreign repos
152  if ( !$image ) {
153  foreach ( $this->foreignRepos as $repo ) {
154  $image = $repo->findFile( $title, $options );
155  if ( $image ) {
156  break;
157  }
158  }
159  }
160 
161  $image = $image ? $image : false; // type sanity
162  # Cache file existence or non-existence
163  if ( $useCache && ( !$image || $image->isCacheable() ) ) {
164  $this->cache->set( $dbkey, $time, $image );
165  }
166 
167  return $image;
168  }
169 
191  function findFiles( array $inputItems, $flags = 0 ) {
192  if ( !$this->reposInitialised ) {
193  $this->initialiseRepos();
194  }
195 
196  $items = array();
197  foreach ( $inputItems as $item ) {
198  if ( !is_array( $item ) ) {
199  $item = array( 'title' => $item );
200  }
201  $item['title'] = File::normalizeTitle( $item['title'] );
202  if ( $item['title'] ) {
203  $items[$item['title']->getDBkey()] = $item;
204  }
205  }
206 
207  $images = $this->localRepo->findFiles( $items, $flags );
208 
209  foreach ( $this->foreignRepos as $repo ) {
210  // Remove found files from $items
211  foreach ( $images as $name => $image ) {
212  unset( $items[$name] );
213  }
214 
215  $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
216  }
217 
218  return $images;
219  }
220 
226  function checkRedirect( Title $title ) {
227  if ( !$this->reposInitialised ) {
228  $this->initialiseRepos();
229  }
230 
231  $redir = $this->localRepo->checkRedirect( $title );
232  if ( $redir ) {
233  return $redir;
234  }
235 
236  foreach ( $this->foreignRepos as $repo ) {
237  $redir = $repo->checkRedirect( $title );
238  if ( $redir ) {
239  return $redir;
240  }
241  }
242 
243  return false;
244  }
245 
254  function findFileFromKey( $hash, $options = array() ) {
255  if ( !$this->reposInitialised ) {
256  $this->initialiseRepos();
257  }
258 
259  $file = $this->localRepo->findFileFromKey( $hash, $options );
260  if ( !$file ) {
261  foreach ( $this->foreignRepos as $repo ) {
262  $file = $repo->findFileFromKey( $hash, $options );
263  if ( $file ) {
264  break;
265  }
266  }
267  }
268 
269  return $file;
270  }
271 
278  function findBySha1( $hash ) {
279  if ( !$this->reposInitialised ) {
280  $this->initialiseRepos();
281  }
282 
283  $result = $this->localRepo->findBySha1( $hash );
284  foreach ( $this->foreignRepos as $repo ) {
285  $result = array_merge( $result, $repo->findBySha1( $hash ) );
286  }
287  usort( $result, 'File::compare' );
288 
289  return $result;
290  }
291 
298  function findBySha1s( array $hashes ) {
299  if ( !$this->reposInitialised ) {
300  $this->initialiseRepos();
301  }
302 
303  $result = $this->localRepo->findBySha1s( $hashes );
304  foreach ( $this->foreignRepos as $repo ) {
305  $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
306  }
307  //sort the merged (and presorted) sublist of each hash
308  foreach ( $result as $hash => $files ) {
309  usort( $result[$hash], 'File::compare' );
310  }
311 
312  return $result;
313  }
314 
320  function getRepo( $index ) {
321  if ( !$this->reposInitialised ) {
322  $this->initialiseRepos();
323  }
324  if ( $index === 'local' ) {
325  return $this->localRepo;
326  } elseif ( isset( $this->foreignRepos[$index] ) ) {
327  return $this->foreignRepos[$index];
328  } else {
329  return false;
330  }
331  }
332 
338  function getRepoByName( $name ) {
339  if ( !$this->reposInitialised ) {
340  $this->initialiseRepos();
341  }
342  foreach ( $this->foreignRepos as $repo ) {
343  if ( $repo->name == $name ) {
344  return $repo;
345  }
346  }
347 
348  return false;
349  }
350 
357  function getLocalRepo() {
358  return $this->getRepo( 'local' );
359  }
360 
369  function forEachForeignRepo( $callback, $params = array() ) {
370  foreach ( $this->foreignRepos as $repo ) {
371  $args = array_merge( array( $repo ), $params );
372  if ( call_user_func_array( $callback, $args ) ) {
373  return true;
374  }
375  }
376 
377  return false;
378  }
379 
384  function hasForeignRepos() {
385  return (bool)$this->foreignRepos;
386  }
387 
391  function initialiseRepos() {
392  if ( $this->reposInitialised ) {
393  return;
394  }
395  $this->reposInitialised = true;
396 
397  $this->localRepo = $this->newRepo( $this->localInfo );
398  $this->foreignRepos = array();
399  foreach ( $this->foreignInfo as $key => $info ) {
400  $this->foreignRepos[$key] = $this->newRepo( $info );
401  }
402  }
403 
407  protected function newRepo( $info ) {
408  $class = $info['class'];
409 
410  return new $class( $info );
411  }
412 
419  function splitVirtualUrl( $url ) {
420  if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
421  throw new MWException( __METHOD__ . ': unknown protocol' );
422  }
423 
424  $bits = explode( '/', substr( $url, 9 ), 3 );
425  if ( count( $bits ) != 3 ) {
426  throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
427  }
428 
429  return $bits;
430  }
431 
436  function getFileProps( $fileName ) {
437  if ( FileRepo::isVirtualUrl( $fileName ) ) {
438  list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
439  if ( $repoName === '' ) {
440  $repoName = 'local';
441  }
442  $repo = $this->getRepo( $repoName );
443 
444  return $repo->getFileProps( $fileName );
445  } else {
446  return FSFile::getPropsFromPath( $fileName );
447  }
448  }
449 
454  public function clearCache( Title $title = null ) {
455  if ( $title == null ) {
456  $this->cache->clear();
457  } else {
458  $this->cache->clear( $title->getDBkey() );
459  }
460  }
461 }
RepoGroup\findFiles
findFiles(array $inputItems, $flags=0)
Search repositories for many files at once.
Definition: RepoGroup.php:185
RepoGroup\hasForeignRepos
hasForeignRepos()
Does the installation have any foreign repos set up?
Definition: RepoGroup.php:378
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$files
$files
Definition: importImages.php:67
FSFile\getPropsFromPath
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition: FSFile.php:243
$time
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1358
$params
$params
Definition: styleTest.css.php:40
RepoGroup\findFile
findFile( $title, $options=array())
Search repositories for an image.
Definition: RepoGroup.php:114
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2118
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
RepoGroup\__construct
__construct( $localInfo, $foreignInfo)
Construct a group of file repositories.
Definition: RepoGroup.php:92
RepoGroup\$localRepo
LocalRepo $localRepo
Definition: RepoGroup.php:30
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:161
RepoGroup\findBySha1s
findBySha1s(array $hashes)
Find all instances of files with this keys.
Definition: RepoGroup.php:292
FileRepo
Base class for file repositories.
Definition: FileRepo.php:37
RepoGroup\$reposInitialised
bool $reposInitialised
Definition: RepoGroup.php:34
MWException
MediaWiki exception.
Definition: MWException.php:26
RepoGroup\clearCache
clearCache(Title $title=null)
Clear RepoGroup process cache used for finding a file.
Definition: RepoGroup.php:448
RepoGroup\$foreignInfo
array $foreignInfo
Definition: RepoGroup.php:38
RepoGroup\MAX_CACHE_SIZE
const MAX_CACHE_SIZE
Maximum number of cache items.
Definition: RepoGroup.php:46
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
RepoGroup\splitVirtualUrl
splitVirtualUrl( $url)
Split a virtual URL into repo, zone and rel parts.
Definition: RepoGroup.php:413
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
RepoGroup\initialiseRepos
initialiseRepos()
Initialise the $repos array.
Definition: RepoGroup.php:385
RepoGroup\newRepo
newRepo( $info)
Create a repo class based on an info structure.
Definition: RepoGroup.php:401
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
RepoGroup\$cache
ProcessCacheLRU $cache
Definition: RepoGroup.php:40
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
RepoGroup\destroySingleton
static destroySingleton()
Destroy the singleton instance, so that a new one will be created next time singleton() is called.
Definition: RepoGroup.php:67
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
RepoGroup\getLocalRepo
getLocalRepo()
Get the local repository, i.e.
Definition: RepoGroup.php:351
RepoGroup\$localInfo
array $localInfo
Definition: RepoGroup.php:36
RepoGroup\findFileFromKey
findFileFromKey( $hash, $options=array())
Find an instance of the file with this key, created at the specified time Returns false if the file d...
Definition: RepoGroup.php:248
$hash
return false to override stock group addition can be modified try getUserPermissionsErrors userCan checks are continued by internal code can override on output return false to not delete it return false to override the default password checks & $hash
Definition: hooks.txt:2702
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
$args
if( $line===false) $args
Definition: cdb.php:62
Title
Represents a title within MediaWiki.
Definition: Title.php:35
RepoGroup\findBySha1
findBySha1( $hash)
Find all instances of files with this key.
Definition: RepoGroup.php:272
RepoGroup\$foreignRepos
FileRepo[] $foreignRepos
Definition: RepoGroup.php:32
RepoGroup\forEachForeignRepo
forEachForeignRepo( $callback, $params=array())
Call a function for each foreign repo, with the repo object as the first parameter.
Definition: RepoGroup.php:363
RepoGroup\$instance
static $instance
Definition: RepoGroup.php:43
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
FileRepo\isVirtualUrl
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
Definition: FileRepo.php:229
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:29
RepoGroup\getRepoByName
getRepoByName( $name)
Get the repo instance by its name.
Definition: RepoGroup.php:332
RepoGroup\getFileProps
getFileProps( $fileName)
Definition: RepoGroup.php:430
$hashes
$hashes
Definition: testCompression.php:62
RepoGroup\checkRedirect
checkRedirect(Title $title)
Interface for FileRepo::checkRedirect()
Definition: RepoGroup.php:220
RepoGroup\getRepo
getRepo( $index)
Get the repo instance with a given key.
Definition: RepoGroup.php:314
ProcessCacheLRU
Handles per process caching of items.
Definition: ProcessCacheLRU.php:28
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:31
RepoGroup\setSingleton
static setSingleton( $instance)
Set the singleton instance to a given object Used by extensions which hook into the Repo chain.
Definition: RepoGroup.php:79