MediaWiki  REL1_24
TempFSFile.php
Go to the documentation of this file.
00001 <?php
00030 class TempFSFile extends FSFile {
00032     protected $canDelete = false;
00033 
00035     protected static $instances = array();
00036 
00038     protected static $pathsCollect = null;
00039 
00040     public function __construct( $path ) {
00041         parent::__construct( $path );
00042 
00043         if ( self::$pathsCollect === null ) {
00044             self::$pathsCollect = array();
00045             register_shutdown_function( array( __CLASS__, 'purgeAllOnShutdown' ) );
00046         }
00047     }
00048 
00057     public static function factory( $prefix, $extension = '' ) {
00058         wfProfileIn( __METHOD__ );
00059         $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
00060         $ext = ( $extension != '' ) ? ".{$extension}" : "";
00061         for ( $attempt = 1; true; $attempt++ ) {
00062             $path = "{$base}-{$attempt}{$ext}";
00063             wfSuppressWarnings();
00064             $newFileHandle = fopen( $path, 'x' );
00065             wfRestoreWarnings();
00066             if ( $newFileHandle ) {
00067                 fclose( $newFileHandle );
00068                 break; // got it
00069             }
00070             if ( $attempt >= 5 ) {
00071                 wfProfileOut( __METHOD__ );
00072 
00073                 return null; // give up
00074             }
00075         }
00076         $tmpFile = new self( $path );
00077         $tmpFile->autocollect(); // safely instantiated
00078         wfProfileOut( __METHOD__ );
00079 
00080         return $tmpFile;
00081     }
00082 
00088     public function purge() {
00089         $this->canDelete = false; // done
00090         wfSuppressWarnings();
00091         $ok = unlink( $this->path );
00092         wfRestoreWarnings();
00093 
00094         unset( self::$pathsCollect[$this->path] );
00095 
00096         return $ok;
00097     }
00098 
00105     public function bind( $object ) {
00106         if ( is_object( $object ) ) {
00107             if ( !isset( $object->tempFSFileReferences ) ) {
00108                 // Init first since $object might use __get() and return only a copy variable
00109                 $object->tempFSFileReferences = array();
00110             }
00111             $object->tempFSFileReferences[] = $this;
00112         }
00113 
00114         return $this;
00115     }
00116 
00122     public function preserve() {
00123         $this->canDelete = false;
00124 
00125         unset( self::$pathsCollect[$this->path] );
00126 
00127         return $this;
00128     }
00129 
00135     public function autocollect() {
00136         $this->canDelete = true;
00137 
00138         self::$pathsCollect[$this->path] = 1;
00139 
00140         return $this;
00141     }
00142 
00148     public static function purgeAllOnShutdown() {
00149         foreach ( self::$pathsCollect as $path ) {
00150             wfSuppressWarnings();
00151             unlink( $path );
00152             wfRestoreWarnings();
00153         }
00154     }
00155 
00159     function __destruct() {
00160         if ( $this->canDelete ) {
00161             $this->purge();
00162         }
00163     }
00164 }