MediaWiki REL1_28
MimeMagic.php
Go to the documentation of this file.
1<?php
22
23class MimeMagic extends MimeAnalyzer {
29 public static function singleton() {
30 return MediaWikiServices::getInstance()->getMIMEAnalyzer();
31 }
32
38 public static function applyDefaultParameters( array $params, Config $mainConfig ) {
39 $logger = LoggerFactory::getInstance( 'Mime' );
40 $params += [
41 'typeFile' => $mainConfig->get( 'MimeTypeFile' ),
42 'infoFile' => $mainConfig->get( 'MimeInfoFile' ),
43 'xmlTypes' => $mainConfig->get( 'XMLMimeTypes' ),
44 'guessCallback' =>
45 function ( $mimeAnalyzer, &$head, &$tail, $file, &$mime ) use ( $logger ) {
46 // Also test DjVu
47 $deja = new DjVuImage( $file );
48 if ( $deja->isValid() ) {
49 $logger->info( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
50 $mime = 'image/vnd.djvu';
51
52 return;
53 }
54 // Some strings by reference for performance - assuming well-behaved hooks
55 Hooks::run(
56 'MimeMagicGuessFromContent',
57 [ $mimeAnalyzer, &$head, &$tail, $file, &$mime ]
58 );
59 },
60 'extCallback' => function ( $mimeAnalyzer, $ext, &$mime ) {
61 // Media handling extensions can improve the MIME detected
62 Hooks::run( 'MimeMagicImproveFromExtension', [ $mimeAnalyzer, $ext, &$mime ] );
63 },
64 'initCallback' => function ( $mimeAnalyzer ) {
65 // Allow media handling extensions adding MIME-types and MIME-info
66 Hooks::run( 'MimeMagicInit', [ $mimeAnalyzer ] );
67 },
68 'logger' => $logger
69 ];
70
71 if ( $params['infoFile'] === 'includes/mime.info' ) {
72 $params['infoFile'] = __DIR__ . "/libs/mime/mime.info";
73 }
74
75 if ( $params['typeFile'] === 'includes/mime.types' ) {
76 $params['typeFile'] = __DIR__ . "/libs/mime/mime.types";
77 }
78
79 $detectorCmd = $mainConfig->get( 'MimeDetectorCommand' );
80 if ( $detectorCmd ) {
81 $params['detectCallback'] = function ( $file ) use ( $detectorCmd ) {
82 return wfShellExec( "$detectorCmd " . wfEscapeShellArg( $file ) );
83 };
84 }
85
86 return $params;
87 }
88}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
wfEscapeShellArg()
Version of escapeshellarg() that works better on Windows.
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Definition DjVuImage.php:36
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static singleton()
Get an instance of this class.
Definition MimeMagic.php:29
static applyDefaultParameters(array $params, Config $mainConfig)
Definition MimeMagic.php:38
the array() calling protocol came about after MediaWiki 1.4rc1.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
if( $ext=='php'|| $ext=='php5') $mime
Definition router.php:65
$params