Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / filebackend / TempFSFile.php
blob11e125c11f8deecb15807aa04df9d566375548c7
1 <?php
2 /**
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
20 * @file
21 * @ingroup FileBackend
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)
28 * @ingroup FileBackend
30 class TempFSFile extends FSFile {
31 protected $canDelete = false; // bool; garbage collect the temp file
33 /** @var Array of active temp files to purge on shutdown */
34 protected static $instances = array();
36 /**
37 * Make a new temporary file on the file system.
38 * Temporary files may be purged when the file object falls out of scope.
40 * @param $prefix string
41 * @param $extension string
42 * @return TempFSFile|null
44 public static function factory( $prefix, $extension = '' ) {
45 wfProfileIn( __METHOD__ );
46 $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
47 $ext = ( $extension != '' ) ? ".{$extension}" : "";
48 for ( $attempt = 1; true; $attempt++ ) {
49 $path = "{$base}-{$attempt}{$ext}";
50 wfSuppressWarnings();
51 $newFileHandle = fopen( $path, 'x' );
52 wfRestoreWarnings();
53 if ( $newFileHandle ) {
54 fclose( $newFileHandle );
55 break; // got it
57 if ( $attempt >= 5 ) {
58 wfProfileOut( __METHOD__ );
59 return null; // give up
62 $tmpFile = new self( $path );
63 $tmpFile->canDelete = true; // safely instantiated
64 wfProfileOut( __METHOD__ );
65 return $tmpFile;
68 /**
69 * Purge this file off the file system
71 * @return bool Success
73 public function purge() {
74 $this->canDelete = false; // done
75 wfSuppressWarnings();
76 $ok = unlink( $this->path );
77 wfRestoreWarnings();
78 return $ok;
81 /**
82 * Clean up the temporary file only after an object goes out of scope
84 * @param $object Object
85 * @return TempFSFile This object
87 public function bind( $object ) {
88 if ( is_object( $object ) ) {
89 if ( !isset( $object->tempFSFileReferences ) ) {
90 // Init first since $object might use __get() and return only a copy variable
91 $object->tempFSFileReferences = array();
93 $object->tempFSFileReferences[] = $this;
95 return $this;
98 /**
99 * Set flag to not clean up after the temporary file
101 * @return TempFSFile This object
103 public function preserve() {
104 $this->canDelete = false;
105 return $this;
109 * Set flag clean up after the temporary file
111 * @return TempFSFile This object
113 public function autocollect() {
114 $this->canDelete = true;
115 return $this;
119 * Cleans up after the temporary file by deleting it
121 function __destruct() {
122 if ( $this->canDelete ) {
123 wfSuppressWarnings();
124 unlink( $this->path );
125 wfRestoreWarnings();