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