MediaWiki
REL1_19
|
00001 <?php 00012 class FSFile { 00013 protected $path; // path to file 00014 00020 public function __construct( $path ) { 00021 if ( FileBackend::isStoragePath( $path ) ) { 00022 throw new MWException( __METHOD__ . " given storage path `$path`." ); 00023 } 00024 $this->path = $path; 00025 } 00026 00032 public function getPath() { 00033 return $this->path; 00034 } 00035 00041 public function exists() { 00042 return is_file( $this->path ); 00043 } 00044 00050 public function getSize() { 00051 return filesize( $this->path ); 00052 } 00053 00059 public function getTimestamp() { 00060 wfSuppressWarnings(); 00061 $timestamp = filemtime( $this->path ); 00062 wfRestoreWarnings(); 00063 if ( $timestamp !== false ) { 00064 $timestamp = wfTimestamp( TS_MW, $timestamp ); 00065 } 00066 return $timestamp; 00067 } 00068 00074 public function getMimeType() { 00075 return MimeMagic::singleton()->guessMimeType( $this->path, false ); 00076 } 00077 00087 public function getProps( $ext = true ) { 00088 wfProfileIn( __METHOD__ ); 00089 wfDebug( __METHOD__.": Getting file info for $this->path\n" ); 00090 00091 $info = self::placeholderProps(); 00092 $info['fileExists'] = $this->exists(); 00093 00094 if ( $info['fileExists'] ) { 00095 $magic = MimeMagic::singleton(); 00096 00097 # get the file extension 00098 if ( $ext === true ) { 00099 $ext = self::extensionFromPath( $this->path ); 00100 } 00101 00102 # mime type according to file contents 00103 $info['file-mime'] = $this->getMimeType(); 00104 # logical mime type 00105 $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext ); 00106 00107 list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] ); 00108 $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] ); 00109 00110 # Get size in bytes 00111 $info['size'] = $this->getSize(); 00112 00113 # Height, width and metadata 00114 $handler = MediaHandler::getHandler( $info['mime'] ); 00115 if ( $handler ) { 00116 $tempImage = (object)array(); 00117 $info['metadata'] = $handler->getMetadata( $tempImage, $this->path ); 00118 $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] ); 00119 if ( is_array( $gis ) ) { 00120 $info = $this->extractImageSizeInfo( $gis ) + $info; 00121 } 00122 } 00123 $info['sha1'] = $this->getSha1Base36(); 00124 00125 wfDebug(__METHOD__.": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n"); 00126 } else { 00127 wfDebug(__METHOD__.": $this->path NOT FOUND!\n"); 00128 } 00129 00130 wfProfileOut( __METHOD__ ); 00131 return $info; 00132 } 00133 00139 public static function placeholderProps() { 00140 $info = array(); 00141 $info['fileExists'] = false; 00142 $info['mime'] = null; 00143 $info['media_type'] = MEDIATYPE_UNKNOWN; 00144 $info['metadata'] = ''; 00145 $info['sha1'] = ''; 00146 $info['width'] = 0; 00147 $info['height'] = 0; 00148 $info['bits'] = 0; 00149 return $info; 00150 } 00151 00157 protected function extractImageSizeInfo( array $gis ) { 00158 $info = array(); 00159 # NOTE: $gis[2] contains a code for the image type. This is no longer used. 00160 $info['width'] = $gis[0]; 00161 $info['height'] = $gis[1]; 00162 if ( isset( $gis['bits'] ) ) { 00163 $info['bits'] = $gis['bits']; 00164 } else { 00165 $info['bits'] = 0; 00166 } 00167 return $info; 00168 } 00169 00179 public function getSha1Base36() { 00180 wfProfileIn( __METHOD__ ); 00181 00182 wfSuppressWarnings(); 00183 $hash = sha1_file( $this->path ); 00184 wfRestoreWarnings(); 00185 if ( $hash !== false ) { 00186 $hash = wfBaseConvert( $hash, 16, 36, 31 ); 00187 } 00188 00189 wfProfileOut( __METHOD__ ); 00190 return $hash; 00191 } 00192 00199 public static function extensionFromPath( $path ) { 00200 $i = strrpos( $path, '.' ); 00201 return strtolower( $i ? substr( $path, $i + 1 ) : '' ); 00202 } 00203 00213 public static function getPropsFromPath( $path, $ext = true ) { 00214 $fsFile = new self( $path ); 00215 return $fsFile->getProps( $ext ); 00216 } 00217 00229 public static function getSha1Base36FromPath( $path ) { 00230 $fsFile = new self( $path ); 00231 return $fsFile->getSha1Base36(); 00232 } 00233 }