MediaWiki  1.27.2
MimeMagic Class Reference

Implements functions related to MIME types such as detection and mapping to file extension. More...

Collaboration diagram for MimeMagic:

Public Member Functions

 __construct (Config $config=null)
 Initializes the MimeMagic object. More...
 
 addExtraInfo ($info)
 Adds to the list mapping MIME to media type. More...
 
 addExtraTypes ($types)
 Adds to the list mapping MIME to file extensions. More...
 
 detectZipType ($header, $tail=null, $ext=false)
 Detect application-specific file type of a given ZIP file from its header data. More...
 
 findMediaType ($extMime)
 Returns a media code matching the given MIME type or file extension. More...
 
 getExtensionsForType ($mime)
 Returns a list of file extensions for a given MIME type as a space separated string or null if the MIME type was unrecognized. More...
 
 getIEMimeTypes ($fileName, $chunk, $proposed)
 Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content. More...
 
 getMediaType ($path=null, $mime=null)
 Determine the media type code for a file, using its MIME type, name and possibly its contents. More...
 
 getTypesForExtension ($ext)
 Returns a list of MIME types for a given file extension as a space separated string or null if the extension was unrecognized. More...
 
 guessMimeType ($file, $ext=true)
 MIME type detection. More...
 
 guessTypesForExtension ($ext)
 Returns a single MIME type for a given file extension or null if unknown. More...
 
 improveTypeFromExtension ($mime, $ext)
 Improves a MIME type using the file extension. More...
 
 isMatchingExtension ($extension, $mime)
 Tests if the extension matches the given MIME type. More...
 
 isPHPImageType ($mime)
 Returns true if the MIME type is known to represent an image format supported by the PHP GD library. More...
 
 isRecognizableExtension ($extension)
 Returns true if the extension represents a type which can be reliably detected from its content. More...
 

Static Public Member Functions

static singleton ()
 Get an instance of this class. More...
 

Public Attributes

array $mExtToMime = null
 Map of file extensions types to MIME types (as a space separated list) More...
 

Protected Member Functions

 getIEContentAnalyzer ()
 Get a cached instance of IEContentAnalyzer. More...
 

Protected Attributes

IEContentAnalyzer $mIEAnalyzer
 
array $mMediaTypes = null
 Mapping of media types to arrays of MIME types. More...
 
array $mMimeToExt = null
 Map of MIME types to file extensions (as a space separated list) More...
 
array $mMimeTypeAliases = null
 Map of MIME type aliases. More...
 

Private Member Functions

 detectMimeType ($file, $ext=true)
 Internal MIME type detection. More...
 
 doGuessMimeType ($file, $ext)
 Guess the MIME type from the file contents. More...
 

Private Attributes

Config $mConfig
 
string $mExtraInfo = ''
 Extra MIME info, set for example by media handling extensions. More...
 
string $mExtraTypes = ''
 Extra MIME types, set for example by media handling extensions. More...
 

Static Private Attributes

static MimeMagic $instance = null
 The singleton instance. More...
 

Detailed Description

Implements functions related to MIME types such as detection and mapping to file extension.

Instances of this class are stateless, there only needs to be one global instance of MimeMagic. Please use MimeMagic::singleton() to get that instance.

Definition at line 144 of file MimeMagic.php.

Constructor & Destructor Documentation

