MediaWiki
master
Toggle main menu visibility
MWFileProps.php
Go to the documentation of this file.
1
<?php
8
9
namespace
MediaWiki\Utils;
10
11
use MediaWiki\FileRepo\File\File;
12
use MediaWiki\Media\MediaHandler;
13
use Wikimedia\FileBackend\FileBackend;
14
use Wikimedia\FileBackend\FSFile\FSFile;
15
use Wikimedia\Mime\MimeAnalyzer;
16
22
class
MWFileProps
{
24
private
$magic;
25
26
public
function
__construct
( MimeAnalyzer $magic ) {
27
$this->magic = $magic;
28
}
29
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
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
121
class_alias( MWFileProps::class,
'MWFileProps'
);
MEDIATYPE_UNKNOWN
const MEDIATYPE_UNKNOWN
Definition
defines.php:13
$path
$path
Definition
NoLocalSettings.php:14
MediaWiki\Utils\MWFileProps
MimeMagic helper wrapper.
Definition
MWFileProps.php:22
MediaWiki\Utils\MWFileProps\__construct
__construct(MimeAnalyzer $magic)
Definition
MWFileProps.php:26
MediaWiki\Utils\MWFileProps\newPlaceholderProps
newPlaceholderProps()
Empty place holder props for non-existing files.
Definition
MWFileProps.php:109
MediaWiki\Utils\MWFileProps\getPropsFromPath
getPropsFromPath( $path, $ext)
Get an associative array containing information about a file with the given storage path.
Definition
MWFileProps.php:55
Wikimedia\FileBackend\FSFile\FSFile
Class representing a non-directory file on the file system.
Definition
FSFile.php:20
Wikimedia\FileBackend\FileBackend\extensionFromPath
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
Definition
FileBackend.php:1634
includes
Utils
MWFileProps.php
Generated on
for MediaWiki by
1.18.0