Merge "Add no-dimensions option to MediaTransformOutput::toHtml"
[lhc/web/wiklou.git] / includes / filebackend / FSFile.php
1 <?php
2 /**
3 * Non-directory file on the file system.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 */
23
24 /**
25 * Class representing a non-directory file on the file system
26 *
27 * @ingroup FileBackend
28 */
29 class FSFile {
30 /** @var string Path to file */
31 protected $path;
32
33 /** @var string File SHA-1 in base 36 */
34 protected $sha1Base36;
35
36 /**
37 * Sets up the file object
38 *
39 * @param string $path Path to temporary file on local disk
40 * @throws MWException
41 */
42 public function __construct( $path ) {
43 if ( FileBackend::isStoragePath( $path ) ) {
44 throw new MWException( __METHOD__ . " given storage path `$path`." );
45 }
46 $this->path = $path;
47 }
48
49 /**
50 * Returns the file system path
51 *
52 * @return String
53 */
54 public function getPath() {
55 return $this->path;
56 }
57
58 /**
59 * Checks if the file exists
60 *
61 * @return bool
62 */
63 public function exists() {
64 return is_file( $this->path );
65 }
66
67 /**
68 * Get the file size in bytes
69 *
70 * @return int|bool
71 */
72 public function getSize() {
73 return filesize( $this->path );
74 }
75
76 /**
77 * Get the file's last-modified timestamp
78 *
79 * @return string|bool TS_MW timestamp or false on failure
80 */
81 public function getTimestamp() {
82 wfSuppressWarnings();
83 $timestamp = filemtime( $this->path );
84 wfRestoreWarnings();
85 if ( $timestamp !== false ) {
86 $timestamp = wfTimestamp( TS_MW, $timestamp );
87 }
88
89 return $timestamp;
90 }
91
92 /**
93 * Guess the MIME type from the file contents alone
94 *
95 * @return string
96 */
97 public function getMimeType() {
98 return MimeMagic::singleton()->guessMimeType( $this->path, false );
99 }
100
101 /**
102 * Get an associative array containing information about
103 * a file with the given storage path.
104 *
105 * @param mixed $ext The file extension, or true to extract it from the filename.
106 * Set it to false to ignore the extension.
107 *
108 * @return array
109 */
110 public function getProps( $ext = true ) {
111 wfProfileIn( __METHOD__ );
112 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
113
114 $info = self::placeholderProps();
115 $info['fileExists'] = $this->exists();
116
117 if ( $info['fileExists'] ) {
118 $magic = MimeMagic::singleton();
119
120 # get the file extension
121 if ( $ext === true ) {
122 $ext = self::extensionFromPath( $this->path );
123 }
124
125 # mime type according to file contents
126 $info['file-mime'] = $this->getMimeType();
127 # logical mime type
128 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
129
130 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
131 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
132
133 # Get size in bytes
134 $info['size'] = $this->getSize();
135
136 # Height, width and metadata
137 $handler = MediaHandler::getHandler( $info['mime'] );
138 if ( $handler ) {
139 $tempImage = (object)array(); // XXX (hack for File object)
140 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
141 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
142 if ( is_array( $gis ) ) {
143 $info = $this->extractImageSizeInfo( $gis ) + $info;
144 }
145 }
146 $info['sha1'] = $this->getSha1Base36();
147
148 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" );
149 } else {
150 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" );
151 }
152
153 wfProfileOut( __METHOD__ );
154
155 return $info;
156 }
157
158 /**
159 * Placeholder file properties to use for files that don't exist
160 *
161 * @return array
162 */
163 public static function placeholderProps() {
164 $info = array();
165 $info['fileExists'] = false;
166 $info['mime'] = null;
167 $info['media_type'] = MEDIATYPE_UNKNOWN;
168 $info['metadata'] = '';
169 $info['sha1'] = '';
170 $info['width'] = 0;
171 $info['height'] = 0;
172 $info['bits'] = 0;
173
174 return $info;
175 }
176
177 /**
178 * Exract image size information
179 *
180 * @param array $gis
181 * @return array
182 */
183 protected function extractImageSizeInfo( array $gis ) {
184 $info = array();
185 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
186 $info['width'] = $gis[0];
187 $info['height'] = $gis[1];
188 if ( isset( $gis['bits'] ) ) {
189 $info['bits'] = $gis['bits'];
190 } else {
191 $info['bits'] = 0;
192 }
193
194 return $info;
195 }
196
197 /**
198 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
199 * encoding, zero padded to 31 digits.
200 *
201 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
202 * fairly neatly.
203 *
204 * @param bool $recache
205 * @return bool|string False on failure
206 */
207 public function getSha1Base36( $recache = false ) {
208 wfProfileIn( __METHOD__ );
209
210 if ( $this->sha1Base36 !== null && !$recache ) {
211 wfProfileOut( __METHOD__ );
212
213 return $this->sha1Base36;
214 }
215
216 wfSuppressWarnings();
217 $this->sha1Base36 = sha1_file( $this->path );
218 wfRestoreWarnings();
219
220 if ( $this->sha1Base36 !== false ) {
221 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
222 }
223
224 wfProfileOut( __METHOD__ );
225
226 return $this->sha1Base36;
227 }
228
229 /**
230 * Get the final file extension from a file system path
231 *
232 * @param string $path
233 * @return string
234 */
235 public static function extensionFromPath( $path ) {
236 $i = strrpos( $path, '.' );
237
238 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
239 }
240
241 /**
242 * Get an associative array containing information about a file in the local filesystem.
243 *
244 * @param string $path absolute local filesystem path
245 * @param mixed $ext The file extension, or true to extract it from the filename.
246 * Set it to false to ignore the extension.
247 * @return array
248 */
249 public static function getPropsFromPath( $path, $ext = true ) {
250 $fsFile = new self( $path );
251
252 return $fsFile->getProps( $ext );
253 }
254
255 /**
256 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
257 * encoding, zero padded to 31 digits.
258 *
259 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
260 * fairly neatly.
261 *
262 * @param string $path
263 * @return bool|string False on failure
264 */
265 public static function getSha1Base36FromPath( $path ) {
266 $fsFile = new self( $path );
267
268 return $fsFile->getSha1Base36();
269 }
270 }