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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @subpackage SpecialPage
27 var $reportingInterval = 100;
28 var $reporting = true;
31 var $server = null; // use default
32 var $pages = null; // all pages
33 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
34 var $skipFooter = false; // don't output </mediawiki>
37 var $sink = null; // Output filters
38 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
40 function BackupDumper( $args ) {
41 $this->stderr = fopen( "php://stderr", "wt" );
43 // Built-in output and filter plugins
44 $this->registerOutput( 'file', 'DumpFileOutput' );
45 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
46 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
47 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
49 $this->registerFilter( 'latest', 'DumpLatestFilter' );
50 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
51 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
53 $this->sink = $this->processArgs( $args );
58 * @param string $class name of output filter plugin class
60 function registerOutput( $name, $class ) {
61 $this->outputTypes[$name] = $class;
66 * @param string $class name of filter plugin class
68 function registerFilter( $name, $class ) {
69 $this->filterTypes[$name] = $class;
73 * Load a plugin and register it
74 * @param string $class Name of plugin class; must have a static 'register'
75 * method that takes a BackupDumper as a parameter.
76 * @param string $file Full or relative path to the PHP file to load, or empty
78 function loadPlugin( $class, $file ) {
80 require_once( $file );
82 $register = array( $class, 'register' );
83 call_user_func_array( $register, array( &$this ) );
91 function processArgs( $args ) {
94 foreach( $args as $arg ) {
95 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
96 @list( $full, $opt, $val, $param ) = $matches;
99 $this->loadPlugin( $val, $param );
102 if( !is_null( $sink ) ) {
105 if( !isset( $this->outputTypes[$val] ) ) {
106 wfDie( "Unrecognized output sink type '$val'\n" );
108 $type = $this->outputTypes[$val];
109 $sink = new $type( $param );
112 if( is_null( $sink ) ) {
113 $this->progress( "Warning: assuming stdout for filter output\n" );
114 $sink = new DumpOutput();
116 if( !isset( $this->filterTypes[$val] ) ) {
117 wfDie( "Unrecognized filter type '$val'\n" );
119 $type = $this->filterTypes[$val];
120 $filter = new $type( $sink, $param );
122 // references are lame in php...
128 $this->processOption( $opt, $val, $param );
133 if( is_null( $sink ) ) {
134 $sink = new DumpOutput();
138 if( count( $sinks ) > 1 ) {
139 return new DumpMultiWriter( $sinks );
145 function processOption( $opt, $val, $param ) {
146 // extension point for subclasses to add options
149 function dump( $history, $text = MW_EXPORT_TEXT ) {
150 # This shouldn't happen if on console... ;)
151 header( 'Content-type: text/html; charset=UTF-8' );
153 # Notice messages will foul up your XML output even if they're
154 # relatively harmless.
155 ini_set( 'display_errors', false );
157 $this->startTime = wfTime();
159 $dbr =& wfGetDB( DB_SLAVE );
160 $this->maxCount = $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
161 $this->startTime = wfTime();
163 $db =& $this->backupDb();
164 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM, $text );
166 $wrapper = new ExportProgressFilter( $this->sink, $this );
167 $exporter->setOutputSink( $wrapper );
169 if( !$this->skipHeader )
170 $exporter->openStream();
172 if( is_null( $this->pages ) ) {
173 if( $this->startId || $this->endId ) {
174 $exporter->pagesByRange( $this->startId, $this->endId );
176 $exporter->allPages();
179 $exporter->pagesByName( $this->pages );
182 if( !$this->skipFooter )
183 $exporter->closeStream();
185 $this->report( true );
188 function &backupDb() {
189 global $wgDBadminuser, $wgDBadminpassword;
191 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
192 $timeout = 3600 * 24;
193 $db->query( "SET net_read_timeout=$timeout" );
194 $db->query( "SET net_write_timeout=$timeout" );
198 function backupServer() {
205 function reportPage() {
210 function revCount() {
214 function report( $final = false ) {
215 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
220 function showReport() {
221 if( $this->reporting ) {
222 $delta = wfTime() - $this->startTime;
223 $now = wfTimestamp( TS_DB );
225 $rate = $this->pageCount / $delta;
226 $revrate = $this->revCount / $delta;
227 $portion = $this->pageCount / $this->maxCount;
228 $eta = $this->startTime + $delta / $portion;
229 $etats = wfTimestamp( TS_DB, intval( $eta ) );
236 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
240 function progress( $string ) {
241 fwrite( $this->stderr, $string . "\n" );
245 class ExportProgressFilter extends DumpFilter {
246 function ExportProgressFilter( &$sink, &$progress ) {
247 parent::DumpFilter( $sink );
248 $this->progress = $progress;
251 function writeClosePage( $string ) {
252 parent::writeClosePage( $string );
253 $this->progress->reportPage();
256 function writeRevision( $rev, $string ) {
257 parent::writeRevision( $rev, $string );
258 $this->progress->revCount();