Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / filerepo / RepoGroup.php
blobb2b9477ab3e685e1c1571d11bfa1fb1e19e8c6d5
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileRepo
24 /**
25 * Prioritized list of file repositories
27 * @ingroup FileRepo
29 class RepoGroup {
30 /**
31 * @var LocalRepo
33 var $localRepo;
35 var $foreignRepos, $reposInitialised = false;
36 var $localInfo, $foreignInfo;
37 var $cache;
39 /**
40 * @var RepoGroup
42 protected static $instance;
43 const MAX_CACHE_SIZE = 500;
45 /**
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.
48 * @return RepoGroup
50 static function singleton() {
51 if ( self::$instance ) {
52 return self::$instance;
54 global $wgLocalFileRepo, $wgForeignFileRepos;
55 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
56 return self::$instance;
59 /**
60 * Destroy the singleton instance, so that a new one will be created next
61 * time singleton() is called.
63 static function destroySingleton() {
64 self::$instance = null;
67 /**
68 * Set the singleton instance to a given object
69 * Used by extensions which hook into the Repo chain.
70 * It's not enough to just create a superclass ... you have
71 * to get people to call into it even though all they know is RepoGroup::singleton()
73 * @param $instance RepoGroup
75 static function setSingleton( $instance ) {
76 self::$instance = $instance;
79 /**
80 * Construct a group of file repositories.
82 * @param array $localInfo Associative array for local repo's info
83 * @param array $foreignInfo of repository info arrays.
84 * Each info array is an associative array with the 'class' member
85 * giving the class name. The entire array is passed to the repository
86 * constructor as the first parameter.
88 function __construct( $localInfo, $foreignInfo ) {
89 $this->localInfo = $localInfo;
90 $this->foreignInfo = $foreignInfo;
91 $this->cache = array();
94 /**
95 * Search repositories for an image.
96 * You can also use wfFindFile() to do this.
98 * @param $title Title|string Title object or string
99 * @param array $options Associative array of options:
100 * time: requested time for an archived image, or false for the
101 * current version. An image object will be returned which was
102 * created at the specified time.
104 * ignoreRedirect: If true, do not follow file redirects
106 * private: If true, return restricted (deleted) files if the current
107 * user is allowed to view them. Otherwise, such files will not
108 * be found.
110 * bypassCache: If true, do not use the process-local cache of File objects
111 * @return File object or false if it is not found
113 function findFile( $title, $options = array() ) {
114 if ( !is_array( $options ) ) {
115 // MW 1.15 compat
116 $options = array( 'time' => $options );
118 if ( !$this->reposInitialised ) {
119 $this->initialiseRepos();
121 $title = File::normalizeTitle( $title );
122 if ( !$title ) {
123 return false;
126 # Check the cache
127 if ( empty( $options['ignoreRedirect'] )
128 && empty( $options['private'] )
129 && empty( $options['bypassCache'] ) )
131 $time = isset( $options['time'] ) ? $options['time'] : '';
132 $dbkey = $title->getDBkey();
133 if ( isset( $this->cache[$dbkey][$time] ) ) {
134 wfDebug( __METHOD__ . ": got File:$dbkey from process cache\n" );
135 # Move it to the end of the list so that we can delete the LRU entry later
136 $this->pingCache( $dbkey );
137 # Return the entry
138 return $this->cache[$dbkey][$time];
140 $useCache = true;
141 } else {
142 $useCache = false;
145 # Check the local repo
146 $image = $this->localRepo->findFile( $title, $options );
148 # Check the foreign repos
149 if ( !$image ) {
150 foreach ( $this->foreignRepos as $repo ) {
151 $image = $repo->findFile( $title, $options );
152 if ( $image ) {
153 break;
158 $image = $image ? $image : false; // type sanity
159 # Cache file existence or non-existence
160 if ( $useCache && ( !$image || $image->isCacheable() ) ) {
161 $this->trimCache();
162 $this->cache[$dbkey][$time] = $image;
165 return $image;
169 * @param $inputItems array
170 * @return array
172 function findFiles( $inputItems ) {
173 if ( !$this->reposInitialised ) {
174 $this->initialiseRepos();
177 $items = array();
178 foreach ( $inputItems as $item ) {
179 if ( !is_array( $item ) ) {
180 $item = array( 'title' => $item );
182 $item['title'] = File::normalizeTitle( $item['title'] );
183 if ( $item['title'] ) {
184 $items[$item['title']->getDBkey()] = $item;
188 $images = $this->localRepo->findFiles( $items );
190 foreach ( $this->foreignRepos as $repo ) {
191 // Remove found files from $items
192 foreach ( $images as $name => $image ) {
193 unset( $items[$name] );
196 $images = array_merge( $images, $repo->findFiles( $items ) );
198 return $images;
202 * Interface for FileRepo::checkRedirect()
203 * @param $title Title
204 * @return bool
206 function checkRedirect( Title $title ) {
207 if ( !$this->reposInitialised ) {
208 $this->initialiseRepos();
211 $redir = $this->localRepo->checkRedirect( $title );
212 if ( $redir ) {
213 return $redir;
215 foreach ( $this->foreignRepos as $repo ) {
216 $redir = $repo->checkRedirect( $title );
217 if ( $redir ) {
218 return $redir;
221 return false;
225 * Find an instance of the file with this key, created at the specified time
226 * Returns false if the file does not exist.
228 * @param string $hash base 36 SHA-1 hash
229 * @param array $options Option array, same as findFile()
230 * @return File object or false if it is not found
232 function findFileFromKey( $hash, $options = array() ) {
233 if ( !$this->reposInitialised ) {
234 $this->initialiseRepos();
237 $file = $this->localRepo->findFileFromKey( $hash, $options );
238 if ( !$file ) {
239 foreach ( $this->foreignRepos as $repo ) {
240 $file = $repo->findFileFromKey( $hash, $options );
241 if ( $file ) {
242 break;
246 return $file;
250 * Find all instances of files with this key
252 * @param string $hash base 36 SHA-1 hash
253 * @return Array of File objects
255 function findBySha1( $hash ) {
256 if ( !$this->reposInitialised ) {
257 $this->initialiseRepos();
260 $result = $this->localRepo->findBySha1( $hash );
261 foreach ( $this->foreignRepos as $repo ) {
262 $result = array_merge( $result, $repo->findBySha1( $hash ) );
264 usort( $result, 'File::compare' );
265 return $result;
269 * Find all instances of files with this keys
271 * @param array $hashes base 36 SHA-1 hashes
272 * @return Array of array of File objects
274 function findBySha1s( array $hashes ) {
275 if ( !$this->reposInitialised ) {
276 $this->initialiseRepos();
279 $result = $this->localRepo->findBySha1s( $hashes );
280 foreach ( $this->foreignRepos as $repo ) {
281 $result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
283 //sort the merged (and presorted) sublist of each hash
284 foreach ( $result as $hash => $files ) {
285 usort( $result[$hash], 'File::compare' );
287 return $result;
291 * Get the repo instance with a given key.
292 * @param $index string|int
293 * @return bool|LocalRepo
295 function getRepo( $index ) {
296 if ( !$this->reposInitialised ) {
297 $this->initialiseRepos();
299 if ( $index === 'local' ) {
300 return $this->localRepo;
301 } elseif ( isset( $this->foreignRepos[$index] ) ) {
302 return $this->foreignRepos[$index];
303 } else {
304 return false;
309 * Get the repo instance by its name
310 * @param $name string
311 * @return bool
313 function getRepoByName( $name ) {
314 if ( !$this->reposInitialised ) {
315 $this->initialiseRepos();
317 foreach ( $this->foreignRepos as $repo ) {
318 if ( $repo->name == $name ) {
319 return $repo;
322 return false;
326 * Get the local repository, i.e. the one corresponding to the local image
327 * table. Files are typically uploaded to the local repository.
329 * @return LocalRepo
331 function getLocalRepo() {
332 return $this->getRepo( 'local' );
336 * Call a function for each foreign repo, with the repo object as the
337 * first parameter.
339 * @param $callback Callback: the function to call
340 * @param array $params optional additional parameters to pass to the function
341 * @return bool
343 function forEachForeignRepo( $callback, $params = array() ) {
344 foreach ( $this->foreignRepos as $repo ) {
345 $args = array_merge( array( $repo ), $params );
346 if ( call_user_func_array( $callback, $args ) ) {
347 return true;
350 return false;
354 * Does the installation have any foreign repos set up?
355 * @return Boolean
357 function hasForeignRepos() {
358 return (bool)$this->foreignRepos;
362 * Initialise the $repos array
364 function initialiseRepos() {
365 if ( $this->reposInitialised ) {
366 return;
368 $this->reposInitialised = true;
370 $this->localRepo = $this->newRepo( $this->localInfo );
371 $this->foreignRepos = array();
372 foreach ( $this->foreignInfo as $key => $info ) {
373 $this->foreignRepos[$key] = $this->newRepo( $info );
378 * Create a repo class based on an info structure
380 protected function newRepo( $info ) {
381 $class = $info['class'];
382 return new $class( $info );
386 * Split a virtual URL into repo, zone and rel parts
387 * @param $url string
388 * @throws MWException
389 * @return array containing repo, zone and rel
391 function splitVirtualUrl( $url ) {
392 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
393 throw new MWException( __METHOD__ . ': unknown protocol' );
396 $bits = explode( '/', substr( $url, 9 ), 3 );
397 if ( count( $bits ) != 3 ) {
398 throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
400 return $bits;
404 * @param $fileName string
405 * @return array
407 function getFileProps( $fileName ) {
408 if ( FileRepo::isVirtualUrl( $fileName ) ) {
409 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
410 if ( $repoName === '' ) {
411 $repoName = 'local';
413 $repo = $this->getRepo( $repoName );
414 return $repo->getFileProps( $fileName );
415 } else {
416 return FSFile::getPropsFromPath( $fileName );
421 * Move a cache entry to the top (such as when accessed)
423 protected function pingCache( $key ) {
424 if ( isset( $this->cache[$key] ) ) {
425 $tmp = $this->cache[$key];
426 unset( $this->cache[$key] );
427 $this->cache[$key] = $tmp;
432 * Limit cache memory
434 protected function trimCache() {
435 while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
436 reset( $this->cache );
437 $key = key( $this->cache );
438 wfDebug( __METHOD__ . ": evicting $key\n" );
439 unset( $this->cache[$key] );
444 * Clear RepoGroup process cache used for finding a file
445 * @param $title Title|null Title of the file or null to clear all files
447 public function clearCache( Title $title = null ) {
448 if ( $title == null ) {
449 $this->cache = array();
450 } else {
451 $dbKey = $title->getDBkey();
452 if ( isset( $this->cache[$dbKey] ) ) {
453 unset( $this->cache[$dbKey] );