MediaWiki  1.27.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() {
78  MediaWiki\suppressWarnings();
79  $timestamp = filemtime( $this->path );
80  MediaWiki\restoreWarnings();
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 
119  public function getProps( $ext = true ) {
120  wfDebug( __METHOD__ . ": Getting file info for $this->path\n" );
121 
122  $info = self::placeholderProps();
123  $info['fileExists'] = $this->exists();
124 
125  if ( $info['fileExists'] ) {
126  $magic = MimeMagic::singleton();
127 
128  # get the file extension
129  if ( $ext === true ) {
130  $ext = self::extensionFromPath( $this->path );
131  }
132 
133  # MIME type according to file contents
134  $info['file-mime'] = $this->getMimeType();
135  # logical MIME type
136  $info['mime'] = $magic->improveTypeFromExtension( $info['file-mime'], $ext );
137 
138  list( $info['major_mime'], $info['minor_mime'] ) = File::splitMime( $info['mime'] );
139  $info['media_type'] = $magic->getMediaType( $this->path, $info['mime'] );
140 
141  # Get size in bytes
142  $info['size'] = $this->getSize();
143 
144  # Height, width and metadata
145  $handler = MediaHandler::getHandler( $info['mime'] );
146  if ( $handler ) {
147  $tempImage = (object)[]; // XXX (hack for File object)
148  $info['metadata'] = $handler->getMetadata( $tempImage, $this->path );
149  $gis = $handler->getImageSize( $tempImage, $this->path, $info['metadata'] );
150  if ( is_array( $gis ) ) {
151  $info = $this->extractImageSizeInfo( $gis ) + $info;
152  }
153  }
154  $info['sha1'] = $this->getSha1Base36();
155 
156  wfDebug( __METHOD__ . ": $this->path loaded, {$info['size']} bytes, {$info['mime']}.\n" );
157  } else {
158  wfDebug( __METHOD__ . ": $this->path NOT FOUND!\n" );
159  }
160 
161  return $info;
162  }
163 
179  public static function placeholderProps() {
180  $info = [];
181  $info['fileExists'] = false;
182  $info['mime'] = null;
183  $info['media_type'] = MEDIATYPE_UNKNOWN;
184  $info['metadata'] = '';
185  $info['sha1'] = '';
186  $info['width'] = 0;
187  $info['height'] = 0;
188  $info['bits'] = 0;
189 
190  return $info;
191  }
192 
199  protected function extractImageSizeInfo( array $gis ) {
200  $info = [];
201  # NOTE: $gis[2] contains a code for the image type. This is no longer used.
202  $info['width'] = $gis[0];
203  $info['height'] = $gis[1];
204  if ( isset( $gis['bits'] ) ) {
205  $info['bits'] = $gis['bits'];
206  } else {
207  $info['bits'] = 0;
208  }
209 
210  return $info;
211  }
212 
223  public function getSha1Base36( $recache = false ) {
224  if ( $this->sha1Base36 !== null && !$recache ) {
225  return $this->sha1Base36;
226  }
227 
228  MediaWiki\suppressWarnings();
229  $this->sha1Base36 = sha1_file( $this->path );
230  MediaWiki\restoreWarnings();
231 
232  if ( $this->sha1Base36 !== false ) {
233  $this->sha1Base36 = Wikimedia\base_convert( $this->sha1Base36, 16, 36, 31 );
234  }
235 
236  return $this->sha1Base36;
237  }
238 
245  public static function extensionFromPath( $path ) {
246  $i = strrpos( $path, '.' );
247 
248  return strtolower( $i ? substr( $path, $i + 1 ) : '' );
249  }
250 
259  public static function getPropsFromPath( $path, $ext = true ) {
260  $fsFile = new self( $path );
261 
262  return $fsFile->getProps( $ext );
263  }
264 
275  public static function getSha1Base36FromPath( $path ) {
276  $fsFile = new self( $path );
277 
278  return $fsFile->getSha1Base36();
279  }
280 }
getMimeType()
Guess the MIME type from the file contents alone.
Definition: FSFile.php:93
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
the array() calling protocol came about after MediaWiki 1.4rc1.
__construct($path)
Sets up the file object.
Definition: FSFile.php:41
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:272
getPath()
Returns the file system path.
Definition: FSFile.php:50
string $sha1Base36
File SHA-1 in base 36.
Definition: FSFile.php:34
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:366
string $path
Path to file.
Definition: FSFile.php:31
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
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static extensionFromPath($path)
Get the final file extension from a file system path.
Definition: FSFile.php:245
if($limit) $timestamp
const MEDIATYPE_UNKNOWN
Definition: Defines.php:113
getProps($ext=true)
Get an associative array containing information about a file with the given storage path...
Definition: FSFile.php:119
static placeholderProps()
Placeholder file properties to use for files that don't exist.
Definition: FSFile.php:179
static getSha1Base36FromPath($path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding, zero padded to 31 digits.
Definition: FSFile.php:275
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
extractImageSizeInfo(array $gis)
Exract image size information.
Definition: FSFile.php:199
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
exists()
Checks if the file exists.
Definition: FSFile.php:59
Class representing a non-directory file on the file system.
Definition: FSFile.php:29
getSize()
Get the file size in bytes.
Definition: FSFile.php:68
getSha1Base36($recache=false)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding, zero padded to 31 digits.
Definition: FSFile.php:223
static getHandler($type)
Get a MediaHandler for a given MIME type from the instance cache.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:762
getTimestamp()
Get the file's last-modified timestamp.
Definition: FSFile.php:77
static getPropsFromPath($path, $ext=true)
Get an associative array containing information about a file in the local filesystem.
Definition: FSFile.php:259