Followup 1cf5a6e: use $wgResourceBasePath to refer to resources/assets, not $wgScriptPath
[lhc/web/wiklou.git] / includes / filerepo / file / File.php
index e970e38..b574c5e 100644 (file)
@@ -127,7 +127,7 @@ abstract class File {
        /** @var string Relative path including trailing slash */
        protected $hashPath;
 
-       /** @var string number of pages of a multipage document, or false for
+       /** @var string Number of pages of a multipage document, or false for
         *    documents which aren't multipage documents
         */
        protected $pageCount;
@@ -583,7 +583,7 @@ abstract class File {
         *
         * Currently used to add a warning to the image description page
         *
-        * @return bool false if the main image is both animated
+        * @return bool False if the main image is both animated
         *   and the thumbnail is not. In all other cases must return
         *   true. If image is not renderable whatsoever, should
         *   return true.
@@ -679,7 +679,7 @@ abstract class File {
        }
 
        /**
-        * Returns the mime type of the file.
+        * Returns the MIME type of the file.
         * Overridden by LocalFile, UnregisteredLocalFile
         * STUB
         *
@@ -714,7 +714,7 @@ abstract class File {
         */
        function canRender() {
                if ( !isset( $this->canRender ) ) {
-                       $this->canRender = $this->getHandler() && $this->handler->canRender( $this );
+                       $this->canRender = $this->getHandler() && $this->handler->canRender( $this ) && $this->exists();
                }
 
                return $this->canRender;
@@ -1178,7 +1178,7 @@ abstract class File {
                        return false;
                }
 
-               $this->tmpBucketedThumbCache[ $bucket ] = $tmpFile->getPath();
+               $this->tmpBucketedThumbCache[$bucket] = $tmpFile->getPath();
                // For the caching to work, we need to make the tmp file survive as long as
                // this object exists
                $tmpFile->bind( $this );
@@ -1189,7 +1189,7 @@ abstract class File {
        /**
         * Returns the most appropriate source image for the thumbnail, given a target thumbnail size
         * @param array $params
-        * @return array source path and width/height of the source
+        * @return array Source path and width/height of the source
         */
        public function getThumbnailSource( $params ) {
                if ( $this->repo
@@ -1204,8 +1204,8 @@ abstract class File {
                        }
 
                        // Try to avoid reading from storage if the file was generated by this script
-                       if ( isset( $this->tmpBucketedThumbCache[ $bucket ] ) ) {
-                               $tmpPath = $this->tmpBucketedThumbCache[ $bucket ];
+                       if ( isset( $this->tmpBucketedThumbCache[$bucket] ) ) {
+                               $tmpPath = $this->tmpBucketedThumbCache[$bucket];
 
                                if ( file_exists( $tmpPath ) ) {
                                        return array(
@@ -1231,9 +1231,25 @@ abstract class File {
                        }
                }
 
+               // Thumbnailing a very large file could result in network saturation if
+               // everyone does it at once.
+               if ( $this->getSize() >= 1e7 ) { // 10MB
+                       $that = $this;
+                       $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $this->getName() ),
+                               array(
+                                       'doWork' => function() use ( $that ) {
+                                               return $that->getLocalRefPath();
+                                       }
+                               )
+                       );
+                       $srcPath = $work->execute();
+               } else {
+                       $srcPath = $this->getLocalRefPath();
+               }
+
                // Original file
                return array(
-                       'path' => $this->getLocalRefPath(),
+                       'path' => $srcPath,
                        'width' => $this->getWidth(),
                        'height' => $this->getHeight()
                );
@@ -1261,7 +1277,7 @@ abstract class File {
        /**
         * Creates a temp FS file with the same extension and the thumbnail
         * @param string $thumbPath Thumbnail path
-        * @returns TempFSFile
+        * @return TempFSFile
         */
        protected function makeTransformTmpFile( $thumbPath ) {
                $thumbExt = FileBackend::extensionFromPath( $thumbPath );
@@ -1287,6 +1303,7 @@ abstract class File {
         * Hook into transform() to allow migration of thumbnail files
         * STUB
         * Overridden by LocalFile
+        * @param string $thumbName
         */
        function migrateThumbFile( $thumbName ) {
        }
@@ -1311,16 +1328,16 @@ abstract class File {
         * @return ThumbnailImage
         */
        function iconThumb() {
-               global $wgStylePath, $wgStyleDirectory;
+               global $wgResourceBasePath, $IP;
+               $assetsPath = "$wgResourceBasePath/resources/assets/file-type-icons/";
+               $assetsDirectory = "$IP/resources/assets/file-type-icons/";
 
                $try = array( 'fileicon-' . $this->getExtension() . '.png', 'fileicon.png' );
                foreach ( $try as $icon ) {
-                       $path = '/common/images/icons/' . $icon;
-                       $filepath = $wgStyleDirectory . $path;
-                       if ( file_exists( $filepath ) ) { // always FS
+                       if ( file_exists( $assetsDirectory . $icon ) ) { // always FS
                                $params = array( 'width' => 120, 'height' => 120 );
 
-                               return new ThumbnailImage( $this, $wgStylePath . $path, false, $params );
+                               return new ThumbnailImage( $this, $assetsPath . $icon, false, $params );
                        }
                }
 
@@ -1330,6 +1347,7 @@ abstract class File {
        /**
         * Get last thumbnailing error.
         * Largely obsolete.
+        * @return string
         */
        function getLastError() {
                return $this->lastError;
@@ -1606,7 +1624,7 @@ abstract class File {
         *
         * @param string $zone Name of requested zone
         * @param bool|string $suffix If not false, the name of a file in zone
-        * @return string path
+        * @return string Path
         */
        function getZoneUrl( $zone, $suffix = false ) {
                $this->assertRepoDefined();
@@ -1623,7 +1641,7 @@ abstract class File {
         * Get the URL of the thumbnail directory, or a particular file if $suffix is specified
         *
         * @param bool|string $suffix If not false, the name of a thumbnail file
-        * @return string path
+        * @return string Path
         */
        function getThumbUrl( $suffix = false ) {
                return $this->getZoneUrl( 'thumb', $suffix );
@@ -1633,7 +1651,7 @@ abstract class File {
         * Get the URL of the transcoded directory, or a particular file if $suffix is specified
         *
         * @param bool|string $suffix If not false, the name of a media file
-        * @return string path
+        * @return string Path
         */
        function getTranscodedUrl( $suffix = false ) {
                return $this->getZoneUrl( 'transcoded', $suffix );
@@ -1741,7 +1759,7 @@ abstract class File {
         * @param int $flags A bitwise combination of:
         *   File::DELETE_SOURCE    Delete the source file, i.e. move rather than copy
         * @param array $options Optional additional parameters
-        * @return FileRepoStatus object. On success, the value member contains the
+        * @return FileRepoStatus On success, the value member contains the
         *   archive name, or an empty string if it was a new file.
         *
         * STUB
@@ -1937,7 +1955,7 @@ abstract class File {
         * @note Use getWidth()/getHeight() instead of this method unless you have a
         *  a good reason. This method skips all caches.
         *
-        * @param string $fileName The path to the file (e.g. From getLocalPathRef() )
+        * @param string $filePath The path to the file (e.g. From getLocalPathRef() )
         * @return array The width, followed by height, with optionally more things after
         */
        function getImageSize( $filePath ) {
@@ -2072,44 +2090,6 @@ abstract class File {
                return true;
        }
 
-       /**
-        * Get an associative array containing information about a file in the local filesystem.
-        *
-        * @param string $path Absolute local filesystem path
-        * @param string|bool $ext The file extension, or true to extract it from
-        *   the filename. Set it to false to ignore the extension.
-        *
-        * @return array
-        * @deprecated since 1.19
-        */
-       static function getPropsFromPath( $path, $ext = true ) {
-               wfDebug( __METHOD__ . ": Getting file info for $path\n" );
-               wfDeprecated( __METHOD__, '1.19' );
-
-               $fsFile = new FSFile( $path );
-
-               return $fsFile->getProps();
-       }
-
-       /**
-        * 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.
-        *
-        * @param string $path
-        * @return bool|string False on failure
-        * @deprecated since 1.19
-        */
-       static function sha1Base36( $path ) {
-               wfDeprecated( __METHOD__, '1.19' );
-
-               $fsFile = new FSFile( $path );
-
-               return $fsFile->getSha1Base36();
-       }
-
        /**
         * @return array HTTP header name/value map to use for HEAD/GET request responses
         */