MediaWiki
REL1_24
|
00001 <?php 00029 class FSFile { 00031 protected $path; 00032 00034 protected $sha1Base36; 00035 00041 public function __construct( $path ) { 00042 $this->path = $path; 00043 } 00044 00050 public function getPath() { 00051 return $this->path; 00052 } 00053 00059 public function exists() { 00060 return is_file( $this->path ); 00061 } 00062 00068 public function getSize() { 00069 return filesize( $this->path ); 00070 } 00071 00077 public function getTimestamp() { 00078 wfSuppressWarnings(); 00079 $timestamp = filemtime( $this->path ); 00080 wfRestoreWarnings(); 00081 if ( $timestamp !== false ) { 00082 $timestamp = wfTimestamp( TS_MW, $timestamp ); 00083 } 00084 00085 return $timestamp; 00086 } 00087 00093 public function getMimeType() { 00094 return MimeMagic::singleton()->guessMimeType( $this->path, false ); 00095 } 00096 00106 public function getProps( $ext = true ) { 00107 wfProfileIn( __METHOD__ ); 00108 wfDebug( __METHOD__ . ": Getting file info for $this->path\n" ); 00109 00110 $info = self::placeholderProps(); 00111 $info['fileExists'] = $this->exists(); 00112 00113 if ( $info['fileExists'] ) { 00114 $magic = MimeMagic::singleton(); 00115 00116 # get the file extension 00117 if ( $ext === true ) { 00118 $ext = self::extensionFromPath( $this->path ); 00119 } 00120 00121 # MIME type according to file contents 00122 $info['file-mime'] = $this->getMimeType(); 00123 # logical MIME type 00124 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext ); 00125 00126 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] ); 00127 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] ); 00128 00129 # Get size in bytes 00130 $info['size'] = $this->getSize(); 00131 00132 # Height, width and metadata 00133 $handler = MediaHandler::getHandler( $info['mime'] ); 00134 if ( $handler ) { 00135 $tempImage = (object)array(); // XXX (hack for File object) 00136 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path ); 00137 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] ); 00138 if ( is_array( $gis ) ) { 00139 $info = $this->extractImageSizeInfo( $gis ) + $info; 00140 } 00141 } 00142 $info['sha1'] = $this->getSha1Base36(); 00143 00144 wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" ); 00145 } else { 00146 wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" ); 00147 } 00148 00149 wfProfileOut( __METHOD__ ); 00150 00151 return $info; 00152 } 00153 00159 public static function placeholderProps() { 00160 $info = array(); 00161 $info['fileExists'] = false; 00162 $info['mime'] = null; 00163 $info['media_type'] = MEDIATYPE_UNKNOWN; 00164 $info['metadata'] = ''; 00165 $info['sha1'] = ''; 00166 $info['width'] = 0; 00167 $info['height'] = 0; 00168 $info['bits'] = 0; 00169 00170 return $info; 00171 } 00172 00179 protected function extractImageSizeInfo( array $gis ) { 00180 $info = array(); 00181 # NOTE: $gis[2] contains a code for the image type. This is no longer used. 00182 $info['width'] = $gis[0]; 00183 $info['height'] = $gis[1]; 00184 if ( isset( $gis['bits'] ) ) { 00185 $info['bits'] = $gis['bits']; 00186 } else { 00187 $info['bits'] = 0; 00188 } 00189 00190 return $info; 00191 } 00192 00203 public function getSha1Base36( $recache = false ) { 00204 wfProfileIn( __METHOD__ ); 00205 00206 if ( $this->sha1Base36 !== null && !$recache ) { 00207 wfProfileOut( __METHOD__ ); 00208 00209 return $this->sha1Base36; 00210 } 00211 00212 wfSuppressWarnings(); 00213 $this->sha1Base36 = sha1_file( $this->path ); 00214 wfRestoreWarnings(); 00215 00216 if ( $this->sha1Base36 !== false ) { 00217 $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 ); 00218 } 00219 00220 wfProfileOut( __METHOD__ ); 00221 00222 return $this->sha1Base36; 00223 } 00224 00231 public static function extensionFromPath( $path ) { 00232 $i = strrpos( $path, '.' ); 00233 00234 return strtolower( $i ? substr( $path, $i + 1 ) : '' ); 00235 } 00236 00245 public static function getPropsFromPath( $path, $ext = true ) { 00246 $fsFile = new self( $path ); 00247 00248 return $fsFile->getProps( $ext ); 00249 } 00250 00261 public static function getSha1Base36FromPath( $path ) { 00262 $fsFile = new self( $path ); 00263 00264 return $fsFile->getSha1Base36(); 00265 } 00266 }