MediaWiki master
FSFile.php
Go to the documentation of this file.
1<?php
11
12use Wikimedia\AtEase\AtEase;
13use Wikimedia\Timestamp\ConvertibleTimestamp;
14use Wikimedia\Timestamp\TimestampFormat as TS;
15
21class FSFile {
23 protected $path;
24
26 protected $sha1Base36;
27
33 public function __construct( $path ) {
34 $this->path = $path;
35 }
36
42 public function getPath() {
43 return $this->path;
44 }
45
51 public function exists() {
52 return is_file( $this->path );
53 }
54
60 public function getSize() {
61 AtEase::suppressWarnings();
62 $size = filesize( $this->path );
63 AtEase::restoreWarnings();
64
65 return $size;
66 }
67
73 public function getTimestamp() {
74 AtEase::suppressWarnings();
75 $timestamp = filemtime( $this->path );
76 AtEase::restoreWarnings();
77 if ( $timestamp !== false ) {
78 $timestamp = ConvertibleTimestamp::convert( TS::MW, $timestamp );
79 }
80
81 return $timestamp;
82 }
83
101 public function getProps( $ext = true ) {
102 $info = self::placeholderProps();
103 $info['fileExists'] = $this->exists();
104
105 if ( $info['fileExists'] ) {
106 $info['size'] = $this->getSize(); // bytes
107 $info['sha1'] = $this->getSha1Base36();
108
109 $mime = mime_content_type( $this->path );
110 # MIME type according to file contents
111 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
112 # logical MIME type
113 $info['mime'] = $mime;
114
115 if ( str_contains( $mime, '/' ) ) {
116 [ $info['major_mime'], $info['minor_mime'] ] = explode( '/', $mime, 2 );
117 } else {
118 [ $info['major_mime'], $info['minor_mime'] ] = [ $mime, 'unknown' ];
119 }
120 }
121
122 return $info;
123 }
124
139 public static function placeholderProps() {
140 return [
141 'fileExists' => false,
142 'size' => 0,
143 'file-mime' => null,
144 'major_mime' => null,
145 'minor_mime' => null,
146 'mime' => null,
147 'sha1' => '',
148 ];
149 }
150
161 public function getSha1Base36( $recache = false ) {
162 if ( $this->sha1Base36 !== null && !$recache ) {
163 return $this->sha1Base36;
164 }
165
166 AtEase::suppressWarnings();
167 $this->sha1Base36 = sha1_file( $this->path );
168 AtEase::restoreWarnings();
169
170 if ( $this->sha1Base36 !== false ) {
171 $this->sha1Base36 = \Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
172 }
173
174 return $this->sha1Base36;
175 }
176
183 public static function extensionFromPath( $path ) {
184 $i = strrpos( $path, '.' );
185
186 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
187 }
188
197 public static function getPropsFromPath( $path, $ext = true ) {
198 $fsFile = new self( $path );
199
200 return $fsFile->getProps( $ext );
201 }
202
213 public static function getSha1Base36FromPath( $path ) {
214 $fsFile = new self( $path );
215
216 return $fsFile->getSha1Base36();
217 }
218}
219
221class_alias( FSFile::class, 'FSFile' );
Class representing a non-directory file on the file system.
Definition FSFile.php:21
string $sha1Base36
File SHA-1 in base 36.
Definition FSFile.php:26
string $path
Path to file.
Definition FSFile.php:23
getPath()
Returns the file system path.
Definition FSFile.php:42
getTimestamp()
Get the file's last-modified timestamp.
Definition FSFile.php:73
__construct( $path)
Sets up the file object.
Definition FSFile.php:33
getSha1Base36( $recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:161
getSize()
Get the file size in bytes.
Definition FSFile.php:60
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition FSFile.php:197
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:213
static extensionFromPath( $path)
Get the final file extension from a file system path.
Definition FSFile.php:183
getProps( $ext=true)
Get an associative array containing information about a file with the given storage path.
Definition FSFile.php:101
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition FSFile.php:139
exists()
Checks if the file exists.
Definition FSFile.php:51