MediaWiki  REL1_19
TempFSFile.php
Go to the documentation of this file.
00001 <?php
00013 class TempFSFile extends FSFile {
00014         protected $canDelete = false; // bool; garbage collect the temp file
00015 
00017         protected static $instances = array();
00018 
00027         public static function factory( $prefix, $extension = '' ) {
00028                 $base = wfTempDir() . '/' . $prefix . dechex( mt_rand( 0, 99999999 ) );
00029                 $ext = ( $extension != '' ) ? ".{$extension}" : "";
00030                 for ( $attempt = 1; true; $attempt++ ) {
00031                         $path = "{$base}-{$attempt}{$ext}";
00032                         wfSuppressWarnings();
00033                         $newFileHandle = fopen( $path, 'x' );
00034                         wfRestoreWarnings();
00035                         if ( $newFileHandle ) {
00036                                 fclose( $newFileHandle );
00037                                 break; // got it
00038                         }
00039                         if ( $attempt >= 15 ) {
00040                                 return null; // give up
00041                         }
00042                 }
00043                 $tmpFile = new self( $path );
00044                 $tmpFile->canDelete = true; // safely instantiated
00045                 return $tmpFile;
00046         }
00047 
00053         public function purge() {
00054                 $this->canDelete = false; // done
00055                 wfSuppressWarnings();
00056                 $ok = unlink( $this->path );
00057                 wfRestoreWarnings();
00058                 return $ok;
00059         }
00060 
00067         public function bind( $object ) {
00068                 if ( is_object( $object ) ) {
00069                         $object->tempFSFileReferences[] = $this;
00070                 }
00071         }
00072 
00078         public function preserve() {
00079                 $this->canDelete = false;
00080         }
00081 
00085         function __destruct() {
00086                 if ( $this->canDelete ) {
00087                         wfSuppressWarnings();
00088                         unlink( $this->path );
00089                         wfRestoreWarnings();
00090                 }
00091         }
00092 }