Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 * @return File|bool False if title is not found
119 */
120 function findFile( $title, $options = [] ) {
121 if ( !is_array( $options ) ) {
122 // MW 1.15 compat
123 $options = [ 'time' => $options ];
124 }
125 if ( isset( $options['bypassCache'] ) ) {
126 $options['latest'] = $options['bypassCache']; // b/c
127 }
128 $options += [ 'time' => false ];
129
130 if ( !$this->reposInitialised ) {
131 $this->initialiseRepos();
132 }
133
134 $title = File::normalizeTitle( $title );
135 if ( !$title ) {
136 return false;
137 }
138
139 # Check the cache
140 $dbkey = $title->getDBkey();
141 $timeKey = is_string( $options['time'] ) ? $options['time'] : '';
142 if ( empty( $options['ignoreRedirect'] )
143 && empty( $options['private'] )
144 && empty( $options['latest'] )
145 ) {
146 if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
147 return $this->cache->getField( $dbkey, $timeKey );
148 }
149 $useCache = true;
150 } else {
151 $useCache = false;
152 }
153
154 # Check the local repo
155 $image = $this->localRepo->findFile( $title, $options );
156
157 # Check the foreign repos
158 if ( !$image ) {
159 foreach ( $this->foreignRepos as $repo ) {
160 $image = $repo->findFile( $title, $options );
161 if ( $image ) {
162 break;
163 }
164 }
165 }
166
167 $image = $image instanceof File ? $image : false; // type sanity
168 # Cache file existence or non-existence
169 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
170 $this->cache->setField( $dbkey, $timeKey, $image );
171 }
172
173 return $image;
174 }
175
176 /**
177 * Search repositories for many files at once.
178 *
179 * @param array $inputItems An array of titles, or an array of findFile() options with
180 * the "title" option giving the title. Example:
181 *
182 * $findItem = [ 'title' => $title, 'private' => true ];
183 * $findBatch = [ $findItem ];
184 * $repo->findFiles( $findBatch );
185 *
186 * No title should appear in $items twice, as the result use titles as keys
187 * @param int $flags Supports:
188 * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
189 * The search title uses the input titles; the other is the final post-redirect title.
190 * All titles are returned as string DB keys and the inner array is associative.
191 * @return array Map of (file name => File objects) for matches
192 */
193 function findFiles( array $inputItems, $flags = 0 ) {
194 if ( !$this->reposInitialised ) {
195 $this->initialiseRepos();
196 }
197
198 $items = [];
199 foreach ( $inputItems as $item ) {
200 if ( !is_array( $item ) ) {
201 $item = [ 'title' => $item ];
202 }
203 $item['title'] = File::normalizeTitle( $item['title'] );
204 if ( $item['title'] ) {
205 $items[$item['title']->getDBkey()] = $item;
206 }
207 }
208
209 $images = $this->localRepo->findFiles( $items, $flags );
210
211 foreach ( $this->foreignRepos as $repo ) {
212 // Remove found files from $items
213 foreach ( $images as $name => $image ) {
214 unset( $items[$name] );
215 }
216
217 $images = array_merge( $images, $repo->findFiles( $items, $flags ) );
218 }
219
220 return $images;
221 }
222
223 /**
224 * Interface for FileRepo::checkRedirect()
225 * @param Title $title
226 * @return bool|Title
227 */
228 function checkRedirect( Title $title ) {
229 if ( !$this->reposInitialised ) {
230 $this->initialiseRepos();
231 }
232
233 $redir = $this->localRepo->checkRedirect( $title );
234 if ( $redir ) {
235 return $redir;
236 }
237
238 foreach ( $this->foreignRepos as $repo ) {
239 $redir = $repo->checkRedirect( $title );
240 if ( $redir ) {
241 return $redir;
242 }
243 }
244
245 return false;
246 }
247
248 /**
249 * Find an instance of the file with this key, created at the specified time
250 * Returns false if the file does not exist.
251 *
252 * @param string $hash Base 36 SHA-1 hash
253 * @param array $options Option array, same as findFile()
254 * @return File|bool File object or false if it is not found
255 */
256 function findFileFromKey( $hash, $options = [] ) {
257 if ( !$this->reposInitialised ) {
258 $this->initialiseRepos();
259 }
260
261 $file = $this->localRepo->findFileFromKey( $hash, $options );
262 if ( !$file ) {
263 foreach ( $this->foreignRepos as $repo ) {
264 $file = $repo->findFileFromKey( $hash, $options );
265 if ( $file ) {
266 break;
267 }
268 }
269 }
270
271 return $file;
272 }
273
274 /**
275 * Find all instances of files with this key
276 *
277 * @param string $hash Base 36 SHA-1 hash
278 * @return File[]
279 */
280 function findBySha1( $hash ) {
281 if ( !$this->reposInitialised ) {
282 $this->initialiseRepos();
283 }
284
285 $result = $this->localRepo->findBySha1( $hash );
286 foreach ( $this->foreignRepos as $repo ) {
287 $result = array_merge( $result, $repo->findBySha1( $hash ) );
288 }
289 usort( $result, 'File::compare' );
290
291 return $result;
292 }
293
294 /**
295 * Find all instances of files with this keys
296 *
297 * @param array $hashes Base 36 SHA-1 hashes
298 * @return array Array of array of File objects
299 */
300 function findBySha1s( array $hashes ) {
301 if ( !$this->reposInitialised ) {
302 $this->initialiseRepos();
303 }
304
305 $result = $this->localRepo->findBySha1s( $hashes );
306 foreach ( $this->foreignRepos as $repo ) {
307 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
308 }
309 // sort the merged (and presorted) sublist of each hash
310 foreach ( $result as $hash => $files ) {
311 usort( $result[$hash], 'File::compare' );
312 }
313
314 return $result;
315 }
316
317 /**
318 * Get the repo instance with a given key.
319 * @param string|int $index
320 * @return bool|FileRepo
321 */
322 function getRepo( $index ) {
323 if ( !$this->reposInitialised ) {
324 $this->initialiseRepos();
325 }
326 if ( $index === 'local' ) {
327 return $this->localRepo;
328 }
329 return $this->foreignRepos[$index] ?? false;
330 }
331
332 /**
333 * Get the repo instance by its name
334 * @param string $name
335 * @return FileRepo|bool
336 */
337 function getRepoByName( $name ) {
338 if ( !$this->reposInitialised ) {
339 $this->initialiseRepos();
340 }
341 foreach ( $this->foreignRepos as $repo ) {
342 if ( $repo->name == $name ) {
343 return $repo;
344 }
345 }
346
347 return false;
348 }
349
350 /**
351 * Get the local repository, i.e. the one corresponding to the local image
352 * table. Files are typically uploaded to the local repository.
353 *
354 * @return LocalRepo
355 */
356 function getLocalRepo() {
357 /** @var LocalRepo $repo */
358 $repo = $this->getRepo( 'local' );
359
360 return $repo;
361 }
362
363 /**
364 * Call a function for each foreign repo, with the repo object as the
365 * first parameter.
366 *
367 * @param callable $callback The function to call
368 * @param array $params Optional additional parameters to pass to the function
369 * @return bool
370 */
371 function forEachForeignRepo( $callback, $params = [] ) {
372 if ( !$this->reposInitialised ) {
373 $this->initialiseRepos();
374 }
375 foreach ( $this->foreignRepos as $repo ) {
376 if ( $callback( $repo, ...$params ) ) {
377 return true;
378 }
379 }
380
381 return false;
382 }
383
384 /**
385 * Does the installation have any foreign repos set up?
386 * @return bool
387 */
388 function hasForeignRepos() {
389 if ( !$this->reposInitialised ) {
390 $this->initialiseRepos();
391 }
392 return (bool)$this->foreignRepos;
393 }
394
395 /**
396 * Initialise the $repos array
397 */
398 function initialiseRepos() {
399 if ( $this->reposInitialised ) {
400 return;
401 }
402 $this->reposInitialised = true;
403
404 $this->localRepo = $this->newRepo( $this->localInfo );
405 $this->foreignRepos = [];
406 foreach ( $this->foreignInfo as $key => $info ) {
407 $this->foreignRepos[$key] = $this->newRepo( $info );
408 }
409 }
410
411 /**
412 * Create a repo class based on an info structure
413 * @param array $info
414 * @return FileRepo
415 */
416 protected function newRepo( $info ) {
417 $class = $info['class'];
418
419 $info['wanCache'] = $this->wanCache;
420
421 return new $class( $info );
422 }
423
424 /**
425 * Split a virtual URL into repo, zone and rel parts
426 * @param string $url
427 * @throws MWException
428 * @return string[] Containing repo, zone and rel
429 */
430 function splitVirtualUrl( $url ) {
431 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
432 throw new MWException( __METHOD__ . ': unknown protocol' );
433 }
434
435 $bits = explode( '/', substr( $url, 9 ), 3 );
436 if ( count( $bits ) != 3 ) {
437 throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
438 }
439
440 return $bits;
441 }
442
443 /**
444 * @param string $fileName
445 * @return array
446 */
447 function getFileProps( $fileName ) {
448 if ( FileRepo::isVirtualUrl( $fileName ) ) {
449 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
450 if ( $repoName === '' ) {
451 $repoName = 'local';
452 }
453 $repo = $this->getRepo( $repoName );
454
455 return $repo->getFileProps( $fileName );
456 } else {
457 $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
458
459 return $mwProps->getPropsFromPath( $fileName, true );
460 }
461 }
462
463 /**
464 * Clear RepoGroup process cache used for finding a file
465 * @param Title|null $title Title of the file or null to clear all files
466 */
467 public function clearCache( Title $title = null ) {
468 if ( $title == null ) {
469 $this->cache->clear();
470 } else {
471 $this->cache->clear( $title->getDBkey() );
472 }
473 }
474 }