From: Derk-Jan Hartman Date: Mon, 8 Nov 2010 15:05:06 +0000 (+0000) Subject: Reverts r76070, which was breaking purging. Now caching the thumburls with key filena... X-Git-Tag: 1.31.0-rc.0~33991 X-Git-Url: http://git.cyclocoop.org/%22%2C%20generer_url_ecrire%28?a=commitdiff_plain;h=b9b3cac64fb07a17b1a0fa916577e6ef54233b44;p=lhc%2Fweb%2Fwiklou.git Reverts r76070, which was breaking purging. Now caching the thumburls with key filename and value an associative array (based on width x height), with the urls --- diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index ffae9f463e..8182b013d5 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -230,69 +230,78 @@ class ForeignAPIRepo extends FileRepo { if ( !$this->canCacheThumbs() ) { return $this->getThumbUrl( $name, $width, $height ); } - $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name, $width ); - - $thumbUrl = $wgMemc->get($key); - if ( $thumbUrl ) { - wfDebug("Got thumb from local cache. $thumbUrl \n"); - return $thumbUrl; + $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name ); + $sizekey = "$width:$height"; + + /* Get the array of urls that we already know */ + $knownThumbUrls = $wgMemc->get($key); + if( !$knownThumbUrls ) { + /* No knownThumbUrls for this file */ + $knownThumbUrls = array(); + } else { + if( isset( $knownThumbUrls[$sizekey] ) ) { + wfDebug("Got thumburl from local cache. $thumbUrl \n"); + return $knownThumbUrls[$sizekey]; + } + /* This size is not yet known */ } - else { - $metadata = null; - $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata ); - if( !$foreignUrl ) { - wfDebug( __METHOD__ . " Could not find thumburl\n" ); - return false; - } + $metadata = null; + $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata ); - // We need the same filename as the remote one :) - $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); - if( !$this->validateFilename( $fileName ) ) { - wfDebug( __METHOD__ . " The deduced filename $fileName is not safe\n" ); - return false; - } - $localPath = $this->getZonePath( 'thumb' ) . "/" . $this->getHashPath( $name ) . $name; - $localFilename = $localPath . "/" . $fileName; - $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . rawurlencode( $name ) . "/" . rawurlencode( $fileName ); - - if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) { - wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" ); - $modified = filemtime( $localFilename ); - $remoteModified = strtotime( $metadata['timestamp'] ); - $current = time(); - $diff = abs( $modified - $current ); - if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { - /* Use our current and already downloaded thumbnail */ - $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); - return $localUrl; - } - /* There is a new Commons file, or existing thumbnail older than a month */ - } - $thumb = self::httpGet( $foreignUrl ); - if( !$thumb ) { - wfDebug( __METHOD__ . " Could not download thumb\n" ); - return false; - } - if ( !is_dir($localPath) ) { - if( !wfMkdirParents($localPath) ) { - wfDebug( __METHOD__ . " could not create directory $localPath for thumb\n" ); - return $foreignUrl; - } - } + if( !$foreignUrl ) { + wfDebug( __METHOD__ . " Could not find thumburl\n" ); + return false; + } - # FIXME: Delete old thumbs that aren't being used. Maintenance script? - wfSuppressWarnings(); - if( !file_put_contents( $localFilename, $thumb ) ) { - wfRestoreWarnings(); - wfDebug( __METHOD__ . " could not write to thumb path\n" ); + // We need the same filename as the remote one :) + $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); + if( !$this->validateFilename( $fileName ) ) { + wfDebug( __METHOD__ . " The deduced filename $fileName is not safe\n" ); + return false; + } + $localPath = $this->getZonePath( 'thumb' ) . "/" . $this->getHashPath( $name ) . $name; + $localFilename = $localPath . "/" . $fileName; + $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . rawurlencode( $name ) . "/" . rawurlencode( $fileName ); + + if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) { + wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" ); + $modified = filemtime( $localFilename ); + $remoteModified = strtotime( $metadata['timestamp'] ); + $current = time(); + $diff = abs( $modified - $current ); + if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { + /* Use our current and already downloaded thumbnail */ + $knownThumbUrls["$width:$height"] = $localUrl; + $wgMemc->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); + return $localUrl; + } + /* There is a new Commons file, or existing thumbnail older than a month */ + } + $thumb = self::httpGet( $foreignUrl ); + if( !$thumb ) { + wfDebug( __METHOD__ . " Could not download thumb\n" ); + return false; + } + if ( !is_dir($localPath) ) { + if( !wfMkdirParents($localPath) ) { + wfDebug( __METHOD__ . " could not create directory $localPath for thumb\n" ); return $foreignUrl; } + } + + # FIXME: Delete old thumbs that aren't being used. Maintenance script? + wfSuppressWarnings(); + if( !file_put_contents( $localFilename, $thumb ) ) { wfRestoreWarnings(); - $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); - wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); - return $localUrl; + wfDebug( __METHOD__ . " could not write to thumb path\n" ); + return $foreignUrl; } + wfRestoreWarnings(); + $knownThumbUrls[$sizekey] = $localUrl; + $wgMemc->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); + wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); + return $localUrl; } /** @@ -321,7 +330,7 @@ class ForeignAPIRepo extends FileRepo { default: return false; } - } + } /** * Are we locally caching the thumbnails? @@ -352,11 +361,11 @@ class ForeignAPIRepo extends FileRepo { if ( !isset( $options['timeout'] ) ) { $options['timeout'] = 'default'; } - + $req = HttpRequest::factory( $url, $options ); $req->setUserAgent( ForeignAPIRepo::getUserAgent() ); $status = $req->execute(); - + if ( $status->isOK() ) { return $req->getContent(); } else {