Localisation updates for core and extension messages from translatewiki.net (2011...
[mediawiki.git] / includes / filerepo / RepoGroup.php
blob4c712fd5e4cef26fab7d6cbd432a71bb4985b04b
1 <?php
2 /**
3 * Prioritized list of file repositories
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * @defgroup FileRepo FileRepo
13 /**
14 * Prioritized list of file repositories
16 * @ingroup FileRepo
18 class RepoGroup {
20 /**
21 * @var LocalRepo
23 var $localRepo;
25 var $foreignRepos, $reposInitialised = false;
26 var $localInfo, $foreignInfo;
27 var $cache;
29 protected static $instance;
30 const MAX_CACHE_SIZE = 1000;
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
37 static function singleton() {
38 if ( self::$instance ) {
39 return self::$instance;
41 global $wgLocalFileRepo, $wgForeignFileRepos;
42 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
43 return self::$instance;
46 /**
47 * Destroy the singleton instance, so that a new one will be created next
48 * time singleton() is called.
50 static function destroySingleton() {
51 self::$instance = null;
54 /**
55 * Set the singleton instance to a given object
57 static function setSingleton( $instance ) {
58 self::$instance = $instance;
61 /**
62 * Construct a group of file repositories.
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.
70 function __construct( $localInfo, $foreignInfo ) {
71 $this->localInfo = $localInfo;
72 $this->foreignInfo = $foreignInfo;
73 $this->cache = array();
76 /**
77 * Search repositories for an image.
78 * You can also use wfFindFile() to do this.
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.
86 * ignoreRedirect: If true, do not follow file redirects
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.
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
95 function findFile( $title, $options = array() ) {
96 if ( !is_array( $options ) ) {
97 // MW 1.15 compat
98 $options = array( 'time' => $options );
100 if ( !$this->reposInitialised ) {
101 $this->initialiseRepos();
103 if ( !($title instanceof Title) ) {
104 $title = Title::makeTitleSafe( NS_FILE, $title );
105 if ( !is_object( $title ) ) {
106 return false;
110 if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
111 throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
114 # Check the cache
115 if ( empty( $options['ignoreRedirect'] )
116 && empty( $options['private'] )
117 && empty( $options['bypassCache'] )
118 && $title->getNamespace() == NS_FILE )
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];
137 } else {
138 $useCache = false;
141 # Check the local repo
142 $image = $this->localRepo->findFile( $title, $options );
143 if ( $image ) {
144 if ( $useCache ) {
145 $cacheEntry = $image;
147 return $image;
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;
157 return $image;
160 # Not found, do not override negative cache
161 return false;
164 function findFiles( $inputItems ) {
165 if ( !$this->reposInitialised ) {
166 $this->initialiseRepos();
169 $items = array();
170 foreach ( $inputItems as $item ) {
171 if ( !is_array( $item ) ) {
172 $item = array( 'title' => $item );
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;
180 $images = $this->localRepo->findFiles( $items );
182 foreach ( $this->foreignRepos as $repo ) {
183 // Remove found files from $items
184 foreach ( $images as $name => $image ) {
185 unset( $items[$name] );
188 $images = array_merge( $images, $repo->findFiles( $items ) );
190 return $images;
194 * Interface for FileRepo::checkRedirect()
196 function checkRedirect( $title ) {
197 if ( !$this->reposInitialised ) {
198 $this->initialiseRepos();
201 $redir = $this->localRepo->checkRedirect( $title );
202 if( $redir ) {
203 return $redir;
205 foreach ( $this->foreignRepos as $repo ) {
206 $redir = $repo->checkRedirect( $title );
207 if ( $redir ) {
208 return $redir;
211 return false;
214 function findBySha1( $hash ) {
215 if ( !$this->reposInitialised ) {
216 $this->initialiseRepos();
219 $result = $this->localRepo->findBySha1( $hash );
220 foreach ( $this->foreignRepos as $repo )
221 $result = array_merge( $result, $repo->findBySha1( $hash ) );
222 return $result;
226 * Get the repo instance with a given key.
228 function getRepo( $index ) {
229 if ( !$this->reposInitialised ) {
230 $this->initialiseRepos();
232 if ( $index === 'local' ) {
233 return $this->localRepo;
234 } elseif ( isset( $this->foreignRepos[$index] ) ) {
235 return $this->foreignRepos[$index];
236 } else {
237 return false;
241 * Get the repo instance by its name
243 function getRepoByName( $name ) {
244 if ( !$this->reposInitialised ) {
245 $this->initialiseRepos();
247 foreach ( $this->foreignRepos as $repo ) {
248 if ( $repo->name == $name)
249 return $repo;
251 return false;
255 * Get the local repository, i.e. the one corresponding to the local image
256 * table. Files are typically uploaded to the local repository.
258 function getLocalRepo() {
259 return $this->getRepo( 'local' );
263 * Call a function for each foreign repo, with the repo object as the
264 * first parameter.
266 * @param $callback Callback: the function to call
267 * @param $params Array: optional additional parameters to pass to the function
269 function forEachForeignRepo( $callback, $params = array() ) {
270 foreach( $this->foreignRepos as $repo ) {
271 $args = array_merge( array( $repo ), $params );
272 if( call_user_func_array( $callback, $args ) ) {
273 return true;
276 return false;
280 * Does the installation have any foreign repos set up?
281 * @return Boolean
283 function hasForeignRepos() {
284 return (bool)$this->foreignRepos;
288 * Initialise the $repos array
290 function initialiseRepos() {
291 if ( $this->reposInitialised ) {
292 return;
294 $this->reposInitialised = true;
296 $this->localRepo = $this->newRepo( $this->localInfo );
297 $this->foreignRepos = array();
298 foreach ( $this->foreignInfo as $key => $info ) {
299 $this->foreignRepos[$key] = $this->newRepo( $info );
304 * Create a repo class based on an info structure
306 protected function newRepo( $info ) {
307 $class = $info['class'];
308 return new $class( $info );
312 * Split a virtual URL into repo, zone and rel parts
313 * @return an array containing repo, zone and rel
315 function splitVirtualUrl( $url ) {
316 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
317 throw new MWException( __METHOD__.': unknown protoocl' );
320 $bits = explode( '/', substr( $url, 9 ), 3 );
321 if ( count( $bits ) != 3 ) {
322 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
324 return $bits;
327 function getFileProps( $fileName ) {
328 if ( FileRepo::isVirtualUrl( $fileName ) ) {
329 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
330 if ( $repoName === '' ) {
331 $repoName = 'local';
333 $repo = $this->getRepo( $repoName );
334 return $repo->getFileProps( $fileName );
335 } else {
336 return File::getPropsFromPath( $fileName );
341 * Limit cache memory
343 function trimCache() {
344 while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
345 reset( $this->cache );
346 $key = key( $this->cache );
347 wfDebug( __METHOD__.": evicting $key\n" );
348 unset( $this->cache[$key] );