MediaWiki fundraising/REL1_35
FSFile.php
Go to the documentation of this file.
1<?php
24use Wikimedia\AtEase\AtEase;
25use Wikimedia\Timestamp\ConvertibleTimestamp;
26
32class FSFile {
34 protected $path;
35
37 protected $sha1Base36;
38
44 public function __construct( $path ) {
45 $this->path = $path;
46 }
47
53 public function getPath() {
54 return $this->path;
55 }
56
62 public function exists() {
63 return is_file( $this->path );
64 }
65
71 public function getSize() {
72 AtEase::suppressWarnings();
73 $size = filesize( $this->path );
74 AtEase::restoreWarnings();
75
76 return $size;
77 }
78
84 public function getTimestamp() {
85 AtEase::suppressWarnings();
86 $timestamp = filemtime( $this->path );
87 AtEase::restoreWarnings();
88 if ( $timestamp !== false ) {
89 $timestamp = ConvertibleTimestamp::convert( TS_MW, $timestamp );
90 }
91
92 return $timestamp;
93 }
94
112 public function getProps( $ext = true ) {
113 $info = self::placeholderProps();
114 $info['fileExists'] = $this->exists();
115
116 if ( $info['fileExists'] ) {
117 $info['size'] = $this->getSize(); // bytes
118 $info['sha1'] = $this->getSha1Base36();
119
120 $mime = mime_content_type( $this->path );
121 # MIME type according to file contents
122 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
123 # logical MIME type
124 $info['mime'] = $mime;
125
126 if ( strpos( $mime, '/' ) !== false ) {
127 list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 );
128 } else {
129 list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ];
130 }
131 }
132
133 return $info;
134 }
135
150 public static function placeholderProps() {
151 $info = [];
152 $info['fileExists'] = false;
153 $info['size'] = 0;
154 $info['file-mime'] = null;
155 $info['major_mime'] = null;
156 $info['minor_mime'] = null;
157 $info['mime'] = null;
158 $info['sha1'] = '';
159
160 return $info;
161 }
162
173 public function getSha1Base36( $recache = false ) {
174 if ( $this->sha1Base36 !== null && !$recache ) {
175 return $this->sha1Base36;
176 }
177
178 AtEase::suppressWarnings();
179 $this->sha1Base36 = sha1_file( $this->path );
180 AtEase::restoreWarnings();
181
182 if ( $this->sha1Base36 !== false ) {
183 $this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
184 }
185
186 return $this->sha1Base36;
187 }
188
195 public static function extensionFromPath( $path ) {
196 $i = strrpos( $path, '.' );
197
198 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
199 }
200
209 public static function getPropsFromPath( $path, $ext = true ) {
210 $fsFile = new self( $path );
211
212 return $fsFile->getProps( $ext );
213 }
214
225 public static function getSha1Base36FromPath( $path ) {
226 $fsFile = new self( $path );
227
228 return $fsFile->getSha1Base36();
229 }
230}
Class representing a non-directory file on the file system.
Definition FSFile.php:32
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:225
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition FSFile.php:209
exists()
Checks if the file exists.
Definition FSFile.php:62
getTimestamp()
Get the file's last-modified timestamp.
Definition FSFile.php:84
getProps( $ext=true)
Get an associative array containing information about a file with the given storage path.
Definition FSFile.php:112
__construct( $path)
Sets up the file object.
Definition FSFile.php:44
string $sha1Base36
File SHA-1 in base 36.
Definition FSFile.php:37
getSize()
Get the file size in bytes.
Definition FSFile.php:71
getSha1Base36( $recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:173
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition FSFile.php:150
string $path
Path to file.
Definition FSFile.php:34
static extensionFromPath( $path)
Get the final file extension from a file system path.
Definition FSFile.php:195
getPath()
Returns the file system path.
Definition FSFile.php:53
$mime
Definition router.php:60
if(!is_readable( $file)) $ext
Definition router.php:48