Merge "Update formatting of file backend classes"
[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 protected $path; // path to file
31 protected $sha1Base36; // file SHA-1 in base 36
32
33 /**
34 * Sets up the file object
35 *
36 * @param string $path Path to temporary file on local disk
37 * @throws MWException
38 */
39 public function __construct( $path ) {
40 if ( FileBackend::isStoragePath( $path ) ) {
41 throw new MWException( __METHOD__ . " given storage path `$path`." );
42 }
43 $this->path = $path;
44 }
45
46 /**
47 * Returns the file system path
48 *
49 * @return String
50 */
51 public function getPath() {
52 return $this->path;
53 }
54
55 /**
56 * Checks if the file exists
57 *
58 * @return bool
59 */
60 public function exists() {
61 return is_file( $this->path );
62 }
63
64 /**
65 * Get the file size in bytes
66 *
67 * @return int|bool
68 */
69 public function getSize() {
70 return filesize( $this->path );
71 }
72
73 /**
74 * Get the file's last-modified timestamp
75 *
76 * @return string|bool TS_MW timestamp or false on failure
77 */
78 public function getTimestamp() {
79 wfSuppressWarnings();
80 $timestamp = filemtime( $this->path );
81 wfRestoreWarnings();
82 if ( $timestamp !== false ) {
83 $timestamp = wfTimestamp( TS_MW, $timestamp );
84 }
85
86 return $timestamp;
87 }
88
89 /**
90 * Guess the MIME type from the file contents alone
91 *
92 * @return string
93 */
94 public function getMimeType() {
95 return MimeMagic::singleton()->guessMimeType( $this->path, false );
96 }
97
98 /**
99 * Get an associative array containing information about
100 * a file with the given storage path.
101 *
102 * @param mixed $ext The file extension, or true to extract it from the filename.
103 * Set it to false to ignore the extension.
104 *
105 * @return array
106 */
107 public function getProps( $ext = true ) {
108 wfProfileIn( __METHOD__ );
109 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
110
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'] = $this->getMimeType();
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)array(); // XXX (hack for File object)
137 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
138 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
139 if ( is_array( $gis ) ) {
140 $info = $this->extractImageSizeInfo( $gis ) + $info;
141 }
142 }
143 $info['sha1'] = $this->getSha1Base36();
144
145 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" );
146 } else {
147 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" );
148 }
149
150 wfProfileOut( __METHOD__ );
151
152 return $info;
153 }
154
155 /**
156 * Placeholder file properties to use for files that don't exist
157 *
158 * @return Array
159 */
160 public static function placeholderProps() {
161 $info = array();
162 $info['fileExists'] = false;
163 $info['mime'] = null;
164 $info['media_type'] = MEDIATYPE_UNKNOWN;
165 $info['metadata'] = '';
166 $info['sha1'] = '';
167 $info['width'] = 0;
168 $info['height'] = 0;
169 $info['bits'] = 0;
170
171 return $info;
172 }
173
174 /**
175 * Exract image size information
176 *
177 * @param array $gis
178 * @return Array
179 */
180 protected function extractImageSizeInfo( array $gis ) {
181 $info = array();
182 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
183 $info['width'] = $gis[0];
184 $info['height'] = $gis[1];
185 if ( isset( $gis['bits'] ) ) {
186 $info['bits'] = $gis['bits'];
187 } else {
188 $info['bits'] = 0;
189 }
190
191 return $info;
192 }
193
194 /**
195 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
196 * encoding, zero padded to 31 digits.
197 *
198 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
199 * fairly neatly.
200 *
201 * @param bool $recache
202 * @return bool|string False on failure
203 */
204 public function getSha1Base36( $recache = false ) {
205 wfProfileIn( __METHOD__ );
206
207 if ( $this->sha1Base36 !== null && !$recache ) {
208 wfProfileOut( __METHOD__ );
209
210 return $this->sha1Base36;
211 }
212
213 wfSuppressWarnings();
214 $this->sha1Base36 = sha1_file( $this->path );
215 wfRestoreWarnings();
216
217 if ( $this->sha1Base36 !== false ) {
218 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
219 }
220
221 wfProfileOut( __METHOD__ );
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 mixed $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 }