MediaWiki  master
DumpFileOutput.php
Go to the documentation of this file.
1 <?php
29 class DumpFileOutput extends DumpOutput {
31  protected $handle = false;
33  protected $filename;
34 
38  public function __construct( $file ) {
39  $this->handle = fopen( $file, "wt" );
40  $this->filename = $file;
41  }
42 
46  public function writeCloseStream( $string ) {
47  parent::writeCloseStream( $string );
48  if ( $this->handle ) {
49  fclose( $this->handle );
50  $this->handle = false;
51  }
52  }
53 
57  public function write( $string ) {
58  fputs( $this->handle, $string );
59  }
60 
64  public function closeRenameAndReopen( $newname ) {
65  $this->closeAndRename( $newname, true );
66  }
67 
72  protected function renameOrException( $newname ) {
73  if ( !rename( $this->filename, $newname ) ) {
74  throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
75  }
76  }
77 
83  protected function checkRenameArgCount( $newname ) {
84  if ( is_array( $newname ) ) {
85  if ( count( $newname ) > 1 ) {
86  throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
87  }
88  $newname = $newname[0];
89  }
90  return $newname;
91  }
92 
96  public function closeAndRename( $newname, $open = false ) {
97  $newname = $this->checkRenameArgCount( $newname );
98  if ( $newname ) {
99  if ( $this->handle ) {
100  fclose( $this->handle );
101  $this->handle = false;
102  }
103  $this->renameOrException( $newname );
104  if ( $open ) {
105  $this->handle = fopen( $this->filename, "wt" );
106  }
107  }
108  }
109 
113  public function getFilenames() {
114  return $this->filename;
115  }
116 }
DumpFileOutput\__construct
__construct( $file)
Definition: DumpFileOutput.php:38
DumpFileOutput\closeRenameAndReopen
closeRenameAndReopen( $newname)
Close the old file, move it to a specified name, and reopen new file with the old name....
Definition: DumpFileOutput.php:64
DumpFileOutput\checkRenameArgCount
checkRenameArgCount( $newname)
Definition: DumpFileOutput.php:83
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
DumpFileOutput\renameOrException
renameOrException( $newname)
Definition: DumpFileOutput.php:72
DumpFileOutput\write
write( $string)
Definition: DumpFileOutput.php:57
MWException
MediaWiki exception.
Definition: MWException.php:26
DumpOutput
Definition: DumpOutput.php:29
DumpFileOutput\getFilenames
getFilenames()
Definition: DumpFileOutput.php:113
DumpFileOutput\$handle
resource false $handle
Definition: DumpFileOutput.php:31
DumpFileOutput\$filename
string $filename
Definition: DumpFileOutput.php:33
DumpFileOutput\writeCloseStream
writeCloseStream( $string)
Definition: DumpFileOutput.php:46
DumpFileOutput\closeAndRename
closeAndRename( $newname, $open=false)
Close the old file, and move it to a specified name.Use this for the last piece of a file written out...
Definition: DumpFileOutput.php:96
DumpFileOutput
Definition: DumpFileOutput.php:29