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