Merge "deferred: make DeferredUpdates::attemptUpdate() use callback owners for $fname...
[lhc/web/wiklou.git] / includes / libs / filebackend / fsfile / TempFSFile.php
1 <?php
2
3 use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
4
5 /**
6 * Location holder of files stored temporarily
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup FileBackend
25 */
26
27 /**
28 * This class is used to hold the location and do limited manipulation
29 * of files stored temporarily (this will be whatever wfTempDir() returns)
30 *
31 * @ingroup FileBackend
32 */
33 class TempFSFile extends FSFile {
34 /** @var bool Garbage collect the temp file */
35 protected $canDelete = false;
36
37 /** @var array Map of (path => 1) for paths to delete on shutdown */
38 protected static $pathsCollect = null;
39
40 /**
41 * Do not call directly. Use TempFSFileFactory.
42 */
43 public function __construct( $path ) {
44 parent::__construct( $path );
45
46 if ( self::$pathsCollect === null ) {
47 // @codeCoverageIgnoreStart
48 self::$pathsCollect = [];
49 register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
50 // @codeCoverageIgnoreEnd
51 }
52 }
53
54 /**
55 * Make a new temporary file on the file system.
56 * Temporary files may be purged when the file object falls out of scope.
57 *
58 * @deprecated since 1.34, use TempFSFileFactory directly
59 *
60 * @param string $prefix
61 * @param string $extension Optional file extension
62 * @param string|null $tmpDirectory Optional parent directory
63 * @return TempFSFile|null
64 */
65 public static function factory( $prefix, $extension = '', $tmpDirectory = null ) {
66 return ( new TempFSFileFactory( $tmpDirectory ) )->newTempFSFile( $prefix, $extension );
67 }
68
69 /**
70 * @todo Is there any useful way to test this? Would it be useful to make this non-static on
71 * TempFSFileFactory?
72 *
73 * @return string Filesystem path to a temporary directory
74 * @throws RuntimeException if no writable temporary directory can be found
75 */
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 ) ) {
82 return $tmp;
83 }
84 }
85
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 ) ) {
94 mkdir( $tmp );
95 }
96 if ( is_dir( $tmp ) && is_writable( $tmp ) ) {
97 return $tmp;
98 }
99 }
100
101 throw new RuntimeException(
102 'No writable temporary directory could be found. ' .
103 'Please explicitly specify a writable directory in configuration.' );
104 }
105
106 /**
107 * Purge this file off the file system
108 *
109 * @return bool Success
110 */
111 public function purge() {
112 $this->canDelete = false; // done
113 Wikimedia\suppressWarnings();
114 $ok = unlink( $this->path );
115 Wikimedia\restoreWarnings();
116
117 unset( self::$pathsCollect[$this->path] );
118
119 return $ok;
120 }
121
122 /**
123 * Clean up the temporary file only after an object goes out of scope
124 *
125 * @param object $object
126 * @return TempFSFile This object
127 */
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 = [];
133 }
134 $object->tempFSFileReferences[] = $this;
135 }
136
137 return $this;
138 }
139
140 /**
141 * Set flag to not clean up after the temporary file
142 *
143 * @return TempFSFile This object
144 */
145 public function preserve() {
146 $this->canDelete = false;
147
148 unset( self::$pathsCollect[$this->path] );
149
150 return $this;
151 }
152
153 /**
154 * Set flag clean up after the temporary file
155 *
156 * @return TempFSFile This object
157 */
158 public function autocollect() {
159 $this->canDelete = true;
160
161 self::$pathsCollect[$this->path] = 1;
162
163 return $this;
164 }
165
166 /**
167 * Try to make sure that all files are purged on error
168 *
169 * This method should only be called internally
170 *
171 * @codeCoverageIgnore
172 */
173 public static function purgeAllOnShutdown() {
174 foreach ( self::$pathsCollect as $path => $unused ) {
175 Wikimedia\suppressWarnings();
176 unlink( $path );
177 Wikimedia\restoreWarnings();
178 }
179 }
180
181 /**
182 * Cleans up after the temporary file by deleting it
183 */
184 function __destruct() {
185 if ( $this->canDelete ) {
186 $this->purge();
187 }
188 }
189 }