8 * Class representing a non-directory file on the file system
10 * @ingroup FileBackend
13 protected $path; // path to file
16 * Sets up the file object
18 * @param String $path Path to temporary file on local disk
20 public function __construct( $path ) {
21 if ( FileBackend
::isStoragePath( $path ) ) {
22 throw new MWException( __METHOD__
. " given storage path `$path`." );
28 * Returns the file system path
32 public function getPath() {
37 * Checks if the file exists
41 public function exists() {
42 return is_file( $this->path
);
46 * Get the file size in bytes
50 public function getSize() {
51 return filesize( $this->path
);
55 * Get the file's last-modified timestamp
57 * @return string|bool TS_MW timestamp or false on failure
59 public function getTimestamp() {
61 $timestamp = filemtime( $this->path
);
63 if ( $timestamp !== false ) {
64 $timestamp = wfTimestamp( TS_MW
, $timestamp );
70 * Guess the MIME type from the file contents alone
74 public function getMimeType() {
75 return MimeMagic
::singleton()->guessMimeType( $this->path
, false );
79 * Get an associative array containing information about
80 * a file with the given storage path.
82 * @param $ext Mixed: the file extension, or true to extract it from the filename.
83 * Set it to false to ignore the extension.
87 public function getProps( $ext = true ) {
88 wfProfileIn( __METHOD__
);
89 wfDebug( __METHOD__
.": Getting file info for $this->path\n" );
91 $info = self
::placeholderProps();
92 $info['fileExists'] = $this->exists();
94 if ( $info['fileExists'] ) {
95 $magic = MimeMagic
::singleton();
97 # get the file extension
98 if ( $ext === true ) {
99 $ext = self
::extensionFromPath( $this->path
);
102 # mime type according to file contents
103 $info['file-mime'] = $this->getMimeType();
105 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
107 list( $info['major_mime'], $info['minor_mime'] ) = File
::splitMime( $info['mime'] );
108 $info['media_type'] = $magic->getMediaType( $this->path
, $info['mime'] );
111 $info['size'] = $this->getSize();
113 # Height, width and metadata
114 $handler = MediaHandler
::getHandler( $info['mime'] );
116 $tempImage = (object)array();
117 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path
);
118 $gis = $handler->getImageSize( $tempImage, $this->path
, $info['metadata'] );
119 if ( is_array( $gis ) ) {
120 $info = $this->extractImageSizeInfo( $gis ) +
$info;
123 $info['sha1'] = $this->getSha1Base36();
125 wfDebug(__METHOD__
.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n");
127 wfDebug(__METHOD__
.": $this->path NOT FOUND!\n");
130 wfProfileOut( __METHOD__
);
135 * Placeholder file properties to use for files that don't exist
139 public static function placeholderProps() {
141 $info['fileExists'] = false;
142 $info['mime'] = null;
143 $info['media_type'] = MEDIATYPE_UNKNOWN
;
144 $info['metadata'] = '';
153 * Exract image size information
157 protected function extractImageSizeInfo( array $gis ) {
159 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
160 $info['width'] = $gis[0];
161 $info['height'] = $gis[1];
162 if ( isset( $gis['bits'] ) ) {
163 $info['bits'] = $gis['bits'];
171 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
172 * encoding, zero padded to 31 digits.
174 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
177 * @return bool|string False on failure
179 public function getSha1Base36() {
180 wfProfileIn( __METHOD__
);
182 wfSuppressWarnings();
183 $hash = sha1_file( $this->path
);
185 if ( $hash !== false ) {
186 $hash = wfBaseConvert( $hash, 16, 36, 31 );
189 wfProfileOut( __METHOD__
);
194 * Get the final file extension from a file system path
196 * @param $path string
199 public static function extensionFromPath( $path ) {
200 $i = strrpos( $path, '.' );
201 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
205 * Get an associative array containing information about a file in the local filesystem.
207 * @param $path String: absolute local filesystem path
208 * @param $ext Mixed: the file extension, or true to extract it from the filename.
209 * Set it to false to ignore the extension.
213 public static function getPropsFromPath( $path, $ext = true ) {
214 $fsFile = new self( $path );
215 return $fsFile->getProps( $ext );
219 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
220 * encoding, zero padded to 31 digits.
222 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
225 * @param $path string
227 * @return bool|string False on failure
229 public static function getSha1Base36FromPath( $path ) {
230 $fsFile = new self( $path );
231 return $fsFile->getSha1Base36();