Add TitleIsMovable hook so extensions can create new namespaces in which pages can...
[mediawiki.git] / includes / filerepo / RepoGroup.php
blobd487590884959603e9d0d4bd22f4970baf795470
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 /**
30 * @var RepoGroup
32 protected static $instance;
33 const MAX_CACHE_SIZE = 1000;
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
40 static function singleton() {
41 if ( self::$instance ) {
42 return self::$instance;
44 global $wgLocalFileRepo, $wgForeignFileRepos;
45 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
46 return self::$instance;
49 /**
50 * Destroy the singleton instance, so that a new one will be created next
51 * time singleton() is called.
53 static function destroySingleton() {
54 self::$instance = null;
57 /**
58 * Set the singleton instance to a given object
60 * @param $instance RepoGroup
62 static function setSingleton( $instance ) {
63 self::$instance = $instance;
66 /**
67 * Construct a group of file repositories.
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.
75 function __construct( $localInfo, $foreignInfo ) {
76 $this->localInfo = $localInfo;
77 $this->foreignInfo = $foreignInfo;
78 $this->cache = array();
81 /**
82 * Search repositories for an image.
83 * You can also use wfFindFile() to do this.
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.
91 * ignoreRedirect: If true, do not follow file redirects
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.
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
100 function findFile( $title, $options = array() ) {
101 if ( !is_array( $options ) ) {
102 // MW 1.15 compat
103 $options = array( 'time' => $options );
105 if ( !$this->reposInitialised ) {
106 $this->initialiseRepos();
108 if ( !($title instanceof Title) ) {
109 $title = Title::makeTitleSafe( NS_FILE, $title );
110 if ( !is_object( $title ) ) {
111 return false;
115 if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
116 throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
119 # Check the cache
120 if ( empty( $options['ignoreRedirect'] )
121 && empty( $options['private'] )
122 && empty( $options['bypassCache'] )
123 && $title->getNamespace() == NS_FILE )
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];
142 } else {
143 $useCache = false;
146 # Check the local repo
147 $image = $this->localRepo->findFile( $title, $options );
148 if ( $image ) {
149 if ( $useCache ) {
150 $cacheEntry = $image;
152 return $image;
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;
162 return $image;
165 # Not found, do not override negative cache
166 return false;
169 function findFiles( $inputItems ) {
170 if ( !$this->reposInitialised ) {
171 $this->initialiseRepos();
174 $items = array();
175 foreach ( $inputItems as $item ) {
176 if ( !is_array( $item ) ) {
177 $item = array( 'title' => $item );
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;
185 $images = $this->localRepo->findFiles( $items );
187 foreach ( $this->foreignRepos as $repo ) {
188 // Remove found files from $items
189 foreach ( $images as $name => $image ) {
190 unset( $items[$name] );
193 $images = array_merge( $images, $repo->findFiles( $items ) );
195 return $images;
199 * Interface for FileRepo::checkRedirect()
201 function checkRedirect( $title ) {
202 if ( !$this->reposInitialised ) {
203 $this->initialiseRepos();
206 $redir = $this->localRepo->checkRedirect( $title );
207 if( $redir ) {
208 return $redir;
210 foreach ( $this->foreignRepos as $repo ) {
211 $redir = $repo->checkRedirect( $title );
212 if ( $redir ) {
213 return $redir;
216 return false;
220 * Find an instance of the file with this key, created at the specified time
221 * Returns false if the file does not exist.
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
227 function findFileFromKey( $hash, $options = array() ) {
228 if ( !$this->reposInitialised ) {
229 $this->initialiseRepos();
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;
239 return $file;
243 * Find all instances of files with this key
245 * @param $hash String base 36 SHA-1 hash
246 * @return Array of File objects
248 function findBySha1( $hash ) {
249 if ( !$this->reposInitialised ) {
250 $this->initialiseRepos();
253 $result = $this->localRepo->findBySha1( $hash );
254 foreach ( $this->foreignRepos as $repo ) {
255 $result = array_merge( $result, $repo->findBySha1( $hash ) );
257 return $result;
261 * Get the repo instance with a given key.
263 function getRepo( $index ) {
264 if ( !$this->reposInitialised ) {
265 $this->initialiseRepos();
267 if ( $index === 'local' ) {
268 return $this->localRepo;
269 } elseif ( isset( $this->foreignRepos[$index] ) ) {
270 return $this->foreignRepos[$index];
271 } else {
272 return false;
276 * Get the repo instance by its name
278 function getRepoByName( $name ) {
279 if ( !$this->reposInitialised ) {
280 $this->initialiseRepos();
282 foreach ( $this->foreignRepos as $repo ) {
283 if ( $repo->name == $name)
284 return $repo;
286 return false;
290 * Get the local repository, i.e. the one corresponding to the local image
291 * table. Files are typically uploaded to the local repository.
293 * @return LocalRepo
295 function getLocalRepo() {
296 return $this->getRepo( 'local' );
300 * Call a function for each foreign repo, with the repo object as the
301 * first parameter.
303 * @param $callback Callback: the function to call
304 * @param $params Array: optional additional parameters to pass to the function
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;
313 return false;
317 * Does the installation have any foreign repos set up?
318 * @return Boolean
320 function hasForeignRepos() {
321 return (bool)$this->foreignRepos;
325 * Initialise the $repos array
327 function initialiseRepos() {
328 if ( $this->reposInitialised ) {
329 return;
331 $this->reposInitialised = true;
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 );
341 * Create a repo class based on an info structure
343 protected function newRepo( $info ) {
344 $class = $info['class'];
345 return new $class( $info );
349 * Split a virtual URL into repo, zone and rel parts
350 * @return an array containing repo, zone and rel
352 function splitVirtualUrl( $url ) {
353 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
354 throw new MWException( __METHOD__.': unknown protocol' );
357 $bits = explode( '/', substr( $url, 9 ), 3 );
358 if ( count( $bits ) != 3 ) {
359 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
361 return $bits;
364 function getFileProps( $fileName ) {
365 if ( FileRepo::isVirtualUrl( $fileName ) ) {
366 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
367 if ( $repoName === '' ) {
368 $repoName = 'local';
370 $repo = $this->getRepo( $repoName );
371 return $repo->getFileProps( $fileName );
372 } else {
373 return File::getPropsFromPath( $fileName );
378 * Limit cache memory
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] );