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