Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
RepoGroup.php
Go to the documentation of this file.
1<?php
7namespaceMediaWiki\FileRepo;
8
9use InvalidArgumentException;
10useMediaWiki\FileRepo\File\File;
11useMediaWiki\Linker\LinkTarget;
12useMediaWiki\Page\PageIdentity;
13useMediaWiki\Title\Title;
14useMWFileProps;
15useWikimedia\FileBackend\FileBackend;
16useWikimedia\MapCacheLRU\MapCacheLRU;
17use Wikimedia\Mime\MimeAnalyzer;
18useWikimedia\ObjectCache\WANObjectCache;
19
29classRepoGroup {
31protected$localRepo;
32
34protected$foreignRepos;
35
37protected$wanCache;
38
40protected$reposInitialised =false;
41
46protected$localInfo;
47
49protected$foreignInfo;
50
52protected$cache;
53
55privateconst MAX_CACHE_SIZE = 500;
56
58private $mimeAnalyzer;
59
73publicfunction__construct(
77 MimeAnalyzer $mimeAnalyzer
78 ) {
79 $this->localInfo =$localInfo;
80 $this->foreignInfo =$foreignInfo;
81 $this->cache =newMapCacheLRU( self::MAX_CACHE_SIZE );
82 $this->wanCache =$wanCache;
83 $this->mimeAnalyzer = $mimeAnalyzer;
84 }
85
104publicfunctionfindFile( $title, $options = [] ) {
105if ( !is_array( $options ) ) {
106// MW 1.15 compat
107 $options = ['time' => $options ];
108 }
109if ( isset( $options['bypassCache'] ) ) {
110 $options['latest'] = $options['bypassCache'];// b/c
111 }
112if ( isset( $options['time'] ) && $options['time'] !==false ) {
113 $options['time'] =wfTimestamp( TS_MW, $options['time'] );
114 }else {
115 $options['time'] =false;
116 }
117
118if ( !$this->reposInitialised ) {
119 $this->initialiseRepos();
120 }
121
122 $title = File::normalizeTitle( $title );
123if ( !$title ) {
124returnfalse;
125 }
126
127 # Check the cache
128 $dbkey = $title->getDBkey();
129 $timeKey = is_string( $options['time'] ) ? $options['time'] :'';
130if ( empty( $options['ignoreRedirect'] )
131 && empty( $options['private'] )
132 && empty( $options['latest'] )
133 ) {
134if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
135return $this->cache->getField( $dbkey, $timeKey );
136 }
137 $useCache =true;
138 }else {
139 $useCache =false;
140 }
141
142 # Check the local repo
143 $image = $this->localRepo->findFile( $title, $options );
144
145 # Check the foreign repos
146if ( !$image ) {
147foreach ( $this->foreignRepos as $repo ) {
148 $image = $repo->findFile( $title, $options );
149if ( $image ) {
150break;
151 }
152 }
153 }
154
155 $image = $image instanceofFile ? $image :false;// type check
156 # Cache file existence or non-existence
157if ( $useCache && ( !$image || $image->isCacheable() ) ) {
158 $this->cache->setField( $dbkey, $timeKey, $image );
159 }
160
161return $image;
162 }
163
181publicfunctionfindFiles( array $inputItems, $flags = 0 ) {
182if ( !$this->reposInitialised ) {
183 $this->initialiseRepos();
184 }
185
186 $items = [];
187foreach ( $inputItems as $item ) {
188if ( !is_array( $item ) ) {
189 $item = ['title' => $item ];
190 }
191 $item['title'] = File::normalizeTitle( $item['title'] );
192if ( $item['title'] ) {
193 $items[$item['title']->getDBkey()] = $item;
194 }
195 }
196
197 $images = $this->localRepo->findFiles( $items, $flags );
198
199foreach ( $this->foreignRepos as $repo ) {
200// Remove found files from $items
201 $items = array_diff_key( $items, $images );
202 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
203 }
204
205return $images;
206 }
207
213publicfunctioncheckRedirect( $title ) {
214if ( !$this->reposInitialised ) {
215 $this->initialiseRepos();
216 }
217
218 $title = File::normalizeTitle( $title );
219
220 $redir = $this->localRepo->checkRedirect( $title );
221if ( $redir ) {
222return $redir;
223 }
224
225foreach ( $this->foreignRepos as $repo ) {
226 $redir = $repo->checkRedirect( $title );
227if ( $redir ) {
228return $redir;
229 }
230 }
231
232returnfalse;
233 }
234
243publicfunctionfindFileFromKey( $hash, $options = [] ) {
244if ( !$this->reposInitialised ) {
245 $this->initialiseRepos();
246 }
247
248 $file = $this->localRepo->findFileFromKey( $hash, $options );
249if ( !$file ) {
250foreach ( $this->foreignRepos as $repo ) {
251 $file = $repo->findFileFromKey( $hash, $options );
252if ( $file ) {
253break;
254 }
255 }
256 }
257
258return $file;
259 }
260
267publicfunctionfindBySha1( $hash ) {
268if ( !$this->reposInitialised ) {
269 $this->initialiseRepos();
270 }
271
272 $result = $this->localRepo->findBySha1( $hash );
273foreach ( $this->foreignRepos as $repo ) {
274 $result = array_merge( $result, $repo->findBySha1( $hash ) );
275 }
276 usort( $result,File::compare( ... ) );
277
278return $result;
279 }
280
287publicfunctionfindBySha1s( array $hashes ) {
288if ( !$this->reposInitialised ) {
289 $this->initialiseRepos();
290 }
291
292 $result = $this->localRepo->findBySha1s( $hashes );
293foreach ( $this->foreignRepos as $repo ) {
294 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
295 }
296// sort the merged (and presorted) sublist of each hash
297foreach ( $result as $hash => $files ) {
298 usort( $result[$hash],File::compare( ... ) );
299 }
300
301return $result;
302 }
303
309publicfunctiongetRepo( $index ) {
310if ( !$this->reposInitialised ) {
311 $this->initialiseRepos();
312 }
313if ( $index ==='local' ) {
314return$this->localRepo;
315 }
316return $this->foreignRepos[$index] ??false;
317 }
318
324publicfunctiongetRepoByName( $name ) {
325if ( !$this->reposInitialised ) {
326 $this->initialiseRepos();
327 }
328foreach ( $this->foreignRepos as $repo ) {
329if ( $repo->name == $name ) {
330return $repo;
331 }
332 }
333
334returnfalse;
335 }
336
343publicfunctiongetLocalRepo() {
344// @phan-suppress-next-line PhanTypeMismatchReturnSuperType
345return $this->getRepo('local' );
346 }
347
356publicfunctionforEachForeignRepo( $callback, $params = [] ) {
357if ( !$this->reposInitialised ) {
358 $this->initialiseRepos();
359 }
360foreach ( $this->foreignRepos as $repo ) {
361if ( $callback( $repo, ...$params ) ) {
362returntrue;
363 }
364 }
365
366returnfalse;
367 }
368
373publicfunctionhasForeignRepos() {
374if ( !$this->reposInitialised ) {
375 $this->initialiseRepos();
376 }
377return (bool)$this->foreignRepos;
378 }
379
383publicfunctioninitialiseRepos() {
384if ( $this->reposInitialised ) {
385return;
386 }
387 $this->reposInitialised =true;
388
389 $this->localRepo = $this->newRepo( $this->localInfo );
390 $this->foreignRepos = [];
391foreach ( $this->foreignInfo as $key => $info ) {
392 $this->foreignRepos[$key] = $this->newRepo( $info );
393 }
394 }
395
402publicfunctionnewCustomLocalRepo( $info = [] ) {
403return $this->newRepo( $info + $this->localInfo );
404 }
405
414protectedfunctionnewRepo( $info ) {
415 $class = $info['class'];
416
417 $info['wanCache'] =$this->wanCache;
418
419returnnew $class( $info );
420 }
421
427privatefunction splitVirtualUrl($url ) {
428if ( !str_starts_with($url,'mwrepo://' ) ) {
429thrownew InvalidArgumentException( __METHOD__ .': unknown protocol' );
430 }
431
432 $bits = explode('/', substr($url, 9 ), 3 );
433if ( count( $bits ) != 3 ) {
434thrownew InvalidArgumentException( __METHOD__ .": invalid mwrepo URL: $url" );
435 }
436
437return $bits;
438 }
439
444publicfunctiongetFileProps( $fileName ) {
445if (FileRepo::isVirtualUrl( $fileName ) ) {
446 [ $repoName,/* $zone */,/* $rel */ ] = $this->splitVirtualUrl( $fileName );
447if ( $repoName ==='' ) {
448 $repoName ='local';
449 }
450 $repo = $this->getRepo( $repoName );
451
452return $repo->getFileProps( $fileName );
453 }else {
454 $mwProps =newMWFileProps( $this->mimeAnalyzer );
455
456return $mwProps->getPropsFromPath( $fileName,true );
457 }
458 }
459
464publicfunctionclearCache( $title =null ) {
465if ( $title ==null ) {
466 $this->cache->clear();
467 } elseif ( is_string( $title ) ) {
468 $this->cache->clear( $title );
469 }else {
470 $this->cache->clear( $title->getDBkey() );
471 }
472 }
473}
474
476class_alias( RepoGroup::class,'RepoGroup' );
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
DefinitionGlobalFunctions.php:1300
MWFileProps
MimeMagic helper wrapper.
DefinitionMWFileProps.php:19
MediaWiki\FileRepo\FileRepo
Base class for file repositories.
DefinitionFileRepo.php:52
MediaWiki\FileRepo\FileRepo\isVirtualUrl
static isVirtualUrl( $url)
Determine if a string is an mwrepo:// URL.
DefinitionFileRepo.php:288
MediaWiki\FileRepo\File\File
Implements some public methods and some protected utility functions which are required by multiple ch...
DefinitionFile.php:79
MediaWiki\FileRepo\LocalRepo
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
DefinitionLocalRepo.php:45
MediaWiki\FileRepo\RepoGroup
Prioritized list of file repositories.
DefinitionRepoGroup.php:29
MediaWiki\FileRepo\RepoGroup\getFileProps
getFileProps( $fileName)
DefinitionRepoGroup.php:444
MediaWiki\FileRepo\RepoGroup\$reposInitialised
bool $reposInitialised
DefinitionRepoGroup.php:40
MediaWiki\FileRepo\RepoGroup\getRepo
getRepo( $index)
Get the repo instance with a given key.
DefinitionRepoGroup.php:309
MediaWiki\FileRepo\RepoGroup\forEachForeignRepo
forEachForeignRepo( $callback, $params=[])
Call a function for each foreign repo, with the repo object as the first parameter.
DefinitionRepoGroup.php:356
MediaWiki\FileRepo\RepoGroup\clearCache
clearCache( $title=null)
Clear RepoGroup process cache used for finding a file.
DefinitionRepoGroup.php:464
MediaWiki\FileRepo\RepoGroup\$localRepo
LocalRepo $localRepo
DefinitionRepoGroup.php:31
MediaWiki\FileRepo\RepoGroup\findBySha1s
findBySha1s(array $hashes)
Find all instances of files with this keys.
DefinitionRepoGroup.php:287
MediaWiki\FileRepo\RepoGroup\newRepo
newRepo( $info)
Create a repo class based on an info structure @template T of FileRepo.
DefinitionRepoGroup.php:414
MediaWiki\FileRepo\RepoGroup\findFiles
findFiles(array $inputItems, $flags=0)
Search repositories for many files at once.
DefinitionRepoGroup.php:181
MediaWiki\FileRepo\RepoGroup\checkRedirect
checkRedirect( $title)
Interface for FileRepo::checkRedirect()
DefinitionRepoGroup.php:213
MediaWiki\FileRepo\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...
DefinitionRepoGroup.php:243
MediaWiki\FileRepo\RepoGroup\$localInfo
array $localInfo
DefinitionRepoGroup.php:46
MediaWiki\FileRepo\RepoGroup\newCustomLocalRepo
newCustomLocalRepo( $info=[])
Create a local repo with the specified option overrides.
DefinitionRepoGroup.php:402
MediaWiki\FileRepo\RepoGroup\findFile
findFile( $title, $options=[])
Search repositories for an image.
DefinitionRepoGroup.php:104
MediaWiki\FileRepo\RepoGroup\initialiseRepos
initialiseRepos()
Initialise the $repos array.
DefinitionRepoGroup.php:383
MediaWiki\FileRepo\RepoGroup\hasForeignRepos
hasForeignRepos()
Does the installation have any foreign repos set up?
DefinitionRepoGroup.php:373
MediaWiki\FileRepo\RepoGroup\$cache
MapCacheLRU $cache
DefinitionRepoGroup.php:52
MediaWiki\FileRepo\RepoGroup\getRepoByName
getRepoByName( $name)
Get the repo instance by its name.
DefinitionRepoGroup.php:324
MediaWiki\FileRepo\RepoGroup\$foreignInfo
array $foreignInfo
DefinitionRepoGroup.php:49
MediaWiki\FileRepo\RepoGroup\getLocalRepo
getLocalRepo()
Get the local repository, i.e.
DefinitionRepoGroup.php:343
MediaWiki\FileRepo\RepoGroup\$wanCache
WANObjectCache $wanCache
DefinitionRepoGroup.php:37
MediaWiki\FileRepo\RepoGroup\$foreignRepos
FileRepo[] $foreignRepos
DefinitionRepoGroup.php:34
MediaWiki\FileRepo\RepoGroup\findBySha1
findBySha1( $hash)
Find all instances of files with this key.
DefinitionRepoGroup.php:267
MediaWiki\FileRepo\RepoGroup\__construct
__construct( $localInfo, $foreignInfo, WANObjectCache $wanCache, MimeAnalyzer $mimeAnalyzer)
Construct a group of file repositories.
DefinitionRepoGroup.php:73
MediaWiki\Title\Title
Represents a title within MediaWiki.
DefinitionTitle.php:69
MediaWiki\Title\Title\compare
static compare( $a, $b)
Callback for usort() to do title sorts by (namespace, title)
DefinitionTitle.php:860
Wikimedia\FileBackend\FileBackend
Base class for all file backend classes (including multi-write backends).
DefinitionFileBackend.php:98
Wikimedia\MapCacheLRU\MapCacheLRU
Store key-value entries in a size-limited in-memory LRU cache.
DefinitionMapCacheLRU.php:24
Wikimedia\ObjectCache\WANObjectCache
Multi-datacenter aware caching interface.
DefinitionWANObjectCache.php:153
MediaWiki\Linker\LinkTarget
Represents the target of a wiki link.
DefinitionLinkTarget.php:19
MediaWiki\Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
DefinitionPageIdentity.php:53
MediaWiki\FileRepo
DefinitionAuthenticatedFileEntryPoint.php:12
$url
$url
Definitionopensearch_desc.php:23

[8]ページ先頭

©2009-2025 Movatter.jp