Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / export / DumpPipeOutput.php
blob5f23be61a7399ae596c6f9f501be2526b46bef1f
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.
7 * Copyright © 2003, 2005, 2006 Brooke Vibber <bvibber@wikimedia.org>
8 * https://www.mediawiki.org/
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.
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.
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
25 * @file
28 use MediaWiki\Shell\Shell;
30 /**
31 * @ingroup Dump
33 class DumpPipeOutput extends DumpFileOutput {
34 /** @var string */
35 protected $command;
36 /** @var string|null */
37 protected $filename;
38 /** @var resource|false */
39 protected $procOpenResource = false;
41 /**
42 * @param string $command
43 * @param string|null $file
45 public function __construct( $command, $file = null ) {
46 if ( $file !== null ) {
47 $command .= " > " . Shell::escape( $file );
50 $this->startCommand( $command );
51 $this->command = $command;
52 $this->filename = $file;
55 /**
56 * @param string $string
58 public function writeCloseStream( $string ) {
59 parent::writeCloseStream( $string );
60 if ( $this->procOpenResource ) {
61 proc_close( $this->procOpenResource );
62 $this->procOpenResource = false;
66 /**
67 * @param string $command
69 public function startCommand( $command ) {
70 $spec = [
71 0 => [ "pipe", "r" ],
73 $pipes = [];
74 $this->procOpenResource = proc_open( $command, $spec, $pipes );
75 $this->handle = $pipes[0];
78 /**
79 * @inheritDoc
81 public function closeRenameAndReopen( $newname ) {
82 $this->closeAndRename( $newname, true );
85 /**
86 * @inheritDoc
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;
95 if ( $this->procOpenResource ) {
96 proc_close( $this->procOpenResource );
97 $this->procOpenResource = false;
99 $this->renameOrException( $newname );
100 if ( $open ) {
101 $command = $this->command;
102 $command .= " > " . Shell::escape( $this->filename );
103 $this->startCommand( $command );