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
21 * @ingroup FileBackend
25 * Class representing a non-directory file on the file system
27 * @ingroup FileBackend
30 /** @var string Path to file */
33 /** @var string File SHA-1 in base 36 */
34 protected $sha1Base36;
37 * Sets up the file object
39 * @param string $path Path to temporary file on local disk
42 public function __construct( $path ) {
43 if ( FileBackend
::isStoragePath( $path ) ) {
44 throw new MWException( __METHOD__
. " given storage path `$path`." );
50 * Returns the file system path
54 public function getPath() {
59 * Checks if the file exists
63 public function exists() {
64 return is_file( $this->path
);
68 * Get the file size in bytes
72 public function getSize() {
73 return filesize( $this->path
);
77 * Get the file's last-modified timestamp
79 * @return string|bool TS_MW timestamp or false on failure
81 public function getTimestamp() {
83 $timestamp = filemtime( $this->path
);
85 if ( $timestamp !== false ) {
86 $timestamp = wfTimestamp( TS_MW
, $timestamp );
93 * Guess the MIME type from the file contents alone
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.
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();
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'] );
134 $info['size'] = $this->getSize();
136 # Height, width and metadata
137 $handler = MediaHandler
::getHandler( $info['mime'] );
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" );
150 wfDebug( __METHOD__
. ": $this->path NOT FOUND!\n" );
153 wfProfileOut( __METHOD__
);
159 * Placeholder file properties to use for files that don't exist
163 public static function placeholderProps() {
165 $info['fileExists'] = false;
166 $info['mime'] = null;
167 $info['media_type'] = MEDIATYPE_UNKNOWN
;
168 $info['metadata'] = '';
178 * Exract image size information
183 protected function extractImageSizeInfo( array $gis ) {
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'];
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
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
);
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
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.
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
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();