API:
[lhc/web/wiklou.git] / includes / Image.php
index 0c40c92..adcee14 100644 (file)
@@ -63,6 +63,7 @@ class Image
         * Create an Image object from an image name
         *
         * @param string $name name of the image, used to create a title object using Title::makeTitleSafe
+        * @return Image
         * @public
         */
        public static function newFromName( $name ) {
@@ -76,12 +77,19 @@ class Image
 
        /**
         * Obsolete factory function, use constructor
+        * @param Title $title
+        * @return Image
         * @deprecated
         */
        function newFromTitle( $title ) {
                return new Image( $title );
        }
 
+       /**
+        * Constructor
+        * @param Title $title
+        * @return void
+        */
        function Image( $title ) {
                if( !is_object( $title ) ) {
                        throw new MWException( 'Image constructor given bogus title.' );
@@ -102,7 +110,7 @@ class Image
         * Normalize a file extension to the common form, and ensure it's clean.
         * Extensions with non-alphanumeric characters will be discarded.
         *
-        * @param $ext string (without the .)
+        * @param string $ext (without the .)
         * @return string
         */
        static function normalizeExtension( $ext ) {
@@ -123,7 +131,7 @@ class Image
 
        /**
         * Get the memcached keys
-        * Returns an array, first element is the local cache key, second is the shared cache key, if there is one
+        * @return array[int]mixed Returns an array, first element is the local cache key, second is the shared cache key, if there is one
         */
        function getCacheKeys( ) {
                global $wgUseSharedUploads, $wgSharedUploadDBname, $wgCacheSharedUploads;
@@ -321,7 +329,6 @@ class Image
                wfProfileIn( __METHOD__ );
 
                $dbr = wfGetDB( DB_SLAVE );
-               $this->checkDBSchema($dbr);
 
                $row = $dbr->selectRow( 'image',
                        array( 'img_size', 'img_width', 'img_height', 'img_bits',
@@ -421,6 +428,7 @@ class Image
 
        /**
         * Upgrade a row if it needs it
+        * @return void
         */
        function maybeUpgradeRow() {
                if ( is_null($this->type) || $this->mime == 'image/svg' ) {
@@ -455,8 +463,6 @@ class Image
                        $dbw = wfGetDB( DB_MASTER );
                }
 
-               $this->checkDBSchema($dbw);
-
                list( $major, $minor ) = self::splitMime( $this->mime );
 
                wfDebug(__METHOD__.': upgrading '.$this->name." to the current schema\n");
@@ -482,7 +488,7 @@ class Image
         * Split an internet media type into its two components; if not
         * a two-part name, set the minor type to 'unknown'.
         *
-        * @param $mime "text/html" etc
+        * @param string $mime "text/html" etc
         * @return array ("text", "html") etc
         */
        static function splitMime( $mime ) {
@@ -550,9 +556,8 @@ class Image
        }
 
        /**
-        * Return the width of the image
-        *
-        * Returns false on error
+        * @return mixed Return the width of the image; returns false on error.
+        * @param int $page Page number to find the width of.
         * @public
         */
        function getWidth( $page = 1 ) {
@@ -570,9 +575,8 @@ class Image
        }
 
        /**
-        * Return the height of the image
-        *
-        * Returns false on error
+        * @return mixed Return the height of the image; Returns false on error.
+        * @param int $page Page number to find the height of.
         * @public
         */
        function getHeight( $page = 1 ) {
@@ -598,7 +602,7 @@ class Image
        }
 
        /**
-        * Return the size of the image file, in bytes
+        * @return int the size of the image file, in bytes
         * @public
         */
        function getSize() {
@@ -607,7 +611,7 @@ class Image
        }
 
        /**
-        * Returns the mime type of the file.
+        * @return string the mime type of the file.
         */
        function getMimeType() {
                $this->load();
@@ -632,6 +636,7 @@ class Image
         * or if it is an SVG image and SVG conversion is enabled.
         *
         * @todo remember the result of this check.
+        * @return boolean
         */
        function canRender() {
                $handler = $this->getHandler();
@@ -675,6 +680,8 @@ class Image
         *
         * Note that this function will always return true if allowInlineDisplay()
         * or isTrustedFile() is true for this file.
+        * 
+        * @return boolean
         */
        function isSafeFile() {
                if ($this->allowInlineDisplay()) return true;
@@ -695,24 +702,27 @@ class Image
                return false;
        }
 
-       /** Returns true if the file is flagged as trusted. Files flagged that way
-       * can be linked to directly, even if that is not allowed for this type of
-       * file normally.
-       *
-       * This is a dummy function right now and always returns false. It could be
-       * implemented to extract a flag from the database. The trusted flag could be
-       * set on upload, if the user has sufficient privileges, to bypass script-
-       * and html-filters. It may even be coupled with cryptographics signatures
-       * or such.
-       */
+       /**
+        * Returns true if the file is flagged as trusted. Files flagged that way
+        * can be linked to directly, even if that is not allowed for this type of
+        * file normally.
+        *
+        * This is a dummy function right now and always returns false. It could be
+        * implemented to extract a flag from the database. The trusted flag could be
+        * set on upload, if the user has sufficient privileges, to bypass script-
+        * and html-filters. It may even be coupled with cryptographics signatures
+        * or such.
+        * @return boolean
+        */
        function isTrustedFile() {
-               #this could be implemented to check a flag in the databas,
+               #this could be implemented to check a flag in the database,
                #look for signatures, etc
                return false;
        }
 
        /**
         * Return the escapeLocalURL of this image
+        * @param string $query URL query string
         * @public
         */
        function getEscapeLocalURL( $query=false) {
@@ -735,9 +745,8 @@ class Image
         * @param boolean $fromSharedDirectory  Should this be in $wgSharedUploadPath?
         * @return string URL of $name image
         * @public
-        * @static
         */
-       function imageUrl( $name, $fromSharedDirectory = false ) {
+       static function imageUrl( $name, $fromSharedDirectory = false ) {
                global $wgUploadPath,$wgUploadBaseUrl,$wgSharedUploadPath;
                if($fromSharedDirectory) {
                        $base = '';
@@ -762,6 +771,9 @@ class Image
 
        /**
         * @todo document
+        * @param string $thumbName
+        * @param string $subdir
+        * @return string
         * @private
         */
        function thumbUrlFromName( $thumbName, $subdir = 'thumb' ) {
@@ -795,6 +807,9 @@ class Image
                }
        }
 
+       /**
+        * @return mixed
+        */
        function getTransformScript() {
                global $wgSharedThumbnailScriptPath, $wgThumbnailScriptPath;
                if ( $this->fromSharedDirectory ) {
@@ -811,6 +826,8 @@ class Image
 
        /**
         * Get a ThumbnailImage which is the same size as the source
+        * @param mixed $page
+        * @return MediaTransformOutput
         */
        function getUnscaledThumb( $page = false ) {
                if ( $page ) {
@@ -828,6 +845,7 @@ class Image
         * Return the file name of a thumbnail with the specified parameters
         *
         * @param array $params Handler-specific parameters
+        * @return string file name of a thumbnail with the specified parameters
         * @private
         */
        function thumbName( $params ) {
@@ -835,7 +853,7 @@ class Image
                if ( !$handler ) {
                        return null;
                }
-               list( $thumbExt, $thumbMime ) = self::getThumbType( $this->extension, $this->mime );
+               list( $thumbExt, /* $thumbMime */ ) = self::getThumbType( $this->extension, $this->mime );
                $thumbName = $handler->makeParamString( $params ) . '-' . $this->name;
                if ( $thumbExt != $this->extension ) {
                        $thumbName .= ".$thumbExt";
@@ -898,8 +916,8 @@ class Image
        /**
         * Transform a media file
         *
-        * @param array $params An associative array of handler-specific parameters. Typical 
-        *                      keys are width, height and page.
+        * @param array[string]mixed $params An associative array of handler-specific parameters.
+        *                  Typical keys are width, height and page.
         * @param integer $flags A bitfield, may contain self::RENDER_NOW to force rendering
         * @return MediaTransformOutput
         */
@@ -924,22 +942,23 @@ class Image
 
                        $normalisedParams = $params;
                        $handler->normaliseParams( $this, $normalisedParams );
-                       list( $thumbExt, $thumbMime ) = self::getThumbType( $this->extension, $this->mime );
                        $thumbName = $this->thumbName( $normalisedParams );     
                        $thumbPath = wfImageThumbDir( $this->name, $this->fromSharedDirectory ) .  "/$thumbName";
                        $thumbUrl = $this->thumbUrlFromName( $thumbName );
 
-                       $this->migrateThumbFile( $thumbName );
 
-                       if ( file_exists( $thumbPath ) ) {
+                       if ( !$wgGenerateThumbnailOnParse && !($flags & self::RENDER_NOW ) ) {
                                $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
                                break;
                        }
-
-                       if ( !$wgGenerateThumbnailOnParse && !($flags & self::RENDER_NOW ) ) {
+                       
+                       wfDebug( "Doing stat for $thumbPath\n" );
+                       $this->migrateThumbFile( $thumbName );
+                       if ( file_exists( $thumbPath ) ) {
                                $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
                                break;
                        }
+
                        $thumb = $handler->doTransform( $this, $thumbPath, $thumbUrl, $params );
 
                        // Ignore errors if requested
@@ -963,6 +982,8 @@ class Image
 
        /**
         * Fix thumbnail files from 1.4 or before, with extreme prejudice
+        * @param string $thumbName File name of thumbnail.
+        * @return void
         */
        function migrateThumbFile( $thumbName ) {
                $thumbDir = wfImageThumbDir( $this->name, $this->fromSharedDirectory );
@@ -1004,8 +1025,8 @@ class Image
        function iconThumb() {
                global $wgStylePath, $wgStyleDirectory;
 
-               $try = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
-               foreach( $try as $icon ) {
+               $icons = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
+               foreach( $icons as $icon ) {
                        $path = '/common/images/icons/' . $icon;
                        $filepath = $wgStyleDirectory . $path;
                        if( file_exists( $filepath ) ) {
@@ -1018,6 +1039,7 @@ class Image
        /**
         * Get last thumbnailing error.
         * Largely obsolete.
+        * @return mixed
         */
        function getLastError() {
                return $this->lastError;
@@ -1025,6 +1047,8 @@ class Image
 
        /**
         * Get all thumbnail names previously generated for this image
+        * @param boolean $shared
+        * @return array[]string
         */
        function getThumbnails( $shared = false ) {
                if ( Image::isHashed( $shared ) ) {
@@ -1037,7 +1061,7 @@ class Image
 
                                if ( $handle ) {
                                        while ( false !== ( $file = readdir($handle) ) ) {
-                                               if ( $file{0} != '.' ) {
+                                               if ( $file[0] != '.' ) {
                                                        $files[] = $file;
                                                }
                                        }
@@ -1053,15 +1077,19 @@ class Image
 
        /**
         * Refresh metadata in memcached, but don't touch thumbnails or squid
+        * @return void
         */
        function purgeMetadataCache() {
                clearstatcache();
-               $this->upgradeRow();
+               $this->loadFromFile();
                $this->saveToCache();
        }
 
        /**
         * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid
+        * @param array $archiveFiles
+        * @param boolean $shared
+        * @return void
         */
        function purgeCache( $archiveFiles = array(), $shared = false ) {
                global $wgUseSquid;
@@ -1074,8 +1102,9 @@ class Image
                $dir = wfImageThumbDir( $this->name, $shared );
                $urls = array();
                foreach ( $files as $file ) {
-                       $m = array();
-                       if ( preg_match( '/^\d+px/', $file, $m ) ) {
+                       # Check that the base image name is part of the thumb name
+                       # This is a basic sanity check to avoid erasing unrelated directories
+                       if ( strpos( $file, $this->name ) !== false ) {
                                $url = $this->thumbUrlFromName( $file );
                                $urls[] = $url;
                                @unlink( "$dir/$file" );
@@ -1096,6 +1125,7 @@ class Image
         * Purge the image description page, but don't go after
         * pages using the image. Use when modifying file history
         * but not the current data.
+        * @return void
         */
        function purgeDescription() {
                $page = Title::makeTitle( NS_IMAGE, $this->name );
@@ -1105,9 +1135,10 @@ class Image
 
        /**
         * Purge metadata and all affected pages when the image is created,
-        * deleted, or majorly updated. A set of additional URLs may be
-        * passed to purge, such as specific image files which have changed.
-        * @param $urlArray array
+        * deleted, or majorly updated.
+        * @param array $urlArray A set of additional URLs may be passed to purge, 
+        *        such as specific image files which have changed (param not used?)
+        * @return void
         */
        function purgeEverything( $urlArr=array() ) {
                // Delete thumbnails and refresh image metadata cache
@@ -1119,38 +1150,6 @@ class Image
                $update->doUpdate();
        }
 
-       /**
-        * Check the image table schema on the given connection for subtle problems
-        */
-       function checkDBSchema(&$db) {
-               static $checkDone = false;
-               global $wgCheckDBSchema;
-               if (!$wgCheckDBSchema || $checkDone) {
-                       return;
-               }
-               # img_name must be unique
-               if ( !$db->indexUnique( 'image', 'img_name' ) && !$db->indexExists('image','PRIMARY') ) {
-                       throw new MWException( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
-               }
-               $checkDone = true;
-
-               # new fields must exist
-               # 
-               # Not really, there's hundreds of checks like this that we could do and they're all pointless, because 
-               # if the fields are missing, the database will loudly report a query error, the first time you try to do 
-               # something. The only reason I put the above schema check in was because the absence of that particular
-               # index would lead to an annoying subtle bug. No error message, just some very odd behaviour on duplicate
-               # uploads. -- TS
-               /*
-               if ( !$db->fieldExists( 'image', 'img_media_type' )
-                 || !$db->fieldExists( 'image', 'img_metadata' )
-                 || !$db->fieldExists( 'image', 'img_width' ) ) {
-
-                       throw new MWException( 'Database schema not up to date, please run maintenance/update.php' );
-                }
-                */
-       }
-
        /**
         * Return the image history of this image, line by line.
         * starts with current version, then old versions.
@@ -1160,12 +1159,11 @@ class Image
         *  2, ... return next old version from above query
         *
         * @public
+        * @return mixed false on no next history, object otherwise.
         */
        function nextHistoryLine() {
                $dbr = wfGetDB( DB_SLAVE );
 
-               $this->checkDBSchema($dbr);
-
                if ( $this->historyLine == 0 ) {// called for the first time, return line from cur
                        $this->historyRes = $dbr->select( 'image',
                                array(
@@ -1208,6 +1206,7 @@ class Image
        /**
         * Reset the history pointer to the first element of the history
         * @public
+        * @return void
         */
        function resetHistory() {
                $this->historyLine = 0;
@@ -1225,7 +1224,7 @@ class Image
        * @param boolean $fromSharedDirectory Return the path to the file
        *   in a shared repository (see $wgUseSharedRepository and related
        *   options in DefaultSettings.php) instead of a local one.
-       *
+       * @return string Full filesystem path to the file.
        */
        function getFullPath( $fromSharedRepository = false ) {
                global $wgUploadDirectory, $wgSharedUploadDirectory;
@@ -1244,8 +1243,8 @@ class Image
        }
 
        /**
+        * @param boolean $shared
         * @return bool
-        * @static
         */
        public static function isHashed( $shared ) {
                global $wgHashedUploadDirectory, $wgHashedSharedUploadDirectory;
@@ -1254,14 +1253,19 @@ class Image
 
        /**
         * Record an image upload in the upload log and the image table
+        * @param string $oldver
+        * @param string $desc
+        * @param string $license
+        * @param string $copyStatus
+        * @param string $source
+        * @param boolean $watch
+        * @return boolean
         */
        function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
                global $wgUser, $wgUseCopyrightUpload;
 
                $dbw = wfGetDB( DB_MASTER );
 
-               $this->checkDBSchema($dbw);
-
                // Delete thumbnails and refresh the metadata cache
                $this->purgeCache();
 
@@ -1412,6 +1416,8 @@ class Image
         * This is mostly copied from Title::getLinksTo()
         *
         * @deprecated Use HTMLCacheUpdate, this function uses too much memory
+        * @param string $options
+        * @return array[int]Title
         */
        function getLinksTo( $options = '' ) {
                wfProfileIn( __METHOD__ );
@@ -1442,8 +1448,10 @@ class Image
                return $retVal;
        }
 
+       /**
+        * @return array
+        */
        function getExifData() {
-               global $wgRequest;
                $handler = $this->getHandler();
                if ( !$handler || $handler->getMetadataType( $this ) != 'exif' ) {
                        return array();
@@ -1489,8 +1497,9 @@ class Image
         *
         * Cache purging is done; logging is caller's responsibility.
         *
-        * @param $reason
-        * @return true on success, false on some kind of failure
+        * @param string $reason
+        * @param boolean $suppress
+        * @return boolean true on success, false on some kind of failure
         */
        function delete( $reason, $suppress=false ) {
                $transaction = new FSTransaction();
@@ -1554,9 +1563,11 @@ class Image
         *
         * Cache purging is done; logging is caller's responsibility.
         *
-        * @param $reason
+        * @param string $archiveName
+        * @param string $reason
+        * @param boolean $suppress
         * @throws MWException or FSException on database or filestore failure
-        * @return true on success, false on some kind of failure
+        * @return boolean true on success, false on some kind of failure
         */
        function deleteOld( $archiveName, $reason, $suppress=false ) {
                $transaction = new FSTransaction();
@@ -1600,7 +1611,9 @@ class Image
        /**
         * Delete the current version of a file.
         * May throw a database error.
-        * @return true on success, false on failure
+        * @param string $reason
+        * @param boolean $suppress
+        * @return boolean true on success, false on failure
         */
        private function prepareDeleteCurrent( $reason, $suppress=false ) {
                return $this->prepareDeleteVersion(
@@ -1630,7 +1643,10 @@ class Image
        /**
         * Delete a given older version of a file.
         * May throw a database error.
-        * @return true on success, false on failure
+        * @param string $archiveName
+        * @param string $reason
+        * @param boolean $suppress
+        * @return boolean true on success, false on failure
         */
        private function prepareDeleteOld( $archiveName, $reason, $suppress=false ) {
                $oldpath = wfImageArchiveDir( $this->name ) .