3 * @defgroup FileRepo FileRepo
11 * Prioritized list of file repositories
14 var $localRepo, $foreignRepos, $reposInitialised = false;
15 var $localInfo, $foreignInfo;
18 protected static $instance;
19 const MAX_CACHE_SIZE
= 1000;
22 * Get a RepoGroup instance. At present only one instance of RepoGroup is
23 * needed in a MediaWiki invocation, this may change in the future.
25 static function singleton() {
26 if ( self
::$instance ) {
27 return self
::$instance;
29 global $wgLocalFileRepo, $wgForeignFileRepos;
30 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
31 return self
::$instance;
35 * Destroy the singleton instance, so that a new one will be created next
36 * time singleton() is called.
38 static function destroySingleton() {
39 self
::$instance = null;
43 * Set the singleton instance to a given object
45 static function setSingleton( $instance ) {
46 self
::$instance = $instance;
50 * Construct a group of file repositories.
52 * @param $localInfo Associative array for local repo's info
53 * @param $foreignInfo Array of repository info arrays.
54 * Each info array is an associative array with the 'class' member
55 * giving the class name. The entire array is passed to the repository
56 * constructor as the first parameter.
58 function __construct( $localInfo, $foreignInfo ) {
59 $this->localInfo
= $localInfo;
60 $this->foreignInfo
= $foreignInfo;
61 $this->cache
= array();
65 * Search repositories for an image.
66 * You can also use wfFindFile() to do this.
68 * @param $title Mixed: Title object or string
69 * @param $options Associative array of options:
70 * time: requested time for an archived image, or false for the
71 * current version. An image object will be returned which was
72 * created at the specified time.
74 * ignoreRedirect: If true, do not follow file redirects
76 * private: If true, return restricted (deleted) files if the current
77 * user is allowed to view them. Otherwise, such files will not
80 * bypassCache: If true, do not use the process-local cache of File objects
81 * @return File object or false if it is not found
83 function findFile( $title, $options = array() ) {
84 if ( !is_array( $options ) ) {
86 $options = array( 'time' => $options );
88 if ( !$this->reposInitialised
) {
89 $this->initialiseRepos();
91 if ( !($title instanceof Title
) ) {
92 $title = Title
::makeTitleSafe( NS_FILE
, $title );
93 if ( !is_object( $title ) ) {
99 if ( empty( $options['ignoreRedirect'] )
100 && empty( $options['private'] )
101 && empty( $options['bypassCache'] ) )
104 $time = isset( $options['time'] ) ?
$options['time'] : '';
105 $dbkey = $title->getDBkey();
106 if ( isset( $this->cache
[$dbkey][$time] ) ) {
107 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
108 # Move it to the end of the list so that we can delete the LRU entry later
109 $tmp = $this->cache
[$dbkey];
110 unset( $this->cache
[$dbkey] );
111 $this->cache
[$dbkey] = $tmp;
113 return $this->cache
[$dbkey][$time];
115 # Add a negative cache entry, may be overridden
117 $this->cache
[$dbkey][$time] = false;
118 $cacheEntry =& $this->cache
[$dbkey][$time];
124 # Check the local repo
125 $image = $this->localRepo
->findFile( $title, $options );
128 $cacheEntry = $image;
133 # Check the foreign repos
134 foreach ( $this->foreignRepos
as $repo ) {
135 $image = $repo->findFile( $title, $options );
138 $cacheEntry = $image;
143 # Not found, do not override negative cache
147 function findFiles( $inputItems ) {
148 if ( !$this->reposInitialised
) {
149 $this->initialiseRepos();
153 foreach ( $inputItems as $item ) {
154 if ( !is_array( $item ) ) {
155 $item = array( 'title' => $item );
157 if ( !( $item['title'] instanceof Title
) )
158 $item['title'] = Title
::makeTitleSafe( NS_FILE
, $item['title'] );
159 if ( $item['title'] )
160 $items[$item['title']->getDBkey()] = $item;
163 $images = $this->localRepo
->findFiles( $items );
165 foreach ( $this->foreignRepos
as $repo ) {
166 // Remove found files from $items
167 foreach ( $images as $name => $image ) {
168 unset( $items[$name] );
171 $images = array_merge( $images, $repo->findFiles( $items ) );
177 * Interface for FileRepo::checkRedirect()
179 function checkRedirect( $title ) {
180 if ( !$this->reposInitialised
) {
181 $this->initialiseRepos();
184 $redir = $this->localRepo
->checkRedirect( $title );
188 foreach ( $this->foreignRepos
as $repo ) {
189 $redir = $repo->checkRedirect( $title );
197 function findBySha1( $hash ) {
198 if ( !$this->reposInitialised
) {
199 $this->initialiseRepos();
202 $result = $this->localRepo
->findBySha1( $hash );
203 foreach ( $this->foreignRepos
as $repo )
204 $result = array_merge( $result, $repo->findBySha1( $hash ) );
209 * Get the repo instance with a given key.
211 function getRepo( $index ) {
212 if ( !$this->reposInitialised
) {
213 $this->initialiseRepos();
215 if ( $index === 'local' ) {
216 return $this->localRepo
;
217 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
218 return $this->foreignRepos
[$index];
224 * Get the repo instance by its name
226 function getRepoByName( $name ) {
227 if ( !$this->reposInitialised
) {
228 $this->initialiseRepos();
230 foreach ( $this->foreignRepos
as $key => $repo ) {
231 if ( $repo->name
== $name)
238 * Get the local repository, i.e. the one corresponding to the local image
239 * table. Files are typically uploaded to the local repository.
241 function getLocalRepo() {
242 return $this->getRepo( 'local' );
246 * Call a function for each foreign repo, with the repo object as the
249 * @param $callback Callback: the function to call
250 * @param $params Array: optional additional parameters to pass to the function
252 function forEachForeignRepo( $callback, $params = array() ) {
253 foreach( $this->foreignRepos
as $repo ) {
254 $args = array_merge( array( $repo ), $params );
255 if( call_user_func_array( $callback, $args ) ) {
263 * Does the installation have any foreign repos set up?
266 function hasForeignRepos() {
267 return (bool)$this->foreignRepos
;
271 * Initialise the $repos array
273 function initialiseRepos() {
274 if ( $this->reposInitialised
) {
277 $this->reposInitialised
= true;
279 $this->localRepo
= $this->newRepo( $this->localInfo
);
280 $this->foreignRepos
= array();
281 foreach ( $this->foreignInfo
as $key => $info ) {
282 $this->foreignRepos
[$key] = $this->newRepo( $info );
287 * Create a repo class based on an info structure
289 protected function newRepo( $info ) {
290 $class = $info['class'];
291 return new $class( $info );
295 * Split a virtual URL into repo, zone and rel parts
296 * @return an array containing repo, zone and rel
298 function splitVirtualUrl( $url ) {
299 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
300 throw new MWException( __METHOD__
.': unknown protoocl' );
303 $bits = explode( '/', substr( $url, 9 ), 3 );
304 if ( count( $bits ) != 3 ) {
305 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
310 function getFileProps( $fileName ) {
311 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
312 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
313 if ( $repoName === '' ) {
316 $repo = $this->getRepo( $repoName );
317 return $repo->getFileProps( $fileName );
319 return File
::getPropsFromPath( $fileName );
326 function trimCache() {
327 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
328 reset( $this->cache
);
329 $key = key( $this->cache
);
330 wfDebug( __METHOD__
.": evicting $key\n" );
331 unset( $this->cache
[$key] );