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 protected $path; // path to file
33 * Sets up the file object
35 * @param String $path Path to temporary file on local disk
37 public function __construct( $path ) {
38 if ( FileBackend
::isStoragePath( $path ) ) {
39 throw new MWException( __METHOD__
. " given storage path `$path`." );
45 * Returns the file system path
49 public function getPath() {
54 * Checks if the file exists
58 public function exists() {
59 return is_file( $this->path
);
63 * Get the file size in bytes
67 public function getSize() {
68 return filesize( $this->path
);
72 * Get the file's last-modified timestamp
74 * @return string|bool TS_MW timestamp or false on failure
76 public function getTimestamp() {
78 $timestamp = filemtime( $this->path
);
80 if ( $timestamp !== false ) {
81 $timestamp = wfTimestamp( TS_MW
, $timestamp );
87 * Guess the MIME type from the file contents alone
91 public function getMimeType() {
92 return MimeMagic
::singleton()->guessMimeType( $this->path
, false );
96 * Get an associative array containing information about
97 * a file with the given storage path.
99 * @param $ext Mixed: the file extension, or true to extract it from the filename.
100 * Set it to false to ignore the extension.
104 public function getProps( $ext = true ) {
105 wfProfileIn( __METHOD__
);
106 wfDebug( __METHOD__
.": Getting file info for $this->path\n" );
108 $info = self
::placeholderProps();
109 $info['fileExists'] = $this->exists();
111 if ( $info['fileExists'] ) {
112 $magic = MimeMagic
::singleton();
114 # get the file extension
115 if ( $ext === true ) {
116 $ext = self
::extensionFromPath( $this->path
);
119 # mime type according to file contents
120 $info['file-mime'] = $this->getMimeType();
122 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
124 list( $info['major_mime'], $info['minor_mime'] ) = File
::splitMime( $info['mime'] );
125 $info['media_type'] = $magic->getMediaType( $this->path
, $info['mime'] );
128 $info['size'] = $this->getSize();
130 # Height, width and metadata
131 $handler = MediaHandler
::getHandler( $info['mime'] );
133 $tempImage = (object)array();
134 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path
);
135 $gis = $handler->getImageSize( $tempImage, $this->path
, $info['metadata'] );
136 if ( is_array( $gis ) ) {
137 $info = $this->extractImageSizeInfo( $gis ) +
$info;
140 $info['sha1'] = $this->getSha1Base36();
142 wfDebug(__METHOD__
.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n");
144 wfDebug(__METHOD__
.": $this->path NOT FOUND!\n");
147 wfProfileOut( __METHOD__
);
152 * Placeholder file properties to use for files that don't exist
156 public static function placeholderProps() {
158 $info['fileExists'] = false;
159 $info['mime'] = null;
160 $info['media_type'] = MEDIATYPE_UNKNOWN
;
161 $info['metadata'] = '';
170 * Exract image size information
174 protected function extractImageSizeInfo( array $gis ) {
176 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
177 $info['width'] = $gis[0];
178 $info['height'] = $gis[1];
179 if ( isset( $gis['bits'] ) ) {
180 $info['bits'] = $gis['bits'];
188 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
189 * encoding, zero padded to 31 digits.
191 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
194 * @return bool|string False on failure
196 public function getSha1Base36() {
197 wfProfileIn( __METHOD__
);
199 wfSuppressWarnings();
200 $hash = sha1_file( $this->path
);
202 if ( $hash !== false ) {
203 $hash = wfBaseConvert( $hash, 16, 36, 31 );
206 wfProfileOut( __METHOD__
);
211 * Get the final file extension from a file system path
213 * @param $path string
216 public static function extensionFromPath( $path ) {
217 $i = strrpos( $path, '.' );
218 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
222 * Get an associative array containing information about a file in the local filesystem.
224 * @param $path String: absolute local filesystem path
225 * @param $ext Mixed: the file extension, or true to extract it from the filename.
226 * Set it to false to ignore the extension.
230 public static function getPropsFromPath( $path, $ext = true ) {
231 $fsFile = new self( $path );
232 return $fsFile->getProps( $ext );
236 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
237 * encoding, zero padded to 31 digits.
239 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
242 * @param $path string
244 * @return bool|string False on failure
246 public static function getSha1Base36FromPath( $path ) {
247 $fsFile = new self( $path );
248 return $fsFile->getSha1Base36();