Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php |
— | — | @@ -230,69 +230,78 @@ |
231 | 231 | if ( !$this->canCacheThumbs() ) { |
232 | 232 | return $this->getThumbUrl( $name, $width, $height ); |
233 | 233 | } |
234 | | - $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name, $width ); |
| 234 | + $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name ); |
| 235 | + $sizekey = "$width:$height"; |
235 | 236 | |
236 | | - $thumbUrl = $wgMemc->get($key); |
237 | | - if ( $thumbUrl ) { |
238 | | - wfDebug("Got thumb from local cache. $thumbUrl \n"); |
239 | | - return $thumbUrl; |
| 237 | + /* Get the array of urls that we already know */ |
| 238 | + $knownThumbUrls = $wgMemc->get($key); |
| 239 | + if( !$knownThumbUrls ) { |
| 240 | + /* No knownThumbUrls for this file */ |
| 241 | + $knownThumbUrls = array(); |
| 242 | + } else { |
| 243 | + if( isset( $knownThumbUrls[$sizekey] ) ) { |
| 244 | + wfDebug("Got thumburl from local cache. $thumbUrl \n"); |
| 245 | + return $knownThumbUrls[$sizekey]; |
| 246 | + } |
| 247 | + /* This size is not yet known */ |
240 | 248 | } |
241 | | - else { |
242 | | - $metadata = null; |
243 | | - $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata ); |
244 | 249 | |
245 | | - if( !$foreignUrl ) { |
246 | | - wfDebug( __METHOD__ . " Could not find thumburl\n" ); |
247 | | - return false; |
248 | | - } |
| 250 | + $metadata = null; |
| 251 | + $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata ); |
249 | 252 | |
250 | | - // We need the same filename as the remote one :) |
251 | | - $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); |
252 | | - if( !$this->validateFilename( $fileName ) ) { |
253 | | - wfDebug( __METHOD__ . " The deduced filename $fileName is not safe\n" ); |
254 | | - return false; |
255 | | - } |
256 | | - $localPath = $this->getZonePath( 'thumb' ) . "/" . $this->getHashPath( $name ) . $name; |
257 | | - $localFilename = $localPath . "/" . $fileName; |
258 | | - $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . rawurlencode( $name ) . "/" . rawurlencode( $fileName ); |
| 253 | + if( !$foreignUrl ) { |
| 254 | + wfDebug( __METHOD__ . " Could not find thumburl\n" ); |
| 255 | + return false; |
| 256 | + } |
259 | 257 | |
260 | | - if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) { |
261 | | - wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" ); |
262 | | - $modified = filemtime( $localFilename ); |
263 | | - $remoteModified = strtotime( $metadata['timestamp'] ); |
264 | | - $current = time(); |
265 | | - $diff = abs( $modified - $current ); |
266 | | - if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { |
267 | | - /* Use our current and already downloaded thumbnail */ |
268 | | - $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); |
269 | | - return $localUrl; |
270 | | - } |
271 | | - /* There is a new Commons file, or existing thumbnail older than a month */ |
| 258 | + // We need the same filename as the remote one :) |
| 259 | + $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); |
| 260 | + if( !$this->validateFilename( $fileName ) ) { |
| 261 | + wfDebug( __METHOD__ . " The deduced filename $fileName is not safe\n" ); |
| 262 | + return false; |
| 263 | + } |
| 264 | + $localPath = $this->getZonePath( 'thumb' ) . "/" . $this->getHashPath( $name ) . $name; |
| 265 | + $localFilename = $localPath . "/" . $fileName; |
| 266 | + $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . rawurlencode( $name ) . "/" . rawurlencode( $fileName ); |
| 267 | + |
| 268 | + if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) { |
| 269 | + wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" ); |
| 270 | + $modified = filemtime( $localFilename ); |
| 271 | + $remoteModified = strtotime( $metadata['timestamp'] ); |
| 272 | + $current = time(); |
| 273 | + $diff = abs( $modified - $current ); |
| 274 | + if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { |
| 275 | + /* Use our current and already downloaded thumbnail */ |
| 276 | + $knownThumbUrls["$width:$height"] = $localUrl; |
| 277 | + $wgMemc->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); |
| 278 | + return $localUrl; |
272 | 279 | } |
273 | | - $thumb = self::httpGet( $foreignUrl ); |
274 | | - if( !$thumb ) { |
275 | | - wfDebug( __METHOD__ . " Could not download thumb\n" ); |
276 | | - return false; |
| 280 | + /* There is a new Commons file, or existing thumbnail older than a month */ |
| 281 | + } |
| 282 | + $thumb = self::httpGet( $foreignUrl ); |
| 283 | + if( !$thumb ) { |
| 284 | + wfDebug( __METHOD__ . " Could not download thumb\n" ); |
| 285 | + return false; |
| 286 | + } |
| 287 | + if ( !is_dir($localPath) ) { |
| 288 | + if( !wfMkdirParents($localPath) ) { |
| 289 | + wfDebug( __METHOD__ . " could not create directory $localPath for thumb\n" ); |
| 290 | + return $foreignUrl; |
277 | 291 | } |
278 | | - if ( !is_dir($localPath) ) { |
279 | | - if( !wfMkdirParents($localPath) ) { |
280 | | - wfDebug( __METHOD__ . " could not create directory $localPath for thumb\n" ); |
281 | | - return $foreignUrl; |
282 | | - } |
283 | | - } |
| 292 | + } |
284 | 293 | |
285 | | - # FIXME: Delete old thumbs that aren't being used. Maintenance script? |
286 | | - wfSuppressWarnings(); |
287 | | - if( !file_put_contents( $localFilename, $thumb ) ) { |
288 | | - wfRestoreWarnings(); |
289 | | - wfDebug( __METHOD__ . " could not write to thumb path\n" ); |
290 | | - return $foreignUrl; |
291 | | - } |
| 294 | + # FIXME: Delete old thumbs that aren't being used. Maintenance script? |
| 295 | + wfSuppressWarnings(); |
| 296 | + if( !file_put_contents( $localFilename, $thumb ) ) { |
292 | 297 | wfRestoreWarnings(); |
293 | | - $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); |
294 | | - wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); |
295 | | - return $localUrl; |
| 298 | + wfDebug( __METHOD__ . " could not write to thumb path\n" ); |
| 299 | + return $foreignUrl; |
296 | 300 | } |
| 301 | + wfRestoreWarnings(); |
| 302 | + $knownThumbUrls[$sizekey] = $localUrl; |
| 303 | + $wgMemc->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); |
| 304 | + wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); |
| 305 | + return $localUrl; |
297 | 306 | } |
298 | 307 | |
299 | 308 | /** |
— | — | @@ -321,7 +330,7 @@ |
322 | 331 | default: |
323 | 332 | return false; |
324 | 333 | } |
325 | | - } |
| 334 | + } |
326 | 335 | |
327 | 336 | /** |
328 | 337 | * Are we locally caching the thumbnails? |
— | — | @@ -352,11 +361,11 @@ |
353 | 362 | if ( !isset( $options['timeout'] ) ) { |
354 | 363 | $options['timeout'] = 'default'; |
355 | 364 | } |
356 | | - |
| 365 | + |
357 | 366 | $req = HttpRequest::factory( $url, $options ); |
358 | 367 | $req->setUserAgent( ForeignAPIRepo::getUserAgent() ); |
359 | 368 | $status = $req->execute(); |
360 | | - |
| 369 | + |
361 | 370 | if ( $status->isOK() ) { |
362 | 371 | return $req->getContent(); |
363 | 372 | } else { |