3 * Location holder of files stored temporarily
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.
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.
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
21 * @ingroup FileBackend
25 * This class is used to hold the location and do limited manipulation
26 * of files stored temporarily (this will be whatever wfTempDir() returns)
28 * @ingroup FileBackend
30 class TempFSFile
extends FSFile
{
31 /** @var bool Garbage collect the temp file */
32 protected $canDelete = false;
34 /** @var array Active temp files to purge on shutdown */
35 protected static $instances = array();
37 /** @var array Map of (path => 1) for paths to delete on shutdown */
38 protected static $pathsCollect = null;
40 public function __construct( $path ) {
41 parent
::__construct( $path );
43 if ( self
::$pathsCollect === null ) {
44 self
::$pathsCollect = array();
45 register_shutdown_function( array( __CLASS__
, 'purgeAllOnShutdown' ) );
50 * Make a new temporary file on the file system.
51 * Temporary files may be purged when the file object falls out of scope.
53 * @param string $prefix
54 * @param string $extension
55 * @return TempFSFile|null
57 public static function factory( $prefix, $extension = '' ) {
58 $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
59 $ext = ( $extension != '' ) ?
".{$extension}" : "";
60 for ( $attempt = 1; true; $attempt++
) {
61 $path = "{$base}-{$attempt}{$ext}";
62 MediaWiki\
suppressWarnings();
63 $newFileHandle = fopen( $path, 'x' );
64 MediaWiki\restoreWarnings
();
65 if ( $newFileHandle ) {
66 fclose( $newFileHandle );
69 if ( $attempt >= 5 ) {
70 return null; // give up
73 $tmpFile = new self( $path );
74 $tmpFile->autocollect(); // safely instantiated
80 * Purge this file off the file system
82 * @return bool Success
84 public function purge() {
85 $this->canDelete
= false; // done
86 MediaWiki\
suppressWarnings();
87 $ok = unlink( $this->path
);
88 MediaWiki\restoreWarnings
();
90 unset( self
::$pathsCollect[$this->path
] );
96 * Clean up the temporary file only after an object goes out of scope
98 * @param object $object
99 * @return TempFSFile This object
101 public function bind( $object ) {
102 if ( is_object( $object ) ) {
103 if ( !isset( $object->tempFSFileReferences
) ) {
104 // Init first since $object might use __get() and return only a copy variable
105 $object->tempFSFileReferences
= array();
107 $object->tempFSFileReferences
[] = $this;
114 * Set flag to not clean up after the temporary file
116 * @return TempFSFile This object
118 public function preserve() {
119 $this->canDelete
= false;
121 unset( self
::$pathsCollect[$this->path
] );
127 * Set flag clean up after the temporary file
129 * @return TempFSFile This object
131 public function autocollect() {
132 $this->canDelete
= true;
134 self
::$pathsCollect[$this->path
] = 1;
140 * Try to make sure that all files are purged on error
142 * This method should only be called internally
144 public static function purgeAllOnShutdown() {
145 foreach ( self
::$pathsCollect as $path ) {
146 MediaWiki\
suppressWarnings();
148 MediaWiki\restoreWarnings
();
153 * Cleans up after the temporary file by deleting it
155 function __destruct() {
156 if ( $this->canDelete
) {