MimeMagic::__construct ( Config  $config = 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
Parameters
Config$config

Definition at line 189 of file MimeMagic.php.

References $e, $ext, $extensions, $IP, $lines, $mExtraInfo, $mExtraTypes, $mime, $s, as, ConfigFactory\getDefaultInstance(), global, MEDIATYPE_UNKNOWN, MM_WELL_KNOWN_MIME_INFO, MM_WELL_KNOWN_MIME_TYPES, Hooks\run(), and wfDebug().

Member Function Documentation

MimeMagic::addExtraInfo (   $info)

Adds to the list mapping MIME to media type.

As an extension author, you are encouraged to submit patches to MediaWiki's core to add new MIME info to mime.info.

Parameters
string$info

Definition at line 391 of file MimeMagic.php.

MimeMagic::addExtraTypes (   $types)

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.

Parameters
string$types

Definition at line 381 of file MimeMagic.php.

MimeMagic::detectMimeType (   $file,
  $ext = true 
)
private

Internal MIME type detection.

Detection is done using an external program, if $wgMimeDetectorCommand is set. Otherwise, the fileinfo 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, this function returns 'unknown/unknown'.

Parameters
string$fileThe file to check
string | bool$extThe file extension, or true (default) to extract it from the filename. Set it to false to ignore the extension. DEPRECATED! Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
Returns
string The MIME type of $file

Definition at line 933 of file MimeMagic.php.

References $args, $ext, etc, guessTypesForExtension(), isRecognizableExtension(), wfDebug(), wfEscapeShellArg(), and wfShellExec().

Referenced by guessMimeType().

MimeMagic::detectZipType (   $header,
  $tail = null,
  $ext = false 
)

Detect application-specific file type of a given ZIP file from its header data.

Currently works for OpenDocument and OpenXML types... If can't tell, returns 'application/zip'.

Parameters
string$headerSome reasonably-sized chunk of file header
string | null$tailThe tail of the file
string | bool$extThe file extension, or true to extract it from the filename. Set it to false (default) to ignore the extension. DEPRECATED! Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
Returns
string

Definition at line 828 of file MimeMagic.php.

References $ext, $matches, $mime, guessTypesForExtension(), isMatchingExtension(), and wfDebug().

Referenced by doGuessMimeType().

MimeMagic::doGuessMimeType (   $file,
  $ext 
)
private

Guess the MIME type from the file contents.

Todo:
Remove $ext param
Parameters
string$file
mixed$ext
Returns
bool|string
Exceptions
MWException

Definition at line 627 of file MimeMagic.php.

References $ext, $mime, as, detectZipType(), Hooks\run(), and wfDebug().

Referenced by guessMimeType().

MimeMagic::findMediaType (   $extMime)

Returns a media code matching the given MIME type or file extension.

File extensions are represented by a string starting with a dot (.) to distinguish them from MIME types.

This function relies on the mapping defined by $this->mMediaTypes

Access:
private
Parameters
string$extMime
Returns
int|string

Definition at line 1098 of file MimeMagic.php.

References $mime, $type, as, getTypesForExtension(), and MEDIATYPE_UNKNOWN.

Referenced by getMediaType(), and improveTypeFromExtension().

MimeMagic::getExtensionsForType (   $mime)

Returns a list of file extensions for a given MIME type as a space separated string or null if the MIME type was unrecognized.

Resolves MIME type aliases.

Parameters
string$mime
Returns
string|null

Definition at line 403 of file MimeMagic.php.

References $mime.

Referenced by isMatchingExtension().

MimeMagic::getIEContentAnalyzer ( )
protected

Get a cached instance of IEContentAnalyzer.

Returns
IEContentAnalyzer

Definition at line 1146 of file MimeMagic.php.

References $mIEAnalyzer.

Referenced by getIEMimeTypes().

MimeMagic::getIEMimeTypes (   $fileName,
  $chunk,
  $proposed 
)

Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content.

Parameters
string$fileNameThe file name (unused at present)
string$chunkThe first 256 bytes of the file
string$proposedThe MIME type proposed by the server
Returns
array

Definition at line 1136 of file MimeMagic.php.

References getIEContentAnalyzer().

MimeMagic::getMediaType (   $path = null,
  $mime = null 
)

Determine the media type code for a file, using its MIME type, name and possibly its contents.

This function relies on the findMediaType(), mapping extensions and MIME types to media types.

Todo:

analyse file if need be

look at multiple extension, separately and together.

Parameters
string$pathFull path to the image file, in case we have to look at the contents (if null, only the MIME type is used to determine the media type code).
string$mimeMIME type. If null it will be guessed using guessMimeType.
Returns
string A value to be used with the MEDIATYPE_xxx constants.

Definition at line 1011 of file MimeMagic.php.

References $e, $mime, $path, $type, findMediaType(), guessMimeType(), MEDIATYPE_AUDIO, MEDIATYPE_MULTIMEDIA, MEDIATYPE_UNKNOWN, and MEDIATYPE_VIDEO.

MimeMagic::getTypesForExtension (   $ext)

Returns a list of MIME types for a given file extension as a space separated string or null if the extension was unrecognized.

Parameters
string$ext
Returns
string|null

Definition at line 429 of file MimeMagic.php.

References $ext.

Referenced by findMediaType(), and guessTypesForExtension().

MimeMagic::guessMimeType (   $file,
  $ext = true 
)

MIME type detection.

This uses detectMimeType to detect the MIME type of the file, but applies additional checks to determine some well known file formats that may be missed or misinterpreted by the default MIME detection (namely XML based formats like XHTML or SVG, as well as ZIP based formats like OPC/ODF files).

Parameters
string$fileThe file to check
string | bool$extThe file extension, or true (default) to extract it from the filename. Set it to false to ignore the extension. DEPRECATED! Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
Returns
string The MIME type of $file

Definition at line 596 of file MimeMagic.php.

References $ext, $mime, detectMimeType(), doGuessMimeType(), and wfDebug().

Referenced by getMediaType().

MimeMagic::guessTypesForExtension (   $ext)

Returns a single MIME type for a given file extension or null if unknown.

This is always the first type from the list returned by getTypesForExtension($ext).

Parameters
string$ext
Returns
string|null

Definition at line 443 of file MimeMagic.php.

References $ext, and getTypesForExtension().

Referenced by detectMimeType(), detectZipType(), and improveTypeFromExtension().

MimeMagic::improveTypeFromExtension (   $mime,
  $ext 
)

Improves a MIME type using the file extension.

Some file formats are very generic, so their MIME type is not very meaningful. A more useful MIME type can be derived by looking at the file extension. Typically, this method would be called on the result of guessMimeType().

Parameters
string$mimeThe MIME type, typically guessed from a file's content.
string$extThe file extension, as taken from the file name
Returns
string The MIME type

Definition at line 543 of file MimeMagic.php.

References $ext, $mime, findMediaType(), guessTypesForExtension(), isMatchingExtension(), isRecognizableExtension(), MEDIATYPE_TEXT, Hooks\run(), and wfDebug().

MimeMagic::isMatchingExtension (   $extension,
  $mime 
)

Tests if the extension matches the given MIME type.

Returns true if a match was found, null if the MIME type is unknown, and false if the MIME type is known but no matches where found.

Parameters
string$extension
string$mime
Returns
bool|null

Definition at line 465 of file MimeMagic.php.

References $ext, $mime, and getExtensionsForType().

Referenced by detectZipType(), and improveTypeFromExtension().

MimeMagic::isPHPImageType (   $mime)

Returns true if the MIME type is known to represent an image format supported by the PHP GD library.

Parameters
string$mime
Returns
bool

Definition at line 486 of file MimeMagic.php.

References $mime.

MimeMagic::isRecognizableExtension (   $extension)

Returns true if the extension represents a type which can be reliably detected from its content.

Use this to determine whether strict content checks should be applied to reject invalid uploads; if we can't identify the type we won't be able to say if it's invalid.

Todo:
Be more accurate when using fancy MIME detector plugins; right now this is the bare minimum getimagesize() list.
Parameters
string$extension
Returns
bool

Definition at line 513 of file MimeMagic.php.

Referenced by detectMimeType(), and improveTypeFromExtension().

Member Data Documentation

MimeMagic MimeMagic::$instance = null
staticprivate

The singleton instance.

Definition at line 180 of file MimeMagic.php.

Config MimeMagic::$mConfig
private

Definition at line 176 of file MimeMagic.php.

string MimeMagic::$mExtraInfo = ''
private

Extra MIME info, set for example by media handling extensions.

Definition at line 173 of file MimeMagic.php.

Referenced by __construct().

string MimeMagic::$mExtraTypes = ''
private

Extra MIME types, set for example by media handling extensions.

Definition at line 169 of file MimeMagic.php.

Referenced by __construct().

array MimeMagic::$mExtToMime = null

Map of file extensions types to MIME types (as a space separated list)

Definition at line 161 of file MimeMagic.php.

IEContentAnalyzer MimeMagic::$mIEAnalyzer
protected

Definition at line 165 of file MimeMagic.php.

Referenced by getIEContentAnalyzer().

array MimeMagic::$mMediaTypes = null
protected

Mapping of media types to arrays of MIME types.

This is used by findMediaType and getMediaType, respectively

Definition at line 149 of file MimeMagic.php.

array MimeMagic::$mMimeToExt = null
protected

Map of MIME types to file extensions (as a space separated list)

Definition at line 157 of file MimeMagic.php.

array MimeMagic::$mMimeTypeAliases = null
protected

Map of MIME type aliases.

Definition at line 153 of file MimeMagic.php.


The documentation for this class was generated from the following file: