3 * Prioritized list of file repositories.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Prioritized list of file repositories
35 var $foreignRepos, $reposInitialised = false;
36 var $localInfo, $foreignInfo;
42 protected static $instance;
43 const MAX_CACHE_SIZE
= 500;
46 * Get a RepoGroup instance. At present only one instance of RepoGroup is
47 * needed in a MediaWiki invocation, this may change in the future.
50 static function singleton() {
51 if ( self
::$instance ) {
52 return self
::$instance;
54 global $wgLocalFileRepo, $wgForeignFileRepos;
55 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
57 return self
::$instance;
61 * Destroy the singleton instance, so that a new one will be created next
62 * time singleton() is called.
64 static function destroySingleton() {
65 self
::$instance = null;
69 * Set the singleton instance to a given object
70 * Used by extensions which hook into the Repo chain.
71 * It's not enough to just create a superclass ... you have
72 * to get people to call into it even though all they know is RepoGroup::singleton()
74 * @param $instance RepoGroup
76 static function setSingleton( $instance ) {
77 self
::$instance = $instance;
81 * Construct a group of file repositories.
83 * @param array $localInfo Associative array for local repo's info
84 * @param array $foreignInfo of repository info arrays.
85 * Each info array is an associative array with the 'class' member
86 * giving the class name. The entire array is passed to the repository
87 * constructor as the first parameter.
89 function __construct( $localInfo, $foreignInfo ) {
90 $this->localInfo
= $localInfo;
91 $this->foreignInfo
= $foreignInfo;
92 $this->cache
= array();
96 * Search repositories for an image.
97 * You can also use wfFindFile() to do this.
99 * @param $title Title|string Title object or string
100 * @param array $options Associative array of options:
101 * time: requested time for an archived image, or false for the
102 * current version. An image object will be returned which was
103 * created at the specified time.
105 * ignoreRedirect: If true, do not follow file redirects
107 * private: If true, return restricted (deleted) files if the current
108 * user is allowed to view them. Otherwise, such files will not
111 * bypassCache: If true, do not use the process-local cache of File objects
112 * @return File object or false if it is not found
114 function findFile( $title, $options = array() ) {
115 if ( !is_array( $options ) ) {
117 $options = array( 'time' => $options );
119 if ( !$this->reposInitialised
) {
120 $this->initialiseRepos();
122 $title = File
::normalizeTitle( $title );
128 if ( empty( $options['ignoreRedirect'] )
129 && empty( $options['private'] )
130 && empty( $options['bypassCache'] )
132 $time = isset( $options['time'] ) ?
$options['time'] : '';
133 $dbkey = $title->getDBkey();
134 if ( isset( $this->cache
[$dbkey][$time] ) ) {
135 wfDebug( __METHOD__
. ": got File:$dbkey from process cache\n" );
136 # Move it to the end of the list so that we can delete the LRU entry later
137 $this->pingCache( $dbkey );
140 return $this->cache
[$dbkey][$time];
147 # Check the local repo
148 $image = $this->localRepo
->findFile( $title, $options );
150 # Check the foreign repos
152 foreach ( $this->foreignRepos
as $repo ) {
153 $image = $repo->findFile( $title, $options );
160 $image = $image ?
$image : false; // type sanity
161 # Cache file existence or non-existence
162 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
164 $this->cache
[$dbkey][$time] = $image;
171 * @param $inputItems array
174 function findFiles( $inputItems ) {
175 if ( !$this->reposInitialised
) {
176 $this->initialiseRepos();
180 foreach ( $inputItems as $item ) {
181 if ( !is_array( $item ) ) {
182 $item = array( 'title' => $item );
184 $item['title'] = File
::normalizeTitle( $item['title'] );
185 if ( $item['title'] ) {
186 $items[$item['title']->getDBkey()] = $item;
190 $images = $this->localRepo
->findFiles( $items );
192 foreach ( $this->foreignRepos
as $repo ) {
193 // Remove found files from $items
194 foreach ( $images as $name => $image ) {
195 unset( $items[$name] );
198 $images = array_merge( $images, $repo->findFiles( $items ) );
205 * Interface for FileRepo::checkRedirect()
206 * @param $title Title
209 function checkRedirect( Title
$title ) {
210 if ( !$this->reposInitialised
) {
211 $this->initialiseRepos();
214 $redir = $this->localRepo
->checkRedirect( $title );
218 foreach ( $this->foreignRepos
as $repo ) {
219 $redir = $repo->checkRedirect( $title );
229 * Find an instance of the file with this key, created at the specified time
230 * Returns false if the file does not exist.
232 * @param string $hash base 36 SHA-1 hash
233 * @param array $options Option array, same as findFile()
234 * @return File object or false if it is not found
236 function findFileFromKey( $hash, $options = array() ) {
237 if ( !$this->reposInitialised
) {
238 $this->initialiseRepos();
241 $file = $this->localRepo
->findFileFromKey( $hash, $options );
243 foreach ( $this->foreignRepos
as $repo ) {
244 $file = $repo->findFileFromKey( $hash, $options );
255 * Find all instances of files with this key
257 * @param string $hash base 36 SHA-1 hash
258 * @return Array of File objects
260 function findBySha1( $hash ) {
261 if ( !$this->reposInitialised
) {
262 $this->initialiseRepos();
265 $result = $this->localRepo
->findBySha1( $hash );
266 foreach ( $this->foreignRepos
as $repo ) {
267 $result = array_merge( $result, $repo->findBySha1( $hash ) );
269 usort( $result, 'File::compare' );
275 * Find all instances of files with this keys
277 * @param array $hashes base 36 SHA-1 hashes
278 * @return Array of array of File objects
280 function findBySha1s( array $hashes ) {
281 if ( !$this->reposInitialised
) {
282 $this->initialiseRepos();
285 $result = $this->localRepo
->findBySha1s( $hashes );
286 foreach ( $this->foreignRepos
as $repo ) {
287 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
289 //sort the merged (and presorted) sublist of each hash
290 foreach ( $result as $hash => $files ) {
291 usort( $result[$hash], 'File::compare' );
298 * Get the repo instance with a given key.
299 * @param $index string|int
300 * @return bool|LocalRepo
302 function getRepo( $index ) {
303 if ( !$this->reposInitialised
) {
304 $this->initialiseRepos();
306 if ( $index === 'local' ) {
307 return $this->localRepo
;
308 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
309 return $this->foreignRepos
[$index];
316 * Get the repo instance by its name
317 * @param $name string
320 function getRepoByName( $name ) {
321 if ( !$this->reposInitialised
) {
322 $this->initialiseRepos();
324 foreach ( $this->foreignRepos
as $repo ) {
325 if ( $repo->name
== $name ) {
334 * Get the local repository, i.e. the one corresponding to the local image
335 * table. Files are typically uploaded to the local repository.
339 function getLocalRepo() {
340 return $this->getRepo( 'local' );
344 * Call a function for each foreign repo, with the repo object as the
347 * @param $callback Callback: the function to call
348 * @param array $params optional additional parameters to pass to the function
351 function forEachForeignRepo( $callback, $params = array() ) {
352 foreach ( $this->foreignRepos
as $repo ) {
353 $args = array_merge( array( $repo ), $params );
354 if ( call_user_func_array( $callback, $args ) ) {
363 * Does the installation have any foreign repos set up?
366 function hasForeignRepos() {
367 return (bool)$this->foreignRepos
;
371 * Initialise the $repos array
373 function initialiseRepos() {
374 if ( $this->reposInitialised
) {
377 $this->reposInitialised
= true;
379 $this->localRepo
= $this->newRepo( $this->localInfo
);
380 $this->foreignRepos
= array();
381 foreach ( $this->foreignInfo
as $key => $info ) {
382 $this->foreignRepos
[$key] = $this->newRepo( $info );
387 * Create a repo class based on an info structure
389 protected function newRepo( $info ) {
390 $class = $info['class'];
392 return new $class( $info );
396 * Split a virtual URL into repo, zone and rel parts
398 * @throws MWException
399 * @return array containing repo, zone and rel
401 function splitVirtualUrl( $url ) {
402 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
403 throw new MWException( __METHOD__
. ': unknown protocol' );
406 $bits = explode( '/', substr( $url, 9 ), 3 );
407 if ( count( $bits ) != 3 ) {
408 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
415 * @param $fileName string
418 function getFileProps( $fileName ) {
419 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
420 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
421 if ( $repoName === '' ) {
424 $repo = $this->getRepo( $repoName );
426 return $repo->getFileProps( $fileName );
428 return FSFile
::getPropsFromPath( $fileName );
433 * Move a cache entry to the top (such as when accessed)
435 protected function pingCache( $key ) {
436 if ( isset( $this->cache
[$key] ) ) {
437 $tmp = $this->cache
[$key];
438 unset( $this->cache
[$key] );
439 $this->cache
[$key] = $tmp;
446 protected function trimCache() {
447 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
448 reset( $this->cache
);
449 $key = key( $this->cache
);
450 wfDebug( __METHOD__
. ": evicting $key\n" );
451 unset( $this->cache
[$key] );
456 * Clear RepoGroup process cache used for finding a file
457 * @param $title Title|null Title of the file or null to clear all files
459 public function clearCache( Title
$title = null ) {
460 if ( $title == null ) {
461 $this->cache
= array();
463 $dbKey = $title->getDBkey();
464 if ( isset( $this->cache
[$dbKey] ) ) {
465 unset( $this->cache
[$dbKey] );