From 724b2cf91205cc1b09c5677a8c01e34978717d4a Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sun, 18 Sep 2016 19:05:48 -0700 Subject: [PATCH] Remove MimeMagic depedency from FSFile and move it to /libs Change-Id: Ieaae909b49c798b6e9bed65d15961cefbdaec49b --- autoload.php | 4 +- includes/MimeMagic.php | 6 +-- includes/filerepo/FileRepo.php | 4 +- includes/{ => libs}/filebackend/FSFile.php | 53 +++++-------------- .../{ => libs}/filebackend/TempFSFile.php | 0 includes/utils/MWFileProps.php | 32 ++++++++++- 6 files changed, 51 insertions(+), 48 deletions(-) rename includes/{ => libs}/filebackend/FSFile.php (78%) rename includes/{ => libs}/filebackend/TempFSFile.php (100%) diff --git a/autoload.php b/autoload.php index cbc079c818..e5cecc9914 100644 --- a/autoload.php +++ b/autoload.php @@ -432,7 +432,7 @@ $wgAutoloadLocalClasses = [ 'ExternalStoreHttp' => __DIR__ . '/includes/externalstore/ExternalStoreHttp.php', 'ExternalStoreMedium' => __DIR__ . '/includes/externalstore/ExternalStoreMedium.php', 'ExternalStoreMwstore' => __DIR__ . '/includes/externalstore/ExternalStoreMwstore.php', - 'FSFile' => __DIR__ . '/includes/filebackend/FSFile.php', + 'FSFile' => __DIR__ . '/includes/libs/filebackend/FSFile.php', 'FSFileBackend' => __DIR__ . '/includes/filebackend/FSFileBackend.php', 'FSFileBackendDirList' => __DIR__ . '/includes/filebackend/FSFileBackend.php', 'FSFileBackendFileList' => __DIR__ . '/includes/filebackend/FSFileBackend.php', @@ -1403,7 +1403,7 @@ $wgAutoloadLocalClasses = [ 'TableDiffFormatter' => __DIR__ . '/includes/diff/TableDiffFormatter.php', 'TablePager' => __DIR__ . '/includes/pager/TablePager.php', 'TagLogFormatter' => __DIR__ . '/includes/logging/TagLogFormatter.php', - 'TempFSFile' => __DIR__ . '/includes/filebackend/TempFSFile.php', + 'TempFSFile' => __DIR__ . '/includes/libs/filebackend/TempFSFile.php', 'TempFileRepo' => __DIR__ . '/includes/filerepo/FileRepo.php', 'TemplateParser' => __DIR__ . '/includes/TemplateParser.php', 'TemplatesOnThisPageFormatter' => __DIR__ . '/includes/TemplatesOnThisPageFormatter.php', diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php index 5551865227..54d58d2d44 100644 --- a/includes/MimeMagic.php +++ b/includes/MimeMagic.php @@ -863,10 +863,8 @@ class MimeMagic { $mime = "application/x-opc+zip"; # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere if ( $ext !== true && $ext !== false ) { - /** This is the mode used by getPropsFromPath - * These MIME's are stored in the database, where we don't really want - * x-opc+zip, because we use it only for internal purposes - */ + // These MIME's are stored in the database, where we don't really want + // x-opc+zip, because we use it only for internal purposes if ( $this->isMatchingExtension( $ext, $mime ) ) { /* A known file extension for an OPC file, * find the proper mime type for that file extension diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index b5c5bf33fb..1a6c8180ef 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -1540,11 +1540,11 @@ class FileRepo { */ public function getFileProps( $virtualUrl ) { $fsFile = $this->getLocalReference( $virtualUrl ); + $mwProps = new MWFileProps( MimeMagic::singleton() ); if ( $fsFile ) { - $mwProps = new MWFileProps( MimeMagic::singleton() ); $props = $mwProps->getPropsFromPath( $fsFile->getPath(), true ); } else { - $props = FSFile::placeholderProps(); + $props = $mwProps->newPlaceholderProps(); } return $props; diff --git a/includes/filebackend/FSFile.php b/includes/libs/filebackend/FSFile.php similarity index 78% rename from includes/filebackend/FSFile.php rename to includes/libs/filebackend/FSFile.php index e7e2608ce8..d0e93da7ba 100644 --- a/includes/filebackend/FSFile.php +++ b/includes/libs/filebackend/FSFile.php @@ -93,18 +93,13 @@ class FSFile { * - fileExists * - size (filesize in bytes) * - mime (as major/minor) - * - media_type (value to be used with the MEDIATYPE_xxx constants) - * - metadata (handler specific) + * - file-mime (as major/minor) * - sha1 (in base 36) - * - width - * - height - * - bits (bitrate) - * - file-mime * - major_mime * - minor_mime * * @param string|bool $ext The file extension, or true to extract it from the filename. - * Set it to false to ignore the extension. + * Set it to false to ignore the extension. Currently unused. * @return array */ public function getProps( $ext = true ) { @@ -114,27 +109,17 @@ class FSFile { if ( $info['fileExists'] ) { $info['size'] = $this->getSize(); // bytes $info['sha1'] = $this->getSha1Base36(); - // @TODO: replace the code below with bare FileInfo use so this can go in /libs - $magic = MimeMagic::singleton(); + $mime = mime_content_type( $this->path ); # MIME type according to file contents - $info['file-mime'] = $magic->guessMimeType( $this->path, false ); - # Logical MIME type - $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $this->path ) : $ext; - $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext ); - - list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] ); - $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] ); - - # Height, width and metadata - $handler = MediaHandler::getHandler( $info['mime'] ); - if ( $handler ) { - $info['metadata'] = $handler->getMetadata( $this, $this->path ); - /** @noinspection PhpMethodParametersCountMismatchInspection */ - $gis = $handler->getImageSize( $this, $this->path, $info['metadata'] ); - if ( is_array( $gis ) ) { - $info = $this->extractImageSizeInfo( $gis ) + $info; - } + $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime; + # logical MIME type + $info['mime'] = $mime; + + if ( strpos( $mime, '/' ) !== false ) { + list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 ); + } else { + list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ]; } } @@ -146,17 +131,12 @@ class FSFile { * * Resulting array fields include: * - fileExists - * - size - * - file-mime (as major/minor) + * - size (filesize in bytes) * - mime (as major/minor) + * - file-mime (as major/minor) + * - sha1 (in base 36) * - major_mime * - minor_mime - * - media_type (value to be used with the MEDIATYPE_xxx constants) - * - metadata (handler specific) - * - sha1 (in base 36) - * - width - * - height - * - bits (bitrate) * * @return array */ @@ -168,12 +148,7 @@ class FSFile { $info['major_mime'] = null; $info['minor_mime'] = null; $info['mime'] = null; - $info['media_type'] = MEDIATYPE_UNKNOWN; - $info['metadata'] = ''; $info['sha1'] = ''; - $info['width'] = 0; - $info['height'] = 0; - $info['bits'] = 0; return $info; } diff --git a/includes/filebackend/TempFSFile.php b/includes/libs/filebackend/TempFSFile.php similarity index 100% rename from includes/filebackend/TempFSFile.php rename to includes/libs/filebackend/TempFSFile.php diff --git a/includes/utils/MWFileProps.php b/includes/utils/MWFileProps.php index b8ecd4c0c3..e60b9ab762 100644 --- a/includes/utils/MWFileProps.php +++ b/includes/utils/MWFileProps.php @@ -63,7 +63,7 @@ class MWFileProps { public function getPropsFromPath( $path, $ext ) { $fsFile = new FSFile( $path ); - $info = FSFile::placeholderProps(); + $info = $this->newPlaceholderProps(); $info['fileExists'] = $fsFile->exists(); if ( $info['fileExists'] ) { $info['size'] = $fsFile->getSize(); // bytes @@ -112,4 +112,34 @@ class MWFileProps { return $info; } + + /** + * Empty place holder props for non-existing files + * + * 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 + * + * @return array + * @since 1.28 + */ + public function newPlaceholderProps() { + return FSFile::placeholderProps() + [ + 'metadata' => '', + 'width' => 0, + 'height' => 0, + 'bits' => 0, + 'media_type' => MEDIATYPE_UNKNOWN + ]; + } } -- 2.20.1