Merged FileBackend branch. Manually avoiding merging the many prop-only changes SVN...
[lhc/web/wiklou.git] / includes / filerepo / ForeignAPIRepo.php
index 6a983ed..91f3c18 100644 (file)
@@ -32,20 +32,19 @@ class ForeignAPIRepo extends FileRepo {
        var $apiThumbCacheExpiry = 86400; /* 24*60*60 */
        /* Redownload thumbnail files after a month */
        var $fileCacheExpiry = 2592000; /* 86400*30 */
-       /* Local image directory */
-       var $directory;
-       var $thumbDir;
 
        protected $mQueryCache = array();
        protected $mFileExists = array();
 
        function __construct( $info ) {
+               global $wgLocalFileRepo, $wgUploadDirectory;
+               if ( !isset( $info['directory'] ) ) { // b/c
+                       $info['directory'] = $wgUploadDirectory; // Local image directory
+               }
                parent::__construct( $info );
-               global $wgUploadDirectory;
 
                // http://commons.wikimedia.org/w/api.php
                $this->mApiBase = isset( $info['apibase'] ) ? $info['apibase'] : null;
-               $this->directory = isset( $info['directory'] ) ? $info['directory'] : $wgUploadDirectory;
 
                if( isset( $info['apiThumbCacheExpiry'] ) ) {
                        $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry'];
@@ -59,17 +58,11 @@ class ForeignAPIRepo extends FileRepo {
                }
                // If we can cache thumbs we can guess sane defaults for these
                if( $this->canCacheThumbs() && !$this->url ) {
-                       global $wgLocalFileRepo;
                        $this->url = $wgLocalFileRepo['url'];
                }
                if( $this->canCacheThumbs() && !$this->thumbUrl ) {
                        $this->thumbUrl = $this->url . '/thumb';
                }
-               if ( isset( $info['thumbDir'] ) ) {
-                       $this->thumbDir =  $info['thumbDir'];
-               } else {
-                       $this->thumbDir = "{$this->directory}/thumb";
-               }
        }
 
        /**
@@ -284,9 +277,9 @@ class ForeignAPIRepo extends FileRepo {
                $localFilename = $localPath . "/" . $fileName;
                $localUrl =  $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . rawurlencode( $name ) . "/" . rawurlencode( $fileName );
 
-               if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) {
+               if( $this->fileExists( $localFilename ) && isset( $metadata['timestamp'] ) ) {
                        wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" );
-                       $modified = filemtime( $localFilename );
+                       $modified = $this->getFileTimestamp( $localFilename );
                        $remoteModified = strtotime( $metadata['timestamp'] );
                        $current = time();
                        $diff = abs( $modified - $current );
@@ -303,16 +296,12 @@ class ForeignAPIRepo extends FileRepo {
                        wfDebug( __METHOD__ . " Could not download thumb\n" );
                        return false;
                }
-               if ( !is_dir($localPath) ) {
-                       if( !wfMkdirParents( $localPath, null, __METHOD__ ) ) {
-                               wfDebug(  __METHOD__ . " could not create directory $localPath for thumb\n" );
-                               return $foreignUrl;
-                       }
-               }
 
                # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
                wfSuppressWarnings();
-               if( !file_put_contents( $localFilename, $thumb ) ) {
+               $backend = $this->getBackend();
+               $op = array( 'op' => 'create', 'dst' => $localFilename, 'content' => $thumb );
+               if( !$backend->doOperation( $op )->isOK() ) {
                        wfRestoreWarnings();
                        wfDebug( __METHOD__ . " could not write to thumb path\n" );
                        return $foreignUrl;
@@ -339,17 +328,14 @@ class ForeignAPIRepo extends FileRepo {
        }
 
        /**
-        * Get the local directory corresponding to one of the three basic zones
+        * Get the local directory corresponding to one of the basic zones
         */
        function getZonePath( $zone ) {
-               switch ( $zone ) {
-                       case 'public':
-                               return $this->directory;
-                       case 'thumb':
-                               return $this->thumbDir;
-                       default:
-                               return false;
+               $supported = array( 'public', 'thumb' );
+               if ( in_array( $zone, $supported ) ) {
+                       return parent::getZonePath( $zone );
                }
+               return false;
        }
 
        /**
@@ -392,4 +378,8 @@ class ForeignAPIRepo extends FileRepo {
                        return false;
                }
        }
+
+       function enumFiles( $callback ) {
+               throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
+       }
 }