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
33 protected $foreignRepos;
36 protected $reposInitialised = false;
39 protected $foreignInfo;
41 /** @var ProcessCacheLRU */
45 protected static $instance;
47 /** Maximum number of cache items */
48 const MAX_CACHE_SIZE
= 500;
51 * Get a RepoGroup instance. At present only one instance of RepoGroup is
52 * needed in a MediaWiki invocation, this may change in the future.
55 static function singleton() {
56 if ( self
::$instance ) {
57 return self
::$instance;
59 global $wgLocalFileRepo, $wgForeignFileRepos;
60 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
62 return self
::$instance;
66 * Destroy the singleton instance, so that a new one will be created next
67 * time singleton() is called.
69 static function destroySingleton() {
70 self
::$instance = null;
74 * Set the singleton instance to a given object
75 * Used by extensions which hook into the Repo chain.
76 * It's not enough to just create a superclass ... you have
77 * to get people to call into it even though all they know is RepoGroup::singleton()
79 * @param $instance RepoGroup
81 static function setSingleton( $instance ) {
82 self
::$instance = $instance;
86 * Construct a group of file repositories.
88 * @param array $localInfo Associative array for local repo's info
89 * @param array $foreignInfo of repository info arrays.
90 * Each info array is an associative array with the 'class' member
91 * giving the class name. The entire array is passed to the repository
92 * constructor as the first parameter.
94 function __construct( $localInfo, $foreignInfo ) {
95 $this->localInfo
= $localInfo;
96 $this->foreignInfo
= $foreignInfo;
97 $this->cache
= new ProcessCacheLRU( self
::MAX_CACHE_SIZE
);
101 * Search repositories for an image.
102 * You can also use wfFindFile() to do this.
104 * @param $title Title|string Title object or string
105 * @param array $options Associative array of options:
106 * time: requested time for an archived image, or false for the
107 * current version. An image object will be returned which was
108 * created at the specified time.
110 * ignoreRedirect: If true, do not follow file redirects
112 * private: If true, return restricted (deleted) files if the current
113 * user is allowed to view them. Otherwise, such files will not
116 * bypassCache: If true, do not use the process-local cache of File objects
117 * @return File object or false if it is not found
119 function findFile( $title, $options = array() ) {
120 if ( !is_array( $options ) ) {
122 $options = array( 'time' => $options );
124 if ( !$this->reposInitialised
) {
125 $this->initialiseRepos();
127 $title = File
::normalizeTitle( $title );
133 if ( empty( $options['ignoreRedirect'] )
134 && empty( $options['private'] )
135 && empty( $options['bypassCache'] )
137 $time = isset( $options['time'] ) ?
$options['time'] : '';
138 $dbkey = $title->getDBkey();
139 if ( $this->cache
->has( $dbkey, $time, 60 ) ) {
140 return $this->cache
->get( $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() ) ) {
163 $this->cache
->set( $dbkey, $time, $image );
170 * @param $inputItems array
173 function findFiles( $inputItems ) {
174 if ( !$this->reposInitialised
) {
175 $this->initialiseRepos();
179 foreach ( $inputItems as $item ) {
180 if ( !is_array( $item ) ) {
181 $item = array( 'title' => $item );
183 $item['title'] = File
::normalizeTitle( $item['title'] );
184 if ( $item['title'] ) {
185 $items[$item['title']->getDBkey()] = $item;
189 $images = $this->localRepo
->findFiles( $items );
191 foreach ( $this->foreignRepos
as $repo ) {
192 // Remove found files from $items
193 foreach ( $images as $name => $image ) {
194 unset( $items[$name] );
197 $images = array_merge( $images, $repo->findFiles( $items ) );
204 * Interface for FileRepo::checkRedirect()
205 * @param $title Title
208 function checkRedirect( Title
$title ) {
209 if ( !$this->reposInitialised
) {
210 $this->initialiseRepos();
213 $redir = $this->localRepo
->checkRedirect( $title );
217 foreach ( $this->foreignRepos
as $repo ) {
218 $redir = $repo->checkRedirect( $title );
228 * Find an instance of the file with this key, created at the specified time
229 * Returns false if the file does not exist.
231 * @param string $hash base 36 SHA-1 hash
232 * @param array $options Option array, same as findFile()
233 * @return File object or false if it is not found
235 function findFileFromKey( $hash, $options = array() ) {
236 if ( !$this->reposInitialised
) {
237 $this->initialiseRepos();
240 $file = $this->localRepo
->findFileFromKey( $hash, $options );
242 foreach ( $this->foreignRepos
as $repo ) {
243 $file = $repo->findFileFromKey( $hash, $options );
254 * Find all instances of files with this key
256 * @param string $hash base 36 SHA-1 hash
257 * @return Array of File objects
259 function findBySha1( $hash ) {
260 if ( !$this->reposInitialised
) {
261 $this->initialiseRepos();
264 $result = $this->localRepo
->findBySha1( $hash );
265 foreach ( $this->foreignRepos
as $repo ) {
266 $result = array_merge( $result, $repo->findBySha1( $hash ) );
268 usort( $result, 'File::compare' );
274 * Find all instances of files with this keys
276 * @param array $hashes base 36 SHA-1 hashes
277 * @return Array of array of File objects
279 function findBySha1s( array $hashes ) {
280 if ( !$this->reposInitialised
) {
281 $this->initialiseRepos();
284 $result = $this->localRepo
->findBySha1s( $hashes );
285 foreach ( $this->foreignRepos
as $repo ) {
286 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
288 //sort the merged (and presorted) sublist of each hash
289 foreach ( $result as $hash => $files ) {
290 usort( $result[$hash], 'File::compare' );
297 * Get the repo instance with a given key.
298 * @param $index string|int
299 * @return bool|LocalRepo
301 function getRepo( $index ) {
302 if ( !$this->reposInitialised
) {
303 $this->initialiseRepos();
305 if ( $index === 'local' ) {
306 return $this->localRepo
;
307 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
308 return $this->foreignRepos
[$index];
315 * Get the repo instance by its name
316 * @param $name string
319 function getRepoByName( $name ) {
320 if ( !$this->reposInitialised
) {
321 $this->initialiseRepos();
323 foreach ( $this->foreignRepos
as $repo ) {
324 if ( $repo->name
== $name ) {
333 * Get the local repository, i.e. the one corresponding to the local image
334 * table. Files are typically uploaded to the local repository.
338 function getLocalRepo() {
339 return $this->getRepo( 'local' );
343 * Call a function for each foreign repo, with the repo object as the
346 * @param $callback Callback: the function to call
347 * @param array $params optional additional parameters to pass to the function
350 function forEachForeignRepo( $callback, $params = array() ) {
351 foreach ( $this->foreignRepos
as $repo ) {
352 $args = array_merge( array( $repo ), $params );
353 if ( call_user_func_array( $callback, $args ) ) {
362 * Does the installation have any foreign repos set up?
365 function hasForeignRepos() {
366 return (bool)$this->foreignRepos
;
370 * Initialise the $repos array
372 function initialiseRepos() {
373 if ( $this->reposInitialised
) {
376 $this->reposInitialised
= true;
378 $this->localRepo
= $this->newRepo( $this->localInfo
);
379 $this->foreignRepos
= array();
380 foreach ( $this->foreignInfo
as $key => $info ) {
381 $this->foreignRepos
[$key] = $this->newRepo( $info );
386 * Create a repo class based on an info structure
388 protected function newRepo( $info ) {
389 $class = $info['class'];
391 return new $class( $info );
395 * Split a virtual URL into repo, zone and rel parts
397 * @throws MWException
398 * @return array containing repo, zone and rel
400 function splitVirtualUrl( $url ) {
401 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
402 throw new MWException( __METHOD__
. ': unknown protocol' );
405 $bits = explode( '/', substr( $url, 9 ), 3 );
406 if ( count( $bits ) != 3 ) {
407 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
414 * @param $fileName string
417 function getFileProps( $fileName ) {
418 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
419 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
420 if ( $repoName === '' ) {
423 $repo = $this->getRepo( $repoName );
425 return $repo->getFileProps( $fileName );
427 return FSFile
::getPropsFromPath( $fileName );
432 * Clear RepoGroup process cache used for finding a file
433 * @param $title Title|null Title of the file or null to clear all files
435 public function clearCache( Title
$title = null ) {
436 if ( $title == null ) {
437 $this->cache
->clear();
439 $this->cache
->clear( $title->getDBkey() );