3 * Base classes for database dumpers
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
24 * @ingroup Dump Maintenance
28 * @ingroup Dump Maintenance
30 class DumpDBZip2Output extends DumpPipeOutput {
31 function __construct( $file ) {
32 parent::__construct( "dbzip2", $file );
37 * @ingroup Dump Maintenance
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>
46 public $revStartId = 0;
48 public $dumpUploads = false;
49 public $dumpUploadFileContents = false;
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;
60 protected $outputTypes = array();
61 protected $filterTypes = array();
66 * The dependency-injected database to use.
68 * @var DatabaseBase|null
72 protected $forcedDb = null;
74 /** @var LoadBalancer */
78 private $stubText = false; // include rev_text_id instead of text; for 2-pass dump
80 function __construct( $args ) {
81 $this->stderr = fopen( "php://stderr", "wt" );
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' );
90 $this->registerFilter( 'latest', 'DumpLatestFilter' );
91 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
92 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
94 $this->sink = $this->processArgs( $args );
99 * @param string $class Name of output filter plugin class
101 function registerOutput( $name, $class ) {
102 $this->outputTypes[$name] = $class;
106 * @param string $name
107 * @param string $class Name of filter plugin class
109 function registerFilter( $name, $class ) {
110 $this->filterTypes[$name] = $class;
114 * Load a plugin and register it
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
120 function loadPlugin( $class, $file ) {
124 $register = array( $class, 'register' );
125 call_user_func_array( $register, array( &$this ) );
132 function processArgs( $args ) {
135 foreach ( $args as $arg ) {
137 if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
138 wfSuppressWarnings();
139 list( /* $full */, $opt, $val, $param ) = $matches;
144 $this->loadPlugin( $val, $param );
147 if ( !is_null( $sink ) ) {
150 if ( !isset( $this->outputTypes[$val] ) ) {
151 $this->fatalError( "Unrecognized output sink type '$val'" );
153 $type = $this->outputTypes[$val];
154 $sink = new $type( $param );
157 if ( is_null( $sink ) ) {
158 $sink = new DumpOutput();
160 if ( !isset( $this->filterTypes[$val] ) ) {
161 $this->fatalError( "Unrecognized filter type '$val'" );
163 $type = $this->filterTypes[$val];
164 $filter = new $type( $sink, $param );
166 // references are lame in php...
172 $this->reportingInterval = intval( $val );
175 $this->server = $val;
178 if ( !function_exists( 'utf8_normalize' ) ) {
179 $this->fatalError( "UTF-8 normalization extension not loaded. " .
180 "Install or remove --force-normal parameter to use slower code." );
184 $this->processOption( $opt, $val, $param );
189 if ( is_null( $sink ) ) {
190 $sink = new DumpOutput();
194 if ( count( $sinks ) > 1 ) {
195 return new DumpMultiWriter( $sinks );
201 function processOption( $opt, $val, $param ) {
202 // extension point for subclasses to add options
205 function dump( $history, $text = WikiExporter::TEXT ) {
206 # Notice messages will foul up your XML output even if they're
207 # relatively harmless.
208 if ( ini_get( 'display_errors' ) ) {
209 ini_set( 'display_errors', 'stderr' );
212 $this->initProgress( $history );
214 $db = $this->backupDb();
215 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
216 $exporter->dumpUploads = $this->dumpUploads;
217 $exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
219 $wrapper = new ExportProgressFilter( $this->sink, $this );
220 $exporter->setOutputSink( $wrapper );
222 if ( !$this->skipHeader ) {
223 $exporter->openStream();
225 # Log item dumps: all or by range
226 if ( $history & WikiExporter::LOGS ) {
227 if ( $this->startId || $this->endId ) {
228 $exporter->logsByRange( $this->startId, $this->endId );
230 $exporter->allLogs();
232 } elseif ( is_null( $this->pages ) ) {
233 # Page dumps: all or by page ID range
234 if ( $this->startId || $this->endId ) {
235 $exporter->pagesByRange( $this->startId, $this->endId );
236 } elseif ( $this->revStartId || $this->revEndId ) {
237 $exporter->revsByRange( $this->revStartId, $this->revEndId );
239 $exporter->allPages();
242 # Dump of specific pages
243 $exporter->pagesByName( $this->pages );
246 if ( !$this->skipFooter ) {
247 $exporter->closeStream();
250 $this->report( true );
254 * Initialise starting time and maximum revision count.
255 * We'll make ETA calculations based an progress, assuming relatively
256 * constant per-revision rate.
257 * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
259 function initProgress( $history = WikiExporter::FULL ) {
260 $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
261 $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
263 $dbr = $this->forcedDb;
264 if ( $this->forcedDb === null ) {
265 $dbr = wfGetDB( DB_SLAVE );
267 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
268 $this->startTime = microtime( true );
269 $this->lastTime = $this->startTime;
270 $this->ID = getmypid();
274 * @todo Fixme: the --server parameter is currently not respected, as it
275 * doesn't seem terribly easy to ask the load balancer for a particular
276 * connection by name.
277 * @return DatabaseBase
279 function backupDb() {
280 if ( $this->forcedDb !== null ) {
281 return $this->forcedDb;
284 $this->lb = wfGetLBFactory()->newMainLB();
285 $db = $this->lb->getConnection( DB_SLAVE, 'dump' );
287 // Discourage the server from disconnecting us if it takes a long time
288 // to read out the big ol' batch query.
289 $db->setSessionOptions( array( 'connTimeout' => 3600 * 24 ) );
295 * Force the dump to use the provided database connection for database
296 * operations, wherever possible.
298 * @param DatabaseBase|null $db (Optional) the database connection to use. If null, resort to
299 * use the globally provided ways to get database connections.
301 function setDb( DatabaseBase $db = null ) {
302 $this->forcedDb = $db;
305 function __destruct() {
306 if ( isset( $this->lb ) ) {
307 $this->lb->closeAll();
311 function backupServer() {
319 function reportPage() {
323 function revCount() {
328 function report( $final = false ) {
329 if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
334 function showReport() {
335 if ( $this->reporting ) {
336 $now = wfTimestamp( TS_DB );
337 $nowts = microtime( true );
338 $deltaAll = $nowts - $this->startTime;
339 $deltaPart = $nowts - $this->lastTime;
340 $this->pageCountPart = $this->pageCount - $this->pageCountLast;
341 $this->revCountPart = $this->revCount - $this->revCountLast;
344 $portion = $this->revCount / $this->maxCount;
345 $eta = $this->startTime + $deltaAll / $portion;
346 $etats = wfTimestamp( TS_DB, intval( $eta ) );
347 $pageRate = $this->pageCount / $deltaAll;
348 $revRate = $this->revCount / $deltaAll;
355 $pageRatePart = $this->pageCountPart / $deltaPart;
356 $revRatePart = $this->revCountPart / $deltaPart;
361 $this->progress( sprintf(
362 "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), "
363 . "%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]",
364 $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate,
365 $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats,
368 $this->lastTime = $nowts;
369 $this->revCountLast = $this->revCount;
373 function progress( $string ) {
374 fwrite( $this->stderr, $string . "\n" );
377 function fatalError( $msg ) {
378 $this->progress( "$msg\n" );
383 class ExportProgressFilter extends DumpFilter {
384 function __construct( &$sink, &$progress ) {
385 parent::__construct( $sink );
386 $this->progress = $progress;
389 function writeClosePage( $string ) {
390 parent::writeClosePage( $string );
391 $this->progress->reportPage();
394 function writeRevision( $rev, $string ) {
395 parent::writeRevision( $rev, $string );
396 $this->progress->revCount();