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