MediaWiki  master
TempFSFile.php
Go to the documentation of this file.
1 <?php
2 
26 use Wikimedia\AtEase\AtEase;
27 
34 class TempFSFile extends FSFile {
36  protected $canDelete = false;
37 
39  protected static $pathsCollect = null;
40 
47  private static $references;
48 
54  public function __construct( $path ) {
55  parent::__construct( $path );
56 
57  if ( self::$pathsCollect === null ) {
58  // @codeCoverageIgnoreStart
59  self::$pathsCollect = [];
60  register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
61  // @codeCoverageIgnoreEnd
62  }
63  }
64 
76  public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
77  return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
78  }
79 
87  public static function getUsableTempDirectory() {
88  $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
89  $tmpDir[] = sys_get_temp_dir();
90  $tmpDir[] = ini_get( 'upload_tmp_dir' );
91  foreach ( $tmpDir as $tmp ) {
92  if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
93  return $tmp;
94  }
95  }
96 
97  // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
98  // it so create a directory within that called 'mwtmp' with a suffix of the user running
99  // the current process.
100  // The user is included as if various scripts are run by different users they will likely
101  // not be able to access each others temporary files.
102  if ( PHP_OS_FAMILY === 'Windows' ) {
103  $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp-' . get_current_user();
104  if ( !is_dir( $tmp ) ) {
105  mkdir( $tmp );
106  }
107  if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
108  return $tmp;
109  }
110  }
111 
112  throw new RuntimeException(
113  'No writable temporary directory could be found. ' .
114  'Please explicitly specify a writable directory in configuration.' );
115  }
116 
122  public function purge() {
123  $this->canDelete = false; // done
124  AtEase::suppressWarnings();
125  $ok = unlink( $this->path );
126  AtEase::restoreWarnings();
127 
128  unset( self::$pathsCollect[$this->path] );
129 
130  return $ok;
131  }
132 
139  public function bind( $object ) {
140  if ( is_object( $object ) ) {
141  // Use a WeakMap on PHP >= 8.0 to avoid dynamic property creation (T324894)
142  if ( PHP_VERSION_ID >= 80000 ) {
143  if ( self::$references === null ) {
144  self::$references = new WeakMap;
145  }
146  self::$references[$object] = $this;
147  } else {
148  // PHP 7.4
149  if ( !isset( $object->tempFSFileReferences ) ) {
150  // Init first since $object might use __get() and return only a copy variable
151  $object->tempFSFileReferences = [];
152  }
153  $object->tempFSFileReferences[] = $this;
154  }
155  }
156 
157  return $this;
158  }
159 
165  public function preserve() {
166  $this->canDelete = false;
167 
168  unset( self::$pathsCollect[$this->path] );
169 
170  return $this;
171  }
172 
178  public function autocollect() {
179  $this->canDelete = true;
180 
181  self::$pathsCollect[$this->path] = 1;
182 
183  return $this;
184  }
185 
193  public static function purgeAllOnShutdown() {
194  foreach ( self::$pathsCollect as $path => $unused ) {
195  AtEase::suppressWarnings();
196  unlink( $path );
197  AtEase::restoreWarnings();
198  }
199  }
200 
204  public function __destruct() {
205  if ( $this->canDelete ) {
206  $this->purge();
207  }
208  }
209 }
Class representing a non-directory file on the file system.
Definition: FSFile.php:32
string $path
Path to file.
Definition: FSFile.php:34
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
Definition: TempFSFile.php:34
__construct( $path)
Do not call directly.
Definition: TempFSFile.php:54
bind( $object)
Clean up the temporary file only after an object goes out of scope.
Definition: TempFSFile.php:139
autocollect()
Set flag clean up after the temporary file.
Definition: TempFSFile.php:178
__destruct()
Cleans up after the temporary file by deleting it.
Definition: TempFSFile.php:204
purge()
Purge this file off the file system.
Definition: TempFSFile.php:122
preserve()
Set flag to not clean up after the temporary file.
Definition: TempFSFile.php:165
bool $canDelete
Garbage collect the temp file.
Definition: TempFSFile.php:36
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
Definition: TempFSFile.php:76
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
Definition: TempFSFile.php:193
static getUsableTempDirectory()
Definition: TempFSFile.php:87
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
Definition: TempFSFile.php:39