FU r102073:
[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 $title = File::normalizeTitle( $title );
112 if ( !$title ) {
113 return false;
114 }
115
116 # Check the cache
117 if ( empty( $options['ignoreRedirect'] )
118 && empty( $options['private'] )
119 && empty( $options['bypassCache'] ) )
120 {
121 $useCache = true;
122 $time = isset( $options['time'] ) ? $options['time'] : '';
123 $dbkey = $title->getDBkey();
124 if ( isset( $this->cache[$dbkey][$time] ) ) {
125 wfDebug( __METHOD__.": got File:$dbkey from process cache\n" );
126 # Move it to the end of the list so that we can delete the LRU entry later
127 $tmp = $this->cache[$dbkey];
128 unset( $this->cache[$dbkey] );
129 $this->cache[$dbkey] = $tmp;
130 # Return the entry
131 return $this->cache[$dbkey][$time];
132 } else {
133 # Add a negative cache entry, may be overridden
134 $this->trimCache();
135 $this->cache[$dbkey][$time] = false;
136 $cacheEntry =& $this->cache[$dbkey][$time];
137 }
138 } else {
139 $useCache = false;
140 }
141
142 # Check the local repo
143 $image = $this->localRepo->findFile( $title, $options );
144 if ( $image ) {
145 if ( $useCache ) {
146 $cacheEntry = $image;
147 }
148 return $image;
149 }
150
151 # Check the foreign repos
152 foreach ( $this->foreignRepos as $repo ) {
153 $image = $repo->findFile( $title, $options );
154 if ( $image ) {
155 if ( $useCache ) {
156 $cacheEntry = $image;
157 }
158 return $image;
159 }
160 }
161 # Not found, do not override negative cache
162 return false;
163 }
164
165 function findFiles( $inputItems ) {
166 if ( !$this->reposInitialised ) {
167 $this->initialiseRepos();
168 }
169
170 $items = array();
171 foreach ( $inputItems as $item ) {
172 if ( !is_array( $item ) ) {
173 $item = array( 'title' => $item );
174 }
175 $item['title'] = File::normalizeTitle( $item['title'] );
176 if ( $item['title'] ) {
177 $items[$item['title']->getDBkey()] = $item;
178 }
179 }
180
181 $images = $this->localRepo->findFiles( $items );
182
183 foreach ( $this->foreignRepos as $repo ) {
184 // Remove found files from $items
185 foreach ( $images as $name => $image ) {
186 unset( $items[$name] );
187 }
188
189 $images = array_merge( $images, $repo->findFiles( $items ) );
190 }
191 return $images;
192 }
193
194 /**
195 * Interface for FileRepo::checkRedirect()
196 */
197 function checkRedirect( Title $title ) {
198 if ( !$this->reposInitialised ) {
199 $this->initialiseRepos();
200 }
201
202 $redir = $this->localRepo->checkRedirect( $title );
203 if( $redir ) {
204 return $redir;
205 }
206 foreach ( $this->foreignRepos as $repo ) {
207 $redir = $repo->checkRedirect( $title );
208 if ( $redir ) {
209 return $redir;
210 }
211 }
212 return false;
213 }
214
215 /**
216 * Find an instance of the file with this key, created at the specified time
217 * Returns false if the file does not exist.
218 *
219 * @param $hash String base 36 SHA-1 hash
220 * @param $options Option array, same as findFile()
221 * @return File object or false if it is not found
222 */
223 function findFileFromKey( $hash, $options = array() ) {
224 if ( !$this->reposInitialised ) {
225 $this->initialiseRepos();
226 }
227
228 $file = $this->localRepo->findFileFromKey( $hash, $options );
229 if ( !$file ) {
230 foreach ( $this->foreignRepos as $repo ) {
231 $file = $repo->findFileFromKey( $hash, $options );
232 if ( $file ) break;
233 }
234 }
235 return $file;
236 }
237
238 /**
239 * Find all instances of files with this key
240 *
241 * @param $hash String base 36 SHA-1 hash
242 * @return Array of File objects
243 */
244 function findBySha1( $hash ) {
245 if ( !$this->reposInitialised ) {
246 $this->initialiseRepos();
247 }
248
249 $result = $this->localRepo->findBySha1( $hash );
250 foreach ( $this->foreignRepos as $repo ) {
251 $result = array_merge( $result, $repo->findBySha1( $hash ) );
252 }
253 return $result;
254 }
255
256 /**
257 * Get the repo instance with a given key.
258 */
259 function getRepo( $index ) {
260 if ( !$this->reposInitialised ) {
261 $this->initialiseRepos();
262 }
263 if ( $index === 'local' ) {
264 return $this->localRepo;
265 } elseif ( isset( $this->foreignRepos[$index] ) ) {
266 return $this->foreignRepos[$index];
267 } else {
268 return false;
269 }
270 }
271 /**
272 * Get the repo instance by its name
273 */
274 function getRepoByName( $name ) {
275 if ( !$this->reposInitialised ) {
276 $this->initialiseRepos();
277 }
278 foreach ( $this->foreignRepos as $repo ) {
279 if ( $repo->name == $name)
280 return $repo;
281 }
282 return false;
283 }
284
285 /**
286 * Get the local repository, i.e. the one corresponding to the local image
287 * table. Files are typically uploaded to the local repository.
288 *
289 * @return LocalRepo
290 */
291 function getLocalRepo() {
292 return $this->getRepo( 'local' );
293 }
294
295 /**
296 * Call a function for each foreign repo, with the repo object as the
297 * first parameter.
298 *
299 * @param $callback Callback: the function to call
300 * @param $params Array: optional additional parameters to pass to the function
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 * @return an array containing repo, zone and rel
347 */
348 function splitVirtualUrl( $url ) {
349 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
350 throw new MWException( __METHOD__.': unknown protocol' );
351 }
352
353 $bits = explode( '/', substr( $url, 9 ), 3 );
354 if ( count( $bits ) != 3 ) {
355 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
356 }
357 return $bits;
358 }
359
360 function getFileProps( $fileName ) {
361 if ( FileRepo::isVirtualUrl( $fileName ) ) {
362 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
363 if ( $repoName === '' ) {
364 $repoName = 'local';
365 }
366 $repo = $this->getRepo( $repoName );
367 return $repo->getFileProps( $fileName );
368 } else {
369 return File::getPropsFromPath( $fileName );
370 }
371 }
372
373 /**
374 * Limit cache memory
375 */
376 protected function trimCache() {
377 while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
378 reset( $this->cache );
379 $key = key( $this->cache );
380 wfDebug( __METHOD__.": evicting $key\n" );
381 unset( $this->cache[$key] );
382 }
383 }
384
385 /**
386 * Clear RepoGroup process cache used for finding a file
387 * @param $title Title|null Title of the file or null to clear all files
388 */
389 public function clearCache( Title $title = null ) {
390 if ( $title == null ) {
391 $this->cache = array();
392 } else {
393 $dbKey = $title->getDBkey();
394 if ( isset( $this->cache[$dbKey] ) ) {
395 unset( $this->cache[$dbKey] );
396 }
397 }
398 }
399 }