MediaWiki
REL1_20
|
00001 <?php 00030 class TempFSFile extends FSFile { 00031 protected $canDelete = false; // bool; garbage collect the temp file 00032 00034 protected static $instances = array(); 00035 00044 public static function factory( $prefix, $extension = '' ) { 00045 wfProfileIn( __METHOD__ ); 00046 $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 ); 00047 $ext = ( $extension != '' ) ? ".{$extension}" : ""; 00048 for ( $attempt = 1; true; $attempt++ ) { 00049 $path = "{$base}-{$attempt}{$ext}"; 00050 wfSuppressWarnings(); 00051 $newFileHandle = fopen( $path, 'x' ); 00052 wfRestoreWarnings(); 00053 if ( $newFileHandle ) { 00054 fclose( $newFileHandle ); 00055 break; // got it 00056 } 00057 if ( $attempt >= 5 ) { 00058 wfProfileOut( __METHOD__ ); 00059 return null; // give up 00060 } 00061 } 00062 $tmpFile = new self( $path ); 00063 $tmpFile->canDelete = true; // safely instantiated 00064 wfProfileOut( __METHOD__ ); 00065 return $tmpFile; 00066 } 00067 00073 public function purge() { 00074 $this->canDelete = false; // done 00075 wfSuppressWarnings(); 00076 $ok = unlink( $this->path ); 00077 wfRestoreWarnings(); 00078 return $ok; 00079 } 00080 00087 public function bind( $object ) { 00088 if ( is_object( $object ) ) { 00089 $object->tempFSFileReferences[] = $this; 00090 } 00091 } 00092 00098 public function preserve() { 00099 $this->canDelete = false; 00100 } 00101 00107 public function autocollect() { 00108 $this->canDelete = true; 00109 } 00110 00114 function __destruct() { 00115 if ( $this->canDelete ) { 00116 wfSuppressWarnings(); 00117 unlink( $this->path ); 00118 wfRestoreWarnings(); 00119 } 00120 } 00121 }