Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311365

Change subject: Migrate callers to new MimeMagic::getPropsFromPath() method
......................................................................

Migrate callers to new MimeMagic::getPropsFromPath() method

FSFile should not be responsible for handling this much

Change-Id: I9fe764b2a7261af507c6555e6a57273cf7d00d36
---
M includes/MimeMagic.php
M includes/filebackend/FSFile.php
M includes/filerepo/RepoGroup.php
M includes/filerepo/file/LocalFile.php
M includes/upload/UploadBase.php
M includes/upload/UploadStash.php
M maintenance/importImages.php
7 files changed, 94 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/65/311365/1

diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php
index a432d44..d33b436 100644
--- a/includes/MimeMagic.php
+++ b/includes/MimeMagic.php
@@ -373,6 +373,88 @@
        }
 
        /**
+        * Get an associative array containing information about
+        * a file with the given storage path.
+        *
+        * Resulting array fields include:
+        *   - fileExists
+        *   - size (filesize in bytes)
+        *   - mime (as major/minor)
+        *   - media_type (value to be used with the MEDIATYPE_xxx constants)
+        *   - metadata (handler specific)
+        *   - sha1 (in base 36)
+        *   - width
+        *   - height
+        *   - bits (bitrate)
+        *   - file-mime
+        *   - major_mime
+        *   - minor_mime
+        *
+        * @param string $path Filesystem path to a file
+        * @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
+        * @since 1.28
+        */
+       public function getPropsFromPath( $path, $ext ) {
+               $fsFile = new FSFile( $path );
+               $info = $fsFile->getProps( $ext );
+
+               if ( $info['fileExists'] ) {
+                       // Enhance FSFile's mime type detection
+                       $path = $fsFile->getPath();
+                       $magic = MimeMagic::singleton();
+
+                       if ( $ext === true ) {
+                               $ext = FileBackend::extensionFromPath( $path );
+                       }
+
+                       # MIME type according to file contents
+                       $info['file-mime'] = $magic->guessMimeType( $path, 
false );
+                       # logical MIME type
+                       $info['mime'] = $magic->improveTypeFromExtension( 
$info['file-mime'], $ext );
+
+                       list( $info['major_mime'], $info['minor_mime'] ) = 
File::splitMime( $info['mime'] );
+                       $info['media_type'] = $magic->getMediaType( $path, 
$info['mime'] );
+
+                       # Height, width and metadata
+                       $handler = MediaHandler::getHandler( $info['mime'] );
+                       if ( $handler ) {
+                               $tempImage = (object)[]; // XXX (hack for File 
object)
+                               /** @noinspection PhpParamsInspection */
+                               $info['metadata'] = $handler->getMetadata( 
$tempImage, $path );
+                               /** @noinspection PhpParamsInspection */
+                               $gis = $handler->getImageSize( $tempImage, 
$path, $info['metadata'] );
+                               if ( is_array( $gis ) ) {
+                                       $info = $this->extractImageSizeInfo( 
$gis ) + $info;
+                               }
+                       }
+               }
+
+               return $info;
+       }
+
+       /**
+        * Exract image size information
+        *
+        * @param array $gis
+        * @return array
+        */
+       protected function extractImageSizeInfo( array $gis ) {
+               $info = [];
+               # NOTE: $gis[2] contains a code for the image type. This is no 
longer used.
+               $info['width'] = $gis[0];
+               $info['height'] = $gis[1];
+               if ( isset( $gis['bits'] ) ) {
+                       $info['bits'] = $gis['bits'];
+               } else {
+                       $info['bits'] = 0;
+               }
+
+               return $info;
+       }
+
+       /**
         * Adds to the list mapping MIME to file extensions.
         * As an extension author, you are encouraged to submit patches to
         * MediaWiki's core to add new MIME types to mime.types.
@@ -1046,6 +1128,7 @@
                        }
                }
 
+               $type = null;
                // Check for entry for full MIME type
                if ( $mime ) {
                        $type = $this->findMediaType( $mime );
diff --git a/includes/filebackend/FSFile.php b/includes/filebackend/FSFile.php
index 221fe9f..c40e48d 100644
--- a/includes/filebackend/FSFile.php
+++ b/includes/filebackend/FSFile.php
@@ -112,9 +112,11 @@
                $info['fileExists'] = $this->exists();
 
                if ( $info['fileExists'] ) {
+                       $info['size'] = $this->getSize(); // bytes
+
+                       // @TODO: replace the below with bare FileInfo use so 
this can go in /libs
                        $magic = MimeMagic::singleton();
 
-                       # get the file extension
                        if ( $ext === true ) {
                                $ext = self::extensionFromPath( $this->path );
                        }
@@ -126,9 +128,6 @@
 
                        list( $info['major_mime'], $info['minor_mime'] ) = 
File::splitMime( $info['mime'] );
                        $info['media_type'] = $magic->getMediaType( 
$this->path, $info['mime'] );
-
-                       # Get size in bytes
-                       $info['size'] = $this->getSize();
 
                        # Height, width and metadata
                        $handler = MediaHandler::getHandler( $info['mime'] );
diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php
index d515b05..47ff092 100644
--- a/includes/filerepo/RepoGroup.php
+++ b/includes/filerepo/RepoGroup.php
@@ -451,7 +451,7 @@
 
                        return $repo->getFileProps( $fileName );
                } else {
-                       return FSFile::getPropsFromPath( $fileName );
+                       return MimeMagic::singleton()->getPropsFromPath( 
$fileName, true );
                }
        }
 
diff --git a/includes/filerepo/file/LocalFile.php 
b/includes/filerepo/file/LocalFile.php
index 618272c..c82c20b 100644
--- a/includes/filerepo/file/LocalFile.php
+++ b/includes/filerepo/file/LocalFile.php
@@ -1179,7 +1179,7 @@
                        ) {
                                $props = $this->repo->getFileProps( $srcPath );
                        } else {
-                               $props = FSFile::getPropsFromPath( $srcPath );
+                               $props = 
MimeMagic::singleton()->getPropsFromPath( $srcPath, true );
                        }
                }
 
diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php
index 1be5c24..5d458a1 100644
--- a/includes/upload/UploadBase.php
+++ b/includes/upload/UploadBase.php
@@ -446,7 +446,8 @@
                        return $status;
                }
 
-               $this->mFileProps = FSFile::getPropsFromPath( $this->mTempPath, 
$this->mFinalExtension );
+               $this->mFileProps = MimeMagic::singleton()->
+                       getPropsFromPath( $this->mTempPath, 
$this->mFinalExtension );
                $mime = $this->mFileProps['mime'];
 
                if ( $wgVerifyMimeType ) {
@@ -504,7 +505,8 @@
                # getTitle() sets some internal parameters like 
$this->mFinalExtension
                $this->getTitle();
 
-               $this->mFileProps = FSFile::getPropsFromPath( $this->mTempPath, 
$this->mFinalExtension );
+               $this->mFileProps = MimeMagic::singleton()->
+                       getPropsFromPath( $this->mTempPath, 
$this->mFinalExtension );
 
                # check MIME type, if desired
                $mime = $this->mFileProps['file-mime'];
diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php
index 000c6a4..32b8548 100644
--- a/includes/upload/UploadStash.php
+++ b/includes/upload/UploadStash.php
@@ -207,7 +207,7 @@
                        wfDebug( __METHOD__ . " tried to stash file at '$path', 
but it doesn't exist\n" );
                        throw new UploadStashBadPathException( "path doesn't 
exist" );
                }
-               $fileProps = FSFile::getPropsFromPath( $path );
+               $fileProps = MimeMagic::singleton()->getPropsFromPath( $path, 
true );
                wfDebug( __METHOD__ . " stashing file at '$path'\n" );
 
                // we will be initializing from some tmpnam files that don't 
have extensions.
diff --git a/maintenance/importImages.php b/maintenance/importImages.php
index ac700ef..1b2c70f 100644
--- a/maintenance/importImages.php
+++ b/maintenance/importImages.php
@@ -245,7 +245,7 @@
                if ( isset( $options['dry'] ) ) {
                        echo " publishing {$file} by '" . $wgUser->getName() . 
"', comment '$commentText'... ";
                } else {
-                       $props = FSFile::getPropsFromPath( $file );
+                       $props = MimeMagic::singleton()->getPropsFromPath( 
$file, true );
                        $flags = 0;
                        $publishOptions = [];
                        $handler = MediaHandler::getHandler( $props['mime'] );

-- 
To view, visit https://gerrit.wikimedia.org/r/311365
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9fe764b2a7261af507c6555e6a57273cf7d00d36
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to