MediaWiki
1.34.0
|
Implements functions related to MIME types such as detection and mapping to file extension. More...
Public Member Functions | |
__construct (array $params) | |
addExtraInfo ( $info) | |
Adds to the list mapping MIME to media type. More... | |
addExtraTypes ( $types) | |
Adds to the list mapping MIME to file extensions. More... | |
detectMicrosoftBinaryType ( $handle) | |
Detect the type of a Microsoft Compound Binary a.k.a. 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... | |
getMediaTypes () | |
Returns an array of media types (MEDIATYPE_xxx constants) 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... | |
setLogger (LoggerInterface $logger) | |
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... | |
loadFiles () | |
Protected Attributes | |
callable | $detectCallback |
callable | $extCallback |
callable | $guessCallback |
IEContentAnalyzer | $IEAnalyzer |
string | $infoFile |
callable | $initCallback |
array | $mediaTypes = null |
Mapping of media types to arrays of MIME types. More... | |
array | $mimetoExt = null |
Map of MIME types to file extensions (as a space separated list) More... | |
array | $mimeTypeAliases = null |
Map of MIME type aliases. More... | |
string | $typeFile |
string | $xmlTypes |
Static Protected Attributes | |
static | $wellKnownInfo = |
Defines a set of well known MIME info entries This is used as a fallback to mime.info files. More... | |
static | $wellKnownTypes = |
Defines a set of well known MIME types This is used as a fallback to mime.types files. 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 | |
string | $extraInfo = '' |
Extra MIME info, set for example by media handling extensions. More... | |
string | $extraTypes = '' |
Extra MIME types, set for example by media handling extensions. More... | |
LoggerInterface | $logger |
Implements functions related to MIME types such as detection and mapping to file extension.
Definition at line 31 of file MimeAnalyzer.php.
MimeAnalyzer::__construct | ( | array | $params | ) |
array | $params | Configuration map, includes:
|
Definition at line 195 of file MimeAnalyzer.php.
References loadFiles().
MimeAnalyzer::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.
string | $info |
Definition at line 390 of file MimeAnalyzer.php.
MimeAnalyzer::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.
string | $types |
Definition at line 380 of file MimeAnalyzer.php.
MimeAnalyzer::detectMicrosoftBinaryType | ( | $handle | ) |
Detect the type of a Microsoft Compound Binary a.k.a.
OLE file. These are old style pre-ODF files such as .doc and .xls
resource | $handle | An opened seekable file handle |
Definition at line 981 of file MimeAnalyzer.php.
References MSCompoundFileReader\readHandle().
Referenced by doGuessMimeType().
|
private |
Internal MIME type detection.
Detection is done using the fileinfo extension if it is available. It can be overriden by callback, which could use an external program, for example. 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'.
string | $file | The file to check |
string | bool | $ext | 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 1011 of file MimeAnalyzer.php.
References $detectCallback, $ext, $file, guessTypesForExtension(), and isRecognizableExtension().
Referenced by guessMimeType().
MimeAnalyzer::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'.
string | $header | Some reasonably-sized chunk of file header |
string | null | $tail | The tail of the file |
string | bool | $ext | 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 873 of file MimeAnalyzer.php.
References $ext, $header, $matches, guessTypesForExtension(), and isMatchingExtension().
Referenced by doGuessMimeType().
|
private |
Guess the MIME type from the file contents.
string | $file | |
mixed | $ext |
UnexpectedValueException |
Definition at line 635 of file MimeAnalyzer.php.
References $chars, $ext, $file, $guessCallback, $xmlTypes, detectMicrosoftBinaryType(), and detectZipType().
Referenced by guessMimeType().
MimeAnalyzer::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
string | $extMime |
Definition at line 1169 of file MimeAnalyzer.php.
References $type, getTypesForExtension(), and MEDIATYPE_UNKNOWN.
Referenced by getMediaType(), and improveTypeFromExtension().
MimeAnalyzer::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.
string | $mime |
Definition at line 402 of file MimeAnalyzer.php.
Referenced by isMatchingExtension().
|
protected |
Get a cached instance of IEContentAnalyzer.
Definition at line 1226 of file MimeAnalyzer.php.
References $IEAnalyzer.
Referenced by getIEMimeTypes().
MimeAnalyzer::getIEMimeTypes | ( | $fileName, | |
$chunk, | |||
$proposed | |||
) |
Get the MIME types that various versions of Internet Explorer would detect from a chunk of the content.
string | $fileName | The file name (unused at present) |
string | $chunk | The first 256 bytes of the file |
string | $proposed | The MIME type proposed by the server |
Definition at line 1216 of file MimeAnalyzer.php.
References getIEContentAnalyzer().
MimeAnalyzer::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.
string | null | $path | 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). |
string | null | $mime | MIME type. If null it will be guessed using guessMimeType. |
Definition at line 1080 of file MimeAnalyzer.php.
References $path, $type, findMediaType(), guessMimeType(), MEDIATYPE_AUDIO, MEDIATYPE_MULTIMEDIA, MEDIATYPE_UNKNOWN, and MEDIATYPE_VIDEO.
MimeAnalyzer::getMediaTypes | ( | ) |
Returns an array of media types (MEDIATYPE_xxx constants)
Definition at line 1203 of file MimeAnalyzer.php.
MimeAnalyzer::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.
string | $ext |
Definition at line 428 of file MimeAnalyzer.php.
References $ext.
Referenced by findMediaType(), and guessTypesForExtension().
MimeAnalyzer::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).
string | $file | The file to check |
string | bool | $ext | 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 602 of file MimeAnalyzer.php.
References $ext, $file, detectMimeType(), and doGuessMimeType().
Referenced by getMediaType().
MimeAnalyzer::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).
string | $ext |
Definition at line 442 of file MimeAnalyzer.php.
References $ext, and getTypesForExtension().
Referenced by detectMimeType(), detectZipType(), and improveTypeFromExtension().
MimeAnalyzer::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().
string | $mime | The MIME type, typically guessed from a file's content. |
string | $ext | The file extension, as taken from the file name |
Definition at line 545 of file MimeAnalyzer.php.
References $ext, $extCallback, findMediaType(), guessTypesForExtension(), isMatchingExtension(), isRecognizableExtension(), and MEDIATYPE_TEXT.
MimeAnalyzer::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.
string | $extension | |
string | $mime |
Definition at line 464 of file MimeAnalyzer.php.
References $ext, and getExtensionsForType().
Referenced by detectZipType(), and improveTypeFromExtension().
MimeAnalyzer::isPHPImageType | ( | $mime | ) |
Returns true if the MIME type is known to represent an image format supported by the PHP GD library.
string | $mime |
Definition at line 485 of file MimeAnalyzer.php.
MimeAnalyzer::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.
string | $extension |
Definition at line 512 of file MimeAnalyzer.php.
Referenced by detectMimeType(), and improveTypeFromExtension().
|
protected |
Definition at line 208 of file MimeAnalyzer.php.
References $ext, $extraInfo, $extraTypes, $infoFile, $lines, $s, $typeFile, $wellKnownInfo, $wellKnownTypes, and MEDIATYPE_UNKNOWN.
Referenced by __construct().
MimeAnalyzer::setLogger | ( | LoggerInterface | $logger | ) |
Definition at line 370 of file MimeAnalyzer.php.
References $logger.
|
protected |
Definition at line 41 of file MimeAnalyzer.php.
Referenced by detectMimeType().
|
protected |
Definition at line 45 of file MimeAnalyzer.php.
Referenced by improveTypeFromExtension().
|
private |
Extra MIME info, set for example by media handling extensions.
Definition at line 62 of file MimeAnalyzer.php.
Referenced by loadFiles().
|
private |
Extra MIME types, set for example by media handling extensions.
Definition at line 60 of file MimeAnalyzer.php.
Referenced by loadFiles().
|
protected |
Definition at line 43 of file MimeAnalyzer.php.
Referenced by doGuessMimeType().
|
protected |
Definition at line 57 of file MimeAnalyzer.php.
Referenced by getIEContentAnalyzer().
|
protected |
Definition at line 35 of file MimeAnalyzer.php.
Referenced by loadFiles().
|
protected |
Definition at line 39 of file MimeAnalyzer.php.
|
private |
Definition at line 65 of file MimeAnalyzer.php.
Referenced by setLogger().
|
protected |
Mapping of media types to arrays of MIME types.
Definition at line 47 of file MimeAnalyzer.php.
array MimeAnalyzer::$mExtToMime = null |
Map of file extensions types to MIME types (as a space separated list)
Definition at line 54 of file MimeAnalyzer.php.
|
protected |
Map of MIME types to file extensions (as a space separated list)
Definition at line 51 of file MimeAnalyzer.php.
|
protected |
Map of MIME type aliases.
Definition at line 49 of file MimeAnalyzer.php.
|
protected |
Definition at line 33 of file MimeAnalyzer.php.
Referenced by loadFiles().
|
staticprotected |
Defines a set of well known MIME info entries This is used as a fallback to mime.info files.
An extensive list of well known MIME types is provided by the file mime.info in the includes directory.
Definition at line 138 of file MimeAnalyzer.php.
Referenced by loadFiles().
|
staticprotected |
Defines a set of well known MIME types This is used as a fallback to mime.types files.
An extensive list of well known MIME types is provided by the file mime.types in the includes directory.
This list concatenated with mime.types is used to create a MIME <-> ext map. Each line contains a MIME type followed by a space separated list of extensions. If multiple extensions for a single MIME type exist or if multiple MIME types exist for a single extension then in most cases MediaWiki assumes that the first extension following the MIME type is the canonical extension, and the first time a MIME type appears for a certain extension is considered the canonical MIME type.
(Note that appending the type file list to the end of self::$wellKnownTypes sucks because you can't redefine canonical types. This could be fixed by appending self::$wellKnownTypes behind type file list, but who knows what will break? In practice this probably isn't a problem anyway – Bryan)
Definition at line 86 of file MimeAnalyzer.php.
Referenced by loadFiles().
|
protected |
Definition at line 37 of file MimeAnalyzer.php.
Referenced by doGuessMimeType().