Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26use MediaWiki\Exception\MWException;
27
28/**
29 * @ingroup Dump
30 */
31class DumpFileOutput extends DumpOutput {
32    /** @var resource|false */
33    protected $handle = false;
34    /** @var string */
35    protected $filename;
36
37    /**
38     * @param string $file
39     */
40    public function __construct( $file ) {
41        $this->handle = fopen( $file, "wt" );
42        $this->filename = $file;
43    }
44
45    /**
46     * @param string $string
47     */
48    public function writeCloseStream( $string ) {
49        parent::writeCloseStream( $string );
50        if ( $this->handle ) {
51            fclose( $this->handle );
52            $this->handle = false;
53        }
54    }
55
56    /**
57     * @param string $string
58     */
59    public function write( $string ) {
60        fputs( $this->handle, $string );
61    }
62
63    /**
64     * @inheritDoc
65     */
66    public function closeRenameAndReopen( $newname ) {
67        $this->closeAndRename( $newname, true );
68    }
69
70    /**
71     * @param string $newname
72     */
73    protected function renameOrException( $newname ) {
74        if ( !rename( $this->filename, $newname ) ) {
75            throw new RuntimeException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
76        }
77    }
78
79    /**
80     * @param string|string[] $newname
81     * @return string
82     * @throws MWException
83     */
84    protected function checkRenameArgCount( $newname ) {
85        if ( is_array( $newname ) ) {
86            if ( count( $newname ) > 1 ) {
87                throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
88            }
89            $newname = $newname[0];
90        }
91        return $newname;
92    }
93
94    /**
95     * @inheritDoc
96     */
97    public function closeAndRename( $newname, $open = false ) {
98        $newname = $this->checkRenameArgCount( $newname );
99        if ( $newname ) {
100            if ( $this->handle ) {
101                fclose( $this->handle );
102                $this->handle = false;
103            }
104            $this->renameOrException( $newname );
105            if ( $open ) {
106                $this->handle = fopen( $this->filename, "wt" );
107            }
108        }
109    }
110
111    /**
112     * @return string|null
113     */
114    public function getFilenames() {
115        return $this->filename;
116    }
117}