getParamValue defaults to current page only if url undefined
[mediawiki.git] / includes / filebackend / FSFile.php
blobe07c99d42d5fecc41c855fb14f3bf98775921296
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 protected $path; // path to file
32 /**
33 * Sets up the file object
35 * @param $path string Path to temporary file on local disk
36 * @throws MWException
38 public function __construct( $path ) {
39 if ( FileBackend::isStoragePath( $path ) ) {
40 throw new MWException( __METHOD__ . " given storage path `$path`." );
42 $this->path = $path;
45 /**
46 * Returns the file system path
48 * @return String
50 public function getPath() {
51 return $this->path;
54 /**
55 * Checks if the file exists
57 * @return bool
59 public function exists() {
60 return is_file( $this->path );
63 /**
64 * Get the file size in bytes
66 * @return int|bool
68 public function getSize() {
69 return filesize( $this->path );
72 /**
73 * Get the file's last-modified timestamp
75 * @return string|bool TS_MW timestamp or false on failure
77 public function getTimestamp() {
78 wfSuppressWarnings();
79 $timestamp = filemtime( $this->path );
80 wfRestoreWarnings();
81 if ( $timestamp !== false ) {
82 $timestamp = wfTimestamp( TS_MW, $timestamp );
84 return $timestamp;
87 /**
88 * Guess the MIME type from the file contents alone
90 * @return string
92 public function getMimeType() {
93 return MimeMagic::singleton()->guessMimeType( $this->path, false );
96 /**
97 * Get an associative array containing information about
98 * a file with the given storage path.
100 * @param $ext Mixed: the file extension, or true to extract it from the filename.
101 * Set it to false to ignore the extension.
103 * @return array
105 public function getProps( $ext = true ) {
106 wfProfileIn( __METHOD__ );
107 wfDebug( __METHOD__.": Getting file info for $this->path\n" );
109 $info = self::placeholderProps();
110 $info['fileExists'] = $this->exists();
112 if ( $info['fileExists'] ) {
113 $magic = MimeMagic::singleton();
115 # get the file extension
116 if ( $ext === true ) {
117 $ext = self::extensionFromPath( $this->path );
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 );
125 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
126 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
128 # Get size in bytes
129 $info['size'] = $this->getSize();
131 # Height, width and metadata
132 $handler = MediaHandler::getHandler( $info['mime'] );
133 if ( $handler ) {
134 $tempImage = (object)array();
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;
141 $info['sha1'] = $this->getSha1Base36();
143 wfDebug(__METHOD__.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n");
144 } else {
145 wfDebug(__METHOD__.": $this->path NOT FOUND!\n");
148 wfProfileOut( __METHOD__ );
149 return $info;
153 * Placeholder file properties to use for files that don't exist
155 * @return Array
157 public static function placeholderProps() {
158 $info = array();
159 $info['fileExists'] = false;
160 $info['mime'] = null;
161 $info['media_type'] = MEDIATYPE_UNKNOWN;
162 $info['metadata'] = '';
163 $info['sha1'] = '';
164 $info['width'] = 0;
165 $info['height'] = 0;
166 $info['bits'] = 0;
167 return $info;
171 * Exract image size information
173 * @param $gis array
174 * @return Array
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;
186 return $info;
190 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
191 * encoding, zero padded to 31 digits.
193 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
194 * fairly neatly.
196 * @return bool|string False on failure
198 public function getSha1Base36() {
199 wfProfileIn( __METHOD__ );
201 wfSuppressWarnings();
202 $hash = sha1_file( $this->path );
203 wfRestoreWarnings();
204 if ( $hash !== false ) {
205 $hash = wfBaseConvert( $hash, 16, 36, 31 );
208 wfProfileOut( __METHOD__ );
209 return $hash;
213 * Get the final file extension from a file system path
215 * @param $path string
216 * @return string
218 public static function extensionFromPath( $path ) {
219 $i = strrpos( $path, '.' );
220 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
224 * Get an associative array containing information about a file in the local filesystem.
226 * @param $path String: absolute local filesystem path
227 * @param $ext Mixed: the file extension, or true to extract it from the filename.
228 * Set it to false to ignore the extension.
230 * @return array
232 public static function getPropsFromPath( $path, $ext = true ) {
233 $fsFile = new self( $path );
234 return $fsFile->getProps( $ext );
238 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
239 * encoding, zero padded to 31 digits.
241 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
242 * fairly neatly.
244 * @param $path string
246 * @return bool|string False on failure
248 public static function getSha1Base36FromPath( $path ) {
249 $fsFile = new self( $path );
250 return $fsFile->getSha1Base36();