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 
46  public function __construct( $path ) {
47  parent::__construct( $path );
48 
49  if ( self::$pathsCollect === null ) {
50  // @codeCoverageIgnoreStart
51  self::$pathsCollect = [];
52  register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
53  // @codeCoverageIgnoreEnd
54  }
55  }
56 
68  public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
69  return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
70  }
71 
79  public static function getUsableTempDirectory() {
80  $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
81  $tmpDir[] = sys_get_temp_dir();
82  $tmpDir[] = ini_get( 'upload_tmp_dir' );
83  foreach ( $tmpDir as $tmp ) {
84  if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
85  return $tmp;
86  }
87  }
88 
89  // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
90  // it so create a directory within that called 'mwtmp' with a suffix of the user running
91  // the current process.
92  // The user is included as if various scripts are run by different users they will likely
93  // not be able to access each others temporary files.
94  if ( PHP_OS_FAMILY === 'Windows' ) {
95  $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp-' . get_current_user();
96  if ( !is_dir( $tmp ) ) {
97  mkdir( $tmp );
98  }
99  if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
100  return $tmp;
101  }
102  }
103 
104  throw new RuntimeException(
105  'No writable temporary directory could be found. ' .
106  'Please explicitly specify a writable directory in configuration.' );
107  }
108 
114  public function purge() {
115  $this->canDelete = false; // done
116  AtEase::suppressWarnings();
117  $ok = unlink( $this->path );
118  AtEase::restoreWarnings();
119 
120  unset( self::$pathsCollect[$this->path] );
121 
122  return $ok;
123  }
124 
131  public function bind( $object ) {
132  if ( is_object( $object ) ) {
133  if ( !isset( $object->tempFSFileReferences ) ) {
134  // Init first since $object might use __get() and return only a copy variable
135  $object->tempFSFileReferences = [];
136  }
137  $object->tempFSFileReferences[] = $this;
138  }
139 
140  return $this;
141  }
142 
148  public function preserve() {
149  $this->canDelete = false;
150 
151  unset( self::$pathsCollect[$this->path] );
152 
153  return $this;
154  }
155 
161  public function autocollect() {
162  $this->canDelete = true;
163 
164  self::$pathsCollect[$this->path] = 1;
165 
166  return $this;
167  }
168 
176  public static function purgeAllOnShutdown() {
177  foreach ( self::$pathsCollect as $path => $unused ) {
178  AtEase::suppressWarnings();
179  unlink( $path );
180  AtEase::restoreWarnings();
181  }
182  }
183 
187  public function __destruct() {
188  if ( $this->canDelete ) {
189  $this->purge();
190  }
191  }
192 }
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:46
bind( $object)
Clean up the temporary file only after an object goes out of scope.
Definition: TempFSFile.php:131
autocollect()
Set flag clean up after the temporary file.
Definition: TempFSFile.php:161
__destruct()
Cleans up after the temporary file by deleting it.
Definition: TempFSFile.php:187
purge()
Purge this file off the file system.
Definition: TempFSFile.php:114
preserve()
Set flag to not clean up after the temporary file.
Definition: TempFSFile.php:148
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:68
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
Definition: TempFSFile.php:176
static getUsableTempDirectory()
Definition: TempFSFile.php:79
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
Definition: TempFSFile.php:39