Documentation followups from lastnight
[lhc/web/wiklou.git] / includes / filerepo / backend / FSFile.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 */
6
7 /**
8 * Class representing a non-directory file on the file system
9 *
10 * @ingroup FileBackend
11 */
12 class FSFile {
13 protected $path; // path to file
14
15 /**
16 * Sets up the file object
17 *
18 * @param String $path Path to temporary file on local disk
19 */
20 public function __construct( $path ) {
21 if ( FileBackend::isStoragePath( $path ) ) {
22 throw new MWException( __METHOD__ . " given storage path `$path`." );
23 }
24 $this->path = $path;
25 }
26
27 /**
28 * Returns the file system path
29 *
30 * @return String
31 */
32 public function getPath() {
33 return $this->path;
34 }
35
36 /**
37 * Checks if the file exists
38 *
39 * @return bool
40 */
41 public function exists() {
42 return is_file( $this->path );
43 }
44
45 /**
46 * Get the file size in bytes
47 *
48 * @return int|bool
49 */
50 public function getSize() {
51 return filesize( $this->path );
52 }
53
54 /**
55 * Get the file's last-modified timestamp
56 *
57 * @return string|bool TS_MW timestamp or false on failure
58 */
59 public function getTimestamp() {
60 wfSuppressWarnings();
61 $timestamp = filemtime( $this->path );
62 wfRestoreWarnings();
63 if ( $timestamp !== false ) {
64 $timestamp = wfTimestamp( TS_MW, $timestamp );
65 }
66 return $timestamp;
67 }
68
69 /**
70 * Guess the MIME type from the file contents alone
71 *
72 * @return string
73 */
74 public function getMimeType() {
75 return MimeMagic::singleton()->guessMimeType( $this->path, false );
76 }
77
78 /**
79 * Get an associative array containing information about
80 * a file with the given storage path.
81 *
82 * @param $ext Mixed: the file extension, or true to extract it from the filename.
83 * Set it to false to ignore the extension.
84 *
85 * @return array
86 */
87 public function getProps( $ext = true ) {
88 wfProfileIn( __METHOD__ );
89 wfDebug( __METHOD__.": Getting file info for $this->path\n" );
90
91 $info = self::placeholderProps();
92 $info['fileExists'] = $this->exists();
93
94 if ( $info['fileExists'] ) {
95 $magic = MimeMagic::singleton();
96
97 # get the file extension
98 if ( $ext === true ) {
99 $ext = self::extensionFromPath( $this->path );
100 }
101
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 );
106
107 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
108 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
109
110 # Get size in bytes
111 $info['size'] = $this->getSize();
112
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;
121 }
122 }
123 $info['sha1'] = $this->getSha1Base36();
124
125 wfDebug(__METHOD__.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n");
126 } else {
127 wfDebug(__METHOD__.": $this->path NOT FOUND!\n");
128 }
129
130 wfProfileOut( __METHOD__ );
131 return $info;
132 }
133
134 /**
135 * Placeholder file properties to use for files that don't exist
136 *
137 * @return Array
138 */
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;
150 }
151
152 /**
153 * Exract image size information
154 *
155 * @return Array
156 */
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;
166 }
167 return $info;
168 }
169
170 /**
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.
173 *
174 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
175 * fairly neatly.
176 *
177 * @return bool|string False on failure
178 */
179 public function getSha1Base36() {
180 wfProfileIn( __METHOD__ );
181
182 wfSuppressWarnings();
183 $hash = sha1_file( $this->path );
184 wfRestoreWarnings();
185 if ( $hash !== false ) {
186 $hash = wfBaseConvert( $hash, 16, 36, 31 );
187 }
188
189 wfProfileOut( __METHOD__ );
190 return $hash;
191 }
192
193 /**
194 * Get the final file extension from a file system path
195 *
196 * @param $path string
197 * @return string
198 */
199 public static function extensionFromPath( $path ) {
200 $i = strrpos( $path, '.' );
201 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
202 }
203
204 /**
205 * Get an associative array containing information about a file in the local filesystem.
206 *
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.
210 *
211 * @return array
212 */
213 public static function getPropsFromPath( $path, $ext = true ) {
214 $fsFile = new self( $path );
215 return $fsFile->getProps( $ext );
216 }
217
218 /**
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.
221 *
222 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
223 * fairly neatly.
224 *
225 * @param $path string
226 *
227 * @return bool|string False on failure
228 */
229 public static function getSha1Base36FromPath( $path ) {
230 $fsFile = new self( $path );
231 return $fsFile->getSha1Base36();
232 }
233 }