* Parent message altered, purging old translations
[lhc/web/wiklou.git] / maintenance / backup.inc
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25
26 class BackupDumper {
27 var $reportingInterval = 100;
28 var $reporting = true;
29 var $pageCount = 0;
30 var $revCount = 0;
31 var $server = null; // use default
32 var $pages = null; // all pages
33 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
34 var $skipFooter = false; // don't output </mediawiki>
35 var $startId = 0;
36 var $endId = 0;
37 var $sink = null; // Output filters
38 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
39
40 function BackupDumper( $args ) {
41 $this->stderr = fopen( "php://stderr", "wt" );
42 $this->sink = $this->processArgs( $args );
43 }
44
45 /**
46 * @param array $args
47 * @return array
48 * @static
49 */
50 function processArgs( $args ) {
51 $outputTypes = array(
52 'file' => 'DumpFileOutput',
53 'gzip' => 'DumpGZipOutput',
54 'bzip2' => 'DumpBZip2Output',
55 '7zip' => 'Dump7ZipOutput' );
56 $filterTypes = array(
57 'latest' => 'DumpLatestFilter',
58 'notalk' => 'DumpNotalkFilter',
59 'namespace' => 'DumpNamespaceFilter' );
60 $sink = null;
61 $sinks = array();
62 foreach( $args as $arg ) {
63 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
64 @list( $full, $opt, $val, $param ) = $matches;
65 switch( $opt ) {
66 case "output":
67 if( !is_null( $sink ) ) {
68 $sinks[] = $sink;
69 }
70 if( !isset( $outputTypes[$val] ) ) {
71 die( "Unrecognized output sink type '$val'\n" );
72 }
73 $type = $outputTypes[$val];
74 $sink = new $type( $param );
75 break;
76 case "filter":
77 if( is_null( $sink ) ) {
78 $this->progress( "Warning: assuming stdout for filter output\n" );
79 $sink = new DumpOutput();
80 }
81 if( !isset( $filterTypes[$val] ) ) {
82 die( "Unrecognized filter type '$val'\n" );
83 }
84 $type = $filterTypes[$val];
85 $filter = new $type( $sink, $param );
86
87 // references are lame in php...
88 unset( $sink );
89 $sink = $filter;
90
91 break;
92 default:
93 $this->processOption( $opt, $val, $param );
94 }
95 }
96 }
97
98 if( is_null( $sink ) ) {
99 $sink = new DumpOutput();
100 }
101 $sinks[] = $sink;
102
103 if( count( $sinks ) > 1 ) {
104 return new DumpMultiWriter( $sinks );
105 } else {
106 return $sink;
107 }
108 }
109
110 function processOption( $opt, $val, $param ) {
111 // extension point for subclasses to add options
112 }
113
114 function dump( $history, $text = MW_EXPORT_TEXT ) {
115 # This shouldn't happen if on console... ;)
116 header( 'Content-type: text/html; charset=UTF-8' );
117
118 # Notice messages will foul up your XML output even if they're
119 # relatively harmless.
120 ini_set( 'display_errors', false );
121
122 $this->startTime = wfTime();
123
124 $dbr =& wfGetDB( DB_SLAVE );
125 $this->maxCount = $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
126 $this->startTime = wfTime();
127
128 $db =& $this->backupDb();
129 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
130
131 $wrapper = new ExportProgressFilter( $this->sink, $this );
132 $exporter->setOutputSink( $wrapper );
133
134 if( !$this->skipHeader )
135 $exporter->openStream();
136
137 if( is_null( $this->pages ) ) {
138 if( $this->startId || $this->endId ) {
139 $exporter->pagesByRange( $this->startId, $this->endId );
140 } else {
141 $exporter->allPages();
142 }
143 } else {
144 $exporter->pagesByName( $this->pages );
145 }
146
147 if( !$this->skipFooter )
148 $exporter->closeStream();
149
150 $this->report( true );
151 }
152
153 function &backupDb() {
154 global $wgDBadminuser, $wgDBadminpassword;
155 global $wgDBname;
156 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
157 $timeout = 3600 * 24;
158 $db->query( "SET net_read_timeout=$timeout" );
159 $db->query( "SET net_write_timeout=$timeout" );
160 return $db;
161 }
162
163 function backupServer() {
164 global $wgDBserver;
165 return $this->server
166 ? $this->server
167 : $wgDBserver;
168 }
169
170 function reportPage() {
171 $this->pageCount++;
172 $this->report();
173 }
174
175 function revCount() {
176 $this->revCount++;
177 }
178
179 function report( $final = false ) {
180 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
181 $this->showReport();
182 }
183 }
184
185 function showReport() {
186 if( $this->reporting ) {
187 $delta = wfTime() - $this->startTime;
188 $now = wfTimestamp( TS_DB );
189 if( $delta ) {
190 $rate = $this->pageCount / $delta;
191 $revrate = $this->revCount / $delta;
192 $portion = $this->pageCount / $this->maxCount;
193 $eta = $this->startTime + $delta / $portion;
194 $etats = wfTimestamp( TS_DB, intval( $eta ) );
195 } else {
196 $rate = '-';
197 $revrate = '-';
198 $etats = '-';
199 }
200 global $wgDBname;
201 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
202 }
203 }
204
205 function progress( $string ) {
206 fwrite( $this->stderr, $string . "\n" );
207 }
208 }
209
210 class ExportProgressFilter extends DumpFilter {
211 function ExportProgressFilter( &$sink, &$progress ) {
212 parent::DumpFilter( $sink );
213 $this->progress = $progress;
214 }
215
216 function writeClosePage( $string ) {
217 parent::writeClosePage( $string );
218 $this->progress->reportPage();
219 }
220
221 function writeRevision( $rev, $string ) {
222 parent::writeRevision( $rev, $string );
223 $this->progress->revCount();
224 }
225 }
226
227 ?>