MediaWiki  REL1_21
FSFile.php
Go to the documentation of this file.
00001 <?php
00029 class FSFile {
00030         protected $path; // path to file
00031         private $sha1Base36 = null; // File Sha1Base36
00032 
00039         public function __construct( $path ) {
00040                 if ( FileBackend::isStoragePath( $path ) ) {
00041                         throw new MWException( __METHOD__ . " given storage path `$path`." );
00042                 }
00043                 $this->path = $path;
00044         }
00045 
00051         public function getPath() {
00052                 return $this->path;
00053         }
00054 
00060         public function exists() {
00061                 return is_file( $this->path );
00062         }
00063 
00069         public function getSize() {
00070                 return filesize( $this->path );
00071         }
00072 
00078         public function getTimestamp() {
00079                 wfSuppressWarnings();
00080                 $timestamp = filemtime( $this->path );
00081                 wfRestoreWarnings();
00082                 if ( $timestamp !== false ) {
00083                         $timestamp = wfTimestamp( TS_MW, $timestamp );
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                 return $info;
00151         }
00152 
00158         public static function placeholderProps() {
00159                 $info = array();
00160                 $info['fileExists'] = false;
00161                 $info['mime'] = null;
00162                 $info['media_type'] = MEDIATYPE_UNKNOWN;
00163                 $info['metadata'] = '';
00164                 $info['sha1'] = '';
00165                 $info['width'] = 0;
00166                 $info['height'] = 0;
00167                 $info['bits'] = 0;
00168                 return $info;
00169         }
00170 
00177         protected function extractImageSizeInfo( array $gis ) {
00178                 $info = array();
00179                 # NOTE: $gis[2] contains a code for the image type. This is no longer used.
00180                 $info['width'] = $gis[0];
00181                 $info['height'] = $gis[1];
00182                 if ( isset( $gis['bits'] ) ) {
00183                         $info['bits'] = $gis['bits'];
00184                 } else {
00185                         $info['bits'] = 0;
00186                 }
00187                 return $info;
00188         }
00189 
00200         public function getSha1Base36( $recache = false ) {
00201                 wfProfileIn( __METHOD__ );
00202 
00203                 if ( $this->sha1Base36 !== null && !$recache ) {
00204                         wfProfileOut( __METHOD__ );
00205                         return $this->sha1Base36;
00206                 }
00207 
00208                 wfSuppressWarnings();
00209                 $this->sha1Base36 = sha1_file( $this->path );
00210                 wfRestoreWarnings();
00211 
00212                 if ( $this->sha1Base36 !== false ) {
00213                         $this->sha1Base36 = wfBaseConvert( $this->sha1Base36, 16, 36, 31 );
00214                 }
00215 
00216                 wfProfileOut( __METHOD__ );
00217                 return $this->sha1Base36;
00218         }
00219 
00226         public static function extensionFromPath( $path ) {
00227                 $i = strrpos( $path, '.' );
00228                 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
00229         }
00230 
00240         public static function getPropsFromPath( $path, $ext = true ) {
00241                 $fsFile = new self( $path );
00242                 return $fsFile->getProps( $ext );
00243         }
00244 
00257         public static function getSha1Base36FromPath( $path, $recache = false ) {
00258                 static $sha1Base36 = array();
00259 
00260                 if ( !isset( $sha1Base36[$path] ) || $recache ) {
00261                         $fsFile = new self( $path );
00262                         $sha1Base36[$path] = $fsFile->getSha1Base36();
00263                 }
00264 
00265                 return $sha1Base36[$path];
00266         }
00267 }