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