Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
index 9cd798d..1fcd35b 100644 (file)
@@ -2,18 +2,32 @@
 /**
  * Foreign file accessible through api.php requests.
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
- * @ingroup FileRepo
+ * @ingroup FileAbstraction
  */
 
 /**
  * Foreign file accessible through api.php requests.
  * Very hacky and inefficient, do not use :D
  *
- * @ingroup FileRepo
+ * @ingroup FileAbstraction
  */
 class ForeignAPIFile extends File {
-
        private $mExists;
 
        protected $repoClass = 'ForeignApiRepo';
@@ -40,9 +54,9 @@ class ForeignAPIFile extends File {
         */
        static function newFromTitle( Title $title, $repo ) {
                $data = $repo->fetchImageQuery( array(
-                       'titles' => 'File:' . $title->getDBKey(),
-                       'iiprop' => self::getProps(),
-                       'prop'   => 'imageinfo',
+                       'titles'            => 'File:' . $title->getDBKey(),
+                       'iiprop'            => self::getProps(),
+                       'prop'              => 'imageinfo',
                        'iimetadataversion' => MediaHandler::getMetadataVersion()
                ) );
 
@@ -69,20 +83,33 @@ class ForeignAPIFile extends File {
 
        /**
         * Get the property string for iiprop and aiprop
+        * @return string
         */
        static function getProps() {
                return 'timestamp|user|comment|url|size|sha1|metadata|mime';
        }
 
        // Dummy functions...
+
+       /**
+        * @return bool
+        */
        public function exists() {
                return $this->mExists;
        }
 
+       /**
+        * @return bool
+        */
        public function getPath() {
                return false;
        }
 
+       /**
+        * @param Array $params
+        * @param int $flags
+        * @return bool|MediaTransformOutput
+        */
        function transform( $params, $flags = 0 ) {
                if( !$this->canRender() ) {
                        // show icon
@@ -102,6 +129,11 @@ class ForeignAPIFile extends File {
        }
 
        // Info we can get from API...
+
+       /**
+        * @param $page int
+        * @return int|number
+        */
        public function getWidth( $page = 1 ) {
                return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
        }
@@ -114,6 +146,9 @@ class ForeignAPIFile extends File {
                return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
        }
 
+       /**
+        * @return bool|null|string
+        */
        public function getMetadata() {
                if ( isset( $this->mInfo['metadata'] ) ) {
                        return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
@@ -121,6 +156,10 @@ class ForeignAPIFile extends File {
                return null;
        }
 
+       /**
+        * @param $metadata array
+        * @return array
+        */
        public static function parseMetadata( $metadata ) {
                if( !is_array( $metadata ) ) {
                        return $metadata;
@@ -132,36 +171,58 @@ class ForeignAPIFile extends File {
                return $ret;
        }
 
+       /**
+        * @return bool|int|null
+        */
        public function getSize() {
                return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
        }
 
+       /**
+        * @return null|string
+        */
        public function getUrl() {
                return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
        }
 
+       /**
+        * @param string $method
+        * @return int|null|string
+        */
        public function getUser( $method='text' ) {
                return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
        }
 
+       /**
+        * @return null|string
+        */
        public function getDescription() {
                return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
        }
 
+       /**
+        * @return null|String
+        */
        function getSha1() {
-               return isset( $this->mInfo['sha1'] ) ?
-                       wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) :
-                       null;
+               return isset( $this->mInfo['sha1'] )
+                       ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
+                       null;
        }
 
+       /**
+        * @return bool|Mixed|string
+        */
        function getTimestamp() {
                return wfTimestamp( TS_MW,
-                       isset( $this->mInfo['timestamp'] ) ?
-                       strval( $this->mInfo['timestamp'] ) :
-                       null
+                       isset( $this->mInfo['timestamp'] )
+                               ? strval( $this->mInfo['timestamp'] )
+                               : null
                );
        }
 
+       /**
+        * @return string
+        */
        function getMimeType() {
                if( !isset( $this->mInfo['mime'] ) ) {
                        $magic = MimeMagic::singleton();
@@ -170,12 +231,18 @@ class ForeignAPIFile extends File {
                return $this->mInfo['mime'];
        }
 
-       /// @todo FIXME: May guess wrong on file types that can be eg audio or video
+       /**
+        * @todo FIXME: May guess wrong on file types that can be eg audio or video
+        * @return int|string
+        */
        function getMediaType() {
                $magic = MimeMagic::singleton();
                return $magic->getMediaType( null, $this->getMimeType() );
        }
 
+       /**
+        * @return bool|string
+        */
        function getDescriptionUrl() {
                return isset( $this->mInfo['descriptionurl'] )
                        ? $this->mInfo['descriptionurl']
@@ -184,6 +251,8 @@ class ForeignAPIFile extends File {
 
        /**
         * Only useful if we're locally caching thumbs anyway...
+        * @param $suffix string
+        * @return null|string
         */
        function getThumbPath( $suffix = '' ) {
                if ( $this->repo->canCacheThumbs() ) {
@@ -197,46 +266,63 @@ class ForeignAPIFile extends File {
                }
        }
 
+       /**
+        * @return array
+        */
        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 );
-                       }
+               $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
+
+               $files = array();
+               foreach ( $iter as $file ) {
+                       $files[] = $file;
                }
+
                return $files;
        }
 
-       function purgeCache() {
-               $this->purgeThumbnails();
+       /**
+        * @see File::purgeCache()
+        */
+       function purgeCache( $options = array() ) {
+               $this->purgeThumbnails( $options );
                $this->purgeDescriptionPage();
        }
 
        function purgeDescriptionPage() {
                global $wgMemc, $wgContLang;
+
                $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
                $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
+
                $wgMemc->delete( $key );
        }
 
-       function purgeThumbnails() {
+       /**
+        * @param $options array
+        */
+       function purgeThumbnails( $options = array() ) {
                global $wgMemc;
+
                $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
                $wgMemc->delete( $key );
+
                $files = $this->getThumbnails();
+               // Give media handler a chance to filter the purge list
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       $handler->filterThumbnailPurgeList( $files, $options );
+               }
+
                $dir = $this->getThumbPath( $this->getName() );
+               $purgeList = array();
                foreach ( $files as $file ) {
-                       unlink( $dir . $file );
-               }
-               if ( is_dir( $dir ) ) {
-                       rmdir( $dir ); // Might have already gone away, spews errors if we don't.
+                       $purgeList[] = "{$dir}{$file}";
                }
+
+               # Delete the thumbnails
+               $this->repo->quickPurgeBatch( $purgeList );
+               # Clear out the thumbnail directory if empty
+               $this->repo->quickCleanDir( $dir );
        }
 }