From: Chad Horohoe Date: Thu, 24 Jul 2008 17:14:43 +0000 (+0000) Subject: Allow local caching of thumbs from remote APIs. Still highly hackish and should only... X-Git-Tag: 1.31.0-rc.0~46389 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/operations/recherche.php?a=commitdiff_plain;h=41d44725c722d9968fc72c1b53e12382ae786257;p=lhc%2Fweb%2Fwiklou.git Allow local caching of thumbs from remote APIs. Still highly hackish and should only be used if you're on crack. --- diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index edfc2a9978..64e5c925a3 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -30,7 +30,8 @@ abstract class FileRepo { // Optional settings $this->initialCapital = true; // by default foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription', - 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 'descriptionCacheExpiry' ) as $var ) + 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', + 'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'apiThumbCacheDir' ) as $var ) { if ( isset( $info[$var] ) ) { $this->$var = $info[$var]; diff --git a/includes/filerepo/ForeignAPIFile.php b/includes/filerepo/ForeignAPIFile.php index aaf922048e..1e332e00cd 100644 --- a/includes/filerepo/ForeignAPIFile.php +++ b/includes/filerepo/ForeignAPIFile.php @@ -31,12 +31,18 @@ class ForeignAPIFile extends File { } function transform( $params, $flags = 0 ) { - $thumbUrl = $this->repo->getThumbUrl( - $this->getName(), - isset( $params['width'] ) ? $params['width'] : -1, - isset( $params['height'] ) ? $params['height'] : -1 ); + if ( $this->repo->apiThumbCacheExpiry > 0 ) { + $thumbUrl = $this->repo->getThumbUrlFromCache( + $this->getName(), + isset( $params['width'] ) ? $params['width'] : -1, + isset( $params['height'] ) ? $params['height'] : -1 ); + } else { + $thumbUrl = $this->repo->getThumbUrl( + $this->getName(), + isset( $params['width'] ) ? $params['width'] : -1, + isset( $params['height'] ) ? $params['height'] : -1 ); + } if( $thumbUrl ) { - wfDebug( __METHOD__ . " got remote thumb $thumbUrl\n" ); return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );; } return false; diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index 0dee699fb5..6073e5909d 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -73,7 +73,7 @@ class ForeignAPIRepo extends FileRepo { 'prop' => 'imageinfo' ) ) ); if( !isset( $this->mQueryCache[$url] ) ) { - $key = wfMemcKey( 'ForeignAPIRepo', $url ); + $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', $url ); $data = $wgMemc->get( $key ); if( !$data ) { $data = Http::get( $url ); @@ -102,9 +102,36 @@ class ForeignAPIRepo extends FileRepo { 'iiurlwidth' => $width, 'iiurlheight' => $height ) ); if( $info ) { + wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" ); return $info['thumburl']; } else { return false; } } + + function getThumbUrlFromCache( $name, $width, $height ) { + global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory; +; + + $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name ); + if ( $thumbUrl = $wgMemc->get($key) ) { + wfDebug("Got thumb from local cache. $thumbUrl \n"); + return $thumbUrl; + } + else { + $foreignUrl = $this->getThumbUrl( $name, $width, $height ); + $path = $this->apiThumbCacheDir . '/' . $this->name . '/' . + $name . '/'; + if ( !is_dir($wgUploadDirectory . '/' . $path) ) { + wfMkdirParents($wgUploadDirectory . '/' . $path); + } + $localUrl = $wgServer . $wgUploadPath . '/' . $path . $width . 'px-' . $name; + $thumb = Http::get( $foreignUrl ); + # FIXME: Delete old thumbs that aren't being used. Maintenance script? + file_put_contents($wgUploadDirectory . '/' . $path . $width . 'px-' . $name, $thumb ); + $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); + wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); + return $localUrl; + } + } }