3 use MediaWiki\FileBackend\FSFile\TempFSFileFactory
;
6 * Location holder of files stored temporarily
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup FileBackend
28 * This class is used to hold the location and do limited manipulation
29 * of files stored temporarily (this will be whatever wfTempDir() returns)
31 * @ingroup FileBackend
33 class TempFSFile
extends FSFile
{
34 /** @var bool Garbage collect the temp file */
35 protected $canDelete = false;
37 /** @var array Map of (path => 1) for paths to delete on shutdown */
38 protected static $pathsCollect = null;
41 * Do not call directly. Use TempFSFileFactory.
43 public function __construct( $path ) {
44 parent
::__construct( $path );
46 if ( self
::$pathsCollect === null ) {
47 // @codeCoverageIgnoreStart
48 self
::$pathsCollect = [];
49 register_shutdown_function( [ __CLASS__
, 'purgeAllOnShutdown' ] );
50 // @codeCoverageIgnoreEnd
55 * Make a new temporary file on the file system.
56 * Temporary files may be purged when the file object falls out of scope.
58 * @deprecated since 1.34, use TempFSFileFactory directly
60 * @param string $prefix
61 * @param string $extension Optional file extension
62 * @param string|null $tmpDirectory Optional parent directory
63 * @return TempFSFile|null
65 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
66 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
70 * @todo Is there any useful way to test this? Would it be useful to make this non-static on
73 * @return string Filesystem path to a temporary directory
74 * @throws RuntimeException if no writable temporary directory can be found
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 ) ) {
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 ( strtoupper( substr( PHP_OS
, 0, 3 ) ) === 'WIN' ) {
92 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR
. 'mwtmp-' . get_current_user();
93 if ( !file_exists( $tmp ) ) {
96 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
101 throw new RuntimeException(
102 'No writable temporary directory could be found. ' .
103 'Please explicitly specify a writable directory in configuration.' );
107 * Purge this file off the file system
109 * @return bool Success
111 public function purge() {
112 $this->canDelete
= false; // done
113 Wikimedia\
suppressWarnings();
114 $ok = unlink( $this->path
);
115 Wikimedia\restoreWarnings
();
117 unset( self
::$pathsCollect[$this->path
] );
123 * Clean up the temporary file only after an object goes out of scope
125 * @param object $object
126 * @return TempFSFile This object
128 public function bind( $object ) {
129 if ( is_object( $object ) ) {
130 if ( !isset( $object->tempFSFileReferences
) ) {
131 // Init first since $object might use __get() and return only a copy variable
132 $object->tempFSFileReferences
= [];
134 $object->tempFSFileReferences
[] = $this;
141 * Set flag to not clean up after the temporary file
143 * @return TempFSFile This object
145 public function preserve() {
146 $this->canDelete
= false;
148 unset( self
::$pathsCollect[$this->path
] );
154 * Set flag clean up after the temporary file
156 * @return TempFSFile This object
158 public function autocollect() {
159 $this->canDelete
= true;
161 self
::$pathsCollect[$this->path
] = 1;
167 * Try to make sure that all files are purged on error
169 * This method should only be called internally
171 * @codeCoverageIgnore
173 public static function purgeAllOnShutdown() {
174 foreach ( self
::$pathsCollect as $path => $unused ) {
175 Wikimedia\
suppressWarnings();
177 Wikimedia\restoreWarnings
();
182 * Cleans up after the temporary file by deleting it
184 function __destruct() {
185 if ( $this->canDelete
) {