Merge "Add MessagesBi.php"
[lhc/web/wiklou.git] / includes / libs / filebackend / fsfile / TempFSFile.php
1 <?php
2 /**
3 * Location holder of files stored temporarily
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 */
23
24 /**
25 * This class is used to hold the location and do limited manipulation
26 * of files stored temporarily (this will be whatever wfTempDir() returns)
27 *
28 * @ingroup FileBackend
29 */
30 class TempFSFile extends FSFile {
31 /** @var bool Garbage collect the temp file */
32 protected $canDelete = false;
33
34 /** @var array Map of (path => 1) for paths to delete on shutdown */
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
46 /**
47 * Make a new temporary file on the file system.
48 * Temporary files may be purged when the file object falls out of scope.
49 *
50 * @param string $prefix
51 * @param string $extension Optional file extension
52 * @param string|null $tmpDirectory Optional parent directory
53 * @return TempFSFile|null
54 */
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 = $tmpDirectory . '/' . $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
81 /**
82 * @return string Filesystem path to a temporary directory
83 * @throws RuntimeException
84 */
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
115 /**
116 * Purge this file off the file system
117 *
118 * @return bool Success
119 */
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
131 /**
132 * Clean up the temporary file only after an object goes out of scope
133 *
134 * @param object $object
135 * @return TempFSFile This object
136 */
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
149 /**
150 * Set flag to not clean up after the temporary file
151 *
152 * @return TempFSFile This object
153 */
154 public function preserve() {
155 $this->canDelete = false;
156
157 unset( self::$pathsCollect[$this->path] );
158
159 return $this;
160 }
161
162 /**
163 * Set flag clean up after the temporary file
164 *
165 * @return TempFSFile This object
166 */
167 public function autocollect() {
168 $this->canDelete = true;
169
170 self::$pathsCollect[$this->path] = 1;
171
172 return $this;
173 }
174
175 /**
176 * Try to make sure that all files are purged on error
177 *
178 * This method should only be called internally
179 */
180 public static function purgeAllOnShutdown() {
181 foreach ( self::$pathsCollect as $path ) {
182 Wikimedia\suppressWarnings();
183 unlink( $path );
184 Wikimedia\restoreWarnings();
185 }
186 }
187
188 /**
189 * Cleans up after the temporary file by deleting it
190 */
191 function __destruct() {
192 if ( $this->canDelete ) {
193 $this->purge();
194 }
195 }
196 }