test: skip math parser tests when missing $wgTexvc
[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 protected $path; // path to file
31 private $sha1Base36 = null; // File Sha1Base36
32
33 /**
34 * Sets up the file object
35 *
36 * @param string $path Path to temporary file on local disk
37 * @throws MWException
38 */
39 public function __construct( $path ) {
40 if ( FileBackend::isStoragePath( $path ) ) {
41 throw new MWException( __METHOD__ . " given storage path `$path`." );
42 }
43 $this->path = $path;
44 }
45
46 /**
47 * Returns the file system path
48 *
49 * @return String
50 */
51 public function getPath() {
52 return $this->path;
53 }
54
55 /**
56 * Checks if the file exists
57 *
58 * @return bool
59 */
60 public function exists() {
61 return is_file( $this->path );
62 }
63
64 /**
65 * Get the file size in bytes
66 *
67 * @return int|bool
68 */
69 public function getSize() {
70 return filesize( $this->path );
71 }
72
73 /**
74 * Get the file's last-modified timestamp
75 *
76 * @return string|bool TS_MW timestamp or false on failure
77 */
78 public function getTimestamp() {
79 wfSuppressWarnings();
80 $timestamp = filemtime( $this->path );
81 wfRestoreWarnings();
82 if ( $timestamp !== false ) {
83 $timestamp = wfTimestamp( TS_MW, $timestamp );
84 }
85 return $timestamp;
86 }
87
88 /**
89 * Guess the MIME type from the file contents alone
90 *
91 * @return string
92 */
93 public function getMimeType() {
94 return MimeMagic::singleton()->guessMimeType( $this->path, false );
95 }
96
97 /**
98 * Get an associative array containing information about
99 * a file with the given storage path.
100 *
101 * @param Mixed $ext: the file extension, or true to extract it from the filename.
102 * Set it to false to ignore the extension.
103 *
104 * @return array
105 */
106 public function getProps( $ext = true ) {
107 wfProfileIn( __METHOD__ );
108 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
109
110 $info = self::placeholderProps();
111 $info['fileExists'] = $this->exists();
112
113 if ( $info['fileExists'] ) {
114 $magic = MimeMagic::singleton();
115
116 # get the file extension
117 if ( $ext === true ) {
118 $ext = self::extensionFromPath( $this->path );
119 }
120
121 # mime type according to file contents
122 $info['file-mime'] = $this->getMimeType();
123 # logical mime type
124 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
125
126 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
127 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
128
129 # Get size in bytes
130 $info['size'] = $this->getSize();
131
132 # Height, width and metadata
133 $handler = MediaHandler::getHandler( $info['mime'] );
134 if ( $handler ) {
135 $tempImage = (object)array(); // XXX (hack for File object)
136 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
137 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
138 if ( is_array( $gis ) ) {
139 $info = $this->extractImageSizeInfo( $gis ) + $info;
140 }
141 }
142 $info['sha1'] = $this->getSha1Base36();
143
144 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" );
145 } else {
146 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" );
147 }
148
149 wfProfileOut( __METHOD__ );
150 return $info;
151 }
152
153 /**
154 * Placeholder file properties to use for files that don't exist
155 *
156 * @return Array
157 */
158 public static function placeholderProps() {
159 $info = array();
160 $info['fileExists'] = false;
161 $info['mime'] = null;
162 $info['media_type'] = MEDIATYPE_UNKNOWN;
163 $info['metadata'] = '';
164 $info['sha1'] = '';
165 $info['width'] = 0;
166 $info['height'] = 0;
167 $info['bits'] = 0;
168 return $info;
169 }
170
171 /**
172 * Exract image size information
173 *
174 * @param array $gis
175 * @return Array
176 */
177 protected function extractImageSizeInfo( array $gis ) {
178 $info = array();
179 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
180 $info['width'] = $gis[0];
181 $info['height'] = $gis[1];
182 if ( isset( $gis['bits'] ) ) {
183 $info['bits'] = $gis['bits'];
184 } else {
185 $info['bits'] = 0;
186 }
187 return $info;
188 }
189
190 /**
191 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
192 * encoding, zero padded to 31 digits.
193 *
194 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
195 * fairly neatly.
196 *
197 * @param bool $recache
198 * @return bool|string False on failure
199 */
200 public function getSha1Base36( $recache = false ) {
201 wfProfileIn( __METHOD__ );
202
203 if ( $this->sha1Base36 !== null && !$recache ) {
204 wfProfileOut( __METHOD__ );
205 return $this->sha1Base36;
206 }
207
208 wfSuppressWarnings();
209 $this->sha1Base36 = sha1_file( $this->path );
210 wfRestoreWarnings();
211
212 if ( $this->sha1Base36 !== false ) {
213 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
214 }
215
216 wfProfileOut( __METHOD__ );
217 return $this->sha1Base36;
218 }
219
220 /**
221 * Get the final file extension from a file system path
222 *
223 * @param string $path
224 * @return string
225 */
226 public static function extensionFromPath( $path ) {
227 $i = strrpos( $path, '.' );
228 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
229 }
230
231 /**
232 * Get an associative array containing information about a file in the local filesystem.
233 *
234 * @param string $path absolute local filesystem path
235 * @param Mixed $ext: the file extension, or true to extract it from the filename.
236 * Set it to false to ignore the extension.
237 *
238 * @return array
239 */
240 public static function getPropsFromPath( $path, $ext = true ) {
241 $fsFile = new self( $path );
242 return $fsFile->getProps( $ext );
243 }
244
245 /**
246 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
247 * encoding, zero padded to 31 digits.
248 *
249 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
250 * fairly neatly.
251 *
252 * @param string $path
253 * @param bool $recache
254 *
255 * @return bool|string False on failure
256 */
257 public static function getSha1Base36FromPath( $path, $recache = false ) {
258 static $sha1Base36 = array();
259
260 if ( !isset( $sha1Base36[$path] ) || $recache ) {
261 $fsFile = new self( $path );
262 $sha1Base36[$path] = $fsFile->getSha1Base36();
263 }
264
265 return $sha1Base36[$path];
266 }
267 }