Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
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
7
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
5
 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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23/**
24 * MimeMagic helper wrapper
25 *
26 * @since 1.28
27 */
28class MWFileProps {
29    /** @var MimeAnalyzer */
30    private $magic;
31
32    /**
33     * @param MimeAnalyzer $magic
34     */
35    public function __construct( MimeAnalyzer $magic ) {
36        $this->magic = $magic;
37    }
38
39    /**
40     * Get an associative array containing information about
41     * a file with the given storage path.
42     *
43     * Resulting array fields include:
44     *   - fileExists
45     *   - size (filesize in bytes)
46     *   - mime (as major/minor)
47     *   - media_type (value to be used with the MEDIATYPE_xxx constants)
48     *   - metadata (handler specific)
49     *   - sha1 (in base 36)
50     *   - width
51     *   - height
52     *   - bits (bitrate)
53     *   - file-mime
54     *   - major_mime
55     *   - minor_mime
56     *
57     * @param string $path Filesystem path to a file
58     * @param string|bool|null $ext The file extension, or true to extract it from the filename.
59     *  Set it to false to ignore the extension. Might be null in case the file is going to be
60     *  stashed.
61     * @return array
62     * @since 1.28
63     */
64    public function getPropsFromPath( $path, $ext ) {
65        $fsFile = new FSFile( $path );
66
67        $info = $this->newPlaceholderProps();
68        $info['fileExists'] = $fsFile->exists();
69        if ( $info['fileExists'] ) {
70            $info['size'] = $fsFile->getSize(); // bytes
71            $info['sha1'] = $fsFile->getSha1Base36();
72
73            # MIME type according to file contents
74            $info['file-mime'] = $this->magic->guessMimeType( $path, false );
75            # Logical MIME type
76            $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;
77
78            # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483).
79            # Unclear if callers of this method expect that.
80            $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
81
82            [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
83            $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
84
85            # Height, width and metadata
86            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above
87            $handler = MediaHandler::getHandler( $info['mime'] );
88            if ( $handler ) {
89                $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
90                if ( $sizeAndMetadata ) {
91                    $info = $sizeAndMetadata + $info;
92                }
93            }
94        }
95
96        return $info;
97    }
98
99    /**
100     * Empty place holder props for non-existing files
101     *
102     * Resulting array fields include:
103     *   - fileExists
104     *   - size (filesize in bytes)
105     *   - mime (as major/minor)
106     *   - media_type (value to be used with the MEDIATYPE_xxx constants)
107     *   - metadata (handler specific)
108     *   - sha1 (in base 36)
109     *   - width
110     *   - height
111     *   - bits (bitrate)
112     *   - file-mime
113     *   - major_mime
114     *   - minor_mime
115     *
116     * @return array
117     * @since 1.28
118     */
119    public function newPlaceholderProps() {
120        return FSFile::placeholderProps() + [
121            'metadata' => [],
122            'width' => 0,
123            'height' => 0,
124            'bits' => 0,
125            'media_type' => MEDIATYPE_UNKNOWN
126        ];
127    }
128}