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