r38002 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38001‎ | r38002 | r38003 >
Date:17:14, 24 July 2008
Author:demon
Status:old
Tags:
Comment:
Allow local caching of thumbs from remote APIs. Still highly hackish and should only be used if you're on crack.
Modified paths:
  • /trunk/phase3/includes/filerepo/FileRepo.php (modified) (history)
  • /trunk/phase3/includes/filerepo/ForeignAPIFile.php (modified) (history)
  • /trunk/phase3/includes/filerepo/ForeignAPIRepo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/FileRepo.php
@@ -30,7 +30,8 @@
3131 // Optional settings
3232 $this->initialCapital = true; // by default
3333 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
34 - 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 'descriptionCacheExpiry' ) as $var )
 34+ 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
 35+ 'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'apiThumbCacheDir' ) as $var )
3536 {
3637 if ( isset( $info[$var] ) ) {
3738 $this->$var = $info[$var];
Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php
@@ -73,7 +73,7 @@
7474 'prop' => 'imageinfo' ) ) );
7575
7676 if( !isset( $this->mQueryCache[$url] ) ) {
77 - $key = wfMemcKey( 'ForeignAPIRepo', $url );
 77+ $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', $url );
7878 $data = $wgMemc->get( $key );
7979 if( !$data ) {
8080 $data = Http::get( $url );
@@ -102,9 +102,36 @@
103103 'iiurlwidth' => $width,
104104 'iiurlheight' => $height ) );
105105 if( $info ) {
 106+ wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
106107 return $info['thumburl'];
107108 } else {
108109 return false;
109110 }
110111 }
 112+
 113+ function getThumbUrlFromCache( $name, $width, $height ) {
 114+ global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
 115+;
 116+
 117+ $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
 118+ if ( $thumbUrl = $wgMemc->get($key) ) {
 119+ wfDebug("Got thumb from local cache. $thumbUrl \n");
 120+ return $thumbUrl;
 121+ }
 122+ else {
 123+ $foreignUrl = $this->getThumbUrl( $name, $width, $height );
 124+ $path = $this->apiThumbCacheDir . '/' . $this->name . '/' .
 125+ $name . '/';
 126+ if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
 127+ wfMkdirParents($wgUploadDirectory . '/' . $path);
 128+ }
 129+ $localUrl = $wgServer . $wgUploadPath . '/' . $path . $width . 'px-' . $name;
 130+ $thumb = Http::get( $foreignUrl );
 131+ # FIXME: Delete old thumbs that aren't being used. Maintenance script?
 132+ file_put_contents($wgUploadDirectory . '/' . $path . $width . 'px-' . $name, $thumb );
 133+ $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
 134+ wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
 135+ return $localUrl;
 136+ }
 137+ }
111138 }
Index: trunk/phase3/includes/filerepo/ForeignAPIFile.php
@@ -31,12 +31,18 @@
3232 }
3333
3434 function transform( $params, $flags = 0 ) {
35 - $thumbUrl = $this->repo->getThumbUrl(
36 - $this->getName(),
37 - isset( $params['width'] ) ? $params['width'] : -1,
38 - isset( $params['height'] ) ? $params['height'] : -1 );
 35+ if ( $this->repo->apiThumbCacheExpiry > 0 ) {
 36+ $thumbUrl = $this->repo->getThumbUrlFromCache(
 37+ $this->getName(),
 38+ isset( $params['width'] ) ? $params['width'] : -1,
 39+ isset( $params['height'] ) ? $params['height'] : -1 );
 40+ } else {
 41+ $thumbUrl = $this->repo->getThumbUrl(
 42+ $this->getName(),
 43+ isset( $params['width'] ) ? $params['width'] : -1,
 44+ isset( $params['height'] ) ? $params['height'] : -1 );
 45+ }
3946 if( $thumbUrl ) {
40 - wfDebug( __METHOD__ . " got remote thumb $thumbUrl\n" );
4147 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
4248 }
4349 return false;

Status & tagging log