3 * Prioritized list of file repositories
10 * @defgroup FileRepo FileRepo
14 * Prioritized list of file repositories
19 var $localRepo, $foreignRepos, $reposInitialised = false;
20 var $localInfo, $foreignInfo;
23 protected static $instance;
24 const MAX_CACHE_SIZE
= 1000;
27 * Get a RepoGroup instance. At present only one instance of RepoGroup is
28 * needed in a MediaWiki invocation, this may change in the future.
30 static function singleton() {
31 if ( self
::$instance ) {
32 return self
::$instance;
34 global $wgLocalFileRepo, $wgForeignFileRepos;
35 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
36 return self
::$instance;
40 * Destroy the singleton instance, so that a new one will be created next
41 * time singleton() is called.
43 static function destroySingleton() {
44 self
::$instance = null;
48 * Set the singleton instance to a given object
50 static function setSingleton( $instance ) {
51 self
::$instance = $instance;
55 * Construct a group of file repositories.
57 * @param $localInfo Associative array for local repo's info
58 * @param $foreignInfo Array of repository info arrays.
59 * Each info array is an associative array with the 'class' member
60 * giving the class name. The entire array is passed to the repository
61 * constructor as the first parameter.
63 function __construct( $localInfo, $foreignInfo ) {
64 $this->localInfo
= $localInfo;
65 $this->foreignInfo
= $foreignInfo;
66 $this->cache
= array();
70 * Search repositories for an image.
71 * You can also use wfFindFile() to do this.
73 * @param $title Mixed: Title object or string
74 * @param $options Associative array of options:
75 * time: requested time for an archived image, or false for the
76 * current version. An image object will be returned which was
77 * created at the specified time.
79 * ignoreRedirect: If true, do not follow file redirects
81 * private: If true, return restricted (deleted) files if the current
82 * user is allowed to view them. Otherwise, such files will not
85 * bypassCache: If true, do not use the process-local cache of File objects
86 * @return File object or false if it is not found
88 function findFile( $title, $options = array() ) {
89 if ( !is_array( $options ) ) {
91 $options = array( 'time' => $options );
93 if ( !$this->reposInitialised
) {
94 $this->initialiseRepos();
96 if ( !($title instanceof Title
) ) {
97 $title = Title
::makeTitleSafe( NS_FILE
, $title );
98 if ( !is_object( $title ) ) {
103 if ( $title->getNamespace() != NS_MEDIA
&& $title->getNamespace() != NS_FILE
) {
104 throw new MWException( __METHOD__
. ' recieved an Title object with incorrect namespace' );
108 if ( empty( $options['ignoreRedirect'] )
109 && empty( $options['private'] )
110 && empty( $options['bypassCache'] )
111 && $title->getNamespace() == NS_FILE
)
114 $time = isset( $options['time'] ) ?
$options['time'] : '';
115 $dbkey = $title->getDBkey();
116 if ( isset( $this->cache
[$dbkey][$time] ) ) {
117 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
118 # Move it to the end of the list so that we can delete the LRU entry later
119 $tmp = $this->cache
[$dbkey];
120 unset( $this->cache
[$dbkey] );
121 $this->cache
[$dbkey] = $tmp;
123 return $this->cache
[$dbkey][$time];
125 # Add a negative cache entry, may be overridden
127 $this->cache
[$dbkey][$time] = false;
128 $cacheEntry =& $this->cache
[$dbkey][$time];
134 # Check the local repo
135 $image = $this->localRepo
->findFile( $title, $options );
138 $cacheEntry = $image;
143 # Check the foreign repos
144 foreach ( $this->foreignRepos
as $repo ) {
145 $image = $repo->findFile( $title, $options );
148 $cacheEntry = $image;
153 # Not found, do not override negative cache
157 function findFiles( $inputItems ) {
158 if ( !$this->reposInitialised
) {
159 $this->initialiseRepos();
163 foreach ( $inputItems as $item ) {
164 if ( !is_array( $item ) ) {
165 $item = array( 'title' => $item );
167 if ( !( $item['title'] instanceof Title
) )
168 $item['title'] = Title
::makeTitleSafe( NS_FILE
, $item['title'] );
169 if ( $item['title'] )
170 $items[$item['title']->getDBkey()] = $item;
173 $images = $this->localRepo
->findFiles( $items );
175 foreach ( $this->foreignRepos
as $repo ) {
176 // Remove found files from $items
177 foreach ( $images as $name => $image ) {
178 unset( $items[$name] );
181 $images = array_merge( $images, $repo->findFiles( $items ) );
187 * Interface for FileRepo::checkRedirect()
189 function checkRedirect( $title ) {
190 if ( !$this->reposInitialised
) {
191 $this->initialiseRepos();
194 $redir = $this->localRepo
->checkRedirect( $title );
198 foreach ( $this->foreignRepos
as $repo ) {
199 $redir = $repo->checkRedirect( $title );
207 function findBySha1( $hash ) {
208 if ( !$this->reposInitialised
) {
209 $this->initialiseRepos();
212 $result = $this->localRepo
->findBySha1( $hash );
213 foreach ( $this->foreignRepos
as $repo )
214 $result = array_merge( $result, $repo->findBySha1( $hash ) );
219 * Get the repo instance with a given key.
221 function getRepo( $index ) {
222 if ( !$this->reposInitialised
) {
223 $this->initialiseRepos();
225 if ( $index === 'local' ) {
226 return $this->localRepo
;
227 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
228 return $this->foreignRepos
[$index];
234 * Get the repo instance by its name
236 function getRepoByName( $name ) {
237 if ( !$this->reposInitialised
) {
238 $this->initialiseRepos();
240 foreach ( $this->foreignRepos
as $repo ) {
241 if ( $repo->name
== $name)
248 * Get the local repository, i.e. the one corresponding to the local image
249 * table. Files are typically uploaded to the local repository.
251 function getLocalRepo() {
252 return $this->getRepo( 'local' );
256 * Call a function for each foreign repo, with the repo object as the
259 * @param $callback Callback: the function to call
260 * @param $params Array: optional additional parameters to pass to the function
262 function forEachForeignRepo( $callback, $params = array() ) {
263 foreach( $this->foreignRepos
as $repo ) {
264 $args = array_merge( array( $repo ), $params );
265 if( call_user_func_array( $callback, $args ) ) {
273 * Does the installation have any foreign repos set up?
276 function hasForeignRepos() {
277 return (bool)$this->foreignRepos
;
281 * Initialise the $repos array
283 function initialiseRepos() {
284 if ( $this->reposInitialised
) {
287 $this->reposInitialised
= true;
289 $this->localRepo
= $this->newRepo( $this->localInfo
);
290 $this->foreignRepos
= array();
291 foreach ( $this->foreignInfo
as $key => $info ) {
292 $this->foreignRepos
[$key] = $this->newRepo( $info );
297 * Create a repo class based on an info structure
299 protected function newRepo( $info ) {
300 $class = $info['class'];
301 return new $class( $info );
305 * Split a virtual URL into repo, zone and rel parts
306 * @return an array containing repo, zone and rel
308 function splitVirtualUrl( $url ) {
309 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
310 throw new MWException( __METHOD__
.': unknown protoocl' );
313 $bits = explode( '/', substr( $url, 9 ), 3 );
314 if ( count( $bits ) != 3 ) {
315 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
320 function getFileProps( $fileName ) {
321 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
322 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
323 if ( $repoName === '' ) {
326 $repo = $this->getRepo( $repoName );
327 return $repo->getFileProps( $fileName );
329 return File
::getPropsFromPath( $fileName );
336 function trimCache() {
337 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
338 reset( $this->cache
);
339 $key = key( $this->cache
);
340 wfDebug( __METHOD__
.": evicting $key\n" );
341 unset( $this->cache
[$key] );