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