MediaWiki  1.27.1
TempFSFile.php
Go to the documentation of this file.
1 <?php
30 class TempFSFile extends FSFile {
32  protected $canDelete = false;
33 
35  protected static $pathsCollect = null;
36 
37  public function __construct( $path ) {
38  parent::__construct( $path );
39 
40  if ( self::$pathsCollect === null ) {
41  self::$pathsCollect = [];
42  register_shutdown_function( [ __CLASS__, 'purgeAllOnShutdown' ] );
43  }
44  }
45 
54  public static function factory( $prefix, $extension = '' ) {
55  $ext = ( $extension != '' ) ? ".{$extension}" : '';
56 
57  $attempts = 5;
58  while ( $attempts-- ) {
59  $path = wfTempDir() . '/' . $prefix . wfRandomString( 12 ) . $ext;
60  MediaWiki\suppressWarnings();
61  $newFileHandle = fopen( $path, 'x' );
62  MediaWiki\restoreWarnings();
63  if ( $newFileHandle ) {
64  fclose( $newFileHandle );
65  $tmpFile = new self( $path );
66  $tmpFile->autocollect();
67  // Safely instantiated, end loop.
68  return $tmpFile;
69  }
70  }
71 
72  // Give up
73  return null;
74  }
75 
81  public function purge() {
82  $this->canDelete = false; // done
83  MediaWiki\suppressWarnings();
84  $ok = unlink( $this->path );
85  MediaWiki\restoreWarnings();
86 
87  unset( self::$pathsCollect[$this->path] );
88 
89  return $ok;
90  }
91 
98  public function bind( $object ) {
99  if ( is_object( $object ) ) {
100  if ( !isset( $object->tempFSFileReferences ) ) {
101  // Init first since $object might use __get() and return only a copy variable
102  $object->tempFSFileReferences = [];
103  }
104  $object->tempFSFileReferences[] = $this;
105  }
106 
107  return $this;
108  }
109 
115  public function preserve() {
116  $this->canDelete = false;
117 
118  unset( self::$pathsCollect[$this->path] );
119 
120  return $this;
121  }
122 
128  public function autocollect() {
129  $this->canDelete = true;
130 
131  self::$pathsCollect[$this->path] = 1;
132 
133  return $this;
134  }
135 
141  public static function purgeAllOnShutdown() {
142  foreach ( self::$pathsCollect as $path ) {
143  MediaWiki\suppressWarnings();
144  unlink( $path );
145  MediaWiki\restoreWarnings();
146  }
147  }
148 
152  function __destruct() {
153  if ( $this->canDelete ) {
154  $this->purge();
155  }
156  }
157 }
static factory($prefix, $extension= '')
Make a new temporary file on the file system.
Definition: TempFSFile.php:54
string $path
Path to file.
Definition: FSFile.php:31
bool $canDelete
Garbage collect the temp file.
Definition: TempFSFile.php:32
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
Definition: TempFSFile.php:30
autocollect()
Set flag clean up after the temporary file.
Definition: TempFSFile.php:128
__destruct()
Cleans up after the temporary file by deleting it.
Definition: TempFSFile.php:152
wfRandomString($length=32)
Get a random string containing a number of pseudo-random hex characters.
wfTempDir()
Tries to get the system directory for temporary files.
static array $pathsCollect
Map of (path => 1) for paths to delete on shutdown.
Definition: TempFSFile.php:35
preserve()
Set flag to not clean up after the temporary file.
Definition: TempFSFile.php:115
purge()
Purge this file off the file system.
Definition: TempFSFile.php:81
static purgeAllOnShutdown()
Try to make sure that all files are purged on error.
Definition: TempFSFile.php:141
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
__construct($path)
Definition: TempFSFile.php:37
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Class representing a non-directory file on the file system.
Definition: FSFile.php:29
bind($object)
Clean up the temporary file only after an object goes out of scope.
Definition: TempFSFile.php:98