MediaWiki
REL1_20
|
00001 <?php 00029 class FSFile { 00030 protected $path; // path to file 00031 00038 public function __construct( $path ) { 00039 if ( FileBackend::isStoragePath( $path ) ) { 00040 throw new MWException( __METHOD__ . " given storage path `$path`." ); 00041 } 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 return $timestamp; 00085 } 00086 00092 public function getMimeType() { 00093 return MimeMagic::singleton()->guessMimeType( $this->path, false ); 00094 } 00095 00105 public function getProps( $ext = true ) { 00106 wfProfileIn( __METHOD__ ); 00107 wfDebug( __METHOD__.": Getting file info for $this->path\n" ); 00108 00109 $info = self::placeholderProps(); 00110 $info['fileExists'] = $this->exists(); 00111 00112 if ( $info['fileExists'] ) { 00113 $magic = MimeMagic::singleton(); 00114 00115 # get the file extension 00116 if ( $ext === true ) { 00117 $ext = self::extensionFromPath( $this->path ); 00118 } 00119 00120 # mime type according to file contents 00121 $info['file-mime'] = $this->getMimeType(); 00122 # logical mime type 00123 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext ); 00124 00125 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] ); 00126 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] ); 00127 00128 # Get size in bytes 00129 $info['size'] = $this->getSize(); 00130 00131 # Height, width and metadata 00132 $handler = MediaHandler::getHandler( $info['mime'] ); 00133 if ( $handler ) { 00134 $tempImage = (object)array(); 00135 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path ); 00136 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] ); 00137 if ( is_array( $gis ) ) { 00138 $info = $this->extractImageSizeInfo( $gis ) + $info; 00139 } 00140 } 00141 $info['sha1'] = $this->getSha1Base36(); 00142 00143 wfDebug(__METHOD__.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n"); 00144 } else { 00145 wfDebug(__METHOD__.": $this->path NOT FOUND!\n"); 00146 } 00147 00148 wfProfileOut( __METHOD__ ); 00149 return $info; 00150 } 00151 00157 public static function placeholderProps() { 00158 $info = array(); 00159 $info['fileExists'] = false; 00160 $info['mime'] = null; 00161 $info['media_type'] = MEDIATYPE_UNKNOWN; 00162 $info['metadata'] = ''; 00163 $info['sha1'] = ''; 00164 $info['width'] = 0; 00165 $info['height'] = 0; 00166 $info['bits'] = 0; 00167 return $info; 00168 } 00169 00176 protected function extractImageSizeInfo( array $gis ) { 00177 $info = array(); 00178 # NOTE: $gis[2] contains a code for the image type. This is no longer used. 00179 $info['width'] = $gis[0]; 00180 $info['height'] = $gis[1]; 00181 if ( isset( $gis['bits'] ) ) { 00182 $info['bits'] = $gis['bits']; 00183 } else { 00184 $info['bits'] = 0; 00185 } 00186 return $info; 00187 } 00188 00198 public function getSha1Base36() { 00199 wfProfileIn( __METHOD__ ); 00200 00201 wfSuppressWarnings(); 00202 $hash = sha1_file( $this->path ); 00203 wfRestoreWarnings(); 00204 if ( $hash !== false ) { 00205 $hash = wfBaseConvert( $hash, 16, 36, 31 ); 00206 } 00207 00208 wfProfileOut( __METHOD__ ); 00209 return $hash; 00210 } 00211 00218 public static function extensionFromPath( $path ) { 00219 $i = strrpos( $path, '.' ); 00220 return strtolower( $i ? substr( $path, $i + 1 ) : '' ); 00221 } 00222 00232 public static function getPropsFromPath( $path, $ext = true ) { 00233 $fsFile = new self( $path ); 00234 return $fsFile->getProps( $ext ); 00235 } 00236 00248 public static function getSha1Base36FromPath( $path ) { 00249 $fsFile = new self( $path ); 00250 return $fsFile->getSha1Base36(); 00251 } 00252 }