Allow local caching of thumbs from remote APIs. Still highly hackish and should only...
authorChad Horohoe <demon@users.mediawiki.org>
Thu, 24 Jul 2008 17:14:43 +0000 (17:14 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Thu, 24 Jul 2008 17:14:43 +0000 (17:14 +0000)
includes/filerepo/FileRepo.php
includes/filerepo/ForeignAPIFile.php
includes/filerepo/ForeignAPIRepo.php

index edfc2a9..64e5c92 100644 (file)
@@ -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];
index aaf9220..1e332e0 100644 (file)
@@ -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;
index 0dee699..6073e59 100644 (file)
@@ -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;
+               }
+       }
 }