* Add a .htaccess to deleted images directory for additional protection
[lhc/web/wiklou.git] / includes / filerepo / ForeignAPIFile.php
index b2b9ddb..847f9dc 100644 (file)
@@ -31,17 +31,10 @@ class ForeignAPIFile extends File {
        }
 
        function transform( $params, $flags = 0 ) {
-               if ( $this->repo->apiThumbCacheExpiry > 0 && $this->repo->apiThumbCacheDir ) {
-                       $thumbUrl = $this->repo->getThumbUrlFromCache(
+               $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 ) {
                        return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
                }
@@ -104,4 +97,64 @@ class ForeignAPIFile extends File {
                        ? $this->mInfo['descriptionurl']
                        : false;
        }
+       
+       /**
+        * Only useful if we're locally caching thumbs anyway...
+        */
+       function getThumbPath( $suffix = '' ) {
+               if ( $this->repo->canCacheThumbs() ) {
+                       global $wgUploadDirectory;
+                       $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
+                       if ( $suffix ) {
+                               $path = $path . $suffix . '/';
+                       }
+                       return $path;
+               }
+               else {
+                       return null;    
+               }
+       }
+       
+       function getThumbnails() {
+               $files = array();
+               $dir = $this->getThumbPath( $this->getName() );
+               if ( is_dir( $dir ) ) {
+                       $handle = opendir( $dir );
+                       if ( $handle ) {
+                               while ( false !== ( $file = readdir($handle) ) ) {
+                                       if ( $file{0} != '.'  ) {
+                                               $files[] = $file;
+                                       }
+                               }
+                               closedir( $handle );
+                       }
+               }
+               return $files;
+       }
+       
+       function purgeCache() {
+               $this->purgeThumbnails();
+               $this->purgeDescriptionPage();
+       }
+       
+       function purgeDescriptionPage() {
+               global $wgMemc;
+               $url = $this->repo->getDescriptionRenderUrl( $this->getName() );
+               $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
+               $wgMemc->delete( $key );
+       }
+       
+       function purgeThumbnails() {
+               global $wgMemc;
+               $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
+               $wgMemc->delete( $key );
+               $files = $this->getThumbnails();
+               $dir = $this->getThumbPath( $this->getName() );
+               foreach ( $files as $file ) {
+                       unlink( $dir . $file );
+               }
+               if ( is_dir( $dir ) ) {
+                       rmdir( $dir ); // Might have already gone away, spews errors if we don't.
+               }
+       }
 }