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.
31 static function singleton() {
32 if ( self
::$instance ) {
33 return self
::$instance;
35 global $wgLocalFileRepo, $wgForeignFileRepos;
36 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
37 return self
::$instance;
41 * Destroy the singleton instance, so that a new one will be created next
42 * time singleton() is called.
44 static function destroySingleton() {
45 self
::$instance = null;
49 * Set the singleton instance to a given object
51 static function setSingleton( $instance ) {
52 self
::$instance = $instance;
56 * Construct a group of file repositories.
58 * @param $localInfo Associative array for local repo's info
59 * @param $foreignInfo Array of repository info arrays.
60 * Each info array is an associative array with the 'class' member
61 * giving the class name. The entire array is passed to the repository
62 * constructor as the first parameter.
64 function __construct( $localInfo, $foreignInfo ) {
65 $this->localInfo
= $localInfo;
66 $this->foreignInfo
= $foreignInfo;
67 $this->cache
= array();
71 * Search repositories for an image.
72 * You can also use wfFindFile() to do this.
74 * @param $title Mixed: Title object or string
75 * @param $options Associative array of options:
76 * time: requested time for an archived image, or false for the
77 * current version. An image object will be returned which was
78 * created at the specified time.
80 * ignoreRedirect: If true, do not follow file redirects
82 * private: If true, return restricted (deleted) files if the current
83 * user is allowed to view them. Otherwise, such files will not
86 * bypassCache: If true, do not use the process-local cache of File objects
87 * @return File object or false if it is not found
89 function findFile( $title, $options = array() ) {
90 if ( !is_array( $options ) ) {
92 $options = array( 'time' => $options );
94 if ( !$this->reposInitialised
) {
95 $this->initialiseRepos();
97 if ( !($title instanceof Title
) ) {
98 $title = Title
::makeTitleSafe( NS_FILE
, $title );
99 if ( !is_object( $title ) ) {
104 if ( $title->getNamespace() != NS_MEDIA
&& $title->getNamespace() != NS_FILE
) {
105 throw new MWException( __METHOD__
. ' received an Title object with incorrect namespace' );
109 if ( empty( $options['ignoreRedirect'] )
110 && empty( $options['private'] )
111 && empty( $options['bypassCache'] )
112 && $title->getNamespace() == NS_FILE
)
115 $time = isset( $options['time'] ) ?
$options['time'] : '';
116 $dbkey = $title->getDBkey();
117 if ( isset( $this->cache
[$dbkey][$time] ) ) {
118 wfDebug( __METHOD__
.": got File:$dbkey from process cache\n" );
119 # Move it to the end of the list so that we can delete the LRU entry later
120 $tmp = $this->cache
[$dbkey];
121 unset( $this->cache
[$dbkey] );
122 $this->cache
[$dbkey] = $tmp;
124 return $this->cache
[$dbkey][$time];
126 # Add a negative cache entry, may be overridden
128 $this->cache
[$dbkey][$time] = false;
129 $cacheEntry =& $this->cache
[$dbkey][$time];
135 # Check the local repo
136 $image = $this->localRepo
->findFile( $title, $options );
139 $cacheEntry = $image;
144 # Check the foreign repos
145 foreach ( $this->foreignRepos
as $repo ) {
146 $image = $repo->findFile( $title, $options );
149 $cacheEntry = $image;
154 # Not found, do not override negative cache
158 function findFiles( $inputItems ) {
159 if ( !$this->reposInitialised
) {
160 $this->initialiseRepos();
164 foreach ( $inputItems as $item ) {
165 if ( !is_array( $item ) ) {
166 $item = array( 'title' => $item );
168 if ( !( $item['title'] instanceof Title
) )
169 $item['title'] = Title
::makeTitleSafe( NS_FILE
, $item['title'] );
170 if ( $item['title'] )
171 $items[$item['title']->getDBkey()] = $item;
174 $images = $this->localRepo
->findFiles( $items );
176 foreach ( $this->foreignRepos
as $repo ) {
177 // Remove found files from $items
178 foreach ( $images as $name => $image ) {
179 unset( $items[$name] );
182 $images = array_merge( $images, $repo->findFiles( $items ) );
188 * Interface for FileRepo::checkRedirect()
190 function checkRedirect( $title ) {
191 if ( !$this->reposInitialised
) {
192 $this->initialiseRepos();
195 $redir = $this->localRepo
->checkRedirect( $title );
199 foreach ( $this->foreignRepos
as $repo ) {
200 $redir = $repo->checkRedirect( $title );
208 function findBySha1( $hash ) {
209 if ( !$this->reposInitialised
) {
210 $this->initialiseRepos();
213 $result = $this->localRepo
->findBySha1( $hash );
214 foreach ( $this->foreignRepos
as $repo )
215 $result = array_merge( $result, $repo->findBySha1( $hash ) );
220 * Get the repo instance with a given key.
222 function getRepo( $index ) {
223 if ( !$this->reposInitialised
) {
224 $this->initialiseRepos();
226 if ( $index === 'local' ) {
227 return $this->localRepo
;
228 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
229 return $this->foreignRepos
[$index];
235 * Get the repo instance by its name
237 function getRepoByName( $name ) {
238 if ( !$this->reposInitialised
) {
239 $this->initialiseRepos();
241 foreach ( $this->foreignRepos
as $repo ) {
242 if ( $repo->name
== $name)
249 * Get the local repository, i.e. the one corresponding to the local image
250 * table. Files are typically uploaded to the local repository.
252 function getLocalRepo() {
253 return $this->getRepo( 'local' );
257 * Call a function for each foreign repo, with the repo object as the
260 * @param $callback Callback: the function to call
261 * @param $params Array: optional additional parameters to pass to the function
263 function forEachForeignRepo( $callback, $params = array() ) {
264 foreach( $this->foreignRepos
as $repo ) {
265 $args = array_merge( array( $repo ), $params );
266 if( call_user_func_array( $callback, $args ) ) {
274 * Does the installation have any foreign repos set up?
277 function hasForeignRepos() {
278 return (bool)$this->foreignRepos
;
282 * Initialise the $repos array
284 function initialiseRepos() {
285 if ( $this->reposInitialised
) {
288 $this->reposInitialised
= true;
290 $this->localRepo
= $this->newRepo( $this->localInfo
);
291 $this->foreignRepos
= array();
292 foreach ( $this->foreignInfo
as $key => $info ) {
293 $this->foreignRepos
[$key] = $this->newRepo( $info );
298 * Create a repo class based on an info structure
300 protected function newRepo( $info ) {
301 $class = $info['class'];
302 return new $class( $info );
306 * Split a virtual URL into repo, zone and rel parts
307 * @return an array containing repo, zone and rel
309 function splitVirtualUrl( $url ) {
310 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
311 throw new MWException( __METHOD__
.': unknown protoocl' );
314 $bits = explode( '/', substr( $url, 9 ), 3 );
315 if ( count( $bits ) != 3 ) {
316 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
321 function getFileProps( $fileName ) {
322 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
323 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
324 if ( $repoName === '' ) {
327 $repo = $this->getRepo( $repoName );
328 return $repo->getFileProps( $fileName );
330 return File
::getPropsFromPath( $fileName );
337 function trimCache() {
338 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
339 reset( $this->cache
);
340 $key = key( $this->cache
);
341 wfDebug( __METHOD__
.": evicting $key\n" );
342 unset( $this->cache
[$key] );