More return documentation
[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 * @return bool
193 */
194 function checkRedirect( Title $title ) {
195 if ( !$this->reposInitialised ) {
196 $this->initialiseRepos();
197 }
198
199 $redir = $this->localRepo->checkRedirect( $title );
200 if( $redir ) {
201 return $redir;
202 }
203 foreach ( $this->foreignRepos as $repo ) {
204 $redir = $repo->checkRedirect( $title );
205 if ( $redir ) {
206 return $redir;
207 }
208 }
209 return false;
210 }
211
212 /**
213 * Find an instance of the file with this key, created at the specified time
214 * Returns false if the file does not exist.
215 *
216 * @param $hash String base 36 SHA-1 hash
217 * @param $options array Option array, same as findFile()
218 * @return File object or false if it is not found
219 */
220 function findFileFromKey( $hash, $options = array() ) {
221 if ( !$this->reposInitialised ) {
222 $this->initialiseRepos();
223 }
224
225 $file = $this->localRepo->findFileFromKey( $hash, $options );
226 if ( !$file ) {
227 foreach ( $this->foreignRepos as $repo ) {
228 $file = $repo->findFileFromKey( $hash, $options );
229 if ( $file ) break;
230 }
231 }
232 return $file;
233 }
234
235 /**
236 * Find all instances of files with this key
237 *
238 * @param $hash String base 36 SHA-1 hash
239 * @return Array of File objects
240 */
241 function findBySha1( $hash ) {
242 if ( !$this->reposInitialised ) {
243 $this->initialiseRepos();
244 }
245
246 $result = $this->localRepo->findBySha1( $hash );
247 foreach ( $this->foreignRepos as $repo ) {
248 $result = array_merge( $result, $repo->findBySha1( $hash ) );
249 }
250 return $result;
251 }
252
253 /**
254 * Get the repo instance with a given key.
255 * @return bool|LocalRepo
256 */
257 function getRepo( $index ) {
258 if ( !$this->reposInitialised ) {
259 $this->initialiseRepos();
260 }
261 if ( $index === 'local' ) {
262 return $this->localRepo;
263 } elseif ( isset( $this->foreignRepos[$index] ) ) {
264 return $this->foreignRepos[$index];
265 } else {
266 return false;
267 }
268 }
269 /**
270 * Get the repo instance by its name
271 * @return bool
272 */
273 function getRepoByName( $name ) {
274 if ( !$this->reposInitialised ) {
275 $this->initialiseRepos();
276 }
277 foreach ( $this->foreignRepos as $repo ) {
278 if ( $repo->name == $name)
279 return $repo;
280 }
281 return false;
282 }
283
284 /**
285 * Get the local repository, i.e. the one corresponding to the local image
286 * table. Files are typically uploaded to the local repository.
287 *
288 * @return LocalRepo
289 */
290 function getLocalRepo() {
291 return $this->getRepo( 'local' );
292 }
293
294 /**
295 * Call a function for each foreign repo, with the repo object as the
296 * first parameter.
297 *
298 * @param $callback Callback: the function to call
299 * @param $params Array: optional additional parameters to pass to the function
300 * @return bool
301 */
302 function forEachForeignRepo( $callback, $params = array() ) {
303 foreach( $this->foreignRepos as $repo ) {
304 $args = array_merge( array( $repo ), $params );
305 if( call_user_func_array( $callback, $args ) ) {
306 return true;
307 }
308 }
309 return false;
310 }
311
312 /**
313 * Does the installation have any foreign repos set up?
314 * @return Boolean
315 */
316 function hasForeignRepos() {
317 return (bool)$this->foreignRepos;
318 }
319
320 /**
321 * Initialise the $repos array
322 */
323 function initialiseRepos() {
324 if ( $this->reposInitialised ) {
325 return;
326 }
327 $this->reposInitialised = true;
328
329 $this->localRepo = $this->newRepo( $this->localInfo );
330 $this->foreignRepos = array();
331 foreach ( $this->foreignInfo as $key => $info ) {
332 $this->foreignRepos[$key] = $this->newRepo( $info );
333 }
334 }
335
336 /**
337 * Create a repo class based on an info structure
338 */
339 protected function newRepo( $info ) {
340 $class = $info['class'];
341 return new $class( $info );
342 }
343
344 /**
345 * Split a virtual URL into repo, zone and rel parts
346 * @param $url string
347 * @return array containing repo, zone and rel
348 */
349 function splitVirtualUrl( $url ) {
350 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
351 throw new MWException( __METHOD__.': unknown protocol' );
352 }
353
354 $bits = explode( '/', substr( $url, 9 ), 3 );
355 if ( count( $bits ) != 3 ) {
356 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
357 }
358 return $bits;
359 }
360
361 function getFileProps( $fileName ) {
362 if ( FileRepo::isVirtualUrl( $fileName ) ) {
363 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
364 if ( $repoName === '' ) {
365 $repoName = 'local';
366 }
367 $repo = $this->getRepo( $repoName );
368 return $repo->getFileProps( $fileName );
369 } else {
370 return FSFile::getPropsFromPath( $fileName );
371 }
372 }
373
374 /**
375 * Limit cache memory
376 */
377 protected function trimCache() {
378 while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
379 reset( $this->cache );
380 $key = key( $this->cache );
381 wfDebug( __METHOD__.": evicting $key\n" );
382 unset( $this->cache[$key] );
383 }
384 }
385
386 /**
387 * Clear RepoGroup process cache used for finding a file
388 * @param $title Title|null Title of the file or null to clear all files
389 */
390 public function clearCache( Title $title = null ) {
391 if ( $title == null ) {
392 $this->cache = array();
393 } else {
394 $dbKey = $title->getDBkey();
395 if ( isset( $this->cache[$dbKey] ) ) {
396 unset( $this->cache[$dbKey] );
397 }
398 }
399 }
400 }