8dae87cb5278887c2f81ec00a10c71c83bb7d32a
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 ) ) {
104 if ( empty( $options['ignoreRedirect'] )
105 && empty( $options['private'] )
106 && empty( $options['bypassCache'] ) )
109 $time = isset( $options['time'] ) ?
$options['time'] : '';
110 $dbkey = $title->getDBkey();
111 if ( isset( $this->cache
[$dbkey][$time] ) ) {
112 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
113 # Move it to the end of the list so that we can delete the LRU entry later
114 $tmp = $this->cache
[$dbkey];
115 unset( $this->cache
[$dbkey] );
116 $this->cache
[$dbkey] = $tmp;
118 return $this->cache
[$dbkey][$time];
120 # Add a negative cache entry, may be overridden
122 $this->cache
[$dbkey][$time] = false;
123 $cacheEntry =& $this->cache
[$dbkey][$time];
129 # Check the local repo
130 $image = $this->localRepo
->findFile( $title, $options );
133 $cacheEntry = $image;
138 # Check the foreign repos
139 foreach ( $this->foreignRepos
as $repo ) {
140 $image = $repo->findFile( $title, $options );
143 $cacheEntry = $image;
148 # Not found, do not override negative cache
152 function findFiles( $inputItems ) {
153 if ( !$this->reposInitialised
) {
154 $this->initialiseRepos();
158 foreach ( $inputItems as $item ) {
159 if ( !is_array( $item ) ) {
160 $item = array( 'title' => $item );
162 if ( !( $item['title'] instanceof Title
) )
163 $item['title'] = Title
::makeTitleSafe( NS_FILE
, $item['title'] );
164 if ( $item['title'] )
165 $items[$item['title']->getDBkey()] = $item;
168 $images = $this->localRepo
->findFiles( $items );
170 foreach ( $this->foreignRepos
as $repo ) {
171 // Remove found files from $items
172 foreach ( $images as $name => $image ) {
173 unset( $items[$name] );
176 $images = array_merge( $images, $repo->findFiles( $items ) );
182 * Interface for FileRepo::checkRedirect()
184 function checkRedirect( $title ) {
185 if ( !$this->reposInitialised
) {
186 $this->initialiseRepos();
189 $redir = $this->localRepo
->checkRedirect( $title );
193 foreach ( $this->foreignRepos
as $repo ) {
194 $redir = $repo->checkRedirect( $title );
202 function findBySha1( $hash ) {
203 if ( !$this->reposInitialised
) {
204 $this->initialiseRepos();
207 $result = $this->localRepo
->findBySha1( $hash );
208 foreach ( $this->foreignRepos
as $repo )
209 $result = array_merge( $result, $repo->findBySha1( $hash ) );
214 * Get the repo instance with a given key.
216 function getRepo( $index ) {
217 if ( !$this->reposInitialised
) {
218 $this->initialiseRepos();
220 if ( $index === 'local' ) {
221 return $this->localRepo
;
222 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
223 return $this->foreignRepos
[$index];
229 * Get the repo instance by its name
231 function getRepoByName( $name ) {
232 if ( !$this->reposInitialised
) {
233 $this->initialiseRepos();
235 foreach ( $this->foreignRepos
as $key => $repo ) {
236 if ( $repo->name
== $name)
243 * Get the local repository, i.e. the one corresponding to the local image
244 * table. Files are typically uploaded to the local repository.
246 function getLocalRepo() {
247 return $this->getRepo( 'local' );
251 * Call a function for each foreign repo, with the repo object as the
254 * @param $callback Callback: the function to call
255 * @param $params Array: optional additional parameters to pass to the function
257 function forEachForeignRepo( $callback, $params = array() ) {
258 foreach( $this->foreignRepos
as $repo ) {
259 $args = array_merge( array( $repo ), $params );
260 if( call_user_func_array( $callback, $args ) ) {
268 * Does the installation have any foreign repos set up?
271 function hasForeignRepos() {
272 return (bool)$this->foreignRepos
;
276 * Initialise the $repos array
278 function initialiseRepos() {
279 if ( $this->reposInitialised
) {
282 $this->reposInitialised
= true;
284 $this->localRepo
= $this->newRepo( $this->localInfo
);
285 $this->foreignRepos
= array();
286 foreach ( $this->foreignInfo
as $key => $info ) {
287 $this->foreignRepos
[$key] = $this->newRepo( $info );
292 * Create a repo class based on an info structure
294 protected function newRepo( $info ) {
295 $class = $info['class'];
296 return new $class( $info );
300 * Split a virtual URL into repo, zone and rel parts
301 * @return an array containing repo, zone and rel
303 function splitVirtualUrl( $url ) {
304 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
305 throw new MWException( __METHOD__
.': unknown protoocl' );
308 $bits = explode( '/', substr( $url, 9 ), 3 );
309 if ( count( $bits ) != 3 ) {
310 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
315 function getFileProps( $fileName ) {
316 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
317 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
318 if ( $repoName === '' ) {
321 $repo = $this->getRepo( $repoName );
322 return $repo->getFileProps( $fileName );
324 return File
::getPropsFromPath( $fileName );
331 function trimCache() {
332 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
333 reset( $this->cache
);
334 $key = key( $this->cache
);
335 wfDebug( __METHOD__
.": evicting $key\n" );
336 unset( $this->cache
[$key] );