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