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