r76306 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76305‎ | r76306 | r76307 >
Date:15:05, 8 November 2010
Author:hartman
Status:ok (Comments)
Tags:
Comment:
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
Modified paths:
  • /trunk/phase3/includes/filerepo/ForeignAPIRepo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php
@@ -230,69 +230,78 @@
231231 if ( !$this->canCacheThumbs() ) {
232232 return $this->getThumbUrl( $name, $width, $height );
233233 }
234 - $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name, $width );
 234+ $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
 235+ $sizekey = "$width:$height";
235236
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 */
240248 }
241 - else {
242 - $metadata = null;
243 - $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata );
244249
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 );
249252
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+ }
259257
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;
272279 }
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;
277291 }
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+ }
284293
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 ) ) {
292297 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;
296300 }
 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;
297306 }
298307
299308 /**
@@ -321,7 +330,7 @@
322331 default:
323332 return false;
324333 }
325 - }
 334+ }
326335
327336 /**
328337 * Are we locally caching the thumbnails?
@@ -352,11 +361,11 @@
353362 if ( !isset( $options['timeout'] ) ) {
354363 $options['timeout'] = 'default';
355364 }
356 -
 365+
357366 $req = HttpRequest::factory( $url, $options );
358367 $req->setUserAgent( ForeignAPIRepo::getUserAgent() );
359368 $status = $req->execute();
360 -
 369+
361370 if ( $status->isOK() ) {
362371 return $req->getContent();
363372 } else {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r76070Add width to the cache key for thumburls in InstantCommons. No point in not c...hartman01:42, 5 November 2010

Comments

#Comment by Raymond (talk | contribs)   20:16, 8 November 2010

Seen on translatewiki:

PHP Notice: Undefined variable: thumbUrl in /www/w/includes/filerepo/ForeignAPIRepo.php on line 243
#Comment by 😂 (talk | contribs)   20:19, 8 November 2010

Fixed in r76334

Status & tagging log