Merge changes I6d95c1b3,I780070e0
[lhc/web/wiklou.git] / includes / filebackend / 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 Active temp files to purge on shutdown */
35 protected static $instances = array();
36
37 /**
38 * Make a new temporary file on the file system.
39 * Temporary files may be purged when the file object falls out of scope.
40 *
41 * @param string $prefix
42 * @param string $extension
43 * @return TempFSFile|null
44 */
45 public static function factory( $prefix, $extension = '' ) {
46 wfProfileIn( __METHOD__ );
47 $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
48 $ext = ( $extension != '' ) ? ".{$extension}" : "";
49 for ( $attempt = 1; true; $attempt++ ) {
50 $path = "{$base}-{$attempt}{$ext}";
51 wfSuppressWarnings();
52 $newFileHandle = fopen( $path, 'x' );
53 wfRestoreWarnings();
54 if ( $newFileHandle ) {
55 fclose( $newFileHandle );
56 break; // got it
57 }
58 if ( $attempt >= 5 ) {
59 wfProfileOut( __METHOD__ );
60
61 return null; // give up
62 }
63 }
64 $tmpFile = new self( $path );
65 $tmpFile->canDelete = true; // safely instantiated
66 wfProfileOut( __METHOD__ );
67
68 return $tmpFile;
69 }
70
71 /**
72 * Purge this file off the file system
73 *
74 * @return bool Success
75 */
76 public function purge() {
77 $this->canDelete = false; // done
78 wfSuppressWarnings();
79 $ok = unlink( $this->path );
80 wfRestoreWarnings();
81
82 return $ok;
83 }
84
85 /**
86 * Clean up the temporary file only after an object goes out of scope
87 *
88 * @param stdClass $object
89 * @return TempFSFile This object
90 */
91 public function bind( $object ) {
92 if ( is_object( $object ) ) {
93 if ( !isset( $object->tempFSFileReferences ) ) {
94 // Init first since $object might use __get() and return only a copy variable
95 $object->tempFSFileReferences = array();
96 }
97 $object->tempFSFileReferences[] = $this;
98 }
99
100 return $this;
101 }
102
103 /**
104 * Set flag to not clean up after the temporary file
105 *
106 * @return TempFSFile This object
107 */
108 public function preserve() {
109 $this->canDelete = false;
110
111 return $this;
112 }
113
114 /**
115 * Set flag clean up after the temporary file
116 *
117 * @return TempFSFile This object
118 */
119 public function autocollect() {
120 $this->canDelete = true;
121
122 return $this;
123 }
124
125 /**
126 * Cleans up after the temporary file by deleting it
127 */
128 function __destruct() {
129 if ( $this->canDelete ) {
130 wfSuppressWarnings();
131 unlink( $this->path );
132 wfRestoreWarnings();
133 }
134 }
135 }