Revert "Adding sanity check to Title::isRedirect()."
[lhc/web/wiklou.git] / includes / filerepo / backend / 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
32 /**
33 * Sets up the file object
34 *
35 * @param String $path Path to temporary file on local disk
36 */
37 public function __construct( $path ) {
38 if ( FileBackend::isStoragePath( $path ) ) {
39 throw new MWException( __METHOD__ . " given storage path `$path`." );
40 }
41 $this->path = $path;
42 }
43
44 /**
45 * Returns the file system path
46 *
47 * @return String
48 */
49 public function getPath() {
50 return $this->path;
51 }
52
53 /**
54 * Checks if the file exists
55 *
56 * @return bool
57 */
58 public function exists() {
59 return is_file( $this->path );
60 }
61
62 /**
63 * Get the file size in bytes
64 *
65 * @return int|bool
66 */
67 public function getSize() {
68 return filesize( $this->path );
69 }
70
71 /**
72 * Get the file's last-modified timestamp
73 *
74 * @return string|bool TS_MW timestamp or false on failure
75 */
76 public function getTimestamp() {
77 wfSuppressWarnings();
78 $timestamp = filemtime( $this->path );
79 wfRestoreWarnings();
80 if ( $timestamp !== false ) {
81 $timestamp = wfTimestamp( TS_MW, $timestamp );
82 }
83 return $timestamp;
84 }
85
86 /**
87 * Guess the MIME type from the file contents alone
88 *
89 * @return string
90 */
91 public function getMimeType() {
92 return MimeMagic::singleton()->guessMimeType( $this->path, false );
93 }
94
95 /**
96 * Get an associative array containing information about
97 * a file with the given storage path.
98 *
99 * @param $ext Mixed: the file extension, or true to extract it from the filename.
100 * Set it to false to ignore the extension.
101 *
102 * @return array
103 */
104 public function getProps( $ext = true ) {
105 wfProfileIn( __METHOD__ );
106 wfDebug( __METHOD__.": Getting file info for $this->path\n" );
107
108 $info = self::placeholderProps();
109 $info['fileExists'] = $this->exists();
110
111 if ( $info['fileExists'] ) {
112 $magic = MimeMagic::singleton();
113
114 # get the file extension
115 if ( $ext === true ) {
116 $ext = self::extensionFromPath( $this->path );
117 }
118
119 # mime type according to file contents
120 $info['file-mime'] = $this->getMimeType();
121 # logical mime type
122 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
123
124 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
125 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
126
127 # Get size in bytes
128 $info['size'] = $this->getSize();
129
130 # Height, width and metadata
131 $handler = MediaHandler::getHandler( $info['mime'] );
132 if ( $handler ) {
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;
138 }
139 }
140 $info['sha1'] = $this->getSha1Base36();
141
142 wfDebug(__METHOD__.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n");
143 } else {
144 wfDebug(__METHOD__.": $this->path NOT FOUND!\n");
145 }
146
147 wfProfileOut( __METHOD__ );
148 return $info;
149 }
150
151 /**
152 * Placeholder file properties to use for files that don't exist
153 *
154 * @return Array
155 */
156 public static function placeholderProps() {
157 $info = array();
158 $info['fileExists'] = false;
159 $info['mime'] = null;
160 $info['media_type'] = MEDIATYPE_UNKNOWN;
161 $info['metadata'] = '';
162 $info['sha1'] = '';
163 $info['width'] = 0;
164 $info['height'] = 0;
165 $info['bits'] = 0;
166 return $info;
167 }
168
169 /**
170 * Exract image size information
171 *
172 * @return Array
173 */
174 protected function extractImageSizeInfo( array $gis ) {
175 $info = array();
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'];
181 } else {
182 $info['bits'] = 0;
183 }
184 return $info;
185 }
186
187 /**
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.
190 *
191 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
192 * fairly neatly.
193 *
194 * @return bool|string False on failure
195 */
196 public function getSha1Base36() {
197 wfProfileIn( __METHOD__ );
198
199 wfSuppressWarnings();
200 $hash = sha1_file( $this->path );
201 wfRestoreWarnings();
202 if ( $hash !== false ) {
203 $hash = wfBaseConvert( $hash, 16, 36, 31 );
204 }
205
206 wfProfileOut( __METHOD__ );
207 return $hash;
208 }
209
210 /**
211 * Get the final file extension from a file system path
212 *
213 * @param $path string
214 * @return string
215 */
216 public static function extensionFromPath( $path ) {
217 $i = strrpos( $path, '.' );
218 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
219 }
220
221 /**
222 * Get an associative array containing information about a file in the local filesystem.
223 *
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.
227 *
228 * @return array
229 */
230 public static function getPropsFromPath( $path, $ext = true ) {
231 $fsFile = new self( $path );
232 return $fsFile->getProps( $ext );
233 }
234
235 /**
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.
238 *
239 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
240 * fairly neatly.
241 *
242 * @param $path string
243 *
244 * @return bool|string False on failure
245 */
246 public static function getSha1Base36FromPath( $path ) {
247 $fsFile = new self( $path );
248 return $fsFile->getSha1Base36();
249 }
250 }