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