MediaWiki  REL1_23
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 
00045     public static function factory( $prefix, $extension = '' ) {
00046         wfProfileIn( __METHOD__ );
00047         $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
00048         $ext = ( $extension != '' ) ? ".{$extension}" : "";
00049         for ( $attempt = 1; true; $attempt++ ) {
00050             $path = "{$base}-{$attempt}{$ext}";
00051             wfSuppressWarnings();
00052             $newFileHandle = fopen( $path, 'x' );
00053             wfRestoreWarnings();
00054             if ( $newFileHandle ) {
00055                 fclose( $newFileHandle );
00056                 break; // got it
00057             }
00058             if ( $attempt >= 5 ) {
00059                 wfProfileOut( __METHOD__ );
00060 
00061                 return null; // give up
00062             }
00063         }
00064         $tmpFile = new self( $path );
00065         $tmpFile->canDelete = true; // safely instantiated
00066         wfProfileOut( __METHOD__ );
00067 
00068         return $tmpFile;
00069     }
00070 
00076     public function purge() {
00077         $this->canDelete = false; // done
00078         wfSuppressWarnings();
00079         $ok = unlink( $this->path );
00080         wfRestoreWarnings();
00081 
00082         return $ok;
00083     }
00084 
00091     public function bind( $object ) {
00092         if ( is_object( $object ) ) {
00093             if ( !isset( $object->tempFSFileReferences ) ) {
00094                 // Init first since $object might use __get() and return only a copy variable
00095                 $object->tempFSFileReferences = array();
00096             }
00097             $object->tempFSFileReferences[] = $this;
00098         }
00099 
00100         return $this;
00101     }
00102 
00108     public function preserve() {
00109         $this->canDelete = false;
00110 
00111         return $this;
00112     }
00113 
00119     public function autocollect() {
00120         $this->canDelete = true;
00121 
00122         return $this;
00123     }
00124 
00128     function __destruct() {
00129         if ( $this->canDelete ) {
00130             wfSuppressWarnings();
00131             unlink( $this->path );
00132             wfRestoreWarnings();
00133         }
00134     }
00135 }