MediaWiki master
FSFile.php
Go to the documentation of this file.
1<?php
11
12use Wikimedia\Timestamp\ConvertibleTimestamp;
13use Wikimedia\Timestamp\TimestampFormat as TS;
14
20class FSFile {
22 protected $path;
23
25 protected $sha1Base36;
26
32 public function __construct( $path ) {
33 $this->path = $path;
34 }
35
41 public function getPath() {
42 return $this->path;
43 }
44
50 public function exists() {
51 return is_file( $this->path );
52 }
53
59 public function getSize() {
60 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
61 return @filesize( $this->path );
62 }
63
69 public function getTimestamp() {
70 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
71 $timestamp = @filemtime( $this->path );
72 if ( $timestamp !== false ) {
73 $timestamp = ConvertibleTimestamp::convert( TS::MW, $timestamp );
74 }
75
76 return $timestamp;
77 }
78
96 public function getProps( $ext = true ) {
97 $info = self::placeholderProps();
98 $info['fileExists'] = $this->exists();
99
100 if ( $info['fileExists'] ) {
101 $info['size'] = $this->getSize(); // bytes
102 $info['sha1'] = $this->getSha1Base36();
103
104 $mime = mime_content_type( $this->path );
105 # MIME type according to file contents
106 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
107 # logical MIME type
108 $info['mime'] = $mime;
109
110 if ( str_contains( $mime, '/' ) ) {
111 [ $info['major_mime'], $info['minor_mime'] ] = explode( '/', $mime, 2 );
112 } else {
113 [ $info['major_mime'], $info['minor_mime'] ] = [ $mime, 'unknown' ];
114 }
115 }
116
117 return $info;
118 }
119
134 public static function placeholderProps() {
135 return [
136 'fileExists' => false,
137 'size' => 0,
138 'file-mime' => null,
139 'major_mime' => null,
140 'minor_mime' => null,
141 'mime' => null,
142 'sha1' => '',
143 ];
144 }
145
156 public function getSha1Base36( $recache = false ) {
157 if ( $this->sha1Base36 !== null && !$recache ) {
158 return $this->sha1Base36;
159 }
160
161 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
162 $this->sha1Base36 = @sha1_file( $this->path );
163
164 if ( $this->sha1Base36 !== false ) {
165 $this->sha1Base36 = \Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
166 }
167
168 return $this->sha1Base36;
169 }
170
177 public static function extensionFromPath( $path ) {
178 $i = strrpos( $path, '.' );
179
180 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
181 }
182
191 public static function getPropsFromPath( $path, $ext = true ) {
192 $fsFile = new self( $path );
193
194 return $fsFile->getProps( $ext );
195 }
196
207 public static function getSha1Base36FromPath( $path ) {
208 $fsFile = new self( $path );
209
210 return $fsFile->getSha1Base36();
211 }
212}
213
215class_alias( FSFile::class, 'FSFile' );
Class representing a non-directory file on the file system.
Definition FSFile.php:20
string $sha1Base36
File SHA-1 in base 36.
Definition FSFile.php:25
string $path
Path to file.
Definition FSFile.php:22
getPath()
Returns the file system path.
Definition FSFile.php:41
getTimestamp()
Get the file's last-modified timestamp.
Definition FSFile.php:69
__construct( $path)
Sets up the file object.
Definition FSFile.php:32
getSha1Base36( $recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:156
getSize()
Get the file size in bytes.
Definition FSFile.php:59
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition FSFile.php:191
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:207
static extensionFromPath( $path)
Get the final file extension from a file system path.
Definition FSFile.php:177
getProps( $ext=true)
Get an associative array containing information about a file with the given storage path.
Definition FSFile.php:96
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition FSFile.php:134
exists()
Checks if the file exists.
Definition FSFile.php:50