MediaWiki  1.23.2
FSFile.php
Go to the documentation of this file.
1 <?php
29 class FSFile {
31  protected $path;
32 
34  protected $sha1Base36;
35 
41  public function __construct( $path ) {
42  $this->path = $path;
43  }
44 
50  public function getPath() {
51  return $this->path;
52  }
53 
59  public function exists() {
60  return is_file( $this->path );
61  }
62 
68  public function getSize() {
69  return filesize( $this->path );
70  }
71 
77  public function getTimestamp() {
79  $timestamp = filemtime( $this->path );
81  if ( $timestamp !== false ) {
83  }
84 
85  return $timestamp;
86  }
87 
93  public function getMimeType() {
94  return MimeMagic::singleton()->guessMimeType( $this->path, false );
95  }
96 
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 
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 
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 
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 
213  $this->sha1Base36 = sha1_file( $this->path );
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 
231  public static function extensionFromPath( $path ) {
232  $i = strrpos( $path, '.' );
233 
234  return strtolower( $i ? substr( $path, $i + 1 ) : '' );
235  }
236 
245  public static function getPropsFromPath( $path, $ext = true ) {
246  $fsFile = new self( $path );
247 
248  return $fsFile->getProps( $ext );
249  }
250 
261  public static function getSha1Base36FromPath( $path ) {
262  $fsFile = new self( $path );
263 
264  return $fsFile->getSha1Base36();
265  }
266 }
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
FSFile\getPropsFromPath
static getPropsFromPath( $path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition: FSFile.php:243
$timestamp
if( $limit) $timestamp
Definition: importImages.php:104
FSFile\getProps
getProps( $ext=true)
Get an associative array containing information about a file with the given storage path.
Definition: FSFile.php:104
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2387
MEDIATYPE_UNKNOWN
const MEDIATYPE_UNKNOWN
Definition: Defines.php:123
FSFile\getMimeType
getMimeType()
Guess the MIME type from the file contents alone.
Definition: FSFile.php:91
File\splitMime
static splitMime( $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition: File.php:249
FSFile\$path
string $path
Path to file *.
Definition: FSFile.php:30
FSFile\placeholderProps
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition: FSFile.php:157
FSFile\__construct
__construct( $path)
Sets up the file object.
Definition: FSFile.php:39
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2417
FSFile\getSha1Base36FromPath
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition: FSFile.php:259
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
FSFile\extractImageSizeInfo
extractImageSizeInfo(array $gis)
Exract image size information.
Definition: FSFile.php:177
FSFile\exists
exists()
Checks if the file exists.
Definition: FSFile.php:57
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FSFile\getSha1Base36
getSha1Base36( $recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition: FSFile.php:201
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
FSFile\getSize
getSize()
Get the file size in bytes.
Definition: FSFile.php:66
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
FSFile
Class representing a non-directory file on the file system.
Definition: FSFile.php:29
FSFile\getTimestamp
getTimestamp()
Get the file's last-modified timestamp.
Definition: FSFile.php:75
$ext
$ext
Definition: NoLocalSettings.php:34
wfBaseConvert
wfBaseConvert( $input, $sourceBase, $destBase, $pad=1, $lowercase=true, $engine='auto')
Convert an arbitrarily-long digit string from one numeric base to another, optionally zero-padding to...
Definition: GlobalFunctions.php:3368
MediaHandler\getHandler
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Definition: MediaHandler.php:48
FSFile\getPath
getPath()
Returns the file system path.
Definition: FSFile.php:48
FSFile\$sha1Base36
string $sha1Base36
File SHA-1 in base 36 *.
Definition: FSFile.php:32
FSFile\extensionFromPath
static extensionFromPath( $path)
Get the final file extension from a file system path.
Definition: FSFile.php:229