BlueSpice MediaWiki master
 All Classes Namespaces Files Functions Variables Groups Pages
MimeAnalyzer Class Reference
Inheritance diagram for MimeAnalyzer:
Collaboration diagram for MimeAnalyzer:

Public Member Functions

 __construct (array $params)
 
 setLogger (LoggerInterface $logger)
 
 addExtraTypes ($types)
 
 addExtraInfo ($info)
 
 getExtensionsForType ($mime)
 
 getTypesForExtension ($ext)
 
 guessTypesForExtension ($ext)
 
 isMatchingExtension ($extension, $mime)
 
 isPHPImageType ($mime)
 
 isRecognizableExtension ($extension)
 
 improveTypeFromExtension ($mime, $ext)
 
 guessMimeType ($file, $ext=true)
 
 detectZipType ($header, $tail=null, $ext=false)
 
 getMediaType ($path=null, $mime=null)
 
 findMediaType ($extMime)
 
 getMediaTypes ()
 
 getIEMimeTypes ($fileName, $chunk, $proposed)
 

Public Attributes

 $mExtToMime = null
 

Protected Member Functions

 loadFiles ()
 
 getIEContentAnalyzer ()
 

Protected Attributes

 $typeFile
 
 $infoFile
 
 $xmlTypes
 
 $initCallback
 
 $detectCallback
 
 $guessCallback
 
 $extCallback
 
 $mediaTypes = null
 
 $mimeTypeAliases = null
 
 $mimetoExt = null
 
 $IEAnalyzer
 

Static Protected Attributes

static $wellKnownTypes
 
static $wellKnownInfo
 

Detailed Description

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

Since
1.28

Constructor & Destructor Documentation

MimeAnalyzer::__construct ( array  $params)
Parameters
array$paramsConfiguration map, includes:
  • typeFile: path to file with the list of known MIME types
  • infoFile: path to file with the MIME type info
  • xmlTypes: map of root element names to XML MIME types
  • initCallback: initialization callback that is passed this object [optional]
  • detectCallback: alternative to finfo that returns the mime type for a file. For example, the callback can return the output of "file -bi". [optional]
  • guessCallback: callback to improve the guessed MIME type using the file data. This is intended for fixing mistakes in fileinfo or "detectCallback". [optional]
  • extCallback: callback to improve the guessed MIME type using the extension. [optional]
  • logger: PSR-3 logger [optional]
Note
Constructing these instances is expensive due to file reads. A service or singleton pattern should be used to avoid creating instances again and again.

Member Function Documentation

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.

Parameters
string$info
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.

Parameters
string$types
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'.

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

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

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 private

Parameters
string$extMime
Returns
int|string
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.

Parameters
string$mime
Returns
string|null
MimeAnalyzer::getIEContentAnalyzer ( )
protected

Get a cached instance of IEContentAnalyzer

Returns
IEContentAnalyzer
MimeAnalyzer::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
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.

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.
MimeAnalyzer::getMediaTypes ( )

Returns an array of media types (MEDIATYPE_xxx constants)

Returns
array
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.

Parameters
string$ext
Returns
string|null
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).

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
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).

Parameters
string$ext
Returns
string|null
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().

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
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.

Parameters
string$extension
string$mime
Returns
bool|null
MimeAnalyzer::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
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.

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

— load mime.types —

— load mime.info —

MimeAnalyzer::setLogger ( LoggerInterface  $logger)

Sets a logger instance on the object.

Parameters
LoggerInterface$logger
Returns
void

Implements Psr\Log\LoggerAwareInterface.

Member Data Documentation

MimeAnalyzer::$wellKnownInfo
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.

MimeAnalyzer::$wellKnownTypes
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)


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