Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
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 * @license GPL-2.0-or-later
11 * @file
12 */
13
14namespace MediaWiki\Export;
15
16use MediaWiki\Shell\Shell;
17
18/**
19 * @ingroup Dump
20 */
21class DumpPipeOutput extends DumpFileOutput {
22    /** @var string */
23    protected $command;
24    /** @var string|null */
25    protected $filename;
26    /** @var resource|false */
27    protected $procOpenResource = false;
28
29    /**
30     * @param string $command
31     * @param string|null $file
32     */
33    public function __construct( $command, $file = null ) {
34        if ( $file !== null ) {
35            $command .= " > " . Shell::escape( $file );
36        }
37
38        $this->startCommand( $command );
39        $this->command = $command;
40        $this->filename = $file;
41    }
42
43    /**
44     * @param string $string
45     */
46    public function writeCloseStream( $string ) {
47        parent::writeCloseStream( $string );
48        if ( $this->procOpenResource ) {
49            proc_close( $this->procOpenResource );
50            $this->procOpenResource = false;
51        }
52    }
53
54    /**
55     * @param string $command
56     */
57    public function startCommand( $command ) {
58        $spec = [
59            0 => [ "pipe", "r" ],
60        ];
61        $pipes = [];
62        $this->procOpenResource = proc_open( $command, $spec, $pipes );
63        $this->handle = $pipes[0];
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function closeRenameAndReopen( $newname ) {
70        $this->closeAndRename( $newname, true );
71    }
72
73    /**
74     * @inheritDoc
75     */
76    public function closeAndRename( $newname, $open = false ) {
77        $newname = $this->checkRenameArgCount( $newname );
78        if ( $newname ) {
79            if ( $this->handle ) {
80                fclose( $this->handle );
81                $this->handle = false;
82            }
83            if ( $this->procOpenResource ) {
84                proc_close( $this->procOpenResource );
85                $this->procOpenResource = false;
86            }
87            $this->renameOrException( $newname );
88            if ( $open ) {
89                $command = $this->command;
90                $command .= " > " . Shell::escape( $this->filename );
91                $this->startCommand( $command );
92            }
93        }
94    }
95}
96
97/** @deprecated class alias since 1.46 */
98class_alias( DumpPipeOutput::class, 'DumpPipeOutput' );