MediaWiki  1.30.0
TempFSFile.php
Go to the documentation of this file.
1 <?php
30 class TempFSFile extends FSFile {
32  protected $canDelete = false;
33 
35  protected static $pathsCollect = null;
36 
37  public function __construct( $path ) {
38  parent::__construct( $path );
39 
40  if ( self::$pathsCollect === null ) {
41  self::$pathsCollect = [];
42  register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
43  }
44  }
45 
55  public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
56  $ext = ( $extension != '' ) ? ".{$extension}" : '';
57 
58  $attempts = 5;
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();
63  }
64  $path = wfTempDir() . '/' . $prefix . $hex . $ext;
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();
72  // Safely instantiated, end loop.
73  return $tmpFile;
74  }
75  }
76 
77  // Give up
78  return null;
79  }
80 
85  public static function getUsableTempDirectory() {
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 ) ) {
91  return $tmp;
92  }
93  }
94 
95  // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
96  // it so create a directory within that called 'mwtmp' with a suffix of the user running
97  // the current process.
98  // The user is included as if various scripts are run by different users they will likely
99  // not be able to access each others temporary files.
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 ) ) {
103  mkdir( $tmp );
104  }
105  if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
106  return $tmp;
107  }
108  }
109 
110  throw new RuntimeException(
111  'No writable temporary directory could be found. ' .
112  'Please explicitly specify a writable directory in configuration.' );
113  }
114 
120  public function purge() {
121  $this->canDelete = false; // done
122  MediaWiki\suppressWarnings();
123  $ok = unlink( $this->path );
124  MediaWiki\restoreWarnings();
125 
126  unset( self::$pathsCollect[$this->path] );
127 
128  return $ok;
129  }
130 
137  public function bind( $object ) {
138  if ( is_object( $object ) ) {
139  if ( !isset( $object->tempFSFileReferences ) ) {
140  // Init first since $object might use __get() and return only a copy variable
141  $object->tempFSFileReferences = [];
142  }
143  $object->tempFSFileReferences[] = $this;
144  }
145 
146  return $this;
147  }
148 
154  public function preserve() {
155  $this->canDelete = false;
156 
157  unset( self::$pathsCollect[$this->path] );
158 
159  return $this;
160  }
161 
167  public function autocollect() {
168  $this->canDelete = true;
169 
170  self::$pathsCollect[$this->path] = 1;
171 
172  return $this;
173  }
174 
180  public static function purgeAllOnShutdown() {
181  foreach ( self::$pathsCollect as $path ) {
182  MediaWiki\suppressWarnings();
183  unlink( $path );
184  MediaWiki\restoreWarnings();
185  }
186  }
187 
191  function __destruct() {
192  if ( $this->canDelete ) {
193  $this->purge();
194  }
195  }
196 }
TempFSFile\$canDelete
bool $canDelete
Garbage collect the temp file.
Definition: TempFSFile.php:32
TempFSFile\purgeAllOnShutdown
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
Definition: TempFSFile.php:180
TempFSFile\purge
purge()
Purge this file off the file system.
Definition: TempFSFile.php:120
TempFSFile\__construct
__construct( $path)
Sets up the file object.
Definition: TempFSFile.php:37
php
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
FSFile\$path
string $path
Path to file.
Definition: FSFile.php:31
TempFSFile\bind
bind( $object)
Clean up the temporary file only after an object goes out of scope.
Definition: TempFSFile.php:137
TempFSFile\factory
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
Definition: TempFSFile.php:55
TempFSFile
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
Definition: TempFSFile.php:30
TempFSFile\$pathsCollect
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
Definition: TempFSFile.php:35
FSFile
Class representing a non-directory file on the file system.
Definition: FSFile.php:29
TempFSFile\getUsableTempDirectory
static getUsableTempDirectory()
Definition: TempFSFile.php:85
TempFSFile\autocollect
autocollect()
Set flag clean up after the temporary file.
Definition: TempFSFile.php:167
TempFSFile\__destruct
__destruct()
Cleans up after the temporary file by deleting it.
Definition: TempFSFile.php:191
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:2107
$ext
$ext
Definition: NoLocalSettings.php:25
as
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
Definition: distributors.txt:9
TempFSFile\preserve
preserve()
Set flag to not clean up after the temporary file.
Definition: TempFSFile.php:154
array
the array() calling protocol came about after MediaWiki 1.4rc1.