Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / utils / MWFileProps.php
blobe60b9ab76213c6ef63d7d44a17c1e2db23e56d00
1 <?php
2 /**
3 * MimeMagic helper functions for detecting and dealing with MIME types.
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.
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.
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
20 * @file
23 /**
24 * MimeMagic helper wrapper
26 * @since 1.28
28 class MWFileProps {
29 /** @var MimeMagic */
30 private $magic;
32 /**
33 * @param MimeMagic $magic
35 public function __construct( MimeMagic $magic ) {
36 $this->magic = $magic;
39 /**
40 * Get an associative array containing information about
41 * a file with the given storage path.
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
57 * @param string $path Filesystem path to a file
58 * @param string|bool $ext The file extension, or true to extract it from the filename.
59 * Set it to false to ignore the extension.
60 * @return array
61 * @since 1.28
63 public function getPropsFromPath( $path, $ext ) {
64 $fsFile = new FSFile( $path );
66 $info = $this->newPlaceholderProps();
67 $info['fileExists'] = $fsFile->exists();
68 if ( $info['fileExists'] ) {
69 $info['size'] = $fsFile->getSize(); // bytes
70 $info['sha1'] = $fsFile->getSha1Base36();
72 # MIME type according to file contents
73 $info['file-mime'] = $this->magic->guessMimeType( $path, false );
74 # Logical MIME type
75 $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : $ext;
76 $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
78 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
79 $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
81 # Height, width and metadata
82 $handler = MediaHandler::getHandler( $info['mime'] );
83 if ( $handler ) {
84 $info['metadata'] = $handler->getMetadata( $fsFile, $path );
85 /** @noinspection PhpMethodParametersCountMismatchInspection */
86 $gis = $handler->getImageSize( $fsFile, $path, $info['metadata'] );
87 if ( is_array( $gis ) ) {
88 $info = $this->extractImageSizeInfo( $gis ) + $info;
93 return $info;
96 /**
97 * Exract image size information
99 * @param array $gis
100 * @return array
102 private function extractImageSizeInfo( array $gis ) {
103 $info = [];
104 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
105 $info['width'] = $gis[0];
106 $info['height'] = $gis[1];
107 if ( isset( $gis['bits'] ) ) {
108 $info['bits'] = $gis['bits'];
109 } else {
110 $info['bits'] = 0;
113 return $info;
117 * Empty place holder props for non-existing files
119 * Resulting array fields include:
120 * - fileExists
121 * - size (filesize in bytes)
122 * - mime (as major/minor)
123 * - media_type (value to be used with the MEDIATYPE_xxx constants)
124 * - metadata (handler specific)
125 * - sha1 (in base 36)
126 * - width
127 * - height
128 * - bits (bitrate)
129 * - file-mime
130 * - major_mime
131 * - minor_mime
133 * @return array
134 * @since 1.28
136 public function newPlaceholderProps() {
137 return FSFile::placeholderProps() + [
138 'metadata' => '',
139 'width' => 0,
140 'height' => 0,
141 'bits' => 0,
142 'media_type' => MEDIATYPE_UNKNOWN