Fix missing commit() flag in postgres savepoint class
[mediawiki.git] / includes / MimeMagic.php
blobc03bce77f0ec7dc48d74bc9de55adf3e64c73362
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.
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.
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
18 * @file
20 use MediaWiki\MediaWikiServices;
21 use MediaWiki\Logger\LoggerFactory;
23 class MimeMagic extends MimeAnalyzer {
24 /**
25 * Get an instance of this class
26 * @return MimeMagic
27 * @deprecated since 1.28
29 public static function singleton() {
30 return MediaWikiServices::getInstance()->getMIMEAnalyzer();
33 /**
34 * @param array $params
35 * @param Config $mainConfig
36 * @return array
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';
52 return;
54 // Some strings by reference for performance - assuming well-behaved hooks
55 Hooks::run(
56 'MimeMagicGuessFromContent',
57 [ $mimeAnalyzer, &$head, &$tail, $file, &$mime ]
60 'extCallback' => function ( $mimeAnalyzer, $ext, &$mime ) {
61 // Media handling extensions can improve the MIME detected
62 Hooks::run( 'MimeMagicImproveFromExtension', [ $mimeAnalyzer, $ext, &$mime ] );
64 'initCallback' => function ( $mimeAnalyzer ) {
65 // Allow media handling extensions adding MIME-types and MIME-info
66 Hooks::run( 'MimeMagicInit', [ $mimeAnalyzer ] );
68 'logger' => $logger
71 if ( $params['infoFile'] === 'includes/mime.info' ) {
72 $params['infoFile'] = __DIR__ . "/libs/mime/mime.info";
75 if ( $params['typeFile'] === 'includes/mime.types' ) {
76 $params['typeFile'] = __DIR__ . "/libs/mime/mime.types";
79 $detectorCmd = $mainConfig->get( 'MimeDetectorCommand' );
80 if ( $detectorCmd ) {
81 $params['detectCallback'] = function ( $file ) use ( $detectorCmd ) {
82 return wfShellExec( "$detectorCmd " . wfEscapeShellArg( $file ) );
86 return $params;