dumpBackup.php: Remove --force-normal option
[lhc/web/wiklou.git] / maintenance / backup.inc
1 <?php
2 /**
3 * Base classes for database dumpers
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Dump Maintenance
25 */
26
27 /**
28 * @ingroup Dump Maintenance
29 */
30 class DumpDBZip2Output extends DumpPipeOutput {
31 function __construct( $file ) {
32 parent::__construct( "dbzip2", $file );
33 }
34 }
35
36 /**
37 * @ingroup Dump Maintenance
38 */
39 class BackupDumper {
40 public $reporting = true;
41 public $pages = null; // all pages
42 public $skipHeader = false; // don't output <mediawiki> and <siteinfo>
43 public $skipFooter = false; // don't output </mediawiki>
44 public $startId = 0;
45 public $endId = 0;
46 public $revStartId = 0;
47 public $revEndId = 0;
48 public $dumpUploads = false;
49 public $dumpUploadFileContents = false;
50
51 protected $reportingInterval = 100;
52 protected $pageCount = 0;
53 protected $revCount = 0;
54 protected $server = null; // use default
55 protected $sink = null; // Output filters
56 protected $lastTime = 0;
57 protected $pageCountLast = 0;
58 protected $revCountLast = 0;
59
60 protected $outputTypes = array();
61 protected $filterTypes = array();
62
63 protected $ID = 0;
64
65 /**
66 * The dependency-injected database to use.
67 *
68 * @var DatabaseBase|null
69 *
70 * @see self::setDb
71 */
72 protected $forcedDb = null;
73
74 /** @var LoadBalancer */
75 protected $lb;
76
77 // @todo Unused?
78 private $stubText = false; // include rev_text_id instead of text; for 2-pass dump
79
80 function __construct( $args ) {
81 $this->stderr = fopen( "php://stderr", "wt" );
82
83 // Built-in output and filter plugins
84 $this->registerOutput( 'file', 'DumpFileOutput' );
85 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
86 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
87 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
88 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
89
90 $this->registerFilter( 'latest', 'DumpLatestFilter' );
91 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
92 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
93
94 $this->sink = $this->processArgs( $args );
95 }
96
97 /**
98 * @param string $name
99 * @param string $class Name of output filter plugin class
100 */
101 function registerOutput( $name, $class ) {
102 $this->outputTypes[$name] = $class;
103 }
104
105 /**
106 * @param string $name
107 * @param string $class Name of filter plugin class
108 */
109 function registerFilter( $name, $class ) {
110 $this->filterTypes[$name] = $class;
111 }
112
113 /**
114 * Load a plugin and register it
115 *
116 * @param string $class Name of plugin class; must have a static 'register'
117 * method that takes a BackupDumper as a parameter.
118 * @param string $file Full or relative path to the PHP file to load, or empty
119 */
120 function loadPlugin( $class, $file ) {
121 if ( $file != '' ) {
122 require_once $file;
123 }
124 $register = array( $class, 'register' );
125 call_user_func_array( $register, array( &$this ) );
126 }
127
128 /**
129 * @param array $args
130 * @return array
131 */
132 function processArgs( $args ) {
133 $sink = null;
134 $sinks = array();
135 foreach ( $args as $arg ) {
136 $matches = array();
137 if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
138 MediaWiki\suppressWarnings();
139 list( /* $full */, $opt, $val, $param ) = $matches;
140 MediaWiki\restoreWarnings();
141
142 switch ( $opt ) {
143 case "plugin":
144 $this->loadPlugin( $val, $param );
145 break;
146 case "output":
147 if ( !is_null( $sink ) ) {
148 $sinks[] = $sink;
149 }
150 if ( !isset( $this->outputTypes[$val] ) ) {
151 $this->fatalError( "Unrecognized output sink type '$val'" );
152 }
153 $type = $this->outputTypes[$val];
154 $sink = new $type( $param );
155 break;
156 case "filter":
157 if ( is_null( $sink ) ) {
158 $sink = new DumpOutput();
159 }
160 if ( !isset( $this->filterTypes[$val] ) ) {
161 $this->fatalError( "Unrecognized filter type '$val'" );
162 }
163 $type = $this->filterTypes[$val];
164 $filter = new $type( $sink, $param );
165
166 // references are lame in php...
167 unset( $sink );
168 $sink = $filter;
169
170 break;
171 case "report":
172 $this->reportingInterval = intval( $val );
173 break;
174 case "server":
175 $this->server = $val;
176 break;
177 default:
178 $this->processOption( $opt, $val, $param );
179 }
180 }
181 }
182
183 if ( is_null( $sink ) ) {
184 $sink = new DumpOutput();
185 }
186 $sinks[] = $sink;
187
188 if ( count( $sinks ) > 1 ) {
189 return new DumpMultiWriter( $sinks );
190 } else {
191 return $sink;
192 }
193 }
194
195 function processOption( $opt, $val, $param ) {
196 // extension point for subclasses to add options
197 }
198
199 function dump( $history, $text = WikiExporter::TEXT ) {
200 # Notice messages will foul up your XML output even if they're
201 # relatively harmless.
202 if ( ini_get( 'display_errors' ) ) {
203 ini_set( 'display_errors', 'stderr' );
204 }
205
206 $this->initProgress( $history );
207
208 $db = $this->backupDb();
209 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
210 $exporter->dumpUploads = $this->dumpUploads;
211 $exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
212
213 $wrapper = new ExportProgressFilter( $this->sink, $this );
214 $exporter->setOutputSink( $wrapper );
215
216 if ( !$this->skipHeader ) {
217 $exporter->openStream();
218 }
219 # Log item dumps: all or by range
220 if ( $history & WikiExporter::LOGS ) {
221 if ( $this->startId || $this->endId ) {
222 $exporter->logsByRange( $this->startId, $this->endId );
223 } else {
224 $exporter->allLogs();
225 }
226 } elseif ( is_null( $this->pages ) ) {
227 # Page dumps: all or by page ID range
228 if ( $this->startId || $this->endId ) {
229 $exporter->pagesByRange( $this->startId, $this->endId );
230 } elseif ( $this->revStartId || $this->revEndId ) {
231 $exporter->revsByRange( $this->revStartId, $this->revEndId );
232 } else {
233 $exporter->allPages();
234 }
235 } else {
236 # Dump of specific pages
237 $exporter->pagesByName( $this->pages );
238 }
239
240 if ( !$this->skipFooter ) {
241 $exporter->closeStream();
242 }
243
244 $this->report( true );
245 }
246
247 /**
248 * Initialise starting time and maximum revision count.
249 * We'll make ETA calculations based an progress, assuming relatively
250 * constant per-revision rate.
251 * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
252 */
253 function initProgress( $history = WikiExporter::FULL ) {
254 $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
255 $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
256
257 $dbr = $this->forcedDb;
258 if ( $this->forcedDb === null ) {
259 $dbr = wfGetDB( DB_SLAVE );
260 }
261 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
262 $this->startTime = microtime( true );
263 $this->lastTime = $this->startTime;
264 $this->ID = getmypid();
265 }
266
267 /**
268 * @todo Fixme: the --server parameter is currently not respected, as it
269 * doesn't seem terribly easy to ask the load balancer for a particular
270 * connection by name.
271 * @return DatabaseBase
272 */
273 function backupDb() {
274 if ( $this->forcedDb !== null ) {
275 return $this->forcedDb;
276 }
277
278 $this->lb = wfGetLBFactory()->newMainLB();
279 $db = $this->lb->getConnection( DB_SLAVE, 'dump' );
280
281 // Discourage the server from disconnecting us if it takes a long time
282 // to read out the big ol' batch query.
283 $db->setSessionOptions( array( 'connTimeout' => 3600 * 24 ) );
284
285 return $db;
286 }
287
288 /**
289 * Force the dump to use the provided database connection for database
290 * operations, wherever possible.
291 *
292 * @param DatabaseBase|null $db (Optional) the database connection to use. If null, resort to
293 * use the globally provided ways to get database connections.
294 */
295 function setDb( DatabaseBase $db = null ) {
296 $this->forcedDb = $db;
297 }
298
299 function __destruct() {
300 if ( isset( $this->lb ) ) {
301 $this->lb->closeAll();
302 }
303 }
304
305 function backupServer() {
306 global $wgDBserver;
307
308 return $this->server
309 ? $this->server
310 : $wgDBserver;
311 }
312
313 function reportPage() {
314 $this->pageCount++;
315 }
316
317 function revCount() {
318 $this->revCount++;
319 $this->report();
320 }
321
322 function report( $final = false ) {
323 if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
324 $this->showReport();
325 }
326 }
327
328 function showReport() {
329 if ( $this->reporting ) {
330 $now = wfTimestamp( TS_DB );
331 $nowts = microtime( true );
332 $deltaAll = $nowts - $this->startTime;
333 $deltaPart = $nowts - $this->lastTime;
334 $this->pageCountPart = $this->pageCount - $this->pageCountLast;
335 $this->revCountPart = $this->revCount - $this->revCountLast;
336
337 if ( $deltaAll ) {
338 $portion = $this->revCount / $this->maxCount;
339 $eta = $this->startTime + $deltaAll / $portion;
340 $etats = wfTimestamp( TS_DB, intval( $eta ) );
341 $pageRate = $this->pageCount / $deltaAll;
342 $revRate = $this->revCount / $deltaAll;
343 } else {
344 $pageRate = '-';
345 $revRate = '-';
346 $etats = '-';
347 }
348 if ( $deltaPart ) {
349 $pageRatePart = $this->pageCountPart / $deltaPart;
350 $revRatePart = $this->revCountPart / $deltaPart;
351 } else {
352 $pageRatePart = '-';
353 $revRatePart = '-';
354 }
355 $this->progress( sprintf(
356 "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), "
357 . "%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]",
358 $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate,
359 $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats,
360 $this->maxCount
361 ) );
362 $this->lastTime = $nowts;
363 $this->revCountLast = $this->revCount;
364 }
365 }
366
367 function progress( $string ) {
368 fwrite( $this->stderr, $string . "\n" );
369 }
370
371 function fatalError( $msg ) {
372 $this->progress( "$msg\n" );
373 die( 1 );
374 }
375 }
376
377 class ExportProgressFilter extends DumpFilter {
378 function __construct( &$sink, &$progress ) {
379 parent::__construct( $sink );
380 $this->progress = $progress;
381 }
382
383 function writeClosePage( $string ) {
384 parent::writeClosePage( $string );
385 $this->progress->reportPage();
386 }
387
388 function writeRevision( $rev, $string ) {
389 parent::writeRevision( $rev, $string );
390 $this->progress->revCount();
391 }
392 }