[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 /** @var string Path to file */ 31 protected $path; 32 33 /** @var string File SHA-1 in base 36 */ 34 protected $sha1Base36; 35 36 /** 37 * Sets up the file object 38 * 39 * @param string $path Path to temporary file on local disk 40 */ 41 public function __construct( $path ) { 42 $this->path = $path; 43 } 44 45 /** 46 * Returns the file system path 47 * 48 * @return string 49 */ 50 public function getPath() { 51 return $this->path; 52 } 53 54 /** 55 * Checks if the file exists 56 * 57 * @return bool 58 */ 59 public function exists() { 60 return is_file( $this->path ); 61 } 62 63 /** 64 * Get the file size in bytes 65 * 66 * @return int|bool 67 */ 68 public function getSize() { 69 return filesize( $this->path ); 70 } 71 72 /** 73 * Get the file's last-modified timestamp 74 * 75 * @return string|bool TS_MW timestamp or false on failure 76 */ 77 public function getTimestamp() { 78 wfSuppressWarnings(); 79 $timestamp = filemtime( $this->path ); 80 wfRestoreWarnings(); 81 if ( $timestamp !== false ) { 82 $timestamp = wfTimestamp( TS_MW, $timestamp ); 83 } 84 85 return $timestamp; 86 } 87 88 /** 89 * Guess the MIME type from the file contents alone 90 * 91 * @return string 92 */ 93 public function getMimeType() { 94 return MimeMagic::singleton()->guessMimeType( $this->path, false ); 95 } 96 97 /** 98 * Get an associative array containing information about 99 * a file with the given storage path. 100 * 101 * @param string|bool $ext The file extension, or true to extract it from the filename. 102 * Set it to false to ignore the extension. 103 * 104 * @return array 105 */ 106 public function getProps( $ext = true ) { 107 wfProfileIn( __METHOD__ ); 108 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" ); 109 110 $info = self::placeholderProps(); 111 $info['fileExists'] = $this->exists(); 112 113 if ( $info['fileExists'] ) { 114 $magic = MimeMagic::singleton(); 115 116 # get the file extension 117 if ( $ext === true ) { 118 $ext = self::extensionFromPath( $this->path ); 119 } 120 121 # MIME type according to file contents 122 $info['file-mime'] = $this->getMimeType(); 123 # logical MIME type 124 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext ); 125 126 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] ); 127 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] ); 128 129 # Get size in bytes 130 $info['size'] = $this->getSize(); 131 132 # Height, width and metadata 133 $handler = MediaHandler::getHandler( $info['mime'] ); 134 if ( $handler ) { 135 $tempImage = (object)array(); // XXX (hack for File object) 136 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path ); 137 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] ); 138 if ( is_array( $gis ) ) { 139 $info = $this->extractImageSizeInfo( $gis ) + $info; 140 } 141 } 142 $info['sha1'] = $this->getSha1Base36(); 143 144 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" ); 145 } else { 146 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" ); 147 } 148 149 wfProfileOut( __METHOD__ ); 150 151 return $info; 152 } 153 154 /** 155 * Placeholder file properties to use for files that don't exist 156 * 157 * @return array 158 */ 159 public static function placeholderProps() { 160 $info = array(); 161 $info['fileExists'] = false; 162 $info['mime'] = null; 163 $info['media_type'] = MEDIATYPE_UNKNOWN; 164 $info['metadata'] = ''; 165 $info['sha1'] = ''; 166 $info['width'] = 0; 167 $info['height'] = 0; 168 $info['bits'] = 0; 169 170 return $info; 171 } 172 173 /** 174 * Exract image size information 175 * 176 * @param array $gis 177 * @return array 178 */ 179 protected function extractImageSizeInfo( array $gis ) { 180 $info = array(); 181 # NOTE: $gis[2] contains a code for the image type. This is no longer used. 182 $info['width'] = $gis[0]; 183 $info['height'] = $gis[1]; 184 if ( isset( $gis['bits'] ) ) { 185 $info['bits'] = $gis['bits']; 186 } else { 187 $info['bits'] = 0; 188 } 189 190 return $info; 191 } 192 193 /** 194 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case 195 * encoding, zero padded to 31 digits. 196 * 197 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36 198 * fairly neatly. 199 * 200 * @param bool $recache 201 * @return bool|string False on failure 202 */ 203 public function getSha1Base36( $recache = false ) { 204 wfProfileIn( __METHOD__ ); 205 206 if ( $this->sha1Base36 !== null && !$recache ) { 207 wfProfileOut( __METHOD__ ); 208 209 return $this->sha1Base36; 210 } 211 212 wfSuppressWarnings(); 213 $this->sha1Base36 = sha1_file( $this->path ); 214 wfRestoreWarnings(); 215 216 if ( $this->sha1Base36 !== false ) { 217 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 ); 218 } 219 220 wfProfileOut( __METHOD__ ); 221 222 return $this->sha1Base36; 223 } 224 225 /** 226 * Get the final file extension from a file system path 227 * 228 * @param string $path 229 * @return string 230 */ 231 public static function extensionFromPath( $path ) { 232 $i = strrpos( $path, '.' ); 233 234 return strtolower( $i ? substr( $path, $i + 1 ) : '' ); 235 } 236 237 /** 238 * Get an associative array containing information about a file in the local filesystem. 239 * 240 * @param string $path Absolute local filesystem path 241 * @param string|bool $ext The file extension, or true to extract it from the filename. 242 * Set it to false to ignore the extension. 243 * @return array 244 */ 245 public static function getPropsFromPath( $path, $ext = true ) { 246 $fsFile = new self( $path ); 247 248 return $fsFile->getProps( $ext ); 249 } 250 251 /** 252 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case 253 * encoding, zero padded to 31 digits. 254 * 255 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36 256 * fairly neatly. 257 * 258 * @param string $path 259 * @return bool|string False on failure 260 */ 261 public static function getSha1Base36FromPath( $path ) { 262 $fsFile = new self( $path ); 263 264 return $fsFile->getSha1Base36(); 265 } 266 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |