Completed
Branch master (6ee3f9)
by
unknown
29:15
created

MimeMagic   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A singleton() 0 3 1
B applyDefaultParameters() 0 50 5
1
<?php
2
/**
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @file
19
 */
20
use MediaWiki\MediaWikiServices;
21
use MediaWiki\Logger\LoggerFactory;
22
23
class MimeMagic extends MimeAnalyzer {
24
	/**
25
	 * Get an instance of this class
26
	 * @return MimeMagic
27
	 * @deprecated since 1.28
28
	 */
29
	public static function singleton() {
30
		return MediaWikiServices::getInstance()->getMIMEAnalyzer();
31
	}
32
33
	/**
34
	 * @param array $params
35
	 * @param Config $mainConfig
36
	 * @return array
37
	 */
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
}
89