Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[lhc/web/wiklou.git] / includes / filerepo / backend / 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 protected $canDelete = false; // bool; garbage collect the temp file
32
33 /** @var Array of active temp files to purge on shutdown */
34 protected static $instances = array();
35
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.
39 *
40 * @param $prefix string
41 * @param $extension string
42 * @return TempFSFile|null
43 */
44 public static function factory( $prefix, $extension = '' ) {
45 $base = wfTempDir() . '/' . $prefix . dechex( mt_rand( 0, 99999999 ) );
46 $ext = ( $extension != '' ) ? ".{$extension}" : "";
47 for ( $attempt = 1; true; $attempt++ ) {
48 $path = "{$base}-{$attempt}{$ext}";
49 wfSuppressWarnings();
50 $newFileHandle = fopen( $path, 'x' );
51 wfRestoreWarnings();
52 if ( $newFileHandle ) {
53 fclose( $newFileHandle );
54 break; // got it
55 }
56 if ( $attempt >= 15 ) {
57 return null; // give up
58 }
59 }
60 $tmpFile = new self( $path );
61 $tmpFile->canDelete = true; // safely instantiated
62 return $tmpFile;
63 }
64
65 /**
66 * Purge this file off the file system
67 *
68 * @return bool Success
69 */
70 public function purge() {
71 $this->canDelete = false; // done
72 wfSuppressWarnings();
73 $ok = unlink( $this->path );
74 wfRestoreWarnings();
75 return $ok;
76 }
77
78 /**
79 * Clean up the temporary file only after an object goes out of scope
80 *
81 * @param $object Object
82 * @return void
83 */
84 public function bind( $object ) {
85 if ( is_object( $object ) ) {
86 $object->tempFSFileReferences[] = $this;
87 }
88 }
89
90 /**
91 * Set flag to not clean up after the temporary file
92 *
93 * @return void
94 */
95 public function preserve() {
96 $this->canDelete = false;
97 }
98
99 /**
100 * Cleans up after the temporary file by deleting it
101 */
102 function __destruct() {
103 if ( $this->canDelete ) {
104 wfSuppressWarnings();
105 unlink( $this->path );
106 wfRestoreWarnings();
107 }
108 }
109 }