Merge "Http::getProxy() method to get proxy configuration"
[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 /**
25 * Prioritized list of file repositories
26 *
27 * @ingroup FileRepo
28 */
29 class RepoGroup {
30 /** @var LocalRepo */
31 protected $localRepo;
32
33 /** @var FileRepo[] */
34 protected $foreignRepos;
35
36 /** @var bool */
37 protected $reposInitialised = false;
38
39 /** @var array */
40 protected $localInfo;
41
42 /** @var array */
43 protected $foreignInfo;
44
45 /** @var ProcessCacheLRU */
46 protected $cache;
47
48 /** @var RepoGroup */
49 protected static $instance;
50
51 /** Maximum number of cache items */
52 const MAX_CACHE_SIZE = 500;
53
54 /**
55 * Get a RepoGroup instance. At present only one instance of RepoGroup is
56 * needed in a MediaWiki invocation, this may change in the future.
57 * @return RepoGroup
58 */
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
69 /**
70 * Destroy the singleton instance, so that a new one will be created next
71 * time singleton() is called.
72 */
73 static function destroySingleton() {
74 self::$instance = null;
75 }
76
77 /**
78 * Set the singleton instance to a given object
79 * Used by extensions which hook into the Repo chain.
80 * It's not enough to just create a superclass ... you have
81 * to get people to call into it even though all they know is RepoGroup::singleton()
82 *
83 * @param RepoGroup $instance
84 */
85 static function setSingleton( $instance ) {
86 self::$instance = $instance;
87 }
88
89 /**
90 * Construct a group of file repositories.
91 *
92 * @param array $localInfo Associative array for local repo's info
93 * @param array $foreignInfo Array of repository info arrays.
94 * Each info array is an associative array with the 'class' member
95 * giving the class name. The entire array is passed to the repository
96 * constructor as the first parameter.
97 */
98 function __construct( $localInfo, $foreignInfo ) {
99 $this->localInfo = $localInfo;
100 $this->foreignInfo = $foreignInfo;
101 $this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
102 }
103
104 /**
105 * Search repositories for an image.
106 * You can also use wfFindFile() to do this.
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
129 if ( !$this->reposInitialised ) {
130 $this->initialiseRepos();
131 }
132 $title = File::normalizeTitle( $title );
133 if ( !$title ) {
134 return false;
135 }
136
137 # Check the cache
138 if ( empty( $options['ignoreRedirect'] )
139 && empty( $options['private'] )
140 && empty( $options['bypassCache'] )
141 ) {
142 $time = isset( $options['time'] ) ? $options['time'] : '';
143 $dbkey = $title->getDBkey();
144 if ( $this->cache->has( $dbkey, $time, 60 ) ) {
145 return $this->cache->get( $dbkey, $time );
146 }
147 $useCache = true;
148 } else {
149 $useCache = false;
150 }
151
152 # Check the local repo
153 $image = $this->localRepo->findFile( $title, $options );
154
155 # Check the foreign repos
156 if ( !$image ) {
157 foreach ( $this->foreignRepos as $repo ) {
158 $image = $repo->findFile( $title, $options );
159 if ( $image ) {
160 break;
161 }
162 }
163 }
164
165 $image = $image ? $image : false; // type sanity
166 # Cache file existence or non-existence
167 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
168 $this->cache->set( $dbkey, $time, $image );
169 }
170
171 return $image;
172 }
173
174 /**
175 * Search repositories for many files at once.
176 *
177 * @param array $inputItems An array of titles, or an array of findFile() options with
178 * the "title" option giving the title. Example:
179 *
180 * $findItem = array( 'title' => $title, 'private' => true );
181 * $findBatch = array( $findItem );
182 * $repo->findFiles( $findBatch );
183 *
184 * No title should appear in $items twice, as the result use titles as keys
185 * @param int $flags Supports:
186 * - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
187 * The search title uses the input titles; the other is the final post-redirect title.
188 * All titles are returned as string DB keys and the inner array is associative.
189 * @return array Map of (file name => File objects) for matches
190 */
191 function findFiles( array $inputItems, $flags = 0 ) {
192 if ( !$this->reposInitialised ) {
193 $this->initialiseRepos();
194 }
195
196 $items = [];
197 foreach ( $inputItems as $item ) {
198 if ( !is_array( $item ) ) {
199 $item = [ '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
221 /**
222 * Interface for FileRepo::checkRedirect()
223 * @param Title $title
224 * @return bool|Title
225 */
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
246 /**
247 * Find an instance of the file with this key, created at the specified time
248 * Returns false if the file does not exist.
249 *
250 * @param string $hash Base 36 SHA-1 hash
251 * @param array $options Option array, same as findFile()
252 * @return File|bool File object or false if it is not found
253 */
254 function findFileFromKey( $hash, $options = [] ) {
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
272 /**
273 * Find all instances of files with this key
274 *
275 * @param string $hash Base 36 SHA-1 hash
276 * @return File[]
277 */
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
292 /**
293 * Find all instances of files with this keys
294 *
295 * @param array $hashes Base 36 SHA-1 hashes
296 * @return array Array of array of File objects
297 */
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
315 /**
316 * Get the repo instance with a given key.
317 * @param string|int $index
318 * @return bool|LocalRepo
319 */
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
333 /**
334 * Get the repo instance by its name
335 * @param string $name
336 * @return bool
337 */
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
351 /**
352 * Get the local repository, i.e. the one corresponding to the local image
353 * table. Files are typically uploaded to the local repository.
354 *
355 * @return LocalRepo
356 */
357 function getLocalRepo() {
358 return $this->getRepo( 'local' );
359 }
360
361 /**
362 * Call a function for each foreign repo, with the repo object as the
363 * first parameter.
364 *
365 * @param callable $callback The function to call
366 * @param array $params Optional additional parameters to pass to the function
367 * @return bool
368 */
369 function forEachForeignRepo( $callback, $params = [] ) {
370 if ( !$this->reposInitialised ) {
371 $this->initialiseRepos();
372 }
373 foreach ( $this->foreignRepos as $repo ) {
374 $args = array_merge( [ $repo ], $params );
375 if ( call_user_func_array( $callback, $args ) ) {
376 return true;
377 }
378 }
379
380 return false;
381 }
382
383 /**
384 * Does the installation have any foreign repos set up?
385 * @return bool
386 */
387 function hasForeignRepos() {
388 if ( !$this->reposInitialised ) {
389 $this->initialiseRepos();
390 }
391 return (bool)$this->foreignRepos;
392 }
393
394 /**
395 * Initialise the $repos array
396 */
397 function initialiseRepos() {
398 if ( $this->reposInitialised ) {
399 return;
400 }
401 $this->reposInitialised = true;
402
403 $this->localRepo = $this->newRepo( $this->localInfo );
404 $this->foreignRepos = [];
405 foreach ( $this->foreignInfo as $key => $info ) {
406 $this->foreignRepos[$key] = $this->newRepo( $info );
407 }
408 }
409
410 /**
411 * Create a repo class based on an info structure
412 * @param array $info
413 * @return FileRepo
414 */
415 protected function newRepo( $info ) {
416 $class = $info['class'];
417
418 return new $class( $info );
419 }
420
421 /**
422 * Split a virtual URL into repo, zone and rel parts
423 * @param string $url
424 * @throws MWException
425 * @return array Containing repo, zone and rel
426 */
427 function splitVirtualUrl( $url ) {
428 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
429 throw new MWException( __METHOD__ . ': unknown protocol' );
430 }
431
432 $bits = explode( '/', substr( $url, 9 ), 3 );
433 if ( count( $bits ) != 3 ) {
434 throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
435 }
436
437 return $bits;
438 }
439
440 /**
441 * @param string $fileName
442 * @return array
443 */
444 function getFileProps( $fileName ) {
445 if ( FileRepo::isVirtualUrl( $fileName ) ) {
446 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
447 if ( $repoName === '' ) {
448 $repoName = 'local';
449 }
450 $repo = $this->getRepo( $repoName );
451
452 return $repo->getFileProps( $fileName );
453 } else {
454 return FSFile::getPropsFromPath( $fileName );
455 }
456 }
457
458 /**
459 * Clear RepoGroup process cache used for finding a file
460 * @param Title|null $title Title of the file or null to clear all files
461 */
462 public function clearCache( Title $title = null ) {
463 if ( $title == null ) {
464 $this->cache->clear();
465 } else {
466 $this->cache->clear( $title->getDBkey() );
467 }
468 }
469 }