Merge "Don't check namespace in SpecialWantedtemplates"
[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 * @param string|bool $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 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
108
109 $info = self::placeholderProps();
110 $info['fileExists'] = $this->exists();
111
112 if ( $info['fileExists'] ) {
113 $magic = MimeMagic::singleton();
114
115 # get the file extension
116 if ( $ext === true ) {
117 $ext = self::extensionFromPath( $this->path );
118 }
119
120 # MIME type according to file contents
121 $info['file-mime'] = $this->getMimeType();
122 # logical MIME type
123 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
124
125 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
126 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
127
128 # Get size in bytes
129 $info['size'] = $this->getSize();
130
131 # Height, width and metadata
132 $handler = MediaHandler::getHandler( $info['mime'] );
133 if ( $handler ) {
134 $tempImage = (object)array(); // XXX (hack for File object)
135 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
136 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
137 if ( is_array( $gis ) ) {
138 $info = $this->extractImageSizeInfo( $gis ) + $info;
139 }
140 }
141 $info['sha1'] = $this->getSha1Base36();
142
143 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" );
144 } else {
145 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" );
146 }
147
148 return $info;
149 }
150
151 /**
152 * Placeholder file properties to use for files that don't exist
153 *
154 * @return array
155 */
156 public static function placeholderProps() {
157 $info = array();
158 $info['fileExists'] = false;
159 $info['mime'] = null;
160 $info['media_type'] = MEDIATYPE_UNKNOWN;
161 $info['metadata'] = '';
162 $info['sha1'] = '';
163 $info['width'] = 0;
164 $info['height'] = 0;
165 $info['bits'] = 0;
166
167 return $info;
168 }
169
170 /**
171 * Exract image size information
172 *
173 * @param array $gis
174 * @return array
175 */
176 protected function extractImageSizeInfo( array $gis ) {
177 $info = array();
178 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
179 $info['width'] = $gis[0];
180 $info['height'] = $gis[1];
181 if ( isset( $gis['bits'] ) ) {
182 $info['bits'] = $gis['bits'];
183 } else {
184 $info['bits'] = 0;
185 }
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
202 if ( $this->sha1Base36 !== null && !$recache ) {
203 return $this->sha1Base36;
204 }
205
206 MediaWiki\suppressWarnings();
207 $this->sha1Base36 = sha1_file( $this->path );
208 MediaWiki\restoreWarnings();
209
210 if ( $this->sha1Base36 !== false ) {
211 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
212 }
213
214 return $this->sha1Base36;
215 }
216
217 /**
218 * Get the final file extension from a file system path
219 *
220 * @param string $path
221 * @return string
222 */
223 public static function extensionFromPath( $path ) {
224 $i = strrpos( $path, '.' );
225
226 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
227 }
228
229 /**
230 * Get an associative array containing information about a file in the local filesystem.
231 *
232 * @param string $path Absolute local filesystem path
233 * @param string|bool $ext The file extension, or true to extract it from the filename.
234 * Set it to false to ignore the extension.
235 * @return array
236 */
237 public static function getPropsFromPath( $path, $ext = true ) {
238 $fsFile = new self( $path );
239
240 return $fsFile->getProps( $ext );
241 }
242
243 /**
244 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
245 * encoding, zero padded to 31 digits.
246 *
247 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
248 * fairly neatly.
249 *
250 * @param string $path
251 * @return bool|string False on failure
252 */
253 public static function getSha1Base36FromPath( $path ) {
254 $fsFile = new self( $path );
255
256 return $fsFile->getSha1Base36();
257 }
258 }