MediaWiki master
DumpFileOutput.php
Go to the documentation of this file.
1<?php
12namespace MediaWiki\Export;
13
15use RuntimeException;
16
22 protected $handle = false;
24 protected $filename;
25
29 public function __construct( $file ) {
30 $this->handle = fopen( $file, "wt" );
31 $this->filename = $file;
32 }
33
37 public function writeCloseStream( $string ) {
38 parent::writeCloseStream( $string );
39 if ( $this->handle ) {
40 fclose( $this->handle );
41 $this->handle = false;
42 }
43 }
44
48 public function write( $string ) {
49 fputs( $this->handle, $string );
50 }
51
55 public function closeRenameAndReopen( $newname ) {
56 $this->closeAndRename( $newname, true );
57 }
58
62 protected function renameOrException( $newname ) {
63 if ( !rename( $this->filename, $newname ) ) {
64 throw new RuntimeException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
65 }
66 }
67
73 protected function checkRenameArgCount( $newname ) {
74 if ( is_array( $newname ) ) {
75 if ( count( $newname ) > 1 ) {
76 throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
77 }
78 $newname = $newname[0];
79 }
80 return $newname;
81 }
82
86 public function closeAndRename( $newname, $open = false ) {
87 $newname = $this->checkRenameArgCount( $newname );
88 if ( $newname ) {
89 if ( $this->handle ) {
90 fclose( $this->handle );
91 $this->handle = false;
92 }
93 $this->renameOrException( $newname );
94 if ( $open ) {
95 $this->handle = fopen( $this->filename, "wt" );
96 }
97 }
98 }
99
103 public function getFilenames() {
104 return $this->filename;
105 }
106}
107
109class_alias( DumpFileOutput::class, 'DumpFileOutput' );
closeRenameAndReopen( $newname)
Close the old file, move it to a specified name, and reopen new file with the old name....
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...