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 Brion Vibber <brion@pobox.com>
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
28 use MediaWiki\Shell\Shell
;
33 class DumpPipeOutput
extends DumpFileOutput
{
34 protected $command, $filename;
35 /** @var resource|bool */
36 protected $procOpenResource = false;
39 * @param string $command
40 * @param string|null $file
42 function __construct( $command, $file = null ) {
43 if ( !is_null( $file ) ) {
44 $command .= " > " . Shell
::escape( $file );
47 $this->startCommand( $command );
48 $this->command
= $command;
49 $this->filename
= $file;
53 * @param string $string
55 function writeCloseStream( $string ) {
56 parent
::writeCloseStream( $string );
57 if ( $this->procOpenResource
) {
58 proc_close( $this->procOpenResource
);
59 $this->procOpenResource
= false;
64 * @param string $command
66 function startCommand( $command ) {
71 $this->procOpenResource
= proc_open( $command, $spec, $pipes );
72 $this->handle
= $pipes[0];
76 * @param string $newname
78 function closeRenameAndReopen( $newname ) {
79 $this->closeAndRename( $newname, true );
83 * @param string $newname
86 function closeAndRename( $newname, $open = false ) {
87 $newname = $this->checkRenameArgCount( $newname );
89 if ( $this->handle
) {
90 fclose( $this->handle
);
91 $this->handle
= false;
93 if ( $this->procOpenResource
) {
94 proc_close( $this->procOpenResource
);
95 $this->procOpenResource
= false;
97 $this->renameOrException( $newname );
99 $command = $this->command
;
100 $command .= " > " . Shell
::escape( $this->filename
);
101 $this->startCommand( $command );