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 $optionsWithArgs = array( 'server', 'pagelist' );
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialExport.php' );
31 var $reportingInterval = 100;
32 var $reporting = true;
35 var $server = null; // use default
36 var $pages = null; // all pages
38 function BackupDumper() {
39 $this->stderr
= fopen( "php://stderr", "wt" );
42 function dump( $history ) {
43 # This shouldn't happen if on console... ;)
44 header( 'Content-type: text/html; charset=UTF-8' );
46 # Notice messages will foul up your XML output even if they're
47 # relatively harmless.
48 ini_set( 'display_errors', false );
50 $this->startTime
= wfTime();
52 $dbr =& wfGetDB( DB_SLAVE
);
53 $this->maxCount
= $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
54 $this->startTime
= wfTime();
56 $db =& $this->backupDb();
57 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM
);
58 $exporter->setPageCallback( array( &$this, 'reportPage' ) );
59 $exporter->setRevisionCallback( array( &$this, 'revCount' ) );
61 $exporter->openStream();
63 if ( is_null( $this->pages
) ) {
64 $exporter->allPages();
66 $exporter->pagesByName( $this->pages
);
69 $exporter->closeStream();
71 $this->report( true );
74 function &backupDb() {
75 global $wgDBadminuser, $wgDBadminpassword;
77 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
79 $db->query( "SET net_read_timeout=$timeout" );
80 $db->query( "SET net_write_timeout=$timeout" );
84 function backupServer() {
91 function reportPage( $page ) {
96 function revCount( $rev ) {
100 function report( $final = false ) {
101 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
106 function showReport() {
107 if( $this->reporting
) {
108 $delta = wfTime() - $this->startTime
;
109 $now = wfTimestamp( TS_DB
);
111 $rate = $this->pageCount
/ $delta;
112 $revrate = $this->revCount
/ $delta;
113 $portion = $this->pageCount
/ $this->maxCount
;
114 $eta = $this->startTime +
$delta / $portion;
115 $etats = wfTimestamp( TS_DB
, intval( $eta ) );
122 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
126 function progress( $string ) {
127 fwrite( $this->stderr
, $string . "\n" );
131 $dumper = new BackupDumper();
132 if( isset( $options['quiet'] ) ) {
133 $dumper->reporting
= false;
135 if( isset( $options['report'] ) ) {
136 $dumper->reportingInterval
= intval( $options['report'] );
138 if( isset( $options['server'] ) ) {
139 $dumper->server
= $options['server'];
142 if ( isset( $options['pagelist'] ) ) {
144 chdir( 'maintenance' );
145 $pages = file( $options['pagelist'] );
147 if ( $pages === false ) {
148 print "Unable to open file {$options['pagelist']}\n";
151 $pages = array_map( 'trim', $pages );
152 $dumper->pages
= array_filter( $pages, create_function( '$x', 'return $x !== "";' ) );
155 if( isset( $options['full'] ) ) {
156 $dumper->dump( MW_EXPORT_FULL
);
157 } elseif( isset( $options['current'] ) ) {
158 $dumper->dump( MW_EXPORT_CURRENT
);
160 $dumper->progress( <<<END
161 This script dumps the wiki page database into an XML interchange wrapper
162 format for export or backup.
164 XML output is sent to stdout; progress reports are sent to stderr.
166 Usage: php dumpBackup.php <action> [<options>]
168 --full Dump complete history of every page.
169 --current Includes only the latest revision of each page.
171 --quiet Don't dump status reports to stderr.
172 --report=n Report position and speed after every n pages processed.