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
34 protected $foreignRepos;
37 protected $reposInitialised = false;
43 protected $foreignInfo;
49 protected static $instance;
51 /** Maximum number of cache items */
52 const MAX_CACHE_SIZE
= 500;
55 * Get a RepoGroup instance. At present only one instance of RepoGroup is
56 * needed in a MediaWiki invocation, this may change in the future.
59 static function singleton() {
60 if ( self
::$instance ) {
61 return self
::$instance;
63 global $wgLocalFileRepo, $wgForeignFileRepos;
64 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
66 return self
::$instance;
70 * Destroy the singleton instance, so that a new one will be created next
71 * time singleton() is called.
73 static function destroySingleton() {
74 self
::$instance = null;
78 * Set the singleton instance to a given object
79 * Used by extensions which hook into the Repo chain.
80 * It's not enough to just create a superclass ... you have
81 * to get people to call into it even though all they know is RepoGroup::singleton()
83 * @param RepoGroup $instance
85 static function setSingleton( $instance ) {
86 self
::$instance = $instance;
90 * Construct a group of file repositories.
92 * @param array $localInfo Associative array for local repo's info
93 * @param array $foreignInfo of repository info arrays.
94 * Each info array is an associative array with the 'class' member
95 * giving the class name. The entire array is passed to the repository
96 * constructor as the first parameter.
98 function __construct( $localInfo, $foreignInfo ) {
99 $this->localInfo
= $localInfo;
100 $this->foreignInfo
= $foreignInfo;
101 $this->cache
= array();
105 * Search repositories for an image.
106 * You can also use wfFindFile() to do this.
108 * @param $title Title|string Title object or string
109 * @param array $options Associative array of options:
110 * time: requested time for an archived image, or false for the
111 * current version. An image object will be returned which was
112 * created at the specified time.
113 * ignoreRedirect: If true, do not follow file redirects
114 * private: If true, return restricted (deleted) files if the current
115 * user is allowed to view them. Otherwise, such files will not
117 * bypassCache: If true, do not use the process-local cache of File objects
118 * @return File|bool False if title is not found
120 function findFile( $title, $options = array() ) {
121 if ( !is_array( $options ) ) {
123 $options = array( 'time' => $options );
125 if ( !$this->reposInitialised
) {
126 $this->initialiseRepos();
128 $title = File
::normalizeTitle( $title );
134 if ( empty( $options['ignoreRedirect'] )
135 && empty( $options['private'] )
136 && empty( $options['bypassCache'] )
138 $time = isset( $options['time'] ) ?
$options['time'] : '';
139 $dbkey = $title->getDBkey();
140 if ( isset( $this->cache
[$dbkey][$time] ) ) {
141 wfDebug( __METHOD__
. ": got File:$dbkey from process cache\n" );
142 # Move it to the end of the list so that we can delete the LRU entry later
143 $this->pingCache( $dbkey );
146 return $this->cache
[$dbkey][$time];
153 # Check the local repo
154 $image = $this->localRepo
->findFile( $title, $options );
156 # Check the foreign repos
158 foreach ( $this->foreignRepos
as $repo ) {
159 $image = $repo->findFile( $title, $options );
166 $image = $image ?
$image : false; // type sanity
167 # Cache file existence or non-existence
168 if ( $useCache && ( !$image ||
$image->isCacheable() ) ) {
170 $this->cache
[$dbkey][$time] = $image;
177 * @param array $inputItems
180 function findFiles( $inputItems ) {
181 if ( !$this->reposInitialised
) {
182 $this->initialiseRepos();
186 foreach ( $inputItems as $item ) {
187 if ( !is_array( $item ) ) {
188 $item = array( 'title' => $item );
190 $item['title'] = File
::normalizeTitle( $item['title'] );
191 if ( $item['title'] ) {
192 $items[$item['title']->getDBkey()] = $item;
196 $images = $this->localRepo
->findFiles( $items );
198 foreach ( $this->foreignRepos
as $repo ) {
199 // Remove found files from $items
200 foreach ( $images as $name => $image ) {
201 unset( $items[$name] );
204 $images = array_merge( $images, $repo->findFiles( $items ) );
211 * Interface for FileRepo::checkRedirect()
212 * @param $title Title
215 function checkRedirect( Title
$title ) {
216 if ( !$this->reposInitialised
) {
217 $this->initialiseRepos();
220 $redir = $this->localRepo
->checkRedirect( $title );
224 foreach ( $this->foreignRepos
as $repo ) {
225 $redir = $repo->checkRedirect( $title );
235 * Find an instance of the file with this key, created at the specified time
236 * Returns false if the file does not exist.
238 * @param string $hash base 36 SHA-1 hash
239 * @param array $options Option array, same as findFile()
240 * @return File object or false if it is not found
242 function findFileFromKey( $hash, $options = array() ) {
243 if ( !$this->reposInitialised
) {
244 $this->initialiseRepos();
247 $file = $this->localRepo
->findFileFromKey( $hash, $options );
249 foreach ( $this->foreignRepos
as $repo ) {
250 $file = $repo->findFileFromKey( $hash, $options );
261 * Find all instances of files with this key
263 * @param string $hash base 36 SHA-1 hash
264 * @return Array of File objects
266 function findBySha1( $hash ) {
267 if ( !$this->reposInitialised
) {
268 $this->initialiseRepos();
271 $result = $this->localRepo
->findBySha1( $hash );
272 foreach ( $this->foreignRepos
as $repo ) {
273 $result = array_merge( $result, $repo->findBySha1( $hash ) );
275 usort( $result, 'File::compare' );
281 * Find all instances of files with this keys
283 * @param array $hashes base 36 SHA-1 hashes
284 * @return array of array of File objects
286 function findBySha1s( array $hashes ) {
287 if ( !$this->reposInitialised
) {
288 $this->initialiseRepos();
291 $result = $this->localRepo
->findBySha1s( $hashes );
292 foreach ( $this->foreignRepos
as $repo ) {
293 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
295 //sort the merged (and presorted) sublist of each hash
296 foreach ( $result as $hash => $files ) {
297 usort( $result[$hash], 'File::compare' );
304 * Get the repo instance with a given key.
305 * @param string|int $index
306 * @return bool|LocalRepo
308 function getRepo( $index ) {
309 if ( !$this->reposInitialised
) {
310 $this->initialiseRepos();
312 if ( $index === 'local' ) {
313 return $this->localRepo
;
314 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
315 return $this->foreignRepos
[$index];
322 * Get the repo instance by its name
323 * @param string $name
326 function getRepoByName( $name ) {
327 if ( !$this->reposInitialised
) {
328 $this->initialiseRepos();
330 foreach ( $this->foreignRepos
as $repo ) {
331 if ( $repo->name
== $name ) {
340 * Get the local repository, i.e. the one corresponding to the local image
341 * table. Files are typically uploaded to the local repository.
345 function getLocalRepo() {
346 return $this->getRepo( 'local' );
350 * Call a function for each foreign repo, with the repo object as the
353 * @param callable $callback The function to call
354 * @param array $params Optional additional parameters to pass to the function
357 function forEachForeignRepo( $callback, $params = array() ) {
358 foreach ( $this->foreignRepos
as $repo ) {
359 $args = array_merge( array( $repo ), $params );
360 if ( call_user_func_array( $callback, $args ) ) {
369 * Does the installation have any foreign repos set up?
372 function hasForeignRepos() {
373 return (bool)$this->foreignRepos
;
377 * Initialise the $repos array
379 function initialiseRepos() {
380 if ( $this->reposInitialised
) {
383 $this->reposInitialised
= true;
385 $this->localRepo
= $this->newRepo( $this->localInfo
);
386 $this->foreignRepos
= array();
387 foreach ( $this->foreignInfo
as $key => $info ) {
388 $this->foreignRepos
[$key] = $this->newRepo( $info );
393 * Create a repo class based on an info structure
395 protected function newRepo( $info ) {
396 $class = $info['class'];
398 return new $class( $info );
402 * Split a virtual URL into repo, zone and rel parts
404 * @throws MWException
405 * @return array containing repo, zone and rel
407 function splitVirtualUrl( $url ) {
408 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
409 throw new MWException( __METHOD__
. ': unknown protocol' );
412 $bits = explode( '/', substr( $url, 9 ), 3 );
413 if ( count( $bits ) != 3 ) {
414 throw new MWException( __METHOD__
. ": invalid mwrepo URL: $url" );
421 * @param string $fileName
424 function getFileProps( $fileName ) {
425 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
426 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
427 if ( $repoName === '' ) {
430 $repo = $this->getRepo( $repoName );
432 return $repo->getFileProps( $fileName );
434 return FSFile
::getPropsFromPath( $fileName );
439 * Move a cache entry to the top (such as when accessed)
441 protected function pingCache( $key ) {
442 if ( isset( $this->cache
[$key] ) ) {
443 $tmp = $this->cache
[$key];
444 unset( $this->cache
[$key] );
445 $this->cache
[$key] = $tmp;
452 protected function trimCache() {
453 while ( count( $this->cache
) >= self
::MAX_CACHE_SIZE
) {
454 reset( $this->cache
);
455 $key = key( $this->cache
);
456 wfDebug( __METHOD__
. ": evicting $key\n" );
457 unset( $this->cache
[$key] );
462 * Clear RepoGroup process cache used for finding a file
463 * @param Title|null $title Title of the file or null to clear all files
465 public function clearCache( Title
$title = null ) {
466 if ( $title == null ) {
467 $this->cache
= array();
469 $dbKey = $title->getDBkey();
470 if ( isset( $this->cache
[$dbKey] ) ) {
471 unset( $this->cache
[$dbKey] );