Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
1 <?php
2 /**
3 * Prioritized list of file repositories.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileRepo
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Prioritized list of file repositories
28 *
29 * @ingroup FileRepo
30 */
31 class RepoGroup {
32 /** @var LocalRepo */
33 protected $localRepo;
34
35 /** @var FileRepo[] */
36 protected $foreignRepos;
37
38 /** @var WANObjectCache */
39 protected $wanCache;
40
41 /** @var bool */
42 protected $reposInitialised = false;
43
44 /** @var array */
45 protected $localInfo;
46
47 /** @var array */
48 protected $foreignInfo;
49
50 /** @var MapCacheLRU */
51 protected $cache;
52
53 /** Maximum number of cache items */
54 const MAX_CACHE_SIZE = 500;
55
56 /**
57 * @deprecated since 1.34, use MediaWikiServices::getRepoGroup
58 * @return RepoGroup
59 */
60 static function singleton() {
61 return MediaWikiServices::getInstance()->getRepoGroup();
62 }
63
64 /**
65 * @deprecated since 1.34, use MediaWikiTestCase::overrideMwServices() or similar. This will
66 * cause bugs if you don't reset all other services that depend on this one at the same time.
67 */
68 static function destroySingleton() {
69 MediaWikiServices::getInstance()->resetServiceForTesting( 'RepoGroup' );
70 }
71
72 /**
73 * @deprecated since 1.34, use MediaWikiTestCase::setService, this can mess up state of other
74 * tests
75 * @param RepoGroup $instance
76 */
77 static function setSingleton( $instance ) {
78 $services = MediaWikiServices::getInstance();
79 $services->disableService( 'RepoGroup' );
80 $services->redefineService( 'RepoGroup',
81 function () use ( $instance ) {
82 return $instance;
83 }
84 );
85 }
86
87 /**
88 * Construct a group of file repositories. Do not call this -- use
89 * MediaWikiServices::getRepoGroup.
90 *
91 * @param array $localInfo Associative array for local repo's info
92 * @param array $foreignInfo Array of repository info arrays.
93 * Each info array is an associative array with the 'class' member
94 * giving the class name. The entire array is passed to the repository
95 * constructor as the first parameter.
96 * @param WANObjectCache $wanCache
97 */
98 function __construct( $localInfo, $foreignInfo, $wanCache ) {
99 $this->localInfo = $localInfo;
100 $this->foreignInfo = $foreignInfo;
101 $this->cache = new MapCacheLRU( self::MAX_CACHE_SIZE );
102 $this->wanCache = $wanCache;
103 }
104
105 /**
106 * Search repositories for an image.
107 *
108 * @param Title|string $title Title object or string
109 * @param array $options Associative array of options:
110 * time: requested time for an archived image, or false for the
111 * current version. An image object will be returned which was
112 * created at the specified time.
113 * ignoreRedirect: If true, do not follow file redirects
114 * private: If true, return restricted (deleted) files if the current
115 * user is allowed to view them. Otherwise, such files will not
116 * be found.
117 * latest: If true, load from the latest available data into File objects
118 * @phan-param array{time?:mixed,ignoreRedirect?:bool,private?:bool,latest?:bool} $options
119 * @suppress PhanTypeInvalidDimOffset
120 * @return File|bool False if title is not found
121 */
122 function findFile( $title, $options = [] ) {
123 if ( !is_array( $options ) ) {
124 // MW 1.15 compat
125 $options = [ 'time' => $options ];
126 }
127 if ( isset( $options['bypassCache'] ) ) {
128 $options['latest'] = $options['bypassCache']; // b/c
129 }
130 $options += [ 'time' => false ];
131
132 if ( !$this->reposInitialised ) {
133 $this->initialiseRepos();
134 }
135
136 $title = File::normalizeTitle( $title );
137 if ( !$title ) {
138 return false;
139 }
140
141 # Check the cache
142 $dbkey = $title->getDBkey();
143 $timeKey = is_string( $options['time'] ) ? $options['time'] : '';
144 if ( empty( $options['ignoreRedirect'] )
145 && empty( $options['private'] )
146 && empty( $options['latest'] )
147 ) {
148 if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
149 return $this->cache->getField( $dbkey, $timeKey );
150 }
151 $useCache = true;
152 } else {
153 $useCache = false;
154 }
155
156 # Check the local repo
157 $image = $this->localRepo->findFile( $title, $options );
158
159 # Check the foreign repos
160 if ( !$image ) {
161 foreach ( $this->foreignRepos as $repo ) {
162 $image = $repo->findFile( $title, $options );
163 if ( $image ) {
164 break;
165 }
166 }
167 }
168
169 $image = $image instanceof File ? $image : false; // type sanity
170 # Cache file existence or non-existence
171 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
172 $this->cache->setField( $dbkey, $timeKey, $image );
173 }
174
175 return $image;
176 }
177
178 /**
179 * Search repositories for many files at once.
180 *
181 * @param array $inputItems An array of titles, or an array of findFile() options with
182 * the "title" option giving the title. Example:
183 *
184 * $findItem = [ 'title' => $title, 'private' => true ];
185 * $findBatch = [ $findItem ];
186 * $repo->findFiles( $findBatch );
187 *
188 * No title should appear in $items twice, as the result use titles as keys
189 * @param int $flags Supports:
190 * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
191 * The search title uses the input titles; the other is the final post-redirect title.
192 * All titles are returned as string DB keys and the inner array is associative.
193 * @return array Map of (file name => File objects) for matches
194 */
195 function findFiles( array $inputItems, $flags = 0 ) {
196 if ( !$this->reposInitialised ) {
197 $this->initialiseRepos();
198 }
199
200 $items = [];
201 foreach ( $inputItems as $item ) {
202 if ( !is_array( $item ) ) {
203 $item = [ 'title' => $item ];
204 }
205 $item['title'] = File::normalizeTitle( $item['title'] );
206 if ( $item['title'] ) {
207 $items[$item['title']->getDBkey()] = $item;
208 }
209 }
210
211 $images = $this->localRepo->findFiles( $items, $flags );
212
213 foreach ( $this->foreignRepos as $repo ) {
214 // Remove found files from $items
215 foreach ( $images as $name => $image ) {
216 unset( $items[$name] );
217 }
218
219 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
220 }
221
222 return $images;
223 }
224
225 /**
226 * Interface for FileRepo::checkRedirect()
227 * @param Title $title
228 * @return bool|Title
229 */
230 function checkRedirect( Title $title ) {
231 if ( !$this->reposInitialised ) {
232 $this->initialiseRepos();
233 }
234
235 $redir = $this->localRepo->checkRedirect( $title );
236 if ( $redir ) {
237 return $redir;
238 }
239
240 foreach ( $this->foreignRepos as $repo ) {
241 $redir = $repo->checkRedirect( $title );
242 if ( $redir ) {
243 return $redir;
244 }
245 }
246
247 return false;
248 }
249
250 /**
251 * Find an instance of the file with this key, created at the specified time
252 * Returns false if the file does not exist.
253 *
254 * @param string $hash Base 36 SHA-1 hash
255 * @param array $options Option array, same as findFile()
256 * @return File|bool File object or false if it is not found
257 */
258 function findFileFromKey( $hash, $options = [] ) {
259 if ( !$this->reposInitialised ) {
260 $this->initialiseRepos();
261 }
262
263 $file = $this->localRepo->findFileFromKey( $hash, $options );
264 if ( !$file ) {
265 foreach ( $this->foreignRepos as $repo ) {
266 $file = $repo->findFileFromKey( $hash, $options );
267 if ( $file ) {
268 break;
269 }
270 }
271 }
272
273 return $file;
274 }
275
276 /**
277 * Find all instances of files with this key
278 *
279 * @param string $hash Base 36 SHA-1 hash
280 * @return File[]
281 */
282 function findBySha1( $hash ) {
283 if ( !$this->reposInitialised ) {
284 $this->initialiseRepos();
285 }
286
287 $result = $this->localRepo->findBySha1( $hash );
288 foreach ( $this->foreignRepos as $repo ) {
289 $result = array_merge( $result, $repo->findBySha1( $hash ) );
290 }
291 usort( $result, 'File::compare' );
292
293 return $result;
294 }
295
296 /**
297 * Find all instances of files with this keys
298 *
299 * @param array $hashes Base 36 SHA-1 hashes
300 * @return array Array of array of File objects
301 */
302 function findBySha1s( array $hashes ) {
303 if ( !$this->reposInitialised ) {
304 $this->initialiseRepos();
305 }
306
307 $result = $this->localRepo->findBySha1s( $hashes );
308 foreach ( $this->foreignRepos as $repo ) {
309 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
310 }
311 // sort the merged (and presorted) sublist of each hash
312 foreach ( $result as $hash => $files ) {
313 usort( $result[$hash], 'File::compare' );
314 }
315
316 return $result;
317 }
318
319 /**
320 * Get the repo instance with a given key.
321 * @param string|int $index
322 * @return bool|FileRepo
323 */
324 function getRepo( $index ) {
325 if ( !$this->reposInitialised ) {
326 $this->initialiseRepos();
327 }
328 if ( $index === 'local' ) {
329 return $this->localRepo;
330 }
331 return $this->foreignRepos[$index] ?? false;
332 }
333
334 /**
335 * Get the repo instance by its name
336 * @param string $name
337 * @return FileRepo|bool
338 */
339 function getRepoByName( $name ) {
340 if ( !$this->reposInitialised ) {
341 $this->initialiseRepos();
342 }
343 foreach ( $this->foreignRepos as $repo ) {
344 if ( $repo->name == $name ) {
345 return $repo;
346 }
347 }
348
349 return false;
350 }
351
352 /**
353 * Get the local repository, i.e. the one corresponding to the local image
354 * table. Files are typically uploaded to the local repository.
355 *
356 * @return LocalRepo
357 */
358 function getLocalRepo() {
359 /** @var LocalRepo $repo */
360 $repo = $this->getRepo( 'local' );
361
362 return $repo;
363 }
364
365 /**
366 * Call a function for each foreign repo, with the repo object as the
367 * first parameter.
368 *
369 * @param callable $callback The function to call
370 * @param array $params Optional additional parameters to pass to the function
371 * @return bool
372 */
373 function forEachForeignRepo( $callback, $params = [] ) {
374 if ( !$this->reposInitialised ) {
375 $this->initialiseRepos();
376 }
377 foreach ( $this->foreignRepos as $repo ) {
378 if ( $callback( $repo, ...$params ) ) {
379 return true;
380 }
381 }
382
383 return false;
384 }
385
386 /**
387 * Does the installation have any foreign repos set up?
388 * @return bool
389 */
390 function hasForeignRepos() {
391 if ( !$this->reposInitialised ) {
392 $this->initialiseRepos();
393 }
394 return (bool)$this->foreignRepos;
395 }
396
397 /**
398 * Initialise the $repos array
399 */
400 function initialiseRepos() {
401 if ( $this->reposInitialised ) {
402 return;
403 }
404 $this->reposInitialised = true;
405
406 $this->localRepo = $this->newRepo( $this->localInfo );
407 $this->foreignRepos = [];
408 foreach ( $this->foreignInfo as $key => $info ) {
409 $this->foreignRepos[$key] = $this->newRepo( $info );
410 }
411 }
412
413 /**
414 * Create a repo class based on an info structure
415 * @param array $info
416 * @return FileRepo
417 */
418 protected function newRepo( $info ) {
419 $class = $info['class'];
420
421 $info['wanCache'] = $this->wanCache;
422
423 return new $class( $info );
424 }
425
426 /**
427 * Split a virtual URL into repo, zone and rel parts
428 * @param string $url
429 * @throws MWException
430 * @return string[] Containing repo, zone and rel
431 */
432 function splitVirtualUrl( $url ) {
433 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
434 throw new MWException( __METHOD__ . ': unknown protocol' );
435 }
436
437 $bits = explode( '/', substr( $url, 9 ), 3 );
438 if ( count( $bits ) != 3 ) {
439 throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
440 }
441
442 return $bits;
443 }
444
445 /**
446 * @param string $fileName
447 * @return array
448 */
449 function getFileProps( $fileName ) {
450 if ( FileRepo::isVirtualUrl( $fileName ) ) {
451 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
452 if ( $repoName === '' ) {
453 $repoName = 'local';
454 }
455 $repo = $this->getRepo( $repoName );
456
457 return $repo->getFileProps( $fileName );
458 } else {
459 $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
460
461 return $mwProps->getPropsFromPath( $fileName, true );
462 }
463 }
464
465 /**
466 * Clear RepoGroup process cache used for finding a file
467 * @param Title|null $title Title of the file or null to clear all files
468 */
469 public function clearCache( Title $title = null ) {
470 if ( $title == null ) {
471 $this->cache->clear();
472 } else {
473 $this->cache->clear( $title->getDBkey() );
474 }
475 }
476 }