Merge "Improve lag/flag behavior and docs in LoadBalancer"
[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 * Get an associative array containing information about
90 * a file with the given storage path.
91 *
92 * Resulting array fields include:
93 * - fileExists
94 * - size (filesize in bytes)
95 * - mime (as major/minor)
96 * - media_type (value to be used with the MEDIATYPE_xxx constants)
97 * - metadata (handler specific)
98 * - sha1 (in base 36)
99 * - width
100 * - height
101 * - bits (bitrate)
102 * - file-mime
103 * - major_mime
104 * - minor_mime
105 *
106 * @param string|bool $ext The file extension, or true to extract it from the filename.
107 * Set it to false to ignore the extension.
108 * @return array
109 */
110 public function getProps( $ext = true ) {
111 $info = self::placeholderProps();
112 $info['fileExists'] = $this->exists();
113
114 if ( $info['fileExists'] ) {
115 $magic = MimeMagic::singleton();
116
117 # get the file extension
118 if ( $ext === true ) {
119 $ext = self::extensionFromPath( $this->path );
120 }
121
122 # MIME type according to file contents
123 $info['file-mime'] = $magic->guessMimeType( $this->path, false );
124 # logical MIME type
125 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
126
127 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
128 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
129
130 # Get size in bytes
131 $info['size'] = $this->getSize();
132
133 # Height, width and metadata
134 $handler = MediaHandler::getHandler( $info['mime'] );
135 if ( $handler ) {
136 $tempImage = (object)[]; // XXX (hack for File object)
137 /** @noinspection PhpParamsInspection */
138 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
139 /** @noinspection PhpParamsInspection */
140 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
141 if ( is_array( $gis ) ) {
142 $info = $this->extractImageSizeInfo( $gis ) + $info;
143 }
144 }
145 $info['sha1'] = $this->getSha1Base36();
146 }
147
148 return $info;
149 }
150
151 /**
152 * Placeholder file properties to use for files that don't exist
153 *
154 * Resulting array fields include:
155 * - fileExists
156 * - mime (as major/minor)
157 * - media_type (value to be used with the MEDIATYPE_xxx constants)
158 * - metadata (handler specific)
159 * - sha1 (in base 36)
160 * - width
161 * - height
162 * - bits (bitrate)
163 *
164 * @return array
165 */
166 public static function placeholderProps() {
167 $info = [];
168 $info['fileExists'] = false;
169 $info['mime'] = null;
170 $info['media_type'] = MEDIATYPE_UNKNOWN;
171 $info['metadata'] = '';
172 $info['sha1'] = '';
173 $info['width'] = 0;
174 $info['height'] = 0;
175 $info['bits'] = 0;
176
177 return $info;
178 }
179
180 /**
181 * Exract image size information
182 *
183 * @param array $gis
184 * @return array
185 */
186 protected function extractImageSizeInfo( array $gis ) {
187 $info = [];
188 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
189 $info['width'] = $gis[0];
190 $info['height'] = $gis[1];
191 if ( isset( $gis['bits'] ) ) {
192 $info['bits'] = $gis['bits'];
193 } else {
194 $info['bits'] = 0;
195 }
196
197 return $info;
198 }
199
200 /**
201 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
202 * encoding, zero padded to 31 digits.
203 *
204 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
205 * fairly neatly.
206 *
207 * @param bool $recache
208 * @return bool|string False on failure
209 */
210 public function getSha1Base36( $recache = false ) {
211 if ( $this->sha1Base36 !== null && !$recache ) {
212 return $this->sha1Base36;
213 }
214
215 MediaWiki\suppressWarnings();
216 $this->sha1Base36 = sha1_file( $this->path );
217 MediaWiki\restoreWarnings();
218
219 if ( $this->sha1Base36 !== false ) {
220 $this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
221 }
222
223 return $this->sha1Base36;
224 }
225
226 /**
227 * Get the final file extension from a file system path
228 *
229 * @param string $path
230 * @return string
231 */
232 public static function extensionFromPath( $path ) {
233 $i = strrpos( $path, '.' );
234
235 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
236 }
237
238 /**
239 * Get an associative array containing information about a file in the local filesystem.
240 *
241 * @param string $path Absolute local filesystem path
242 * @param string|bool $ext The file extension, or true to extract it from the filename.
243 * Set it to false to ignore the extension.
244 * @return array
245 */
246 public static function getPropsFromPath( $path, $ext = true ) {
247 $fsFile = new self( $path );
248
249 return $fsFile->getProps( $ext );
250 }
251
252 /**
253 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
254 * encoding, zero padded to 31 digits.
255 *
256 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
257 * fairly neatly.
258 *
259 * @param string $path
260 * @return bool|string False on failure
261 */
262 public static function getSha1Base36FromPath( $path ) {
263 $fsFile = new self( $path );
264
265 return $fsFile->getSha1Base36();
266 }
267 }