MediaWiki
REL1_19
|
Implements functions related to mime types such as detection and mapping to file extension. More...
Public Member Functions | |
__construct () | |
Initializes the MimeMagic object. | |
detectZipType ($header, $tail=null, $ext=false) | |
Detect application-specific file type of a given ZIP file from its header data. | |
findMediaType ($extMime) | |
Returns a media code matching the given mime type or file extension. | |
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. | |
getIEMimeTypes ($fileName, $chunk, $proposed) | |
Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content. | |
getMediaType ($path=null, $mime=null) | |
Determine the media type code for a file, using its mime type, name and possibly its contents. | |
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. | |
guessMimeType ($file, $ext=true) | |
Mime type detection. | |
guessTypesForExtension ($ext) | |
Returns a single mime type for a given file extension or null if unknown. | |
improveTypeFromExtension ($mime, $ext) | |
Improves a mime type using the file extension. | |
isMatchingExtension ($extension, $mime) | |
Tests if the extension matches the given mime type. | |
isPHPImageType ($mime) | |
Returns true if the mime type is known to represent an image format supported by the PHP GD library. | |
isRecognizableExtension ($extension) | |
Returns true if the extension represents a type which can be reliably detected from its content. | |
Static Public Member Functions | |
static & | singleton () |
Get an instance of this class. | |
Public Attributes | |
$mExtToMime = null | |
map of file extensions types to mime types (as a space seprarated list) | |
$mIEAnalyzer | |
IEContentAnalyzer instance. | |
$mMediaTypes = null | |
Mapping of media types to arrays of mime types. | |
$mMimeToExt = null | |
map of mime types to file extensions (as a space seprarated list) | |
$mMimeTypeAliases = null | |
Map of mime type aliases. | |
Protected Member Functions | |
getIEContentAnalyzer () | |
Get a cached instance of IEContentAnalyzer. | |
Private Member Functions | |
detectMimeType ($file, $ext=true) | |
Internal mime type detection. | |
doGuessMimeType ($file, $ext) | |
Guess the mime type from the file contents. | |
Static Private Attributes | |
static | $extensionLoaded = false |
True if the fileinfo extension has been loaded. | |
static | $instance |
The singleton instance. |
Implements functions related to mime types such as detection and mapping to file extension.
Instances of this class are stateles, there only needs to be one global instance of MimeMagic. Please use MimeMagic::singleton() to get that instance.
Definition at line 129 of file MimeMagic.php.
Initializes the MimeMagic object.
This is called by MimeMagic::singleton().
This constructor parses the mime.types and mime.info files and build internal mappings.
Definition at line 165 of file MimeMagic.php.
References $ext, $extensions, $IP, $lines, $mime, $wgLoadFileinfoExtension, $wgMimeInfoFile, $wgMimeTypeFile, wfDebug(), and wfDl().
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 and mime_content_type are tried (in this order), if they are available. If the dections 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'.
$file | String: the file to check |
$ext | Mixed: the 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. |
Definition at line 867 of file MimeMagic.php.
References $ext, $file, $wgMimeDetectorCommand, guessTypesForExtension(), isRecognizableExtension(), wfDebug(), and wfEscapeShellArg().
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'.
$header | String: some reasonably-sized chunk of file header |
$tail | String: the tail of the file |
$ext | Mixed: the 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. |
Definition at line 763 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.
string | $file | |
mixed | $ext |
MWException |
Definition at line 571 of file MimeMagic.php.
References $ext, $f, $file, $mime, $wgXMLMimeTypes, detectZipType(), wfDebug(), wfRestoreWarnings(), and wfSuppressWarnings().
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 funktion relies on the mapping defined by $this->mMediaTypes
Definition at line 1044 of file MimeMagic.php.
References $mime, and getTypesForExtension().
Referenced by getMediaType().
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.
$mime | string |
Definition at line 350 of file MimeMagic.php.
References $mime.
Referenced by isMatchingExtension().
MimeMagic::getIEContentAnalyzer | ( | ) | [protected] |
Get a cached instance of IEContentAnalyzer.
Definition at line 1091 of file MimeMagic.php.
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.
$fileName | String: the file name (unused at present) |
$chunk | String: the first 256 bytes of the file |
$proposed | String: the MIME type proposed by the server |
Definition at line 1081 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.
analyse file if need be
look at multiple extension, separately and together.
$path | String: full 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). |
$mime | String: mime type. If null it will be guessed using guessMimeType. |
Definition at line 967 of file MimeMagic.php.
References $f, $mime, $path, findMediaType(), and guessMimeType().
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.
$ext | string |
Definition at line 376 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 misinterpreter by the default mime detection (namely XML based formats like XHTML or SVG, as well as ZIP based formats like OPC/ODF files).
$file | String: the file to check |
$ext | Mixed: the 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. |
Definition at line 543 of file MimeMagic.php.
References $ext, $file, $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).
$ext | string |
Definition at line 390 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().
Currently, this method does the following:
If $mime is "unknown/unknown" and isRecognizableExtension( $ext ) returns false, return the result of guessTypesForExtension($ext).
If $mime is "application/x-opc+zip" and isMatchingExtension( $ext, $mime ) gives true, return the result of guessTypesForExtension($ext).
$mime | String: the mime type, typically guessed from a file's content. |
$ext | String: the file extension, as taken from the file name |
Definition at line 498 of file MimeMagic.php.
References $ext, $mime, guessTypesForExtension(), isMatchingExtension(), isRecognizableExtension(), 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.
$extension | string |
$mime | string |
Definition at line 413 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.
$mime | string |
Definition at line 434 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.
Definition at line 460 of file MimeMagic.php.
Referenced by detectMimeType(), and improveTypeFromExtension().
static& MimeMagic::singleton | ( | ) | [static] |
Get an instance of this class.
Definition at line 335 of file MimeMagic.php.
References $instance.
Referenced by File\checkExtensionCompatibility(), StreamFile\contentTypeFromPath(), UploadStash\getExtensionForPath(), ForeignAPIFile\getMediaType(), FSFile\getMimeType(), UnregisteredLocalFile\getMimeType(), ForeignAPIFile\getMimeType(), FSFile\getProps(), MediaHandler\getThumbType(), DjVuHandler\getThumbType(), UploadBase\getTitle(), UploadBase\verifyExtension(), and UploadBase\verifyMimeType().
MimeMagic::$extensionLoaded = false [static, private] |
True if the fileinfo extension has been loaded.
Definition at line 159 of file MimeMagic.php.
MimeMagic::$instance [static, private] |
MimeMagic::$mExtToMime = null |
map of file extensions types to mime types (as a space seprarated list)
Definition at line 147 of file MimeMagic.php.
MimeMagic::$mIEAnalyzer |
IEContentAnalyzer instance.
Definition at line 151 of file MimeMagic.php.
MimeMagic::$mMediaTypes = null |
Mapping of media types to arrays of mime types.
This is used by findMediaType and getMediaType, respectively
Definition at line 135 of file MimeMagic.php.
MimeMagic::$mMimeToExt = null |
map of mime types to file extensions (as a space seprarated list)
Definition at line 143 of file MimeMagic.php.
MimeMagic::$mMimeTypeAliases = null |
Map of mime type aliases.
Definition at line 139 of file MimeMagic.php.