MediaWiki REL1_31
TempFSFile.php
Go to the documentation of this file.
1<?php
30class TempFSFile extends FSFile {
32 protected $canDelete = false;
33
35 protected static $pathsCollect = null;
36
37 public function __construct( $path ) {
38 parent::__construct( $path );
39
40 if ( self::$pathsCollect === null ) {
41 self::$pathsCollect = [];
42 register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
43 }
44 }
45
55 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
56 $ext = ( $extension != '' ) ? ".{$extension}" : '';
57
58 $attempts = 5;
59 while ( $attempts-- ) {
60 $hex = sprintf( '%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) );
61 if ( !is_string( $tmpDirectory ) ) {
62 $tmpDirectory = self::getUsableTempDirectory();
63 }
64 $path = wfTempDir() . '/' . $prefix . $hex . $ext;
65 Wikimedia\suppressWarnings();
66 $newFileHandle = fopen( $path, 'x' );
67 Wikimedia\restoreWarnings();
68 if ( $newFileHandle ) {
69 fclose( $newFileHandle );
70 $tmpFile = new self( $path );
71 $tmpFile->autocollect();
72 // Safely instantiated, end loop.
73 return $tmpFile;
74 }
75 }
76
77 // Give up
78 return null;
79 }
80
85 public static function getUsableTempDirectory() {
86 $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
87 $tmpDir[] = sys_get_temp_dir();
88 $tmpDir[] = ini_get( 'upload_tmp_dir' );
89 foreach ( $tmpDir as $tmp ) {
90 if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
91 return $tmp;
92 }
93 }
94
95 // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
96 // it so create a directory within that called 'mwtmp' with a suffix of the user running
97 // the current process.
98 // The user is included as if various scripts are run by different users they will likely
99 // not be able to access each others temporary files.
100 if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) {
101 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp-' . get_current_user();
102 if ( !file_exists( $tmp ) ) {
103 mkdir( $tmp );
104 }
105 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
106 return $tmp;
107 }
108 }
109
110 throw new RuntimeException(
111 'No writable temporary directory could be found. ' .
112 'Please explicitly specify a writable directory in configuration.' );
113 }
114
120 public function purge() {
121 $this->canDelete = false; // done
122 Wikimedia\suppressWarnings();
123 $ok = unlink( $this->path );
124 Wikimedia\restoreWarnings();
125
126 unset( self::$pathsCollect[$this->path] );
127
128 return $ok;
129 }
130
137 public function bind( $object ) {
138 if ( is_object( $object ) ) {
139 if ( !isset( $object->tempFSFileReferences ) ) {
140 // Init first since $object might use __get() and return only a copy variable
141 $object->tempFSFileReferences = [];
142 }
143 $object->tempFSFileReferences[] = $this;
144 }
145
146 return $this;
147 }
148
154 public function preserve() {
155 $this->canDelete = false;
156
157 unset( self::$pathsCollect[$this->path] );
158
159 return $this;
160 }
161
167 public function autocollect() {
168 $this->canDelete = true;
169
170 self::$pathsCollect[$this->path] = 1;
171
172 return $this;
173 }
174
180 public static function purgeAllOnShutdown() {
181 foreach ( self::$pathsCollect as $path ) {
182 Wikimedia\suppressWarnings();
183 unlink( $path );
184 Wikimedia\restoreWarnings();
185 }
186 }
187
191 function __destruct() {
192 if ( $this->canDelete ) {
193 $this->purge();
194 }
195 }
196}
wfTempDir()
Tries to get the system directory for temporary files.
Class representing a non-directory file on the file system.
Definition FSFile.php:29
string $path
Path to file.
Definition FSFile.php:31
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
__construct( $path)
Sets up the file object.
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.
if(!is_readable( $file)) $ext
Definition router.php:55