Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / filerepo / backend / TempFSFile.php
blob7843d6cd2a62285f50833ab08897e08c86e2dbc1
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 */
7 /**
8 * This class is used to hold the location and do limited manipulation
9 * of files stored temporarily (usually this will be $wgTmpDirectory)
11 * @ingroup FileBackend
13 class TempFSFile extends FSFile {
14 protected $canDelete = false; // bool; garbage collect the temp file
16 /** @var Array of active temp files to purge on shutdown */
17 protected static $instances = array();
19 /**
20 * Make a new temporary file on the file system.
21 * Temporary files may be purged when the file object falls out of scope.
23 * @param $prefix string
24 * @param $extension string
25 * @return TempFSFile|null
27 public static function factory( $prefix, $extension = '' ) {
28 $base = wfTempDir() . '/' . $prefix . dechex( mt_rand( 0, 99999999 ) );
29 $ext = ( $extension != '' ) ? ".{$extension}" : "";
30 for ( $attempt = 1; true; $attempt++ ) {
31 $path = "{$base}-{$attempt}{$ext}";
32 wfSuppressWarnings();
33 $newFileHandle = fopen( $path, 'x' );
34 wfRestoreWarnings();
35 if ( $newFileHandle ) {
36 fclose( $newFileHandle );
37 break; // got it
39 if ( $attempt >= 15 ) {
40 return null; // give up
43 $tmpFile = new self( $path );
44 $tmpFile->canDelete = true; // safely instantiated
45 return $tmpFile;
48 /**
49 * Purge this file off the file system
51 * @return bool Success
53 public function purge() {
54 $this->canDelete = false; // done
55 wfSuppressWarnings();
56 $ok = unlink( $this->path );
57 wfRestoreWarnings();
58 return $ok;
61 /**
62 * Clean up the temporary file only after an object goes out of scope
64 * @param $object Object
65 * @return void
67 public function bind( $object ) {
68 if ( is_object( $object ) ) {
69 $object->tempFSFileReferences[] = $this;
73 /**
74 * Set flag to not clean up after the temporary file
76 * @return void
78 public function preserve() {
79 $this->canDelete = false;
82 /**
83 * Cleans up after the temporary file by deleting it
85 function __destruct() {
86 if ( $this->canDelete ) {
87 wfSuppressWarnings();
88 unlink( $this->path );
89 wfRestoreWarnings();