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
28 use MediaWiki\Shell\Shell
;
33 class DumpPipeOutput
extends DumpFileOutput
{
36 /** @var string|null */
38 /** @var resource|false */
39 protected $procOpenResource = false;
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;
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;
67 * @param string $command
69 public function startCommand( $command ) {
74 $this->procOpenResource
= proc_open( $command, $spec, $pipes );
75 $this->handle
= $pipes[0];
81 public function closeRenameAndReopen( $newname ) {
82 $this->closeAndRename( $newname, true );
88 public function closeAndRename( $newname, $open = false ) {
89 $newname = $this->checkRenameArgCount( $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 );
101 $command = $this->command
;
102 $command .= " > " . Shell
::escape( $this->filename
);
103 $this->startCommand( $command );