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