MediaWiki master
MWFileProps.php
Go to the documentation of this file.
1<?php
24
32 private $magic;
33
37 public function __construct( MimeAnalyzer $magic ) {
38 $this->magic = $magic;
39 }
40
66 public function getPropsFromPath( $path, $ext ) {
67 $fsFile = new FSFile( $path );
68
69 $info = $this->newPlaceholderProps();
70 $info['fileExists'] = $fsFile->exists();
71 if ( $info['fileExists'] ) {
72 $info['size'] = $fsFile->getSize(); // bytes
73 $info['sha1'] = $fsFile->getSha1Base36();
74
75 # MIME type according to file contents
76 $info['file-mime'] = $this->magic->guessMimeType( $path, false );
77 # Logical MIME type
78 $ext = ( $ext === true ) ? FileBackend::extensionFromPath( $path ) : (string)$ext;
79
80 # XXX: MimeAnalyzer::improveTypeFromExtension() may return null (T253483).
81 # Unclear if callers of this method expect that.
82 $info['mime'] = $this->magic->improveTypeFromExtension( $info['file-mime'], $ext );
83
84 [ $info['major_mime'], $info['minor_mime'] ] = File::splitMime( $info['mime'] );
85 $info['media_type'] = $this->magic->getMediaType( $path, $info['mime'] );
86
87 # Height, width and metadata
88 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable See XXX above
89 $handler = MediaHandler::getHandler( $info['mime'] );
90 if ( $handler ) {
91 $sizeAndMetadata = $handler->getSizeAndMetadataWithFallback( $fsFile, $path );
92 if ( $sizeAndMetadata ) {
93 $info = $sizeAndMetadata + $info;
94 }
95 }
96 }
97
98 return $info;
99 }
100
121 public function newPlaceholderProps() {
122 return FSFile::placeholderProps() + [
123 'metadata' => [],
124 'width' => 0,
125 'height' => 0,
126 'bits' => 0,
127 'media_type' => MEDIATYPE_UNKNOWN
128 ];
129 }
130}
Class representing a non-directory file on the file system.
Definition FSFile.php:32
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)
Base class for all file backend classes (including multi-write backends).
const MEDIATYPE_UNKNOWN
Definition defines.php:27