c5b05b9bc6114e709de61a17c82787dc8b2564c9
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>
44 function BackupDumper() {
45 $this->stderr
= fopen( "php://stderr", "wt" );
48 function dump( $history ) {
49 # This shouldn't happen if on console... ;)
50 header( 'Content-type: text/html; charset=UTF-8' );
52 # Notice messages will foul up your XML output even if they're
53 # relatively harmless.
54 ini_set( 'display_errors', false );
56 $this->startTime
= wfTime();
58 $dbr =& wfGetDB( DB_SLAVE
);
59 $this->maxCount
= $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
60 $this->startTime
= wfTime();
62 $db =& $this->backupDb();
63 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM
);
64 $exporter->setPageCallback( array( &$this, 'reportPage' ) );
65 $exporter->setRevisionCallback( array( &$this, 'revCount' ) );
67 if( !$this->skipHeader
)
68 $exporter->openStream();
70 if( is_null( $this->pages
) ) {
71 if( $this->startId ||
$this->endId
) {
72 $exporter->pagesByRange( $this->startId
, $this->endId
);
74 $exporter->allPages();
77 $exporter->pagesByName( $this->pages
);
80 if( !$this->skipFooter
)
81 $exporter->closeStream();
83 $this->report( true );
86 function &backupDb() {
87 global $wgDBadminuser, $wgDBadminpassword;
89 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
91 $db->query( "SET net_read_timeout=$timeout" );
92 $db->query( "SET net_write_timeout=$timeout" );
96 function backupServer() {
103 function reportPage( $page ) {
108 function revCount( $rev ) {
112 function report( $final = false ) {
113 if( $final xor ( $this->pageCount %
$this->reportingInterval
== 0 ) ) {
118 function showReport() {
119 if( $this->reporting
) {
120 $delta = wfTime() - $this->startTime
;
121 $now = wfTimestamp( TS_DB
);
123 $rate = $this->pageCount
/ $delta;
124 $revrate = $this->revCount
/ $delta;
125 $portion = $this->pageCount
/ $this->maxCount
;
126 $eta = $this->startTime +
$delta / $portion;
127 $etats = wfTimestamp( TS_DB
, intval( $eta ) );
134 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
138 function progress( $string ) {
139 fwrite( $this->stderr
, $string . "\n" );
143 $dumper = new BackupDumper();
144 if( isset( $options['quiet'] ) ) {
145 $dumper->reporting
= false;
147 if( isset( $options['report'] ) ) {
148 $dumper->reportingInterval
= intval( $options['report'] );
150 if( isset( $options['server'] ) ) {
151 $dumper->server
= $options['server'];
154 if ( isset( $options['pagelist'] ) ) {
156 chdir( $originalDir );
157 $pages = file( $options['pagelist'] );
159 if ( $pages === false ) {
160 print "Unable to open file {$options['pagelist']}\n";
163 $pages = array_map( 'trim', $pages );
164 $dumper->pages
= array_filter( $pages, create_function( '$x', 'return $x !== "";' ) );
167 if( isset( $options['start'] ) ) {
168 $dumper->startId
= intval( $options['start'] );
170 if( isset( $options['end'] ) ) {
171 $dumper->endId
= intval( $options['end'] );
173 $dumper->skipHeader
= isset( $options['skip-header'] );
174 $dumper->skipFooter
= isset( $options['skip-footer'] );
176 if( isset( $options['full'] ) ) {
177 $dumper->dump( MW_EXPORT_FULL
);
178 } elseif( isset( $options['current'] ) ) {
179 $dumper->dump( MW_EXPORT_CURRENT
);
181 $dumper->progress( <<<END
182 This script dumps the wiki page database into an XML interchange wrapper
183 format for export or backup.
185 XML output is sent to stdout; progress reports are sent to stderr.
187 Usage: php dumpBackup.php <action> [<options>]
189 --full Dump complete history of every page.
190 --current Includes only the latest revision of each page.
193 --quiet Don't dump status reports to stderr.
194 --report=n Report position and speed after every n pages processed.
196 --server=h Force reading from MySQL server h
197 --start=n Start from page_id n
198 --end=n Stop before page_id n (exclusive)
199 --skip-header Don't output the <mediawiki> header
200 --skip-footer Don't output the </mediawiki> footer