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;
29 protected static $instance;
30 const MAX_CACHE_SIZE
= 1000;
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.
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;
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;
55 * Set the singleton instance to a given object
57 static function setSingleton( $instance ) {
58 self
::$instance = $instance;
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();
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
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 ) ) {
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 ) ) {
110 if ( $title->getNamespace() != NS_MEDIA
&& $title->getNamespace() != NS_FILE
) {
111 throw new MWException( __METHOD__
. ' received an Title object with incorrect namespace' );
115 if ( empty( $options['ignoreRedirect'] )
116 && empty( $options['private'] )
117 && empty( $options['bypassCache'] )
118 && $title->getNamespace() == NS_FILE
)
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;
130 return $this->cache
[$dbkey][$time];
132 # Add a negative cache entry, may be overridden
134 $this->cache
[$dbkey][$time] = false;
135 $cacheEntry =& $this->cache
[$dbkey][$time];
141 # Check the local repo
142 $image = $this->localRepo
->findFile( $title, $options );
145 $cacheEntry = $image;
150 # Check the foreign repos
151 foreach ( $this->foreignRepos
as $repo ) {
152 $image = $repo->findFile( $title, $options );
155 $cacheEntry = $image;
160 # Not found, do not override negative cache
164 function findFiles( $inputItems ) {
165 if ( !$this->reposInitialised
) {
166 $this->initialiseRepos();
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 ) );
194 * Interface for FileRepo::checkRedirect()
196 function checkRedirect( $title ) {
197 if ( !$this->reposInitialised
) {
198 $this->initialiseRepos();
201 $redir = $this->localRepo
->checkRedirect( $title );
205 foreach ( $this->foreignRepos
as $repo ) {
206 $redir = $repo->checkRedirect( $title );
215 * Find an instance of the file with this key, created at the specified time
216 * Returns false if the file does not exist.
218 * @param $hash String base 36 SHA-1 hash
219 * @param $options Option array, same as findFile()
220 * @return File object or false if it is not found
222 function findFileFromKey( $hash, $options = array() ) {
223 if ( !$this->reposInitialised
) {
224 $this->initialiseRepos();
227 $file = $this->localRepo
->findFileFromKey( $hash, $options );
229 foreach ( $this->foreignRepos
as $repo ) {
230 $file = $repo->findFileFromKey( $hash, $options );
238 * Find all instances of files with this key
240 * @param $hash String base 36 SHA-1 hash
241 * @return Array of File objects
243 function findBySha1( $hash ) {
244 if ( !$this->reposInitialised
) {
245 $this->initialiseRepos();
248 $result = $this->localRepo
->findBySha1( $hash );
249 foreach ( $this->foreignRepos
as $repo ) {
250 $result = array_merge( $result, $repo->findBySha1( $hash ) );
256 * Get the repo instance with a given key.
258 function getRepo( $index ) {
259 if ( !$this->reposInitialised
) {
260 $this->initialiseRepos();
262 if ( $index === 'local' ) {
263 return $this->localRepo
;
264 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
265 return $this->foreignRepos
[$index];
271 * Get the repo instance by its name
273 function getRepoByName( $name ) {
274 if ( !$this->reposInitialised
) {
275 $this->initialiseRepos();
277 foreach ( $this->foreignRepos
as $repo ) {
278 if ( $repo->name
== $name)
285 * Get the local repository, i.e. the one corresponding to the local image
286 * table. Files are typically uploaded to the local repository.
288 function getLocalRepo() {
289 return $this->getRepo( 'local' );
293 * Call a function for each foreign repo, with the repo object as the
296 * @param $callback Callback: the function to call
297 * @param $params Array: optional additional parameters to pass to the function
299 function forEachForeignRepo( $callback, $params = array() ) {
300 foreach( $this->foreignRepos
as $repo ) {
301 $args = array_merge( array( $repo ), $params );
302 if( call_user_func_array( $callback, $args ) ) {
310 * Does the installation have any foreign repos set up?
313 function hasForeignRepos() {
314 return (bool)$this->foreignRepos
;
318 * Initialise the $repos array
320 function initialiseRepos() {
321 if ( $this->reposInitialised
) {
324 $this->reposInitialised
= true;
326 $this->localRepo
= $this->newRepo( $this->localInfo
);
327 $this->foreignRepos
= array();
328 foreach ( $this->foreignInfo
as $key => $info ) {
329 $this->foreignRepos
[$key] = $this->newRepo( $info );
334 * Create a repo class based on an info structure
336 protected function newRepo( $info ) {
337 $class = $info['class'];
338 return new $class( $info );
342 * Split a virtual URL into repo, zone and rel parts
343 * @return an array containing repo, zone and rel
345 function splitVirtualUrl( $url ) {
346 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
347 throw new MWException( __METHOD__
.': unknown protocol' );
350 $bits = explode( '/', substr( $url, 9 ), 3 );
351 if ( count( $bits ) != 3 ) {
352 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
357 function getFileProps( $fileName ) {
358 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
359 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
360 if ( $repoName === '' ) {
363 $repo = $this->getRepo( $repoName );
364 return $repo->getFileProps( $fileName );
366 return File
::getPropsFromPath( $fileName );
373 function trimCache() {
374 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
375 reset( $this->cache
);
376 $key = key( $this->cache
);
377 wfDebug( __METHOD__
.": evicting $key\n" );
378 unset( $this->cache
[$key] );