Revert "Use display name in category page subheadings if provided"
[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 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
52 * @return TempFSFile|null
53 */
54 public static function factory( $prefix, $extension = '' ) {
55 $ext = ( $extension != '' ) ? ".{$extension}" : '';
56
57 $attempts = 5;
58 while ( $attempts-- ) {
59 $path = wfTempDir() . '/' . $prefix . wfRandomString( 12 ) . $ext;
60 MediaWiki\suppressWarnings();
61 $newFileHandle = fopen( $path, 'x' );
62 MediaWiki\restoreWarnings();
63 if ( $newFileHandle ) {
64 fclose( $newFileHandle );
65 $tmpFile = new self( $path );
66 $tmpFile->autocollect();
67 // Safely instantiated, end loop.
68 return $tmpFile;
69 }
70 }
71
72 // Give up
73 return null;
74 }
75
76 /**
77 * Purge this file off the file system
78 *
79 * @return bool Success
80 */
81 public function purge() {
82 $this->canDelete = false; // done
83 MediaWiki\suppressWarnings();
84 $ok = unlink( $this->path );
85 MediaWiki\restoreWarnings();
86
87 unset( self::$pathsCollect[$this->path] );
88
89 return $ok;
90 }
91
92 /**
93 * Clean up the temporary file only after an object goes out of scope
94 *
95 * @param object $object
96 * @return TempFSFile This object
97 */
98 public function bind( $object ) {
99 if ( is_object( $object ) ) {
100 if ( !isset( $object->tempFSFileReferences ) ) {
101 // Init first since $object might use __get() and return only a copy variable
102 $object->tempFSFileReferences = [];
103 }
104 $object->tempFSFileReferences[] = $this;
105 }
106
107 return $this;
108 }
109
110 /**
111 * Set flag to not clean up after the temporary file
112 *
113 * @return TempFSFile This object
114 */
115 public function preserve() {
116 $this->canDelete = false;
117
118 unset( self::$pathsCollect[$this->path] );
119
120 return $this;
121 }
122
123 /**
124 * Set flag clean up after the temporary file
125 *
126 * @return TempFSFile This object
127 */
128 public function autocollect() {
129 $this->canDelete = true;
130
131 self::$pathsCollect[$this->path] = 1;
132
133 return $this;
134 }
135
136 /**
137 * Try to make sure that all files are purged on error
138 *
139 * This method should only be called internally
140 */
141 public static function purgeAllOnShutdown() {
142 foreach ( self::$pathsCollect as $path ) {
143 MediaWiki\suppressWarnings();
144 unlink( $path );
145 MediaWiki\restoreWarnings();
146 }
147 }
148
149 /**
150 * Cleans up after the temporary file by deleting it
151 */
152 function __destruct() {
153 if ( $this->canDelete ) {
154 $this->purge();
155 }
156 }
157 }