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