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