MediaWiki master
MWFileProps.php
Go to the documentation of this file.
1<?php
12use Wikimedia\Mime\MimeAnalyzer;
13
21 private $magic;
22
23 public function __construct( MimeAnalyzer $magic ) {
24 $this->magic = $magic;
25 }
26
52 public function getPropsFromPath( $path, $ext ) {
53 $fsFile = new FSFile( $path );
54
55 $info = $this->newPlaceholderProps();
56 $info['fileExists'] = $fsFile->exists();
57 if ( $info['fileExists'] ) {
58 $info['size'] = $fsFile->getSize(); // bytes
59 $info['sha1'] = $fsFile->getSha1Base36();
60
61 # MIME type according to file contents
62 $info['file-mime'] = $this->magic->guessMimeType( $path, false );
63 # Logical MIME type
64 $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;
65
66 # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483).
67 # Unclear if callers of this method expect that.
68 $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
69
70 [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
71 $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
72
73 # Height, width and metadata
74 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above
75 $handler = MediaHandler::getHandler( $info['mime'] );
76 if ( $handler ) {
77 $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
78 if ( $sizeAndMetadata ) {
79 $info = $sizeAndMetadata + $info;
80 }
81 }
82 }
83
84 return $info;
85 }
86
107 public function newPlaceholderProps() {
108 return FSFile::placeholderProps() + [
109 'metadata' => [],
110 'width' => 0,
111 'height' => 0,
112 'bits' => 0,
113 'media_type' => MEDIATYPE_UNKNOWN
114 ];
115 }
116}
const MEDIATYPE_UNKNOWN
Definition defines.php:13
MimeMagic helper wrapper.
getPropsFromPath( $path, $ext)
Get an associative array containing information about a file with the given storage path.
newPlaceholderProps()
Empty place holder props for non-existing files.
__construct(MimeAnalyzer $magic)
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
Class representing a non-directory file on the file system.
Definition FSFile.php:20
Base class for all file backend classes (including multi-write backends).