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