Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MWFileProps
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPropsFromPath
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
6
 newPlaceholderProps
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * MimeMagic helper functions for detecting and dealing with MIME types.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Utils;
10
11use MediaWiki\FileRepo\File\File;
12use MediaWiki\Media\MediaHandler;
13use Wikimedia\FileBackend\FileBackend;
14use Wikimedia\FileBackend\FSFile\FSFile;
15use Wikimedia\Mime\MimeAnalyzer;
16
17/**
18 * MimeMagic helper wrapper
19 *
20 * @since 1.28
21 */
22class MWFileProps {
23    /** @var MimeAnalyzer */
24    private $magic;
25
26    public function __construct( MimeAnalyzer $magic ) {
27        $this->magic = $magic;
28    }
29
30    /**
31     * Get an associative array containing information about
32     * a file with the given storage path.
33     *
34     * Resulting array fields include:
35     *   - fileExists
36     *   - size (filesize in bytes)
37     *   - mime (as major/minor)
38     *   - media_type (value to be used with the MEDIATYPE_xxx constants)
39     *   - metadata (handler specific)
40     *   - sha1 (in base 36)
41     *   - width
42     *   - height
43     *   - bits (bitrate)
44     *   - file-mime
45     *   - major_mime
46     *   - minor_mime
47     *
48     * @param string $path Filesystem path to a file
49     * @param string|bool|null $ext The file extension, or true to extract it from the filename.
50     *  Set it to false to ignore the extension. Might be null in case the file is going to be
51     *  stashed.
52     * @return array
53     * @since 1.28
54     */
55    public function getPropsFromPath( $path, $ext ) {
56        $fsFile = new FSFile( $path );
57
58        $info = $this->newPlaceholderProps();
59        $info['fileExists'] = $fsFile->exists();
60        if ( $info['fileExists'] ) {
61            $info['size'] = $fsFile->getSize(); // bytes
62            $info['sha1'] = $fsFile->getSha1Base36();
63
64            # MIME type according to file contents
65            $info['file-mime'] = $this->magic->guessMimeType( $path, false );
66            # Logical MIME type
67            $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;
68
69            # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483, T413926).
70            # Unclear if callers of this method expect that.
71            $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
72
73            [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
74            $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
75
76            # Height, width and metadata
77            $handler = $info['mime'] !== null ? MediaHandler::getHandler( $info['mime'] ) : null;
78            if ( $handler ) {
79                $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
80                if ( $sizeAndMetadata ) {
81                    $info = $sizeAndMetadata + $info;
82                }
83            }
84        }
85
86        return $info;
87    }
88
89    /**
90     * Empty place holder props for non-existing files
91     *
92     * Resulting array fields include:
93     *   - fileExists
94     *   - size (filesize in bytes)
95     *   - mime (as major/minor)
96     *   - media_type (value to be used with the MEDIATYPE_xxx constants)
97     *   - metadata (handler specific)
98     *   - sha1 (in base 36)
99     *   - width
100     *   - height
101     *   - bits (bitrate)
102     *   - file-mime
103     *   - major_mime
104     *   - minor_mime
105     *
106     * @return array
107     * @since 1.28
108     */
109    public function newPlaceholderProps() {
110        return FSFile::placeholderProps() + [
111            'metadata' => [],
112            'width' => 0,
113            'height' => 0,
114            'bits' => 0,
115            'media_type' => MEDIATYPE_UNKNOWN
116        ];
117    }
118}
119
120/** @deprecated class alias since 1.46 */
121class_alias( MWFileProps::class, 'MWFileProps' );