3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
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.
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.
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Dump Maintenance
26 * @ingroup Dump Maintenance
28 class DumpDBZip2Output extends DumpPipeOutput {
29 function DumpDBZip2Output( $file ) {
30 parent::DumpPipeOutput( "dbzip2", $file );
35 * @ingroup Dump Maintenance
38 var $reportingInterval = 100;
39 var $reporting = true;
42 var $server = null; // use default
43 var $pages = null; // all pages
44 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
45 var $skipFooter = false; // don't output </mediawiki>
48 var $sink = null; // Output filters
49 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
50 var $dumpUploads = false;
52 function BackupDumper( $args ) {
53 $this->stderr = fopen( "php://stderr", "wt" );
55 // Built-in output and filter plugins
56 $this->registerOutput( 'file', 'DumpFileOutput' );
57 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
58 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
59 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
60 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
62 $this->registerFilter( 'latest', 'DumpLatestFilter' );
63 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
64 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
66 $this->sink = $this->processArgs( $args );
71 * @param string $class name of output filter plugin class
73 function registerOutput( $name, $class ) {
74 $this->outputTypes[$name] = $class;
79 * @param string $class name of filter plugin class
81 function registerFilter( $name, $class ) {
82 $this->filterTypes[$name] = $class;
86 * Load a plugin and register it
87 * @param string $class Name of plugin class; must have a static 'register'
88 * method that takes a BackupDumper as a parameter.
89 * @param string $file Full or relative path to the PHP file to load, or empty
91 function loadPlugin( $class, $file ) {
93 require_once( $file );
95 $register = array( $class, 'register' );
96 call_user_func_array( $register, array( &$this ) );
104 function processArgs( $args ) {
107 foreach( $args as $arg ) {
109 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
110 @list( /* $full */ , $opt, $val, $param ) = $matches;
113 $this->loadPlugin( $val, $param );
116 if( !is_null( $sink ) ) {
119 if( !isset( $this->outputTypes[$val] ) ) {
120 wfDie( "Unrecognized output sink type '$val'\n" );
122 $type = $this->outputTypes[$val];
123 $sink = new $type( $param );
126 if( is_null( $sink ) ) {
127 $this->progress( "Warning: assuming stdout for filter output\n" );
128 $sink = new DumpOutput();
130 if( !isset( $this->filterTypes[$val] ) ) {
131 wfDie( "Unrecognized filter type '$val'\n" );
133 $type = $this->filterTypes[$val];
134 $filter = new $type( $sink, $param );
136 // references are lame in php...
142 $this->reportingInterval = intval( $val );
145 $this->server = $val;
148 if( !function_exists( 'utf8_normalize' ) ) {
149 dl( "php_utfnormal.so" );
150 if( !function_exists( 'utf8_normalize' ) ) {
151 wfDie( "Failed to load UTF-8 normalization extension. " .
152 "Install or remove --force-normal parameter to use slower code.\n" );
157 $this->processOption( $opt, $val, $param );
162 if( is_null( $sink ) ) {
163 $sink = new DumpOutput();
167 if( count( $sinks ) > 1 ) {
168 return new DumpMultiWriter( $sinks );
174 function processOption( $opt, $val, $param ) {
175 // extension point for subclasses to add options
178 function dump( $history, $text = MW_EXPORT_TEXT ) {
179 # Notice messages will foul up your XML output even if they're
180 # relatively harmless.
181 if( ini_get( 'display_errors' ) )
182 ini_set( 'display_errors', 'stderr' );
184 $this->initProgress( $history );
186 $db = $this->backupDb();
187 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
188 $exporter->dumpUploads = $this->dumpUploads;
190 $wrapper = new ExportProgressFilter( $this->sink, $this );
191 $exporter->setOutputSink( $wrapper );
193 if( !$this->skipHeader )
194 $exporter->openStream();
196 if( is_null( $this->pages ) ) {
197 if( $this->startId || $this->endId ) {
198 $exporter->pagesByRange( $this->startId, $this->endId );
200 $exporter->allPages();
203 $exporter->pagesByName( $this->pages );
206 if( !$this->skipFooter )
207 $exporter->closeStream();
209 $this->report( true );
213 * Initialise starting time and maximum revision count.
214 * We'll make ETA calculations based an progress, assuming relatively
215 * constant per-revision rate.
216 * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
218 function initProgress( $history = WikiExporter::FULL ) {
219 $table = ($history == WikiExporter::CURRENT) ? 'page' : 'revision';
220 $field = ($history == WikiExporter::CURRENT) ? 'page_id' : 'rev_id';
222 $dbr = wfGetDB( DB_SLAVE );
223 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
224 $this->startTime = wfTime();
227 function backupDb() {
228 global $wgDBadminuser, $wgDBadminpassword;
229 global $wgDBname, $wgDebugDumpSql, $wgDBtype;
230 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
232 $class = 'Database' . ucfirst($wgDBtype);
233 $db = new $class( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
235 // Discourage the server from disconnecting us if it takes a long time
236 // to read out the big ol' batch query.
237 $db->setTimeout( 3600 * 24 );
242 function backupServer() {
249 function reportPage() {
253 function revCount() {
258 function report( $final = false ) {
259 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
264 function showReport() {
265 if( $this->reporting ) {
266 $delta = wfTime() - $this->startTime;
267 $now = wfTimestamp( TS_DB );
269 $rate = $this->pageCount / $delta;
270 $revrate = $this->revCount / $delta;
271 $portion = $this->revCount / $this->maxCount;
272 $eta = $this->startTime + $delta / $portion;
273 $etats = wfTimestamp( TS_DB, intval( $eta ) );
279 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
280 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
284 function progress( $string ) {
285 fwrite( $this->stderr, $string . "\n" );
289 class ExportProgressFilter extends DumpFilter {
290 function ExportProgressFilter( &$sink, &$progress ) {
291 parent::DumpFilter( $sink );
292 $this->progress = $progress;
295 function writeClosePage( $string ) {
296 parent::writeClosePage( $string );
297 $this->progress->reportPage();
300 function writeRevision( $rev, $string ) {
301 parent::writeRevision( $rev, $string );
302 $this->progress->revCount();