MediaWiki master
TempFSFile.php
Go to the documentation of this file.
1<?php
2
12
13use RuntimeException;
14use WeakMap;
15use Wikimedia\AtEase\AtEase;
16
23class TempFSFile extends FSFile {
25 protected $canDelete = false;
26
28 protected static $pathsCollect = null;
29
36 private static $references;
37
43 public function __construct( $path ) {
44 parent::__construct( $path );
45
46 if ( self::$pathsCollect === null ) {
47 // @codeCoverageIgnoreStart
48 self::$pathsCollect = [];
49 register_shutdown_function( self::purgeAllOnShutdown( ... ) );
50 // @codeCoverageIgnoreEnd
51 }
52 }
53
65 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
66 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
67 }
68
76 public static function getUsableTempDirectory() {
77 $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
78 $tmpDir[] = sys_get_temp_dir();
79 $tmpDir[] = ini_get( 'upload_tmp_dir' );
80 foreach ( $tmpDir as $tmp ) {
81 if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
82 return $tmp;
83 }
84 }
85
86 // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
87 // it so create a directory within that called 'mwtmp' with a suffix of the user running
88 // the current process.
89 // The user is included as if various scripts are run by different users they will likely
90 // not be able to access each others temporary files.
91 if ( PHP_OS_FAMILY === 'Windows' ) {
92 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp-' . get_current_user();
93 if ( !is_dir( $tmp ) ) {
94 mkdir( $tmp );
95 }
96 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
97 return $tmp;
98 }
99 }
100
101 throw new RuntimeException(
102 'No writable temporary directory could be found. ' .
103 'Please explicitly specify a writable directory in configuration.' );
104 }
105
111 public function purge() {
112 $this->canDelete = false; // done
113 AtEase::suppressWarnings();
114 $ok = unlink( $this->path );
115 AtEase::restoreWarnings();
116
117 unset( self::$pathsCollect[$this->path] );
118
119 return $ok;
120 }
121
128 public function bind( $object ) {
129 if ( is_object( $object ) ) {
130 // Use a WeakMap to avoid dynamic property creation (T324894)
131 if ( self::$references === null ) {
132 self::$references = new WeakMap;
133 }
134 self::$references[$object] = $this;
135 }
136
137 return $this;
138 }
139
145 public function preserve() {
146 $this->canDelete = false;
147
148 unset( self::$pathsCollect[$this->path] );
149
150 return $this;
151 }
152
158 public function autocollect() {
159 $this->canDelete = true;
160
161 self::$pathsCollect[$this->path] = 1;
162
163 return $this;
164 }
165
173 public static function purgeAllOnShutdown() {
174 foreach ( self::$pathsCollect as $path => $unused ) {
175 AtEase::suppressWarnings();
176 unlink( $path );
177 AtEase::restoreWarnings();
178 }
179 }
180
184 public function __destruct() {
185 if ( $this->canDelete ) {
186 $this->purge();
187 }
188 }
189}
190
192class_alias( TempFSFile::class, 'TempFSFile' );
Class representing a non-directory file on the file system.
Definition FSFile.php:21
string $path
Path to file.
Definition FSFile.php:23
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
purge()
Purge this file off the file system.
bool $canDelete
Garbage collect the temp file.
__construct( $path)
Do not call directly.
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 purgeAllOnShutdown()
Try to make sure that all files are purged on error.
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
bind( $object)
Clean up the temporary file only after an object goes out of scope.
preserve()
Set flag to not clean up after the temporary file.