MediaWiki master
TempFSFile.php
Go to the documentation of this file.
1<?php
2
12
13use RuntimeException;
14use WeakMap;
15
22class TempFSFile extends FSFile {
24 protected $canDelete = false;
25
27 protected static $pathsCollect = null;
28
35 private static $references;
36
42 public function __construct( $path ) {
43 parent::__construct( $path );
44
45 if ( self::$pathsCollect === null ) {
46 // @codeCoverageIgnoreStart
47 self::$pathsCollect = [];
48 register_shutdown_function( self::purgeAllOnShutdown( ... ) );
49 // @codeCoverageIgnoreEnd
50 }
51 }
52
64 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
65 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
66 }
67
75 public static function getUsableTempDirectory() {
76 $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
77 $tmpDir[] = sys_get_temp_dir();
78 $tmpDir[] = ini_get( 'upload_tmp_dir' );
79 foreach ( $tmpDir as $tmp ) {
80 if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
81 return $tmp;
82 }
83 }
84
85 // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
86 // it so create a directory within that called 'mwtmp' with a suffix of the user running
87 // the current process.
88 // The user is included as if various scripts are run by different users they will likely
89 // not be able to access each others temporary files.
90 if ( PHP_OS_FAMILY === 'Windows' ) {
91 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp-' . get_current_user();
92 if ( !is_dir( $tmp ) ) {
93 mkdir( $tmp );
94 }
95 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
96 return $tmp;
97 }
98 }
99
100 throw new RuntimeException(
101 'No writable temporary directory could be found. ' .
102 'Please explicitly specify a writable directory in configuration.' );
103 }
104
110 public function purge() {
111 $this->canDelete = false; // done
112 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
113 $ok = @unlink( $this->path );
114
115 unset( self::$pathsCollect[$this->path] );
116
117 return $ok;
118 }
119
126 public function bind( $object ) {
127 if ( is_object( $object ) ) {
128 // Use a WeakMap to avoid dynamic property creation (T324894)
129 if ( self::$references === null ) {
130 self::$references = new WeakMap;
131 }
132 self::$references[$object] = $this;
133 }
134
135 return $this;
136 }
137
143 public function preserve() {
144 $this->canDelete = false;
145
146 unset( self::$pathsCollect[$this->path] );
147
148 return $this;
149 }
150
156 public function autocollect() {
157 $this->canDelete = true;
158
159 self::$pathsCollect[$this->path] = 1;
160
161 return $this;
162 }
163
171 public static function purgeAllOnShutdown() {
172 foreach ( self::$pathsCollect as $path => $unused ) {
173 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
174 @unlink( $path );
175 }
176 }
177
181 public function __destruct() {
182 if ( $this->canDelete ) {
183 $this->purge();
184 }
185 }
186}
187
189class_alias( TempFSFile::class, 'TempFSFile' );
Class representing a non-directory file on the file system.
Definition FSFile.php:20
string $path
Path to file.
Definition FSFile.php:22
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.