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