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
25 $originalDir = getcwd();
27 $optionsWithArgs = array( 'server', 'pagelist', 'start', 'end' );
29 require_once( 'commandLine.inc' );
30 require_once( 'SpecialExport.php' );
33 var $reportingInterval = 100;
34 var $reporting = true;
37 var $server = null; // use default
38 var $pages = null; // all pages
39 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
40 var $skipFooter = false; // don't output </mediawiki>
43 var $sink = null; // Output filters
45 function BackupDumper( $args ) {
46 $this->stderr
= fopen( "php://stderr", "wt" );
47 $this->sink
= $this->processArgs( $args );
55 function processArgs( $args ) {
57 'file' => 'DumpFileOutput',
58 'gzip' => 'DumpGZipOutput',
59 'bzip2' => 'DumpBZip2Output',
60 '7zip' => 'Dump7ZipOutput' );
62 'latest' => 'DumpLatestFilter',
63 'notalk' => 'DumpNotalkFilter',
64 'namespace' => 'DumpNamespaceFilter' );
67 foreach( $args as $arg ) {
68 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
69 @list
( $full, $opt, $val, $param ) = $matches;
72 if( !is_null( $sink ) ) {
75 if( !isset( $outputTypes[$val] ) ) {
76 die( "Unrecognized output sink type '$val'\n" );
78 $type = $outputTypes[$val];
79 $sink = new $type( $param );
82 if( is_null( $sink ) ) {
83 $this->progress( "Warning: assuming stdout for filter output\n" );
84 $sink = new DumpOutput();
86 if( !isset( $filterTypes[$val] ) ) {
87 die( "Unrecognized filter type '$val'\n" );
89 $type = $filterTypes[$val];
90 $filter = new $type( $sink, $param );
92 // references are lame in php...
98 //die( "Unrecognized dump option'$opt'\n" );
103 if( is_null( $sink ) ) {
104 $sink = new DumpOutput();
108 if( count( $sinks ) > 1 ) {
109 return new DumpMultiWriter( $sinks );
115 function dump( $history ) {
116 # This shouldn't happen if on console... ;)
117 header( 'Content-type: text/html; charset=UTF-8' );
119 # Notice messages will foul up your XML output even if they're
120 # relatively harmless.
121 ini_set( 'display_errors', false );
123 $this->startTime
= wfTime();
125 $dbr =& wfGetDB( DB_SLAVE
);
126 $this->maxCount
= $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
127 $this->startTime
= wfTime();
129 $db =& $this->backupDb();
130 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM
);
132 $wrapper = new ExportProgressFilter( $this->sink
, $this );
133 $exporter->setOutputSink( $wrapper );
135 if( !$this->skipHeader
)
136 $exporter->openStream();
138 if( is_null( $this->pages
) ) {
139 if( $this->startId ||
$this->endId
) {
140 $exporter->pagesByRange( $this->startId
, $this->endId
);
142 $exporter->allPages();
145 $exporter->pagesByName( $this->pages
);
148 if( !$this->skipFooter
)
149 $exporter->closeStream();
151 $this->report( true );
154 function &backupDb() {
155 global $wgDBadminuser, $wgDBadminpassword;
157 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
158 $timeout = 3600 * 24;
159 $db->query( "SET net_read_timeout=$timeout" );
160 $db->query( "SET net_write_timeout=$timeout" );
164 function backupServer() {
171 function reportPage() {
176 function revCount() {
180 function report( $final = false ) {
181 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
186 function showReport() {
187 if( $this->reporting
) {
188 $delta = wfTime() - $this->startTime
;
189 $now = wfTimestamp( TS_DB
);
191 $rate = $this->pageCount
/ $delta;
192 $revrate = $this->revCount
/ $delta;
193 $portion = $this->pageCount
/ $this->maxCount
;
194 $eta = $this->startTime +
$delta / $portion;
195 $etats = wfTimestamp( TS_DB
, intval( $eta ) );
202 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
206 function progress( $string ) {
207 fwrite( $this->stderr
, $string . "\n" );
211 class ExportProgressFilter
extends DumpFilter
{
212 function ExportProgressFilter( &$sink, &$progress ) {
213 parent
::DumpFilter( $sink );
214 $this->progress
= $progress;
217 function writeClosePage( $string ) {
218 parent
::writeClosePage( $string );
219 $this->progress
->reportPage();
222 function writeRevision( $rev, $string ) {
223 parent
::writeRevision( $rev, $string );
224 $this->progress
->revCount();
228 $dumper = new BackupDumper( $argv );
230 if( isset( $options['quiet'] ) ) {
231 $dumper->reporting
= false;
233 if( isset( $options['report'] ) ) {
234 $dumper->reportingInterval
= intval( $options['report'] );
236 if( isset( $options['server'] ) ) {
237 $dumper->server
= $options['server'];
240 if ( isset( $options['pagelist'] ) ) {
242 chdir( $originalDir );
243 $pages = file( $options['pagelist'] );
245 if ( $pages === false ) {
246 print "Unable to open file {$options['pagelist']}\n";
249 $pages = array_map( 'trim', $pages );
250 $dumper->pages
= array_filter( $pages, create_function( '$x', 'return $x !== "";' ) );
253 if( isset( $options['start'] ) ) {
254 $dumper->startId
= intval( $options['start'] );
256 if( isset( $options['end'] ) ) {
257 $dumper->endId
= intval( $options['end'] );
259 $dumper->skipHeader
= isset( $options['skip-header'] );
260 $dumper->skipFooter
= isset( $options['skip-footer'] );
262 if( isset( $options['full'] ) ) {
263 $dumper->dump( MW_EXPORT_FULL
);
264 } elseif( isset( $options['current'] ) ) {
265 $dumper->dump( MW_EXPORT_CURRENT
);
267 $dumper->progress( <<<END
268 This script dumps the wiki page database into an XML interchange wrapper
269 format for export or backup.
271 XML output is sent to stdout; progress reports are sent to stderr.
273 Usage: php dumpBackup.php <action> [<options>]
275 --full Dump complete history of every page.
276 --current Includes only the latest revision of each page.
279 --quiet Don't dump status reports to stderr.
280 --report=n Report position and speed after every n pages processed.
282 --server=h Force reading from MySQL server h
283 --start=n Start from page_id n
284 --end=n Stop before page_id n (exclusive)
285 --skip-header Don't output the <mediawiki> header
286 --skip-footer Don't output the </mediawiki> footer