Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DumpPipeOutput
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 writeCloseStream
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 startCommand
0.00% covered (danger)
0.00%
0 / 6
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
 closeAndRename
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Stream outputter to send data to a file via some filter program.
4 * Even if compression is available in a library, using a separate
5 * program can allow us to make use of a multi-processor system.
6 *
7 * Copyright © 2003, 2005, 2006 Brooke Vibber <bvibber@wikimedia.org>
8 * https://www.mediawiki.org/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28use MediaWiki\Shell\Shell;
29
30/**
31 * @ingroup Dump
32 */
33class DumpPipeOutput extends DumpFileOutput {
34    /** @var string */
35    protected $command;
36    /** @var string|null */
37    protected $filename;
38    /** @var resource|false */
39    protected $procOpenResource = false;
40
41    /**
42     * @param string $command
43     * @param string|null $file
44     */
45    public function __construct( $command, $file = null ) {
46        if ( $file !== null ) {
47            $command .= " > " . Shell::escape( $file );
48        }
49
50        $this->startCommand( $command );
51        $this->command = $command;
52        $this->filename = $file;
53    }
54
55    /**
56     * @param string $string
57     */
58    public function writeCloseStream( $string ) {
59        parent::writeCloseStream( $string );
60        if ( $this->procOpenResource ) {
61            proc_close( $this->procOpenResource );
62            $this->procOpenResource = false;
63        }
64    }
65
66    /**
67     * @param string $command
68     */
69    public function startCommand( $command ) {
70        $spec = [
71            0 => [ "pipe", "r" ],
72        ];
73        $pipes = [];
74        $this->procOpenResource = proc_open( $command, $spec, $pipes );
75        $this->handle = $pipes[0];
76    }
77
78    /**
79     * @inheritDoc
80     */
81    public function closeRenameAndReopen( $newname ) {
82        $this->closeAndRename( $newname, true );
83    }
84
85    /**
86     * @inheritDoc
87     */
88    public function closeAndRename( $newname, $open = false ) {
89        $newname = $this->checkRenameArgCount( $newname );
90        if ( $newname ) {
91            if ( $this->handle ) {
92                fclose( $this->handle );
93                $this->handle = false;
94            }
95            if ( $this->procOpenResource ) {
96                proc_close( $this->procOpenResource );
97                $this->procOpenResource = false;
98            }
99            $this->renameOrException( $newname );
100            if ( $open ) {
101                $command = $this->command;
102                $command .= " > " . Shell::escape( $this->filename );
103                $this->startCommand( $command );
104            }
105        }
106    }
107}