* (bug 12935, 12981) Fully-qualify archive URLs in delete, revert messages
[lhc/web/wiklou.git] / includes / filerepo / File.php
index 4325f79..5172ad0 100644 (file)
@@ -1,8 +1,6 @@
 <?php
 
 /**
- * Base file class. Do not instantiate.
- *
  * Implements some public methods and some protected utility functions which 
  * are required by multiple child classes. Contains stub functionality for 
  * unimplemented public methods.
@@ -21,7 +19,7 @@
  *
  * @addtogroup FileRepo
  */
-class File {
+abstract class File {
        const DELETED_FILE = 1;
        const DELETED_COMMENT = 2;
        const DELETED_USER = 4;
@@ -48,7 +46,7 @@ class File {
        /**
         * The following member variables are not lazy-initialised
         */
-       var $repo, $title, $lastError;
+       var $repo, $title, $lastError, $redirected;
 
        /**
         * Call this constructor from child classes
@@ -115,9 +113,8 @@ class File {
        
        /**
         * Return the name of this file
-        * @public
         */
-       function getName() {
+       public function getName() {
                if ( !isset( $this->name ) ) {
                        $this->name = $this->repo->getNameFromTitle( $this->title );
                }
@@ -138,20 +135,28 @@ class File {
 
        /**
         * Return the associated title object
-        * @public
         */
-       function getTitle() { return $this->title; }
+       public function getTitle() { return $this->title; }
 
        /**
         * Return the URL of the file
-        * @public
         */
-       function getUrl() { 
+       public function getUrl() { 
                if ( !isset( $this->url ) ) {
                        $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
                }
                return $this->url; 
        }
+       
+       /**
+        * Return a fully-qualified URL to the file.
+        * Upload URL paths _may or may not_ be fully qualified, so
+        * we check. Local paths are assumed to belong on $wgServer.
+        * @return string
+        */
+       public function getFullUrl() {
+               return wfExpandUrl( $this->getUrl() );
+       }
 
        function getViewURL() {
                if( $this->mustRender()) {
@@ -176,10 +181,8 @@ class File {
        * or in hashed paths like /images/3/3c.
        *
        * May return false if the file is not locally accessible.
-       *
-       * @public
        */
-       function getPath() {
+       public function getPath() {
                if ( !isset( $this->path ) ) {
                        $this->path = $this->repo->getZonePath('public') . '/' . $this->getRel();
                }
@@ -188,9 +191,8 @@ class File {
 
        /**
        * Alias for getPath()
-       * @public
        */
-       function getFullPath() {
+       public function getFullPath() {
                return $this->getPath();
        }
 
@@ -200,9 +202,8 @@ class File {
         *
         * STUB
         * Overridden by LocalFile, UnregisteredLocalFile
-        * @public
         */
-       function getWidth( $page = 1 ) { return false; }
+       public function getWidth( $page = 1 ) { return false; }
 
        /**
         * Return the height of the image. Returns false if the height is unknown 
@@ -210,9 +211,28 @@ class File {
         *
         * STUB
         * Overridden by LocalFile, UnregisteredLocalFile
-        * @public
         */
-       function getHeight( $page = 1 ) { return false; }
+       public function getHeight( $page = 1 ) { return false; }
+
+       /**
+        * Returns ID or name of user who uploaded the file
+        * STUB
+        *
+        * @param $type string 'text' or 'id'
+        */
+       public function getUser( $type='text' ) { return null; }
+
+       /**
+        * Get the duration of a media file in seconds
+        */
+       public function getLength() {
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       return $handler->getLength( $this );
+               } else {
+                       return 0;
+               }
+       }
 
        /**
         * Get handler-specific metadata
@@ -225,9 +245,8 @@ class File {
         * Return the size of the image file, in bytes
         * Overridden by LocalFile, UnregisteredLocalFile
         * STUB
-        * @public
         */
-       function getSize() { return false; }
+       public function getSize() { return false; }
 
        /**
         * Returns the mime type of the file.
@@ -245,7 +264,9 @@ class File {
        function getMediaType() { return MEDIATYPE_UNKNOWN; }
 
        /**
-        * Checks if the file can be presented to the browser as a bitmap.
+        * Checks if the output of transform() for this file is likely 
+        * to be valid. If this is false, various user elements will 
+        * display a placeholder instead.
         *
         * Currently, this checks if the file is an image format
         * that can be converted to a format
@@ -254,7 +275,7 @@ class File {
         */
        function canRender() {
                if ( !isset( $this->canRender ) ) {
-                       $this->canRender = $this->getHandler() && $this->handler->canRender();
+                       $this->canRender = $this->getHandler() && $this->handler->canRender( $this );
                }
                return $this->canRender;
        }
@@ -277,16 +298,11 @@ class File {
         * @return bool
         */
        function mustRender() {
-               return $this->getHandler() && $this->handler->mustRender();
+               return $this->getHandler() && $this->handler->mustRender( $this );
        }
 
        /**
-        * Determines if this media file may be shown inline on a page.
-        *
-        * This is currently synonymous to canRender(), but this could be
-        * extended to also allow inline display of other media,
-        * like flash animations or videos. If you do so, please keep in mind that
-        * that could be a security risk.
+        * Alias for canRender()
         */
        function allowInlineDisplay() {
                return $this->canRender();
@@ -357,9 +373,8 @@ class File {
         * Overridden by LocalFile to avoid unnecessary stat calls.
         * 
         * @return boolean Whether file exists in the repository.
-        * @public
         */
-       function exists() {
+       public function exists() {
                return $this->getPath() && file_exists( $this->path );
        }
 
@@ -399,7 +414,7 @@ class File {
         * Return the file name of a thumbnail with the specified parameters
         *
         * @param array $params Handler-specific parameters
-        * @private
+        * @private -ish
         */
        function thumbName( $params ) {
                if ( !$this->getHandler() ) {
@@ -428,9 +443,8 @@ class File {
         *
         * @param integer $width        maximum width of the generated thumbnail
         * @param integer $height       maximum height of the image (optional)
-        * @public
         */
-       function createThumb( $width, $height = -1 ) {
+       public function createThumb( $width, $height = -1 ) {
                $params = array( 'width' => $width );
                if ( $height != -1 ) {
                        $params['height'] = $height;
@@ -453,11 +467,10 @@ class File {
         *                              false to just return the URL
         *
         * @return ThumbnailImage or null on failure
-        * @public
         *
         * @deprecated use transform()
         */
-       function getThumbnail( $width, $height=-1, $render = true ) {
+       public function getThumbnail( $width, $height=-1, $render = true ) {
                $params = array( 'width' => $width );
                if ( $height != -1 ) {
                        $params['height'] = $height;
@@ -479,7 +492,7 @@ class File {
 
                wfProfileIn( __METHOD__ );
                do {
-                       if ( !$this->getHandler() || !$this->handler->canRender() ) {
+                       if ( !$this->canRender() ) {
                                // not a bitmap or renderable image, don't try.
                                $thumb = $this->iconThumb();
                                break;
@@ -487,9 +500,11 @@ class File {
 
                        $script = $this->getTransformScript();
                        if ( $script && !($flags & self::RENDER_NOW) ) {
-                               // Use a script to transform on client request
+                               // Use a script to transform on client request, if possible
                                $thumb = $this->handler->getScriptedTransform( $this, $script, $params );
-                               break;
+                               if( $thumb ) {
+                                       break;
+                               }
                        }
 
                        $normalisedParams = $params;
@@ -497,7 +512,7 @@ class File {
                        $thumbName = $this->thumbName( $normalisedParams );     
                        $thumbPath = $this->getThumbPath( $thumbName );
                        $thumbUrl = $this->getThumbUrl( $thumbName );
-
+                       
                        if ( $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
                                $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
                                break;
@@ -535,7 +550,7 @@ class File {
         * STUB
         * Overridden by LocalFile
         */
-       function migrateThumbFile() {}
+       function migrateThumbFile( $thumbName ) {}
 
        /**
         * Get a MediaHandler instance for this file
@@ -559,7 +574,7 @@ class File {
                        $path = '/common/images/icons/' . $icon;
                        $filepath = $wgStyleDirectory . $path;
                        if( file_exists( $filepath ) ) {
-                               return new ThumbnailImage( $wgStylePath . $path, 120, 120 );
+                               return new ThumbnailImage( $this, $wgStylePath . $path, 120, 120 );
                        }
                }
                return null;
@@ -585,7 +600,7 @@ class File {
         * STUB
         * Overridden by LocalFile
         */
-       function purgeCache( $archiveFiles = array() ) {}
+       function purgeCache() {}
 
        /**
         * Purge the file description page, but don't go after
@@ -602,11 +617,9 @@ class File {
        
        /**
         * Purge metadata and all affected pages when the file is created,
-        * deleted, or majorly updated. A set of additional URLs may be
-        * passed to purge, such as specific file files which have changed.
-        * @param $urlArray array
+        * deleted, or majorly updated.
         */
-       function purgeEverything( $urlArr=array() ) {
+       function purgeEverything() {
                // Delete thumbnails and refresh file metadata cache
                $this->purgeCache();
                $this->purgeDescription();
@@ -619,26 +632,37 @@ class File {
                }
        }
 
+       /**
+        * Return a fragment of the history of file.
+        *
+        * STUB
+        * @param $limit integer Limit of rows to return
+        * @param $start timestamp Only revisions older than $start will be returned
+        * @param $end timestamp Only revisions newer than $end will be returned
+        */
+       function getHistory($limit = null, $start = null, $end = null) {
+               return false;
+       }
+
        /**
         * Return the history of this file, line by line. Starts with current version, 
         * then old versions. Should return an object similar to an image/oldimage 
         * database row.
         *
-        * @public
         * STUB
         * Overridden in LocalFile
         */
-       function nextHistoryLine() {
+       public function nextHistoryLine() {
                return false;
        }
 
        /**
-        * Reset the history pointer to the first element of the history
-        * @public
+        * Reset the history pointer to the first element of the history.
+        * Always call this function after using nextHistoryLine() to free db resources
         * STUB
         * Overridden in LocalFile.
         */
-       function resetHistory() {}
+       public function resetHistory() {}
 
        /**
         * Get the filename hash component of the directory including trailing slash,
@@ -666,9 +690,9 @@ class File {
                return $this->getHashPath() . rawurlencode( $this->getName() );
        }
 
-       /** Get the path of the archive directory, or a particular file if $suffix is specified */
-       function getArchivePath( $suffix = false ) {
-               $path = $this->repo->getZonePath('public') . '/archive/' . $this->getHashPath();
+       /** Get the relative path for an archive file */
+       function getArchiveRel( $suffix = false ) {
+               $path = 'archive/' . $this->getHashPath();
                if ( $suffix === false ) {
                        $path = substr( $path, 0, -1 );
                } else {
@@ -677,15 +701,25 @@ class File {
                return $path;
        }
 
-       /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
-       function getThumbPath( $suffix = false ) {
-               $path = $this->repo->getZonePath('public') . '/thumb/' . $this->getRel();
+       /** Get relative path for a thumbnail file */
+       function getThumbRel( $suffix = false ) {
+               $path = 'thumb/' . $this->getRel();
                if ( $suffix !== false ) {
                        $path .= '/' . $suffix;
                }
                return $path;
        }
 
+       /** Get the path of the archive directory, or a particular file if $suffix is specified */
+       function getArchivePath( $suffix = false ) {
+               return $this->repo->getZonePath('public') . '/' . $this->getArchiveRel();
+       }
+
+       /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
+       function getThumbPath( $suffix = false ) {
+               return $this->repo->getZonePath('public') . '/' . $this->getThumbRel( $suffix );
+       }
+
        /** Get the URL of the archive directory, or a particular file if $suffix is specified */
        function getArchiveUrl( $suffix = false ) {
                $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
@@ -816,22 +850,11 @@ class File {
                return $retVal;
        }
 
-       function getExifData() {
-               if ( !$this->getHandler() || $this->handler->getMetadataType( $this ) != 'exif' ) {
-                       return array();
-               }
-               $metadata = $this->getMetadata();
-               if ( !$metadata ) {
-                       return array();
-               }
-               $exif = unserialize( $metadata );
-               if ( !$exif ) {
-                       return array();
+       function formatMetadata() {
+               if ( !$this->getHandler() ) {
+                       return false;
                }
-               unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
-               $format = new FormatExif( $exif );
-
-               return $format->getFormattedData();
+               return $this->getHandler()->formatMetadata( $this, $this->getMetadata() );
        }
 
        /**
@@ -840,7 +863,16 @@ class File {
         * @return bool
         */
        function isLocal() { 
-               return $this->repo && $this->repo->getName() == 'local'; 
+               return $this->getRepoName() == 'local'; 
+       }
+
+       /**
+        * Returns the name of the repository.
+        *
+        * @return string
+        */
+       function getRepoName() { 
+               return $this->repo ? $this->repo->getName() : 'unknown'; 
        }
 
        /**
@@ -882,7 +914,7 @@ class File {
         * STUB
         * Overridden by LocalFile
         */
-       function delete( $reason, $suppress=false ) {
+       function delete( $reason ) {
                $this->readOnlyError();
        }
 
@@ -910,7 +942,7 @@ class File {
         * @return Bool
         */
        function isMultipage() {
-               return $this->getHandler() && $this->handler->isMultiPage();
+               return $this->getHandler() && $this->handler->isMultiPage( $this );
        }
 
        /**
@@ -919,7 +951,7 @@ class File {
         */
        function pageCount() {
                if ( !isset( $this->pageCount ) ) {
-                       if ( $this->getHandler() && $this->handler->isMultiPage() ) {
+                       if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
                                $this->pageCount = $this->handler->pageCount( $this );
                        } else {
                                $this->pageCount = false;
@@ -979,15 +1011,31 @@ class File {
        }
 
        /**
-        * Get the 14-character timestamp of the file upload, or false if 
+        * Get discription of file revision
+        * STUB
+        */
+       function getDescription() {
+               return null;
+       }
+
+       /**
+        * Get the 14-character timestamp of the file upload, or false if
+        * it doesn't exist 
         */
-       function getTimestmap() {
+       function getTimestamp() {
                $path = $this->getPath();
                if ( !file_exists( $path ) ) {
                        return false;
                }
                return wfTimestamp( filemtime( $path ) );
        }
+
+       /**
+        * Get the SHA-1 base 36 hash of the file
+        */
+       function getSha1() {
+               return self::sha1Base36( $this->getPath() );
+       }
        
        /**
         * Determine if the current user is allowed to view a particular
@@ -1001,17 +1049,23 @@ class File {
        }
 
        /**
-        * Get an associative array containing information about a file in the local filesystem
+        * Get an associative array containing information about a file in the local filesystem.
+        *
+        * @param string $path Absolute local filesystem path
+        * @param mixed $ext The file extension, or true to extract it from the filename. 
+        *                   Set it to false to ignore the extension.
         */
-       static function getPropsFromPath( $path ) {
+       static function getPropsFromPath( $path, $ext = true ) {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__.": Getting file info for $path\n" );
-               $info = array( 'fileExists' => file_exists( $path ) );
+               $info = array( 
+                       'fileExists' => file_exists( $path ) && !is_dir( $path )
+               );
                $gis = false;
                if ( $info['fileExists'] ) {
                        $magic = MimeMagic::singleton();
 
-                       $info['mime'] = $magic->guessMimeType( $path, true );
+                       $info['mime'] = $magic->guessMimeType( $path, $ext );
                        list( $info['major_mime'], $info['minor_mime'] ) = self::splitMime( $info['mime'] );
                        $info['media_type'] = $magic->getMediaType( $path, $info['mime'] );
 
@@ -1022,18 +1076,20 @@ class File {
                        $handler = MediaHandler::getHandler( $info['mime'] );
                        if ( $handler ) {
                                $tempImage = (object)array();
-                               $gis = $handler->getImageSize( $tempImage, $path );
                                $info['metadata'] = $handler->getMetadata( $tempImage, $path );
+                               $gis = $handler->getImageSize( $tempImage, $path, $info['metadata'] );
                        } else {
                                $gis = false;
                                $info['metadata'] = '';
                        }
+                       $info['sha1'] = self::sha1Base36( $path );
 
                        wfDebug(__METHOD__.": $path loaded, {$info['size']} bytes, {$info['mime']}.\n");
                } else {
                        $info['mime'] = NULL;
                        $info['media_type'] = MEDIATYPE_UNKNOWN;
                        $info['metadata'] = '';
+                       $info['sha1'] = '';
                        wfDebug(__METHOD__.": $path NOT FOUND!\n");
                }
                if( $gis ) {
@@ -1048,10 +1104,73 @@ class File {
                } else {
                        $info['width'] = 0;
                        $info['height'] = 0;
+                       $info['bits'] = 0;
                }
                wfProfileOut( __METHOD__ );
                return $info;
        }
+
+       /**
+        * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case 
+        * encoding, zero padded to 31 digits. 
+        *
+        * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
+        * fairly neatly.
+        *
+        * Returns false on failure
+        */
+       static function sha1Base36( $path ) {
+               wfSuppressWarnings();
+               $hash = sha1_file( $path );
+               wfRestoreWarnings();
+               if ( $hash === false ) {
+                       return false;
+               } else {
+                       return wfBaseConvert( $hash, 16, 36, 31 );
+               }
+       }
+
+       function getLongDesc() {
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       return $handler->getLongDesc( $this );
+               } else {
+                       return MediaHandler::getLongDesc( $this );
+               }
+       }
+
+       function getShortDesc() {
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       return $handler->getShortDesc( $this );
+               } else {
+                       return MediaHandler::getShortDesc( $this );
+               }
+       }
+
+       function getDimensionsString() {
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       return $handler->getDimensionsString( $this );
+               } else {
+                       return '';
+               }
+       }
+
+       function getRedirected() {
+               return $this->redirected;
+       }
+
+       function redirectedFrom( $from ) {
+               $this->redirected = $from;
+       }
 }
+/**
+ * Aliases for backwards compatibility with 1.6
+ */
+define( 'MW_IMG_DELETED_FILE', File::DELETED_FILE );
+define( 'MW_IMG_DELETED_COMMENT', File::DELETED_COMMENT );
+define( 'MW_IMG_DELETED_USER', File::DELETED_USER );
+define( 'MW_IMG_DELETED_RESTRICTED', File::DELETED_RESTRICTED );
+
 
-?>