Document return type.
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
1 <?php
2 /**
3 * Prioritized list of file repositories
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * @defgroup FileRepo FileRepo
11 */
12
13 /**
14 * Prioritized list of file repositories
15 *
16 * @ingroup FileRepo
17 */
18 class RepoGroup {
19 var $localRepo, $foreignRepos, $reposInitialised = false;
20 var $localInfo, $foreignInfo;
21 var $cache;
22
23 protected static $instance;
24 const MAX_CACHE_SIZE = 1000;
25
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
30 */
31 static function singleton() {
32 if ( self::$instance ) {
33 return self::$instance;
34 }
35 global $wgLocalFileRepo, $wgForeignFileRepos;
36 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
37 return self::$instance;
38 }
39
40 /**
41 * Destroy the singleton instance, so that a new one will be created next
42 * time singleton() is called.
43 */
44 static function destroySingleton() {
45 self::$instance = null;
46 }
47
48 /**
49 * Set the singleton instance to a given object
50 */
51 static function setSingleton( $instance ) {
52 self::$instance = $instance;
53 }
54
55 /**
56 * Construct a group of file repositories.
57 *
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.
63 */
64 function __construct( $localInfo, $foreignInfo ) {
65 $this->localInfo = $localInfo;
66 $this->foreignInfo = $foreignInfo;
67 $this->cache = array();
68 }
69
70 /**
71 * Search repositories for an image.
72 * You can also use wfFindFile() to do this.
73 *
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.
79 *
80 * ignoreRedirect: If true, do not follow file redirects
81 *
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.
85 *
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
88 */
89 function findFile( $title, $options = array() ) {
90 if ( !is_array( $options ) ) {
91 // MW 1.15 compat
92 $options = array( 'time' => $options );
93 }
94 if ( !$this->reposInitialised ) {
95 $this->initialiseRepos();
96 }
97 if ( !($title instanceof Title) ) {
98 $title = Title::makeTitleSafe( NS_FILE, $title );
99 if ( !is_object( $title ) ) {
100 return false;
101 }
102 }
103
104 if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
105 throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
106 }
107
108 # Check the cache
109 if ( empty( $options['ignoreRedirect'] )
110 && empty( $options['private'] )
111 && empty( $options['bypassCache'] )
112 && $title->getNamespace() == NS_FILE )
113 {
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];
130 }
131 } else {
132 $useCache = false;
133 }
134
135 # Check the local repo
136 $image = $this->localRepo->findFile( $title, $options );
137 if ( $image ) {
138 if ( $useCache ) {
139 $cacheEntry = $image;
140 }
141 return $image;
142 }
143
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;
150 }
151 return $image;
152 }
153 }
154 # Not found, do not override negative cache
155 return false;
156 }
157
158 function findFiles( $inputItems ) {
159 if ( !$this->reposInitialised ) {
160 $this->initialiseRepos();
161 }
162
163 $items = array();
164 foreach ( $inputItems as $item ) {
165 if ( !is_array( $item ) ) {
166 $item = array( 'title' => $item );
167 }
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;
172 }
173
174 $images = $this->localRepo->findFiles( $items );
175
176 foreach ( $this->foreignRepos as $repo ) {
177 // Remove found files from $items
178 foreach ( $images as $name => $image ) {
179 unset( $items[$name] );
180 }
181
182 $images = array_merge( $images, $repo->findFiles( $items ) );
183 }
184 return $images;
185 }
186
187 /**
188 * Interface for FileRepo::checkRedirect()
189 */
190 function checkRedirect( $title ) {
191 if ( !$this->reposInitialised ) {
192 $this->initialiseRepos();
193 }
194
195 $redir = $this->localRepo->checkRedirect( $title );
196 if( $redir ) {
197 return $redir;
198 }
199 foreach ( $this->foreignRepos as $repo ) {
200 $redir = $repo->checkRedirect( $title );
201 if ( $redir ) {
202 return $redir;
203 }
204 }
205 return false;
206 }
207
208 function findBySha1( $hash ) {
209 if ( !$this->reposInitialised ) {
210 $this->initialiseRepos();
211 }
212
213 $result = $this->localRepo->findBySha1( $hash );
214 foreach ( $this->foreignRepos as $repo )
215 $result = array_merge( $result, $repo->findBySha1( $hash ) );
216 return $result;
217 }
218
219 /**
220 * Get the repo instance with a given key.
221 */
222 function getRepo( $index ) {
223 if ( !$this->reposInitialised ) {
224 $this->initialiseRepos();
225 }
226 if ( $index === 'local' ) {
227 return $this->localRepo;
228 } elseif ( isset( $this->foreignRepos[$index] ) ) {
229 return $this->foreignRepos[$index];
230 } else {
231 return false;
232 }
233 }
234 /**
235 * Get the repo instance by its name
236 */
237 function getRepoByName( $name ) {
238 if ( !$this->reposInitialised ) {
239 $this->initialiseRepos();
240 }
241 foreach ( $this->foreignRepos as $repo ) {
242 if ( $repo->name == $name)
243 return $repo;
244 }
245 return false;
246 }
247
248 /**
249 * Get the local repository, i.e. the one corresponding to the local image
250 * table. Files are typically uploaded to the local repository.
251 */
252 function getLocalRepo() {
253 return $this->getRepo( 'local' );
254 }
255
256 /**
257 * Call a function for each foreign repo, with the repo object as the
258 * first parameter.
259 *
260 * @param $callback Callback: the function to call
261 * @param $params Array: optional additional parameters to pass to the function
262 */
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;
268 }
269 }
270 return false;
271 }
272
273 /**
274 * Does the installation have any foreign repos set up?
275 * @return Boolean
276 */
277 function hasForeignRepos() {
278 return (bool)$this->foreignRepos;
279 }
280
281 /**
282 * Initialise the $repos array
283 */
284 function initialiseRepos() {
285 if ( $this->reposInitialised ) {
286 return;
287 }
288 $this->reposInitialised = true;
289
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 );
294 }
295 }
296
297 /**
298 * Create a repo class based on an info structure
299 */
300 protected function newRepo( $info ) {
301 $class = $info['class'];
302 return new $class( $info );
303 }
304
305 /**
306 * Split a virtual URL into repo, zone and rel parts
307 * @return an array containing repo, zone and rel
308 */
309 function splitVirtualUrl( $url ) {
310 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
311 throw new MWException( __METHOD__.': unknown protoocl' );
312 }
313
314 $bits = explode( '/', substr( $url, 9 ), 3 );
315 if ( count( $bits ) != 3 ) {
316 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
317 }
318 return $bits;
319 }
320
321 function getFileProps( $fileName ) {
322 if ( FileRepo::isVirtualUrl( $fileName ) ) {
323 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
324 if ( $repoName === '' ) {
325 $repoName = 'local';
326 }
327 $repo = $this->getRepo( $repoName );
328 return $repo->getFileProps( $fileName );
329 } else {
330 return File::getPropsFromPath( $fileName );
331 }
332 }
333
334 /**
335 * Limit cache memory
336 */
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] );
343 }
344 }
345 }