Debian Codegraph @

includes/libs/filebackend/fsfile/TempFSFile.php

raw symbols SCIP
1 <?php
2
3 /**
4 * Location holder of files stored temporarily
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup FileBackend
23 */
24
25 namespace Wikimedia\FileBackend\FSFile;
26
27 use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
28 use RuntimeException;
29 use WeakMap;
30 use Wikimedia\AtEase\AtEase;
31
32 /**
33 * This class is used to hold the location and do limited manipulation
34 * of files stored temporarily (this will be whatever wfTempDir() returns)
35 *
36 * @ingroup FileBackend
37 */
38 class TempFSFile extends FSFile {
39 /** @var bool Garbage collect the temp file */
40 protected $canDelete = false;
41
42 /** @var array Map of (path => 1) for paths to delete on shutdown */
43 protected static $pathsCollect = null;
44
45 /**
46 * A WeakMap where the key is an object which depends on the file, and the
47 * value is a TempFSFile responsible for deleting the file. This keeps each
48 * TempFSFile alive until all associated objects have been destroyed.
49 * @var WeakMap|null
50 */
51 private static $references;
52
53 /**
54 * Do not call directly. Use TempFSFileFactory
55 *
56 * @param string $path
57 */
58 public function __construct( $path ) {
59 parent::__construct( $path );
60
61 if ( self::$pathsCollect === null ) {
62 // @codeCoverageIgnoreStart
63 self::$pathsCollect = [];
64 register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
65 // @codeCoverageIgnoreEnd
66 }
67 }
68
69 /**
70 * Make a new temporary file on the file system.
71 * Temporary files may be purged when the file object falls out of scope.
72 *
73 * @deprecated since 1.34, use TempFSFileFactory directly
74 *
75 * @param string $prefix
76 * @param string $extension Optional file extension
77 * @param string|null $tmpDirectory Optional parent directory
78 * @return TempFSFile|null
79 */
80 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
81 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
82 }
83
84 /**
85 * @todo Is there any useful way to test this? Would it be useful to make this non-static on
86 * TempFSFileFactory?
87 *
88 * @return string Filesystem path to a temporary directory
89 * @throws RuntimeException if no writable temporary directory can be found
90 */
91 public static function getUsableTempDirectory() {
92 $tmpDir = array_map( 'getenv', [ 'TMPDIR', 'TMP', 'TEMP' ] );
93 $tmpDir[] = sys_get_temp_dir();
94 $tmpDir[] = ini_get( 'upload_tmp_dir' );
95 foreach ( $tmpDir as $tmp ) {
96 if ( $tmp != '' && is_dir( $tmp ) && is_writable( $tmp ) ) {
97 return $tmp;
98 }
99 }
100
101 // PHP on Windows will detect C:\Windows\Temp as not writable even though PHP can write to
102 // it so create a directory within that called 'mwtmp' with a suffix of the user running
103 // the current process.
104 // The user is included as if various scripts are run by different users they will likely
105 // not be able to access each others temporary files.
106 if ( PHP_OS_FAMILY === 'Windows' ) {
107 $tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mwtmp-' . get_current_user();
108 if ( !is_dir( $tmp ) ) {
109 mkdir( $tmp );
110 }
111 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
112 return $tmp;
113 }
114 }
115
116 throw new RuntimeException(
117 'No writable temporary directory could be found. ' .
118 'Please explicitly specify a writable directory in configuration.' );
119 }
120
121 /**
122 * Purge this file off the file system
123 *
124 * @return bool Success
125 */
126 public function purge() {
127 $this->canDelete = false; // done
128 AtEase::suppressWarnings();
129 $ok = unlink( $this->path );
130 AtEase::restoreWarnings();
131
132 unset( self::$pathsCollect[$this->path] );
133
134 return $ok;
135 }
136
137 /**
138 * Clean up the temporary file only after an object goes out of scope
139 *
140 * @param mixed $object
141 * @return TempFSFile This object
142 */
143 public function bind( $object ) {
144 if ( is_object( $object ) ) {
145 // Use a WeakMap to avoid dynamic property creation (T324894)
146 if ( self::$references === null ) {
147 self::$references = new WeakMap;
148 }
149 self::$references[$object] = $this;
150 }
151
152 return $this;
153 }
154
155 /**
156 * Set flag to not clean up after the temporary file
157 *
158 * @return TempFSFile This object
159 */
160 public function preserve() {
161 $this->canDelete = false;
162
163 unset( self::$pathsCollect[$this->path] );
164
165 return $this;
166 }
167
168 /**
169 * Set flag clean up after the temporary file
170 *
171 * @return TempFSFile This object
172 */
173 public function autocollect() {
174 $this->canDelete = true;
175
176 self::$pathsCollect[$this->path] = 1;
177
178 return $this;
179 }
180
181 /**
182 * Try to make sure that all files are purged on error
183 *
184 * This method should only be called internally
185 *
186 * @codeCoverageIgnore
187 */
188 public static function purgeAllOnShutdown() {
189 foreach ( self::$pathsCollect as $path => $unused ) {
190 AtEase::suppressWarnings();
191 unlink( $path );
192 AtEase::restoreWarnings();
193 }
194 }
195
196 /**
197 * Cleans up after the temporary file by deleting it
198 */
199 public function __destruct() {
200 if ( $this->canDelete ) {
201 $this->purge();
202 }
203 }
204 }
205
206 /** @deprecated class alias since 1.43 */
207 class_alias( TempFSFile::class, 'TempFSFile' );