Merge "Revert "Use display name in category page subheadings if provided""
[lhc/web/wiklou.git] / includes / MimeMagic.php
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 }