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 ) {
101 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
102 @list( $full, $opt, $val, $param ) = $matches;
105 $this->loadPlugin( $val, $param );
108 if( !is_null( $sink ) ) {
111 if( !isset( $this->outputTypes[$val] ) ) {
112 wfDie( "Unrecognized output sink type '$val'\n" );
114 $type = $this->outputTypes[$val];
115 $sink = new $type( $param );
118 if( is_null( $sink ) ) {
119 $this->progress( "Warning: assuming stdout for filter output\n" );
120 $sink = new DumpOutput();
122 if( !isset( $this->filterTypes[$val] ) ) {
123 wfDie( "Unrecognized filter type '$val'\n" );
125 $type = $this->filterTypes[$val];
126 $filter = new $type( $sink, $param );
128 // references are lame in php...
134 $this->reportingInterval = intval( $val );
137 $this->server = $val;
140 if( !function_exists( 'utf8_normalize' ) ) {
141 dl( "php_utfnormal.so" );
142 if( !function_exists( 'utf8_normalize' ) ) {
143 wfDie( "Failed to load UTF-8 normalization extension. " .
144 "Install or remove --force-normal parameter to use slower code.\n" );
149 $this->processOption( $opt, $val, $param );
154 if( is_null( $sink ) ) {
155 $sink = new DumpOutput();
159 if( count( $sinks ) > 1 ) {
160 return new DumpMultiWriter( $sinks );
166 function processOption( $opt, $val, $param ) {
167 // extension point for subclasses to add options
170 function dump( $history, $text = MW_EXPORT_TEXT ) {
171 # This shouldn't happen if on console... ;)
172 header( 'Content-type: text/html; charset=UTF-8' );
174 # Notice messages will foul up your XML output even if they're
175 # relatively harmless.
176 ini_set( 'display_errors', false );
178 $this->initProgress( $history );
180 $db =& $this->backupDb();
181 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
183 $wrapper = new ExportProgressFilter( $this->sink, $this );
184 $exporter->setOutputSink( $wrapper );
186 if( !$this->skipHeader )
187 $exporter->openStream();
189 if( is_null( $this->pages ) ) {
190 if( $this->startId || $this->endId ) {
191 $exporter->pagesByRange( $this->startId, $this->endId );
193 $exporter->allPages();
196 $exporter->pagesByName( $this->pages );
199 if( !$this->skipFooter )
200 $exporter->closeStream();
202 $this->report( true );
206 * Initialise starting time and maximum revision count.
207 * We'll make ETA calculations based an progress, assuming relatively
208 * constant per-revision rate.
209 * @param int $history MW_EXPORT_CURRENT or MW_EXPORT_FULL
211 function initProgress( $history = MW_EXPORT_FULL ) {
212 $table = ($history == MW_EXPORT_CURRENT) ? 'page' : 'revision';
213 $field = ($history == MW_EXPORT_CURRENT) ? 'page_id' : 'rev_id';
215 $dbr =& wfGetDB( DB_SLAVE );
216 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
217 $this->startTime = wfTime();
220 function &backupDb() {
221 global $wgDBadminuser, $wgDBadminpassword;
222 global $wgDBname, $wgDebugDumpSql;
223 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
224 $db = new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
225 $timeout = 3600 * 24;
226 $db->query( "SET net_read_timeout=$timeout" );
227 $db->query( "SET net_write_timeout=$timeout" );
231 function backupServer() {
238 function reportPage() {
242 function revCount() {
247 function report( $final = false ) {
248 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
253 function showReport() {
254 if( $this->reporting ) {
255 $delta = wfTime() - $this->startTime;
256 $now = wfTimestamp( TS_DB );
258 $rate = $this->pageCount / $delta;
259 $revrate = $this->revCount / $delta;
260 $portion = $this->revCount / $this->maxCount;
261 $eta = $this->startTime + $delta / $portion;
262 $etats = wfTimestamp( TS_DB, intval( $eta ) );
269 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
270 $now, $wgDBname, $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
274 function progress( $string ) {
275 fwrite( $this->stderr, $string . "\n" );
279 class ExportProgressFilter extends DumpFilter {
280 function ExportProgressFilter( &$sink, &$progress ) {
281 parent::DumpFilter( $sink );
282 $this->progress = $progress;
285 function writeClosePage( $string ) {
286 parent::writeClosePage( $string );
287 $this->progress->reportPage();
290 function writeRevision( $rev, $string ) {
291 parent::writeRevision( $rev, $string );
292 $this->progress->revCount();