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