Merge "Ensure users are able to edit the page after changing the content model"
[lhc/web/wiklou.git] / includes / libs / 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 * - file-mime (as major/minor)
97 * - sha1 (in base 36)
98 * - major_mime
99 * - minor_mime
100 *
101 * @param string|bool $ext The file extension, or true to extract it from the filename.
102 * Set it to false to ignore the extension. Currently unused.
103 * @return array
104 */
105 public function getProps( $ext = true ) {
106 $info = self::placeholderProps();
107 $info['fileExists'] = $this->exists();
108
109 if ( $info['fileExists'] ) {
110 $info['size'] = $this->getSize(); // bytes
111 $info['sha1'] = $this->getSha1Base36();
112
113 $mime = mime_content_type( $this->path );
114 # MIME type according to file contents
115 $info['file-mime'] = ( $mime === false ) ? 'unknown/unknown' : $mime;
116 # logical MIME type
117 $info['mime'] = $mime;
118
119 if ( strpos( $mime, '/' ) !== false ) {
120 list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 );
121 } else {
122 list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ];
123 }
124 }
125
126 return $info;
127 }
128
129 /**
130 * Placeholder file properties to use for files that don't exist
131 *
132 * Resulting array fields include:
133 * - fileExists
134 * - size (filesize in bytes)
135 * - mime (as major/minor)
136 * - file-mime (as major/minor)
137 * - sha1 (in base 36)
138 * - major_mime
139 * - minor_mime
140 *
141 * @return array
142 */
143 public static function placeholderProps() {
144 $info = [];
145 $info['fileExists'] = false;
146 $info['size'] = 0;
147 $info['file-mime'] = null;
148 $info['major_mime'] = null;
149 $info['minor_mime'] = null;
150 $info['mime'] = null;
151 $info['sha1'] = '';
152
153 return $info;
154 }
155
156 /**
157 * Exract image size information
158 *
159 * @param array $gis
160 * @return array
161 */
162 protected function extractImageSizeInfo( array $gis ) {
163 $info = [];
164 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
165 $info['width'] = $gis[0];
166 $info['height'] = $gis[1];
167 if ( isset( $gis['bits'] ) ) {
168 $info['bits'] = $gis['bits'];
169 } else {
170 $info['bits'] = 0;
171 }
172
173 return $info;
174 }
175
176 /**
177 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
178 * encoding, zero padded to 31 digits.
179 *
180 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
181 * fairly neatly.
182 *
183 * @param bool $recache
184 * @return bool|string False on failure
185 */
186 public function getSha1Base36( $recache = false ) {
187 if ( $this->sha1Base36 !== null && !$recache ) {
188 return $this->sha1Base36;
189 }
190
191 MediaWiki\suppressWarnings();
192 $this->sha1Base36 = sha1_file( $this->path );
193 MediaWiki\restoreWarnings();
194
195 if ( $this->sha1Base36 !== false ) {
196 $this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
197 }
198
199 return $this->sha1Base36;
200 }
201
202 /**
203 * Get the final file extension from a file system path
204 *
205 * @param string $path
206 * @return string
207 */
208 public static function extensionFromPath( $path ) {
209 $i = strrpos( $path, '.' );
210
211 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
212 }
213
214 /**
215 * Get an associative array containing information about a file in the local filesystem.
216 *
217 * @param string $path Absolute local filesystem path
218 * @param string|bool $ext The file extension, or true to extract it from the filename.
219 * Set it to false to ignore the extension.
220 * @return array
221 */
222 public static function getPropsFromPath( $path, $ext = true ) {
223 $fsFile = new self( $path );
224
225 return $fsFile->getProps( $ext );
226 }
227
228 /**
229 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
230 * encoding, zero padded to 31 digits.
231 *
232 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
233 * fairly neatly.
234 *
235 * @param string $path
236 * @return bool|string False on failure
237 */
238 public static function getSha1Base36FromPath( $path ) {
239 $fsFile = new self( $path );
240
241 return $fsFile->getSha1Base36();
242 }
243 }