*/
private $mExtraInfo = '';
+ /** @var Config */
+ private $mConfig;
+
/** @var MimeMagic The singleton instance
*/
private static $instance = null;
/** Initializes the MimeMagic object. This is called by MimeMagic::singleton().
*
* This constructor parses the mime.types and mime.info files and build internal mappings.
+ *
+ * @todo Make this constructor private once everything uses the singleton instance
+ * @param Config $config
*/
- function __construct() {
+ function __construct( Config $config = null ) {
+ if ( !$config ) {
+ wfDebug( __METHOD__ . ' called with no Config instance passed to it' );
+ $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+ }
+ $this->mConfig = $config;
+
/**
* --- load mime.types ---
*/
- global $wgMimeTypeFile, $IP;
+ global $IP;
# Allow media handling extensions adding MIME-types and MIME-info
- wfRunHooks( 'MimeMagicInit', array( $this ) );
+ Hooks::run( 'MimeMagicInit', array( $this ) );
$types = MM_WELL_KNOWN_MIME_TYPES;
- if ( $wgMimeTypeFile == 'includes/mime.types' ) {
- $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
+ $mimeTypeFile = $this->mConfig->get( 'MimeTypeFile' );
+ if ( $mimeTypeFile == 'includes/mime.types' ) {
+ $mimeTypeFile = "$IP/$mimeTypeFile";
}
- if ( $wgMimeTypeFile ) {
- if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
- wfDebug( __METHOD__ . ": loading mime types from $wgMimeTypeFile\n" );
+ if ( $mimeTypeFile ) {
+ if ( is_file( $mimeTypeFile ) && is_readable( $mimeTypeFile ) ) {
+ wfDebug( __METHOD__ . ": loading mime types from $mimeTypeFile\n" );
$types .= "\n";
- $types .= file_get_contents( $wgMimeTypeFile );
+ $types .= file_get_contents( $mimeTypeFile );
} else {
- wfDebug( __METHOD__ . ": can't load mime types from $wgMimeTypeFile\n" );
+ wfDebug( __METHOD__ . ": can't load mime types from $mimeTypeFile\n" );
}
} else {
- wfDebug( __METHOD__ . ": no mime types file defined, using build-ins only.\n" );
+ wfDebug( __METHOD__ . ": no mime types file defined, using built-ins only.\n" );
}
$types .= "\n" . $this->mExtraTypes;
* --- load mime.info ---
*/
- global $wgMimeInfoFile;
- if ( $wgMimeInfoFile == 'includes/mime.info' ) {
- $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
+ $mimeInfoFile = $this->mConfig->get( 'MimeInfoFile' );
+ if ( $mimeInfoFile == 'includes/mime.info' ) {
+ $mimeInfoFile = "$IP/$mimeInfoFile";
}
$info = MM_WELL_KNOWN_MIME_INFO;
- if ( $wgMimeInfoFile ) {
- if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
- wfDebug( __METHOD__ . ": loading mime info from $wgMimeInfoFile\n" );
+ if ( $mimeInfoFile ) {
+ if ( is_file( $mimeInfoFile ) && is_readable( $mimeInfoFile ) ) {
+ wfDebug( __METHOD__ . ": loading mime info from $mimeInfoFile\n" );
$info .= "\n";
- $info .= file_get_contents( $wgMimeInfoFile );
+ $info .= file_get_contents( $mimeInfoFile );
} else {
- wfDebug( __METHOD__ . ": can't load mime info from $wgMimeInfoFile\n" );
+ wfDebug( __METHOD__ . ": can't load mime info from $mimeInfoFile\n" );
}
} else {
- wfDebug( __METHOD__ . ": no mime info file defined, using build-ins only.\n" );
+ wfDebug( __METHOD__ . ": no mime info file defined, using built-ins only.\n" );
}
$info .= "\n" . $this->mExtraInfo;
continue;
}
- #print "processing MIME INFO line $s<br>";
+ # print "processing MIME INFO line $s<br>";
$match = array();
if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
*/
public static function singleton() {
if ( self::$instance === null ) {
- self::$instance = new MimeMagic;
+ self::$instance = new MimeMagic(
+ ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
+ );
}
return self::$instance;
}
}
# Media handling extensions can improve the MIME detected
- wfRunHooks( 'MimeMagicImproveFromExtension', array( $this, $ext, &$mime ) );
+ Hooks::run( 'MimeMagicImproveFromExtension', array( $this, $ext, &$mime ) );
if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
$mime = $this->mMimeTypeAliases[$mime];
/**
* Guess the MIME type from the file contents.
*
+ * @todo Remove $ext param
+ *
* @param string $file
* @param mixed $ext
* @return bool|string
* @throws MWException
*/
- private function doGuessMimeType( $file, $ext ) { // TODO: remove $ext param
+ private function doGuessMimeType( $file, $ext ) {
// Read a chunk of the file
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
$f = fopen( $file, 'rb' );
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( !$f ) {
return 'unknown/unknown';
}
/* Look for WebP */
- if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 8 ), "WEBPVP8 ", 8 ) == 0 ) {
+ if ( strncmp( $head, "RIFF", 4 ) == 0 && strncmp( substr( $head, 8, 7 ), "WEBPVP8", 7 ) == 0 ) {
wfDebug( __METHOD__ . ": recognized file as image/webp\n" );
return "image/webp";
}
*/
$xml = new XmlTypeCheck( $file );
if ( $xml->wellFormed ) {
- global $wgXMLMimeTypes;
- if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
- return $wgXMLMimeTypes[$xml->getRootElement()];
+ $xmlMimeTypes = $this->mConfig->get( 'XMLMimeTypes' );
+ if ( isset( $xmlMimeTypes[$xml->getRootElement()] ) ) {
+ return $xmlMimeTypes[$xml->getRootElement()];
} else {
return 'application/xml';
}
return $this->detectZipType( $head, $tail, $ext );
}
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
$gis = getimagesize( $file );
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( $gis && isset( $gis['mime'] ) ) {
$mime = $gis['mime'];
# people will hopefully nag and submit patches :)
$mime = false;
# Some strings by reference for performance - assuming well-behaved hooks
- wfRunHooks(
+ Hooks::run(
'MimeMagicGuessFromContent',
array( $this, &$head, &$tail, $file, &$mime )
);
/**
* Internal MIME type detection. Detection is done using an external
* program, if $wgMimeDetectorCommand is set. Otherwise, the fileinfo
- * extension and mime_content_type are tried (in this order), if they
- * are available. If the detections fails and $ext is not false, the MIME
- * type is guessed from the file extension, using guessTypesForExtension.
+ * extension is tried if it is available. If detection fails and $ext
+ * is not false, the MIME type is guessed from the file extension,
+ * using guessTypesForExtension.
*
* If the MIME type is still unknown, getimagesize is used to detect the
* MIME type if the file is an image. If no MIME type can be determined,
* @return string The MIME type of $file
*/
private function detectMimeType( $file, $ext = true ) {
- global $wgMimeDetectorCommand;
-
/** @todo Make $ext default to false. Or better, remove it. */
if ( $ext ) {
wfDebug( __METHOD__ . ": WARNING: use of the \$ext parameter is deprecated. "
. "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
}
+ $mimeDetectorCommand = $this->mConfig->get( 'MimeDetectorCommand' );
$m = null;
- if ( $wgMimeDetectorCommand ) {
+ if ( $mimeDetectorCommand ) {
$args = wfEscapeShellArg( $file );
- $m = wfShellExec( "$wgMimeDetectorCommand $args" );
+ $m = wfShellExec( "$mimeDetectorCommand $args" );
} elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
-
- # This required the fileinfo extension by PECL,
- # see http://pecl.php.net/package/fileinfo
- # This must be compiled into PHP
- #
- # finfo is the official replacement for the deprecated
- # mime_content_type function, see below.
- #
- # If you may need to load the fileinfo extension at runtime, set
- # $wgLoadFileinfoExtension in LocalSettings.php
-
- $mime_magic_resource = finfo_open( FILEINFO_MIME ); /* return MIME type ala mimetype extension */
+ $mime_magic_resource = finfo_open( FILEINFO_MIME );
if ( $mime_magic_resource ) {
$m = finfo_file( $mime_magic_resource, $file );
} else {
wfDebug( __METHOD__ . ": finfo_open failed on " . FILEINFO_MIME . "!\n" );
}
- } elseif ( function_exists( "mime_content_type" ) ) {
-
- # NOTE: this function is available since PHP 4.3.0, but only if
- # PHP was compiled with --with-mime-magic or, before 4.3.2, with
- # --enable-mime-magic.
- #
- # On Windows, you must set mime_magic.magicfile in php.ini to point
- # to the mime.magic file bundled with PHP; sometimes, this may even
- # be needed under *nix.
- #
- # Also note that this has been DEPRECATED in favor of the fileinfo
- # extension by PECL, see above.
- # See http://www.php.net/manual/en/ref.mime-magic.php for details.
-
- $m = mime_content_type( $file );
} else {
wfDebug( __METHOD__ . ": no magic mime detector found!\n" );
}
if ( $m ) {
# normalize
- $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
+ $m = preg_replace( '![;, ].*$!', '', $m ); # strip charset, etc
$m = trim( $m );
$m = strtolower( $m );