MediaWiki REL1_31
FSFile.php
Go to the documentation of this file.
1<?php
29class FSFile {
31 protected $path;
32
34 protected $sha1Base36;
35
41 public function __construct( $path ) {
42 $this->path = $path;
43 }
44
50 public function getPath() {
51 return $this->path;
52 }
53
59 public function exists() {
60 return is_file( $this->path );
61 }
62
68 public function getSize() {
69 return filesize( $this->path );
70 }
71
77 public function getTimestamp() {
78 Wikimedia\suppressWarnings();
79 $timestamp = filemtime( $this->path );
80 Wikimedia\restoreWarnings();
81 if ( $timestamp !== false ) {
82 $timestamp = wfTimestamp( TS_MW, $timestamp );
83 }
84
85 return $timestamp;
86 }
87
105 public function getProps( $ext = true ) {
106 $info = self::placeholderProps();
107 $info['fileExists'] = $this->exists();
108
109 if ( $info['fileExists'] ) {
110 $info['size'] = $this->getSize(); // bytes
111 $info['sha1'] = $this->getSha1Base36();
112
113 $mime = mime_content_type( $this->path );
114 # MIME type according to file contents
115 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
116 # logical MIME type
117 $info['mime'] = $mime;
118
119 if ( strpos( $mime, '/' ) !== false ) {
120 list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 );
121 } else {
122 list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ];
123 }
124 }
125
126 return $info;
127 }
128
143 public static function placeholderProps() {
144 $info = [];
145 $info['fileExists'] = false;
146 $info['size'] = 0;
147 $info['file-mime'] = null;
148 $info['major_mime'] = null;
149 $info['minor_mime'] = null;
150 $info['mime'] = null;
151 $info['sha1'] = '';
152
153 return $info;
154 }
155
166 public function getSha1Base36( $recache = false ) {
167 if ( $this->sha1Base36 !== null && !$recache ) {
168 return $this->sha1Base36;
169 }
170
171 Wikimedia\suppressWarnings();
172 $this->sha1Base36 = sha1_file( $this->path );
173 Wikimedia\restoreWarnings();
174
175 if ( $this->sha1Base36 !== false ) {
176 $this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
177 }
178
179 return $this->sha1Base36;
180 }
181
188 public static function extensionFromPath( $path ) {
189 $i = strrpos( $path, '.' );
190
191 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
192 }
193
202 public static function getPropsFromPath( $path, $ext = true ) {
203 $fsFile = new self( $path );
204
205 return $fsFile->getProps( $ext );
206 }
207
218 public static function getSha1Base36FromPath( $path ) {
219 $fsFile = new self( $path );
220
221 return $fsFile->getSha1Base36();
222 }
223}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Class representing a non-directory file on the file system.
Definition FSFile.php:29
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:218
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition FSFile.php:202
exists()
Checks if the file exists.
Definition FSFile.php:59
getTimestamp()
Get the file's last-modified timestamp.
Definition FSFile.php:77
getProps( $ext=true)
Get an associative array containing information about a file with the given storage path.
Definition FSFile.php:105
__construct( $path)
Sets up the file object.
Definition FSFile.php:41
string $sha1Base36
File SHA-1 in base 36.
Definition FSFile.php:34
getSize()
Get the file size in bytes.
Definition FSFile.php:68
getSha1Base36( $recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition FSFile.php:166
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition FSFile.php:143
string $path
Path to file.
Definition FSFile.php:31
static extensionFromPath( $path)
Get the final file extension from a file system path.
Definition FSFile.php:188
getPath()
Returns the file system path.
Definition FSFile.php:50
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
if( $ext=='php'|| $ext=='php5') $mime
Definition router.php:59
if(!is_readable( $file)) $ext
Definition router.php:55