* Added wfDie() wrapper, and some manual die(-1), to force the return code
[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
43 // Built-in output and filter plugins
44 $this->registerOutput( 'file', 'DumpFileOutput' );
45 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
46 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
47 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
48
49 $this->registerFilter( 'latest', 'DumpLatestFilter' );
50 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
51 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
52
53 $this->sink = $this->processArgs( $args );
54 }
55
56 /**
57 * @param string $name
58 * @param string $class name of output filter plugin class
59 */
60 function registerOutput( $name, $class ) {
61 $this->outputTypes[$name] = $class;
62 }
63
64 /**
65 * @param string $name
66 * @param string $class name of filter plugin class
67 */
68 function registerFilter( $name, $class ) {
69 $this->filterTypes[$name] = $class;
70 }
71
72 /**
73 * Load a plugin and register it
74 * @param string $class Name of plugin class; must have a static 'register'
75 * method that takes a BackupDumper as a parameter.
76 * @param string $file Full or relative path to the PHP file to load, or empty
77 */
78 function loadPlugin( $class, $file ) {
79 if( $file != '' ) {
80 require_once( $file );
81 }
82 $register = array( $class, 'register' );
83 call_user_func_array( $register, array( &$this ) );
84 }
85
86 /**
87 * @param array $args
88 * @return array
89 * @static
90 */
91 function processArgs( $args ) {
92 $sink = null;
93 $sinks = array();
94 foreach( $args as $arg ) {
95 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
96 @list( $full, $opt, $val, $param ) = $matches;
97 switch( $opt ) {
98 case "plugin":
99 $this->loadPlugin( $val, $param );
100 break;
101 case "output":
102 if( !is_null( $sink ) ) {
103 $sinks[] = $sink;
104 }
105 if( !isset( $this->outputTypes[$val] ) ) {
106 wfDie( "Unrecognized output sink type '$val'\n" );
107 }
108 $type = $this->outputTypes[$val];
109 $sink = new $type( $param );
110 break;
111 case "filter":
112 if( is_null( $sink ) ) {
113 $this->progress( "Warning: assuming stdout for filter output\n" );
114 $sink = new DumpOutput();
115 }
116 if( !isset( $this->filterTypes[$val] ) ) {
117 wfDie( "Unrecognized filter type '$val'\n" );
118 }
119 $type = $this->filterTypes[$val];
120 $filter = new $type( $sink, $param );
121
122 // references are lame in php...
123 unset( $sink );
124 $sink = $filter;
125
126 break;
127 default:
128 $this->processOption( $opt, $val, $param );
129 }
130 }
131 }
132
133 if( is_null( $sink ) ) {
134 $sink = new DumpOutput();
135 }
136 $sinks[] = $sink;
137
138 if( count( $sinks ) > 1 ) {
139 return new DumpMultiWriter( $sinks );
140 } else {
141 return $sink;
142 }
143 }
144
145 function processOption( $opt, $val, $param ) {
146 // extension point for subclasses to add options
147 }
148
149 function dump( $history, $text = MW_EXPORT_TEXT ) {
150 # This shouldn't happen if on console... ;)
151 header( 'Content-type: text/html; charset=UTF-8' );
152
153 # Notice messages will foul up your XML output even if they're
154 # relatively harmless.
155 ini_set( 'display_errors', false );
156
157 $this->startTime = wfTime();
158
159 $dbr =& wfGetDB( DB_SLAVE );
160 $this->maxCount = $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
161 $this->startTime = wfTime();
162
163 $db =& $this->backupDb();
164 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
165
166 $wrapper = new ExportProgressFilter( $this->sink, $this );
167 $exporter->setOutputSink( $wrapper );
168
169 if( !$this->skipHeader )
170 $exporter->openStream();
171
172 if( is_null( $this->pages ) ) {
173 if( $this->startId || $this->endId ) {
174 $exporter->pagesByRange( $this->startId, $this->endId );
175 } else {
176 $exporter->allPages();
177 }
178 } else {
179 $exporter->pagesByName( $this->pages );
180 }
181
182 if( !$this->skipFooter )
183 $exporter->closeStream();
184
185 $this->report( true );
186 }
187
188 function &backupDb() {
189 global $wgDBadminuser, $wgDBadminpassword;
190 global $wgDBname;
191 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
192 $timeout = 3600 * 24;
193 $db->query( "SET net_read_timeout=$timeout" );
194 $db->query( "SET net_write_timeout=$timeout" );
195 return $db;
196 }
197
198 function backupServer() {
199 global $wgDBserver;
200 return $this->server
201 ? $this->server
202 : $wgDBserver;
203 }
204
205 function reportPage() {
206 $this->pageCount++;
207 $this->report();
208 }
209
210 function revCount() {
211 $this->revCount++;
212 }
213
214 function report( $final = false ) {
215 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
216 $this->showReport();
217 }
218 }
219
220 function showReport() {
221 if( $this->reporting ) {
222 $delta = wfTime() - $this->startTime;
223 $now = wfTimestamp( TS_DB );
224 if( $delta ) {
225 $rate = $this->pageCount / $delta;
226 $revrate = $this->revCount / $delta;
227 $portion = $this->pageCount / $this->maxCount;
228 $eta = $this->startTime + $delta / $portion;
229 $etats = wfTimestamp( TS_DB, intval( $eta ) );
230 } else {
231 $rate = '-';
232 $revrate = '-';
233 $etats = '-';
234 }
235 global $wgDBname;
236 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
237 }
238 }
239
240 function progress( $string ) {
241 fwrite( $this->stderr, $string . "\n" );
242 }
243 }
244
245 class ExportProgressFilter extends DumpFilter {
246 function ExportProgressFilter( &$sink, &$progress ) {
247 parent::DumpFilter( $sink );
248 $this->progress = $progress;
249 }
250
251 function writeClosePage( $string ) {
252 parent::writeClosePage( $string );
253 $this->progress->reportPage();
254 }
255
256 function writeRevision( $rev, $string ) {
257 parent::writeRevision( $rev, $string );
258 $this->progress->revCount();
259 }
260 }
261
262 ?>