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 * @subpackage SpecialPage
25 class DumpDBZip2Output extends DumpPipeOutput {
26 function DumpDBZip2Output( $file ) {
27 parent::DumpPipeOutput( "dbzip2", $file );
32 var $reportingInterval = 100;
33 var $reporting = true;
36 var $server = null; // use default
37 var $pages = null; // all pages
38 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
39 var $skipFooter = false; // don't output </mediawiki>
42 var $sink = null; // Output filters
43 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
45 function BackupDumper( $args ) {
46 $this->stderr = fopen( "php://stderr", "wt" );
48 // Built-in output and filter plugins
49 $this->registerOutput( 'file', 'DumpFileOutput' );
50 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
51 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
52 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
53 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
55 $this->registerFilter( 'latest', 'DumpLatestFilter' );
56 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
57 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
59 $this->sink = $this->processArgs( $args );
64 * @param string $class name of output filter plugin class
66 function registerOutput( $name, $class ) {
67 $this->outputTypes[$name] = $class;
72 * @param string $class name of filter plugin class
74 function registerFilter( $name, $class ) {
75 $this->filterTypes[$name] = $class;
79 * Load a plugin and register it
80 * @param string $class Name of plugin class; must have a static 'register'
81 * method that takes a BackupDumper as a parameter.
82 * @param string $file Full or relative path to the PHP file to load, or empty
84 function loadPlugin( $class, $file ) {
86 require_once( $file );
88 $register = array( $class, 'register' );
89 call_user_func_array( $register, array( &$this ) );
97 function processArgs( $args ) {
100 foreach( $args as $arg ) {
102 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
103 @list( /* $full */ , $opt, $val, $param ) = $matches;
106 $this->loadPlugin( $val, $param );
109 if( !is_null( $sink ) ) {
112 if( !isset( $this->outputTypes[$val] ) ) {
113 wfDie( "Unrecognized output sink type '$val'\n" );
115 $type = $this->outputTypes[$val];
116 $sink = new $type( $param );
119 if( is_null( $sink ) ) {
120 $this->progress( "Warning: assuming stdout for filter output\n" );
121 $sink = new DumpOutput();
123 if( !isset( $this->filterTypes[$val] ) ) {
124 wfDie( "Unrecognized filter type '$val'\n" );
126 $type = $this->filterTypes[$val];
127 $filter = new $type( $sink, $param );
129 // references are lame in php...
135 $this->reportingInterval = intval( $val );
138 $this->server = $val;
141 if( !function_exists( 'utf8_normalize' ) ) {
142 dl( "php_utfnormal.so" );
143 if( !function_exists( 'utf8_normalize' ) ) {
144 wfDie( "Failed to load UTF-8 normalization extension. " .
145 "Install or remove --force-normal parameter to use slower code.\n" );
150 $this->processOption( $opt, $val, $param );
155 if( is_null( $sink ) ) {
156 $sink = new DumpOutput();
160 if( count( $sinks ) > 1 ) {
161 return new DumpMultiWriter( $sinks );
167 function processOption( $opt, $val, $param ) {
168 // extension point for subclasses to add options
171 function dump( $history, $text = MW_EXPORT_TEXT ) {
172 # Notice messages will foul up your XML output even if they're
173 # relatively harmless.
174 ini_set( 'display_errors', false );
176 $this->initProgress( $history );
178 $db =& $this->backupDb();
179 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
181 $wrapper = new ExportProgressFilter( $this->sink, $this );
182 $exporter->setOutputSink( $wrapper );
184 if( !$this->skipHeader )
185 $exporter->openStream();
187 if( is_null( $this->pages ) ) {
188 if( $this->startId || $this->endId ) {
189 $exporter->pagesByRange( $this->startId, $this->endId );
191 $exporter->allPages();
194 $exporter->pagesByName( $this->pages );
197 if( !$this->skipFooter )
198 $exporter->closeStream();
200 $this->report( true );
204 * Initialise starting time and maximum revision count.
205 * We'll make ETA calculations based an progress, assuming relatively
206 * constant per-revision rate.
207 * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
209 function initProgress( $history = WikiExporter::FULL ) {
210 $table = ($history == WikiExporter::CURRENT) ? 'page' : 'revision';
211 $field = ($history == WikiExporter::CURRENT) ? 'page_id' : 'rev_id';
213 $dbr =& wfGetDB( DB_SLAVE );
214 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
215 $this->startTime = wfTime();
218 function &backupDb() {
219 global $wgDBadminuser, $wgDBadminpassword;
220 global $wgDBname, $wgDebugDumpSql;
221 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
222 $db = new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
223 $timeout = 3600 * 24;
224 $db->query( "SET net_read_timeout=$timeout" );
225 $db->query( "SET net_write_timeout=$timeout" );
229 function backupServer() {
236 function reportPage() {
240 function revCount() {
245 function report( $final = false ) {
246 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
251 function showReport() {
252 if( $this->reporting ) {
253 $delta = wfTime() - $this->startTime;
254 $now = wfTimestamp( TS_DB );
256 $rate = $this->pageCount / $delta;
257 $revrate = $this->revCount / $delta;
258 $portion = $this->revCount / $this->maxCount;
259 $eta = $this->startTime + $delta / $portion;
260 $etats = wfTimestamp( TS_DB, intval( $eta ) );
266 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
267 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
271 function progress( $string ) {
272 fwrite( $this->stderr, $string . "\n" );
276 class ExportProgressFilter extends DumpFilter {
277 function ExportProgressFilter( &$sink, &$progress ) {
278 parent::DumpFilter( $sink );
279 $this->progress = $progress;
282 function writeClosePage( $string ) {
283 parent::writeClosePage( $string );
284 $this->progress->reportPage();
287 function writeRevision( $rev, $string ) {
288 parent::writeRevision( $rev, $string );
289 $this->progress->revCount();