Merge "Fix SlotDiffRenderer documentation"
[lhc/web/wiklou.git] / includes / export / DumpPipeOutput.php
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 Brion Vibber <brion@pobox.com>
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
28 use MediaWiki\Shell\Shell;
29
30 /**
31 * @ingroup Dump
32 */
33 class DumpPipeOutput extends DumpFileOutput {
34 protected $command, $filename;
35 protected $procOpenResource = false;
36
37 /**
38 * @param string $command
39 * @param string|null $file
40 */
41 function __construct( $command, $file = null ) {
42 if ( !is_null( $file ) ) {
43 $command .= " > " . Shell::escape( $file );
44 }
45
46 $this->startCommand( $command );
47 $this->command = $command;
48 $this->filename = $file;
49 }
50
51 /**
52 * @param string $string
53 */
54 function writeCloseStream( $string ) {
55 parent::writeCloseStream( $string );
56 if ( $this->procOpenResource ) {
57 proc_close( $this->procOpenResource );
58 $this->procOpenResource = false;
59 }
60 }
61
62 /**
63 * @param string $command
64 */
65 function startCommand( $command ) {
66 $spec = [
67 0 => [ "pipe", "r" ],
68 ];
69 $pipes = [];
70 $this->procOpenResource = proc_open( $command, $spec, $pipes );
71 $this->handle = $pipes[0];
72 }
73
74 /**
75 * @param string $newname
76 */
77 function closeRenameAndReopen( $newname ) {
78 $this->closeAndRename( $newname, true );
79 }
80
81 /**
82 * @param string $newname
83 * @param bool $open
84 */
85 function closeAndRename( $newname, $open = false ) {
86 $newname = $this->checkRenameArgCount( $newname );
87 if ( $newname ) {
88 if ( $this->handle ) {
89 fclose( $this->handle );
90 $this->handle = false;
91 }
92 if ( $this->procOpenResource ) {
93 proc_close( $this->procOpenResource );
94 $this->procOpenResource = false;
95 }
96 $this->renameOrException( $newname );
97 if ( $open ) {
98 $command = $this->command;
99 $command .= " > " . Shell::escape( $this->filename );
100 $this->startCommand( $command );
101 }
102 }
103 }
104 }