Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
DumpFileOutput
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 8
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 writeCloseStream
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 write
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 closeRenameAndReopen
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 renameOrException
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 checkRenameArgCount
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 closeAndRename
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getFilenames
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Stream outputter to send data to a file.
4 *
5 * Copyright © 2003, 2005, 2006 Brooke Vibber <bvibber@wikimedia.org>
6 * https://www.mediawiki.org/
7 *
8 * @license GPL-2.0-or-later
9 * @file
10 */
11
12namespace MediaWiki\Export;
13
14use MediaWiki\Exception\MWException;
15use RuntimeException;
16
17/**
18 * @ingroup Dump
19 */
20class DumpFileOutput extends DumpOutput {
21    /** @var resource|false */
22    protected $handle = false;
23    /** @var string */
24    protected $filename;
25
26    /**
27     * @param string $file
28     */
29    public function __construct( $file ) {
30        $this->handle = fopen( $file, "wt" );
31        $this->filename = $file;
32    }
33
34    /**
35     * @param string $string
36     */
37    public function writeCloseStream( $string ) {
38        parent::writeCloseStream( $string );
39        if ( $this->handle ) {
40            fclose( $this->handle );
41            $this->handle = false;
42        }
43    }
44
45    /**
46     * @param string $string
47     */
48    public function write( $string ) {
49        fputs( $this->handle, $string );
50    }
51
52    /**
53     * @inheritDoc
54     */
55    public function closeRenameAndReopen( $newname ) {
56        $this->closeAndRename( $newname, true );
57    }
58
59    /**
60     * @param string $newname
61     */
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
68    /**
69     * @param string|string[] $newname
70     * @return string
71     * @throws MWException
72     */
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
83    /**
84     * @inheritDoc
85     */
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
100    /**
101     * @return string|null
102     */
103    public function getFilenames() {
104        return $this->filename;
105    }
106}
107
108/** @deprecated class alias since 1.46 */
109class_alias( DumpFileOutput::class, 'DumpFileOutput' );