[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/filebackend/ -> TempFSFile.php (source)

   1  <?php
   2  /**
   3   * Location holder of files stored temporarily
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   * @ingroup FileBackend
  22   */
  23  
  24  /**
  25   * This class is used to hold the location and do limited manipulation
  26   * of files stored temporarily (this will be whatever wfTempDir() returns)
  27   *
  28   * @ingroup FileBackend
  29   */
  30  class TempFSFile extends FSFile {
  31      /** @var bool Garbage collect the temp file */
  32      protected $canDelete = false;
  33  
  34      /** @var array Active temp files to purge on shutdown */
  35      protected static $instances = array();
  36  
  37      /** @var array Map of (path => 1) for paths to delete on shutdown */
  38      protected static $pathsCollect = null;
  39  
  40  	public function __construct( $path ) {
  41          parent::__construct( $path );
  42  
  43          if ( self::$pathsCollect === null ) {
  44              self::$pathsCollect = array();
  45              register_shutdown_function( array( __CLASS__, 'purgeAllOnShutdown' ) );
  46          }
  47      }
  48  
  49      /**
  50       * Make a new temporary file on the file system.
  51       * Temporary files may be purged when the file object falls out of scope.
  52       *
  53       * @param string $prefix
  54       * @param string $extension
  55       * @return TempFSFile|null
  56       */
  57  	public static function factory( $prefix, $extension = '' ) {
  58          wfProfileIn( __METHOD__ );
  59          $base = wfTempDir() . '/' . $prefix . wfRandomString( 12 );
  60          $ext = ( $extension != '' ) ? ".{$extension}" : "";
  61          for ( $attempt = 1; true; $attempt++ ) {
  62              $path = "{$base}-{$attempt}{$ext}";
  63              wfSuppressWarnings();
  64              $newFileHandle = fopen( $path, 'x' );
  65              wfRestoreWarnings();
  66              if ( $newFileHandle ) {
  67                  fclose( $newFileHandle );
  68                  break; // got it
  69              }
  70              if ( $attempt >= 5 ) {
  71                  wfProfileOut( __METHOD__ );
  72  
  73                  return null; // give up
  74              }
  75          }
  76          $tmpFile = new self( $path );
  77          $tmpFile->autocollect(); // safely instantiated
  78          wfProfileOut( __METHOD__ );
  79  
  80          return $tmpFile;
  81      }
  82  
  83      /**
  84       * Purge this file off the file system
  85       *
  86       * @return bool Success
  87       */
  88  	public function purge() {
  89          $this->canDelete = false; // done
  90          wfSuppressWarnings();
  91          $ok = unlink( $this->path );
  92          wfRestoreWarnings();
  93  
  94          unset( self::$pathsCollect[$this->path] );
  95  
  96          return $ok;
  97      }
  98  
  99      /**
 100       * Clean up the temporary file only after an object goes out of scope
 101       *
 102       * @param stdClass $object
 103       * @return TempFSFile This object
 104       */
 105  	public function bind( $object ) {
 106          if ( is_object( $object ) ) {
 107              if ( !isset( $object->tempFSFileReferences ) ) {
 108                  // Init first since $object might use __get() and return only a copy variable
 109                  $object->tempFSFileReferences = array();
 110              }
 111              $object->tempFSFileReferences[] = $this;
 112          }
 113  
 114          return $this;
 115      }
 116  
 117      /**
 118       * Set flag to not clean up after the temporary file
 119       *
 120       * @return TempFSFile This object
 121       */
 122  	public function preserve() {
 123          $this->canDelete = false;
 124  
 125          unset( self::$pathsCollect[$this->path] );
 126  
 127          return $this;
 128      }
 129  
 130      /**
 131       * Set flag clean up after the temporary file
 132       *
 133       * @return TempFSFile This object
 134       */
 135  	public function autocollect() {
 136          $this->canDelete = true;
 137  
 138          self::$pathsCollect[$this->path] = 1;
 139  
 140          return $this;
 141      }
 142  
 143      /**
 144       * Try to make sure that all files are purged on error
 145       *
 146       * This method should only be called internally
 147       */
 148  	public static function purgeAllOnShutdown() {
 149          foreach ( self::$pathsCollect as $path ) {
 150              wfSuppressWarnings();
 151              unlink( $path );
 152              wfRestoreWarnings();
 153          }
 154      }
 155  
 156      /**
 157       * Cleans up after the temporary file by deleting it
 158       */
 159  	function __destruct() {
 160          if ( $this->canDelete ) {
 161              $this->purge();
 162          }
 163      }
 164  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1