Add config for serving main Page from the domain root
[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 /** @var resource|bool */
36 protected $procOpenResource = false;
37
38 /**
39 * @param string $command
40 * @param string|null $file
41 */
42 function __construct( $command, $file = null ) {
43 if ( !is_null( $file ) ) {
44 $command .= " > " . Shell::escape( $file );
45 }
46
47 $this->startCommand( $command );
48 $this->command = $command;
49 $this->filename = $file;
50 }
51
52 /**
53 * @param string $string
54 */
55 function writeCloseStream( $string ) {
56 parent::writeCloseStream( $string );
57 if ( $this->procOpenResource ) {
58 proc_close( $this->procOpenResource );
59 $this->procOpenResource = false;
60 }
61 }
62
63 /**
64 * @param string $command
65 */
66 function startCommand( $command ) {
67 $spec = [
68 0 => [ "pipe", "r" ],
69 ];
70 $pipes = [];
71 $this->procOpenResource = proc_open( $command, $spec, $pipes );
72 $this->handle = $pipes[0];
73 }
74
75 /**
76 * @param string $newname
77 */
78 function closeRenameAndReopen( $newname ) {
79 $this->closeAndRename( $newname, true );
80 }
81
82 /**
83 * @param string $newname
84 * @param bool $open
85 */
86 function closeAndRename( $newname, $open = false ) {
87 $newname = $this->checkRenameArgCount( $newname );
88 if ( $newname ) {
89 if ( $this->handle ) {
90 fclose( $this->handle );
91 $this->handle = false;
92 }
93 if ( $this->procOpenResource ) {
94 proc_close( $this->procOpenResource );
95 $this->procOpenResource = false;
96 }
97 $this->renameOrException( $newname );
98 if ( $open ) {
99 $command = $this->command;
100 $command .= " > " . Shell::escape( $this->filename );
101 $this->startCommand( $command );
102 }
103 }
104 }
105 }