Big attack on unused variables...
[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 */
30 static function singleton() {
31 if ( self::$instance ) {
32 return self::$instance;
33 }
34 global $wgLocalFileRepo, $wgForeignFileRepos;
35 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
36 return self::$instance;
37 }
38
39 /**
40 * Destroy the singleton instance, so that a new one will be created next
41 * time singleton() is called.
42 */
43 static function destroySingleton() {
44 self::$instance = null;
45 }
46
47 /**
48 * Set the singleton instance to a given object
49 */
50 static function setSingleton( $instance ) {
51 self::$instance = $instance;
52 }
53
54 /**
55 * Construct a group of file repositories.
56 *
57 * @param $localInfo Associative array for local repo's info
58 * @param $foreignInfo Array of repository info arrays.
59 * Each info array is an associative array with the 'class' member
60 * giving the class name. The entire array is passed to the repository
61 * constructor as the first parameter.
62 */
63 function __construct( $localInfo, $foreignInfo ) {
64 $this->localInfo = $localInfo;
65 $this->foreignInfo = $foreignInfo;
66 $this->cache = array();
67 }
68
69 /**
70 * Search repositories for an image.
71 * You can also use wfFindFile() to do this.
72 *
73 * @param $title Mixed: Title object or string
74 * @param $options Associative array of options:
75 * time: requested time for an archived image, or false for the
76 * current version. An image object will be returned which was
77 * created at the specified time.
78 *
79 * ignoreRedirect: If true, do not follow file redirects
80 *
81 * private: If true, return restricted (deleted) files if the current
82 * user is allowed to view them. Otherwise, such files will not
83 * be found.
84 *
85 * bypassCache: If true, do not use the process-local cache of File objects
86 * @return File object or false if it is not found
87 */
88 function findFile( $title, $options = array() ) {
89 if ( !is_array( $options ) ) {
90 // MW 1.15 compat
91 $options = array( 'time' => $options );
92 }
93 if ( !$this->reposInitialised ) {
94 $this->initialiseRepos();
95 }
96 if ( !($title instanceof Title) ) {
97 $title = Title::makeTitleSafe( NS_FILE, $title );
98 if ( !is_object( $title ) ) {
99 return false;
100 }
101 }
102
103 # Check the cache
104 if ( empty( $options['ignoreRedirect'] )
105 && empty( $options['private'] )
106 && empty( $options['bypassCache'] ) )
107 {
108 $useCache = true;
109 $time = isset( $options['time'] ) ? $options['time'] : '';
110 $dbkey = $title->getDBkey();
111 if ( isset( $this->cache[$dbkey][$time] ) ) {
112 wfDebug( __METHOD__.": got File:$dbkey from process cache\n" );
113 # Move it to the end of the list so that we can delete the LRU entry later
114 $tmp = $this->cache[$dbkey];
115 unset( $this->cache[$dbkey] );
116 $this->cache[$dbkey] = $tmp;
117 # Return the entry
118 return $this->cache[$dbkey][$time];
119 } else {
120 # Add a negative cache entry, may be overridden
121 $this->trimCache();
122 $this->cache[$dbkey][$time] = false;
123 $cacheEntry =& $this->cache[$dbkey][$time];
124 }
125 } else {
126 $useCache = false;
127 }
128
129 # Check the local repo
130 $image = $this->localRepo->findFile( $title, $options );
131 if ( $image ) {
132 if ( $useCache ) {
133 $cacheEntry = $image;
134 }
135 return $image;
136 }
137
138 # Check the foreign repos
139 foreach ( $this->foreignRepos as $repo ) {
140 $image = $repo->findFile( $title, $options );
141 if ( $image ) {
142 if ( $useCache ) {
143 $cacheEntry = $image;
144 }
145 return $image;
146 }
147 }
148 # Not found, do not override negative cache
149 return false;
150 }
151
152 function findFiles( $inputItems ) {
153 if ( !$this->reposInitialised ) {
154 $this->initialiseRepos();
155 }
156
157 $items = array();
158 foreach ( $inputItems as $item ) {
159 if ( !is_array( $item ) ) {
160 $item = array( 'title' => $item );
161 }
162 if ( !( $item['title'] instanceof Title ) )
163 $item['title'] = Title::makeTitleSafe( NS_FILE, $item['title'] );
164 if ( $item['title'] )
165 $items[$item['title']->getDBkey()] = $item;
166 }
167
168 $images = $this->localRepo->findFiles( $items );
169
170 foreach ( $this->foreignRepos as $repo ) {
171 // Remove found files from $items
172 foreach ( $images as $name => $image ) {
173 unset( $items[$name] );
174 }
175
176 $images = array_merge( $images, $repo->findFiles( $items ) );
177 }
178 return $images;
179 }
180
181 /**
182 * Interface for FileRepo::checkRedirect()
183 */
184 function checkRedirect( $title ) {
185 if ( !$this->reposInitialised ) {
186 $this->initialiseRepos();
187 }
188
189 $redir = $this->localRepo->checkRedirect( $title );
190 if( $redir ) {
191 return $redir;
192 }
193 foreach ( $this->foreignRepos as $repo ) {
194 $redir = $repo->checkRedirect( $title );
195 if ( $redir ) {
196 return $redir;
197 }
198 }
199 return false;
200 }
201
202 function findBySha1( $hash ) {
203 if ( !$this->reposInitialised ) {
204 $this->initialiseRepos();
205 }
206
207 $result = $this->localRepo->findBySha1( $hash );
208 foreach ( $this->foreignRepos as $repo )
209 $result = array_merge( $result, $repo->findBySha1( $hash ) );
210 return $result;
211 }
212
213 /**
214 * Get the repo instance with a given key.
215 */
216 function getRepo( $index ) {
217 if ( !$this->reposInitialised ) {
218 $this->initialiseRepos();
219 }
220 if ( $index === 'local' ) {
221 return $this->localRepo;
222 } elseif ( isset( $this->foreignRepos[$index] ) ) {
223 return $this->foreignRepos[$index];
224 } else {
225 return false;
226 }
227 }
228 /**
229 * Get the repo instance by its name
230 */
231 function getRepoByName( $name ) {
232 if ( !$this->reposInitialised ) {
233 $this->initialiseRepos();
234 }
235 foreach ( $this->foreignRepos as $repo ) {
236 if ( $repo->name == $name)
237 return $repo;
238 }
239 return false;
240 }
241
242 /**
243 * Get the local repository, i.e. the one corresponding to the local image
244 * table. Files are typically uploaded to the local repository.
245 */
246 function getLocalRepo() {
247 return $this->getRepo( 'local' );
248 }
249
250 /**
251 * Call a function for each foreign repo, with the repo object as the
252 * first parameter.
253 *
254 * @param $callback Callback: the function to call
255 * @param $params Array: optional additional parameters to pass to the function
256 */
257 function forEachForeignRepo( $callback, $params = array() ) {
258 foreach( $this->foreignRepos as $repo ) {
259 $args = array_merge( array( $repo ), $params );
260 if( call_user_func_array( $callback, $args ) ) {
261 return true;
262 }
263 }
264 return false;
265 }
266
267 /**
268 * Does the installation have any foreign repos set up?
269 * @return Boolean
270 */
271 function hasForeignRepos() {
272 return (bool)$this->foreignRepos;
273 }
274
275 /**
276 * Initialise the $repos array
277 */
278 function initialiseRepos() {
279 if ( $this->reposInitialised ) {
280 return;
281 }
282 $this->reposInitialised = true;
283
284 $this->localRepo = $this->newRepo( $this->localInfo );
285 $this->foreignRepos = array();
286 foreach ( $this->foreignInfo as $key => $info ) {
287 $this->foreignRepos[$key] = $this->newRepo( $info );
288 }
289 }
290
291 /**
292 * Create a repo class based on an info structure
293 */
294 protected function newRepo( $info ) {
295 $class = $info['class'];
296 return new $class( $info );
297 }
298
299 /**
300 * Split a virtual URL into repo, zone and rel parts
301 * @return an array containing repo, zone and rel
302 */
303 function splitVirtualUrl( $url ) {
304 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
305 throw new MWException( __METHOD__.': unknown protoocl' );
306 }
307
308 $bits = explode( '/', substr( $url, 9 ), 3 );
309 if ( count( $bits ) != 3 ) {
310 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
311 }
312 return $bits;
313 }
314
315 function getFileProps( $fileName ) {
316 if ( FileRepo::isVirtualUrl( $fileName ) ) {
317 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
318 if ( $repoName === '' ) {
319 $repoName = 'local';
320 }
321 $repo = $this->getRepo( $repoName );
322 return $repo->getFileProps( $fileName );
323 } else {
324 return File::getPropsFromPath( $fileName );
325 }
326 }
327
328 /**
329 * Limit cache memory
330 */
331 function trimCache() {
332 while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
333 reset( $this->cache );
334 $key = key( $this->cache );
335 wfDebug( __METHOD__.": evicting $key\n" );
336 unset( $this->cache[$key] );
337 }
338 }
339 }