Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / filerepo / backend / FSFile.php
blob5d2bb521543ebf1bedda0d77522bf93aae3a9c7b
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 */
7 /**
8 * Class representing a non-directory file on the file system
10 * @ingroup FileBackend
12 class FSFile {
13 protected $path; // path to file
15 /**
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`." );
24 $this->path = $path;
27 /**
28 * Returns the file system path
30 * @return String
32 public function getPath() {
33 return $this->path;
36 /**
37 * Checks if the file exists
39 * @return bool
41 public function exists() {
42 return is_file( $this->path );
45 /**
46 * Get the file size in bytes
48 * @return int|bool
50 public function getSize() {
51 return filesize( $this->path );
54 /**
55 * Get the file's last-modified timestamp
57 * @return string|bool TS_MW timestamp or false on failure
59 public function getTimestamp() {
60 wfSuppressWarnings();
61 $timestamp = filemtime( $this->path );
62 wfRestoreWarnings();
63 if ( $timestamp !== false ) {
64 $timestamp = wfTimestamp( TS_MW, $timestamp );
66 return $timestamp;
69 /**
70 * Guess the MIME type from the file contents alone
72 * @return string
74 public function getMimeType() {
75 return MimeMagic::singleton()->guessMimeType( $this->path, false );
78 /**
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.
85 * @return array
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();
104 # logical mime type
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'] );
110 # Get size in bytes
111 $info['size'] = $this->getSize();
113 # Height, width and metadata
114 $handler = MediaHandler::getHandler( $info['mime'] );
115 if ( $handler ) {
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");
126 } else {
127 wfDebug(__METHOD__.": $this->path NOT FOUND!\n");
130 wfProfileOut( __METHOD__ );
131 return $info;
135 * Placeholder file properties to use for files that don't exist
137 * @return Array
139 public static function placeholderProps() {
140 $info = array();
141 $info['fileExists'] = false;
142 $info['mime'] = null;
143 $info['media_type'] = MEDIATYPE_UNKNOWN;
144 $info['metadata'] = '';
145 $info['sha1'] = '';
146 $info['width'] = 0;
147 $info['height'] = 0;
148 $info['bits'] = 0;
149 return $info;
153 * Exract image size information
155 * @return Array
157 protected function extractImageSizeInfo( array $gis ) {
158 $info = array();
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'];
164 } else {
165 $info['bits'] = 0;
167 return $info;
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
175 * fairly neatly.
177 * @return bool|string False on failure
179 public function getSha1Base36() {
180 wfProfileIn( __METHOD__ );
182 wfSuppressWarnings();
183 $hash = sha1_file( $this->path );
184 wfRestoreWarnings();
185 if ( $hash !== false ) {
186 $hash = wfBaseConvert( $hash, 16, 36, 31 );
189 wfProfileOut( __METHOD__ );
190 return $hash;
194 * Get the final file extension from a file system path
196 * @param $path string
197 * @return 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.
211 * @return array
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
223 * fairly neatly.
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();