38 parent::__construct(
$path );
40 if ( self::$pathsCollect ===
null ) {
41 self::$pathsCollect = [];
42 register_shutdown_function( [ __CLASS__,
'purgeAllOnShutdown' ] );
55 public static function factory( $prefix, $extension =
'', $tmpDirectory =
null ) {
56 $ext = ( $extension !=
'' ) ?
".{$extension}" :
'';
59 while ( $attempts-- ) {
60 $hex = sprintf(
'%06x%06x', mt_rand( 0, 0xffffff ), mt_rand( 0, 0xffffff ) );
61 if ( !is_string( $tmpDirectory ) ) {
65 MediaWiki\suppressWarnings();
66 $newFileHandle = fopen(
$path,
'x' );
67 MediaWiki\restoreWarnings();
68 if ( $newFileHandle ) {
69 fclose( $newFileHandle );
70 $tmpFile =
new self(
$path );
71 $tmpFile->autocollect();
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 ) ) {
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 ) ) {
105 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
110 throw new RuntimeException(
111 'No writable temporary directory could be found. ' .
112 'Please explicitly specify a writable directory in configuration.' );
121 $this->canDelete =
false;
122 MediaWiki\suppressWarnings();
123 $ok = unlink( $this->path );
124 MediaWiki\restoreWarnings();
126 unset( self::$pathsCollect[$this->path] );
137 public function bind( $object ) {
138 if ( is_object( $object ) ) {
139 if ( !isset( $object->tempFSFileReferences ) ) {
141 $object->tempFSFileReferences = [];
143 $object->tempFSFileReferences[] = $this;
155 $this->canDelete =
false;
157 unset( self::$pathsCollect[$this->path] );
168 $this->canDelete =
true;
181 foreach ( self::$pathsCollect
as $path ) {
182 MediaWiki\suppressWarnings();
184 MediaWiki\restoreWarnings();
192 if ( $this->canDelete ) {