MediaWiki master
FSFile.php
Go to the documentation of this file.
1<?php
25
26use Wikimedia\AtEase\AtEase;
27use Wikimedia\Timestamp\ConvertibleTimestamp;
28
34class FSFile {
36 protected $path;
37
39 protected $sha1Base36;
40
46 public function __construct( $path ) {
47 $this->path = $path;
48 }
49
55 public function getPath() {
56 return $this->path;
57 }
58
64 public function exists() {
65 return is_file( $this->path );
66 }
67
73 public function getSize() {
74 AtEase::suppressWarnings();
75 $size = filesize( $this->path );
76 AtEase::restoreWarnings();
77
78 return $size;
79 }
80
86 public function getTimestamp() {
87 AtEase::suppressWarnings();
88 $timestamp = filemtime( $this->path );
89 AtEase::restoreWarnings();
90 if ( $timestamp !== false ) {
91 $timestamp = ConvertibleTimestamp::convert( TS_MW, $timestamp );
92 }
93
94 return $timestamp;
95 }
96
114 public function getProps( $ext = true ) {
115 $info = self::placeholderProps();
116 $info['fileExists'] = $this->exists();
117
118 if ( $info['fileExists'] ) {
119 $info['size'] = $this->getSize(); // bytes
120 $info['sha1'] = $this->getSha1Base36();
121
122 $mime = mime_content_type( $this->path );
123 # MIME type according to file contents
124 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
125 # logical MIME type
126 $info['mime'] = $mime;
127
128 if ( strpos( $mime, '/' ) !== false ) {
129 [ $info['major_mime'], $info['minor_mime'] ] = explode( '/', $mime, 2 );
130 } else {
131 [ $info['major_mime'], $info['minor_mime'] ] = [ $mime, 'unknown' ];
132 }
133 }
134
135 return $info;
136 }
137
152 public static function placeholderProps() {
153 $info = [];
154 $info['fileExists'] = false;
155 $info['size'] = 0;
156 $info['file-mime'] = null;
157 $info['major_mime'] = null;
158 $info['minor_mime'] = null;
159 $info['mime'] = null;
160 $info['sha1'] = '';
161
162 return $info;
163 }
164
175 public function getSha1Base36( $recache = false ) {
176 if ( $this->sha1Base36 !== null && !$recache ) {
177 return $this->sha1Base36;
178 }
179
180 AtEase::suppressWarnings();
181 $this->sha1Base36 = sha1_file( $this->path );
182 AtEase::restoreWarnings();
183
184 if ( $this->sha1Base36 !== false ) {
185 $this->sha1Base36 = \Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
186 }
187
188 return $this->sha1Base36;
189 }
190
197 public static function extensionFromPath( $path ) {
198 $i = strrpos( $path, '.' );
199
200 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
201 }
202
211 public static function getPropsFromPath( $path, $ext = true ) {
212 $fsFile = new self( $path );
213
214 return $fsFile->getProps( $ext );
215 }
216
227 public static function getSha1Base36FromPath( $path ) {
228 $fsFile = new self( $path );
229
230 return $fsFile->getSha1Base36();
231 }
232}
233
235class_alias( FSFile::class, 'FSFile' );
Class representing a non-directory file on the file system.
Definition FSFile.php:34
string $sha1Base36
File SHA-1 in base 36.
Definition FSFile.php:39
string $path
Path to file.
Definition FSFile.php:36
getPath()
Returns the file system path.
Definition FSFile.php:55
getTimestamp()
Get the file's last-modified timestamp.
Definition FSFile.php:86
__construct( $path)
Sets up the file object.
Definition FSFile.php:46
getSha1Base36( $recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:175
getSize()
Get the file size in bytes.
Definition FSFile.php:73
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition FSFile.php:211
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:227
static extensionFromPath( $path)
Get the final file extension from a file system path.
Definition FSFile.php:197
getProps( $ext=true)
Get an associative array containing information about a file with the given storage path.
Definition FSFile.php:114
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition FSFile.php:152
exists()
Checks if the file exists.
Definition FSFile.php:64