3 * Prioritized list of file repositories
10 * @defgroup FileRepo FileRepo
14 * Prioritized list of file repositories
25 var $foreignRepos, $reposInitialised = false;
26 var $localInfo, $foreignInfo;
32 protected static $instance;
33 const MAX_CACHE_SIZE
= 1000;
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.
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;
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;
58 * Set the singleton instance to a given object
60 * @param $instance RepoGroup
62 static function setSingleton( $instance ) {
63 self
::$instance = $instance;
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();
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
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 ) ) {
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 ) ) {
115 if ( $title->getNamespace() != NS_MEDIA
&& $title->getNamespace() != NS_FILE
) {
116 throw new MWException( __METHOD__
. ' received an Title object with incorrect namespace' );
120 if ( empty( $options['ignoreRedirect'] )
121 && empty( $options['private'] )
122 && empty( $options['bypassCache'] )
123 && $title->getNamespace() == NS_FILE
)
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;
135 return $this->cache
[$dbkey][$time];
137 # Add a negative cache entry, may be overridden
139 $this->cache
[$dbkey][$time] = false;
140 $cacheEntry =& $this->cache
[$dbkey][$time];
146 # Check the local repo
147 $image = $this->localRepo
->findFile( $title, $options );
150 $cacheEntry = $image;
155 # Check the foreign repos
156 foreach ( $this->foreignRepos
as $repo ) {
157 $image = $repo->findFile( $title, $options );
160 $cacheEntry = $image;
165 # Not found, do not override negative cache
169 function findFiles( $inputItems ) {
170 if ( !$this->reposInitialised
) {
171 $this->initialiseRepos();
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 ) );
199 * Interface for FileRepo::checkRedirect()
201 function checkRedirect( $title ) {
202 if ( !$this->reposInitialised
) {
203 $this->initialiseRepos();
206 $redir = $this->localRepo
->checkRedirect( $title );
210 foreach ( $this->foreignRepos
as $repo ) {
211 $redir = $repo->checkRedirect( $title );
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 );
234 foreach ( $this->foreignRepos
as $repo ) {
235 $file = $repo->findFileFromKey( $hash, $options );
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 ) );
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];
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)
290 * Get the local repository, i.e. the one corresponding to the local image
291 * table. Files are typically uploaded to the local repository.
295 function getLocalRepo() {
296 return $this->getRepo( 'local' );
300 * Call a function for each foreign repo, with the repo object as the
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 ) ) {
317 * Does the installation have any foreign repos set up?
320 function hasForeignRepos() {
321 return (bool)$this->foreignRepos
;
325 * Initialise the $repos array
327 function initialiseRepos() {
328 if ( $this->reposInitialised
) {
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" );
364 function getFileProps( $fileName ) {
365 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
366 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
367 if ( $repoName === '' ) {
370 $repo = $this->getRepo( $repoName );
371 return $repo->getFileProps( $fileName );
373 return File
::getPropsFromPath( $fileName );
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] );