38 parent::__construct(
$path );
40 if ( self::$pathsCollect === null ) {
41 self::$pathsCollect = [];
42 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
55 public static function factory( $prefix, $extension =
'', $tmpDirectory = null ) {
56 $ext = ( $extension !=
'' ) ?
".{$extension}" :
'';
59 while ( $attempts-- ) {
60 $hex = sprintf(
'%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) );
61 if ( !is_string( $tmpDirectory ) ) {
62 $tmpDirectory = self::getUsableTempDirectory();
65 MediaWiki\suppressWarnings();
66 $newFileHandle = fopen(
$path,
'x' );
67 MediaWiki\restoreWarnings();
68 if ( $newFileHandle ) {
69 fclose( $newFileHandle );
70 $tmpFile =
new self(
$path );
71 $tmpFile->autocollect();
86 $tmpDir = array_map(
'getenv', [
'TMPDIR',
'TMP',
'TEMP' ] );
87 $tmpDir[] = sys_get_temp_dir();
88 $tmpDir[] = ini_get(
'upload_tmp_dir' );
89 foreach ( $tmpDir
as $tmp ) {
90 if ( $tmp !=
'' && is_dir( $tmp ) && is_writable( $tmp ) ) {
100 if ( strtoupper( substr( PHP_OS, 0, 3 ) ) ===
'WIN' ) {
101 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
'mwtmp-' . get_current_user();
102 if ( !file_exists( $tmp ) ) {
105 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
111 'No writable temporary directory could be found. ' .
112 'Please explicitly specify a writable directory in configuration.' );
121 $this->canDelete =
false;
122 MediaWiki\suppressWarnings();
123 $ok = unlink( $this->path );
124 MediaWiki\restoreWarnings();
126 unset( self::$pathsCollect[$this->path] );
137 public function bind( $object ) {
138 if ( is_object( $object ) ) {
139 if ( !isset( $object->tempFSFileReferences ) ) {
141 $object->tempFSFileReferences = [];
143 $object->tempFSFileReferences[] = $this;
155 $this->canDelete =
false;
157 unset( self::$pathsCollect[$this->path] );
168 $this->canDelete =
true;
181 foreach ( self::$pathsCollect
as $path ) {
182 MediaWiki\suppressWarnings();
184 MediaWiki\restoreWarnings();
192 if ( $this->canDelete ) {
string $path
Path to file.
bool $canDelete
Garbage collect the temp file.
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
autocollect()
Set flag clean up after the temporary file.
__destruct()
Cleans up after the temporary file by deleting it.
static factory($prefix, $extension= '', $tmpDirectory=null)
Make a new temporary file on the file system.
static getUsableTempDirectory()
wfTempDir()
Tries to get the system directory for temporary files.
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
preserve()
Set flag to not clean up after the temporary file.
purge()
Purge this file off the file system.
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
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
Class representing a non-directory file on the file system.
bind($object)
Clean up the temporary file only after an object goes out of scope.