Merge "Remove unused messages 'edit-externally' and 'edit-externally-help'"
[mediawiki.git] / includes / filebackend / FSFile.php
blob3a0aa9747db2c36b3d0e66b3268ab1798f33d15a
1 <?php
2 /**
3 * Non-directory file on the file system.
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.
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.
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
20 * @file
21 * @ingroup FileBackend
24 /**
25 * Class representing a non-directory file on the file system
27 * @ingroup FileBackend
29 class FSFile {
30 /** @var string Path to file */
31 protected $path;
33 /** @var string File SHA-1 in base 36 */
34 protected $sha1Base36;
36 /**
37 * Sets up the file object
39 * @param string $path Path to temporary file on local disk
40 * @throws MWException
42 public function __construct( $path ) {
43 if ( FileBackend::isStoragePath( $path ) ) {
44 throw new MWException( __METHOD__ . " given storage path `$path`." );
46 $this->path = $path;
49 /**
50 * Returns the file system path
52 * @return String
54 public function getPath() {
55 return $this->path;
58 /**
59 * Checks if the file exists
61 * @return bool
63 public function exists() {
64 return is_file( $this->path );
67 /**
68 * Get the file size in bytes
70 * @return int|bool
72 public function getSize() {
73 return filesize( $this->path );
76 /**
77 * Get the file's last-modified timestamp
79 * @return string|bool TS_MW timestamp or false on failure
81 public function getTimestamp() {
82 wfSuppressWarnings();
83 $timestamp = filemtime( $this->path );
84 wfRestoreWarnings();
85 if ( $timestamp !== false ) {
86 $timestamp = wfTimestamp( TS_MW, $timestamp );
89 return $timestamp;
92 /**
93 * Guess the MIME type from the file contents alone
95 * @return string
97 public function getMimeType() {
98 return MimeMagic::singleton()->guessMimeType( $this->path, false );
102 * Get an associative array containing information about
103 * a file with the given storage path.
105 * @param mixed $ext The file extension, or true to extract it from the filename.
106 * Set it to false to ignore the extension.
108 * @return array
110 public function getProps( $ext = true ) {
111 wfProfileIn( __METHOD__ );
112 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
114 $info = self::placeholderProps();
115 $info['fileExists'] = $this->exists();
117 if ( $info['fileExists'] ) {
118 $magic = MimeMagic::singleton();
120 # get the file extension
121 if ( $ext === true ) {
122 $ext = self::extensionFromPath( $this->path );
125 # mime type according to file contents
126 $info['file-mime'] = $this->getMimeType();
127 # logical mime type
128 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
130 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
131 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
133 # Get size in bytes
134 $info['size'] = $this->getSize();
136 # Height, width and metadata
137 $handler = MediaHandler::getHandler( $info['mime'] );
138 if ( $handler ) {
139 $tempImage = (object)array(); // XXX (hack for File object)
140 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
141 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
142 if ( is_array( $gis ) ) {
143 $info = $this->extractImageSizeInfo( $gis ) + $info;
146 $info['sha1'] = $this->getSha1Base36();
148 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" );
149 } else {
150 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" );
153 wfProfileOut( __METHOD__ );
155 return $info;
159 * Placeholder file properties to use for files that don't exist
161 * @return array
163 public static function placeholderProps() {
164 $info = array();
165 $info['fileExists'] = false;
166 $info['mime'] = null;
167 $info['media_type'] = MEDIATYPE_UNKNOWN;
168 $info['metadata'] = '';
169 $info['sha1'] = '';
170 $info['width'] = 0;
171 $info['height'] = 0;
172 $info['bits'] = 0;
174 return $info;
178 * Exract image size information
180 * @param array $gis
181 * @return array
183 protected function extractImageSizeInfo( array $gis ) {
184 $info = array();
185 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
186 $info['width'] = $gis[0];
187 $info['height'] = $gis[1];
188 if ( isset( $gis['bits'] ) ) {
189 $info['bits'] = $gis['bits'];
190 } else {
191 $info['bits'] = 0;
194 return $info;
198 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
199 * encoding, zero padded to 31 digits.
201 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
202 * fairly neatly.
204 * @param bool $recache
205 * @return bool|string False on failure
207 public function getSha1Base36( $recache = false ) {
208 wfProfileIn( __METHOD__ );
210 if ( $this->sha1Base36 !== null && !$recache ) {
211 wfProfileOut( __METHOD__ );
213 return $this->sha1Base36;
216 wfSuppressWarnings();
217 $this->sha1Base36 = sha1_file( $this->path );
218 wfRestoreWarnings();
220 if ( $this->sha1Base36 !== false ) {
221 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
224 wfProfileOut( __METHOD__ );
226 return $this->sha1Base36;
230 * Get the final file extension from a file system path
232 * @param string $path
233 * @return string
235 public static function extensionFromPath( $path ) {
236 $i = strrpos( $path, '.' );
238 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
242 * Get an associative array containing information about a file in the local filesystem.
244 * @param string $path absolute local filesystem path
245 * @param mixed $ext The file extension, or true to extract it from the filename.
246 * Set it to false to ignore the extension.
247 * @return array
249 public static function getPropsFromPath( $path, $ext = true ) {
250 $fsFile = new self( $path );
252 return $fsFile->getProps( $ext );
256 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
257 * encoding, zero padded to 31 digits.
259 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
260 * fairly neatly.
262 * @param string $path
263 * @return bool|string False on failure
265 public static function getSha1Base36FromPath( $path ) {
266 $fsFile = new self( $path );
268 return $fsFile->getSha1Base36();