Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php |
— | — | @@ -8,8 +8,6 @@ |
9 | 9 | |
10 | 10 | /** |
11 | 11 | * A foreign repository with a remote MediaWiki with an API thingy |
12 | | - * Very hacky and inefficient |
13 | | - * do not use except for testing :D |
14 | 12 | * |
15 | 13 | * Example config: |
16 | 14 | * |
— | — | @@ -29,6 +27,9 @@ |
30 | 28 | var $apiThumbCacheExpiry = 86400; |
31 | 29 | /* Redownload thumbnail files after a month */ |
32 | 30 | var $fileCacheExpiry = 2629743; |
| 31 | + /* Local image directory */ |
| 32 | + var $directory; |
| 33 | + var $thumbDir; |
33 | 34 | |
34 | 35 | protected $mQueryCache = array(); |
35 | 36 | protected $mFileExists = array(); |
— | — | @@ -38,10 +39,14 @@ |
39 | 40 | |
40 | 41 | // http://commons.wikimedia.org/w/api.php |
41 | 42 | $this->mApiBase = isset( $info['apibase'] ) ? $info['apibase'] : null; |
| 43 | + $this->directory = isset( $info['directory'] ) ? $info['directory'] : $wgUploadDirectory; |
42 | 44 | |
43 | 45 | if( isset( $info['apiThumbCacheExpiry'] ) ) { |
44 | 46 | $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry']; |
45 | 47 | } |
| 48 | + if( isset( $info['fileCacheExpiry'] ) ) { |
| 49 | + $this->fileCacheExpiry = $info['fileCacheExpiry']; |
| 50 | + } |
46 | 51 | if( !$this->scriptDirUrl ) { |
47 | 52 | // hack for description fetches |
48 | 53 | $this->scriptDirUrl = dirname( $this->mApiBase ); |
— | — | @@ -54,6 +59,11 @@ |
55 | 60 | if( $this->canCacheThumbs() && !$this->thumbUrl ) { |
56 | 61 | $this->thumbUrl = $this->url . '/thumb'; |
57 | 62 | } |
| 63 | + if ( isset( $info['thumbDir'] ) ) { |
| 64 | + $this->thumbDir = $info['thumbDir']; |
| 65 | + } else { |
| 66 | + $this->thumbDir = "{$this->directory}/thumb"; |
| 67 | + } |
58 | 68 | } |
59 | 69 | |
60 | 70 | /** |
— | — | @@ -209,12 +219,11 @@ |
210 | 220 | * @param $height |
211 | 221 | */ |
212 | 222 | function getThumbUrlFromCache( $name, $width, $height ) { |
213 | | - global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory; |
| 223 | + global $wgMemc; |
214 | 224 | |
215 | 225 | if ( !$this->canCacheThumbs() ) { |
216 | 226 | return $this->getThumbUrl( $name, $width, $height ); |
217 | 227 | } |
218 | | - |
219 | 228 | $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name, $width ); |
220 | 229 | |
221 | 230 | $thumbUrl = $wgMemc->get($key); |
— | — | @@ -225,44 +234,49 @@ |
226 | 235 | else { |
227 | 236 | $metadata = null; |
228 | 237 | $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata ); |
229 | | - // We need the same filename as the remote one :) |
230 | | - $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); |
231 | | - $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/"; |
232 | | - $localFilename = $wgUploadDirectory . '/' . $path . $fileName; |
233 | | - $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName; |
234 | 238 | |
235 | 239 | if( !$foreignUrl ) { |
236 | 240 | wfDebug( __METHOD__ . " Could not find thumburl\n" ); |
237 | 241 | return false; |
238 | 242 | } |
| 243 | + |
| 244 | + // We need the same filename as the remote one :) |
| 245 | + $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); |
| 246 | + if( !$this->validateFilename( $fileName ) ) { |
| 247 | + wfDebug( __METHOD__ . " The deduced filename $fileName is not safe\n" ); |
| 248 | + return false; |
| 249 | + } |
| 250 | + $localPath = $this->getZonePath( 'thumb' ) . "/" . $this->getHashPath( $name ) . $name; |
| 251 | + $localFilename = $localPath . "/" . $fileName; |
| 252 | + $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . urlencode( $name ) . "/" . urlencode( $fileName ); |
| 253 | + |
239 | 254 | if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) { |
240 | 255 | wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" ); |
241 | 256 | $modified = filemtime( $localFilename ); |
242 | 257 | $remoteModified = strtotime( $metadata['timestamp'] ); |
243 | 258 | $diff = abs( $modified - $remoteModified ); |
244 | 259 | if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { |
245 | | - /* Use our current already downloaded thumbnail */ |
| 260 | + /* Use our current and already downloaded thumbnail */ |
246 | 261 | $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); |
247 | 262 | return $localUrl; |
248 | 263 | } |
249 | 264 | /* There is a new Commons file, or existing thumbnail older than a month */ |
250 | | - |
251 | 265 | } |
252 | 266 | $thumb = Http::get( $foreignUrl ); |
253 | 267 | if( !$thumb ) { |
254 | 268 | wfDebug( __METHOD__ . " Could not download thumb\n" ); |
255 | 269 | return false; |
256 | 270 | } |
257 | | - if ( !is_dir($wgUploadDirectory . '/' . $path) ) { |
258 | | - if( !wfMkdirParents($wgUploadDirectory . '/' . $path) ) { |
259 | | - wfDebug( __METHOD__ . " could not create directory for thumb\n" ); |
| 271 | + if ( !is_dir($localPath) ) { |
| 272 | + if( !wfMkdirParents($localPath) ) { |
| 273 | + wfDebug( __METHOD__ . " could not create directory $localPath for thumb\n" ); |
260 | 274 | return $foreignUrl; |
261 | 275 | } |
262 | 276 | } |
263 | 277 | |
264 | 278 | # FIXME: Delete old thumbs that aren't being used. Maintenance script? |
265 | 279 | wfSuppressWarnings(); |
266 | | - if( !file_put_contents($localFilename, $thumb ) ) { |
| 280 | + if( !file_put_contents( $localFilename, $thumb ) ) { |
267 | 281 | wfRestoreWarnings(); |
268 | 282 | wfDebug( __METHOD__ . " could not write to thumb path\n" ); |
269 | 283 | return $foreignUrl; |
— | — | @@ -289,6 +303,20 @@ |
290 | 304 | } |
291 | 305 | |
292 | 306 | /** |
| 307 | + * Get the local directory corresponding to one of the three basic zones |
| 308 | + */ |
| 309 | + function getZonePath( $zone ) { |
| 310 | + switch ( $zone ) { |
| 311 | + case 'public': |
| 312 | + return $this->directory; |
| 313 | + case 'thumb': |
| 314 | + return $this->thumbDir; |
| 315 | + default: |
| 316 | + return false; |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + /** |
293 | 321 | * Are we locally caching the thumbnails? |
294 | 322 | * @return bool |
295 | 323 | */ |