Use of undefined constant h in line 250. Follow up to r81558
[mediawiki.git] / includes / filerepo / RepoGroup.php
blob65de63bd9f7512dfcf28ea6e191a35c12bb3a190
1 <?php
2 /**
3 * Prioritized list of file repositories
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * @defgroup FileRepo FileRepo
13 /**
14 * Prioritized list of file repositories
16 * @ingroup FileRepo
18 class RepoGroup {
19 var $localRepo, $foreignRepos, $reposInitialised = false;
20 var $localInfo, $foreignInfo;
21 var $cache;
23 protected static $instance;
24 const MAX_CACHE_SIZE = 1000;
26 /**
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.
29 * @return RepoGroup
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;
40 /**
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;
48 /**
49 * Set the singleton instance to a given object
51 static function setSingleton( $instance ) {
52 self::$instance = $instance;
55 /**
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();
70 /**
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
84 * be found.
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 ) ) {
91 // MW 1.15 compat
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 ) ) {
100 return false;
104 if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
105 throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
108 # Check the cache
109 if ( empty( $options['ignoreRedirect'] )
110 && empty( $options['private'] )
111 && empty( $options['bypassCache'] )
112 && $title->getNamespace() == NS_FILE )
114 $useCache = true;
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;
123 # Return the entry
124 return $this->cache[$dbkey][$time];
125 } else {
126 # Add a negative cache entry, may be overridden
127 $this->trimCache();
128 $this->cache[$dbkey][$time] = false;
129 $cacheEntry =& $this->cache[$dbkey][$time];
131 } else {
132 $useCache = false;
135 # Check the local repo
136 $image = $this->localRepo->findFile( $title, $options );
137 if ( $image ) {
138 if ( $useCache ) {
139 $cacheEntry = $image;
141 return $image;
144 # Check the foreign repos
145 foreach ( $this->foreignRepos as $repo ) {
146 $image = $repo->findFile( $title, $options );
147 if ( $image ) {
148 if ( $useCache ) {
149 $cacheEntry = $image;
151 return $image;
154 # Not found, do not override negative cache
155 return false;
158 function findFiles( $inputItems ) {
159 if ( !$this->reposInitialised ) {
160 $this->initialiseRepos();
163 $items = array();
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 ) );
184 return $images;
188 * Interface for FileRepo::checkRedirect()
190 function checkRedirect( $title ) {
191 if ( !$this->reposInitialised ) {
192 $this->initialiseRepos();
195 $redir = $this->localRepo->checkRedirect( $title );
196 if( $redir ) {
197 return $redir;
199 foreach ( $this->foreignRepos as $repo ) {
200 $redir = $repo->checkRedirect( $title );
201 if ( $redir ) {
202 return $redir;
205 return false;
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 ) );
216 return $result;
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];
230 } else {
231 return false;
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)
243 return $repo;
245 return false;
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
258 * first parameter.
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 ) ) {
267 return true;
270 return false;
274 * Does the installation have any foreign repos set up?
275 * @return Boolean
277 function hasForeignRepos() {
278 return (bool)$this->foreignRepos;
282 * Initialise the $repos array
284 function initialiseRepos() {
285 if ( $this->reposInitialised ) {
286 return;
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" );
318 return $bits;
321 function getFileProps( $fileName ) {
322 if ( FileRepo::isVirtualUrl( $fileName ) ) {
323 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
324 if ( $repoName === '' ) {
325 $repoName = 'local';
327 $repo = $this->getRepo( $repoName );
328 return $repo->getFileProps( $fileName );
329 } else {
330 return File::getPropsFromPath( $fileName );
335 * Limit cache memory
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] );