* Accept --server option for database to read main query from on backup
[lhc/web/wiklou.git] / maintenance / dumpBackup.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
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.
10 *
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.
15 *
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
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 $options = array( 'full', 'current', 'server' );
26
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialExport.php' );
29
30 class BackupDumper {
31 var $reportingInterval = 100;
32 var $reporting = true;
33 var $pageCount = 0;
34 var $revCount = 0;
35 var $server = null; // use default
36
37 function BackupDumper() {
38 $this->stderr = fopen( "php://stderr", "wt" );
39 }
40
41 function dump( $history ) {
42 # This shouldn't happen if on console... ;)
43 header( 'Content-type: text/html; charset=UTF-8' );
44
45 # Notice messages will foul up your XML output even if they're
46 # relatively harmless.
47 ini_set( 'display_errors', false );
48
49 $this->startTime = wfTime();
50
51 $db =& $this->backupDb();
52 $exporter = new WikiExporter( $db, $history, MW_EXPORT_STREAM );
53 $exporter->setPageCallback( array( &$this, 'reportPage' ) );
54 $exporter->setRevisionCallback( array( &$this, 'revCount' ) );
55
56 $exporter->openStream();
57 $exporter->allPages();
58 $exporter->closeStream();
59
60 $this->report( true );
61 }
62
63 function &backupDb() {
64 global $wgDBadminuser, $wgDBadminpassword;
65 global $wgDBname;
66 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
67 return $db;
68 }
69
70 function backupServer() {
71 global $wgDBserver;
72 return $this->server
73 ? $this->server
74 : $wgDBserver;
75 }
76
77 function reportPage( $page ) {
78 $this->pageCount++;
79 $this->report();
80 }
81
82 function revCount( $rev ) {
83 $this->revCount++;
84 }
85
86 function report( $final = false ) {
87 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
88 $this->showReport();
89 }
90 }
91
92 function showReport() {
93 if( $this->reporting ) {
94 $delta = wfTime() - $this->startTime;
95 if( $delta ) {
96 $rate = $this->pageCount / $delta;
97 $revrate = $this->revCount / $delta;
98 } else {
99 $rate = '-';
100 $revrate = '-';
101 }
102 global $wgDBname;
103 $this->progress( "$wgDBname $this->pageCount ($rate pages/sec $revrate revs/sec)" );
104 }
105 }
106
107 function progress( $string ) {
108 fwrite( $this->stderr, $string . "\n" );
109 }
110 }
111
112 $dumper = new BackupDumper();
113 if( isset( $options['quiet'] ) ) {
114 $dumper->reporting = false;
115 }
116 if( isset( $options['report'] ) ) {
117 $dumper->reportingInterval = IntVal( $options['report'] );
118 }
119 if( isset( $options['server'] ) ) {
120 $dumper->server = $options['server'];
121 }
122 if( isset( $options['full'] ) ) {
123 $dumper->dump( MW_EXPORT_FULL );
124 } elseif( isset( $options['current'] ) ) {
125 $dumper->dump( MW_EXPORT_CURRENT );
126 } else {
127 $dumper->progress( <<<END
128 This script dumps the wiki page database into an XML interchange wrapper
129 format for export or backup.
130
131 XML output is sent to stdout; progress reports are sent to stderr.
132
133 Usage: php dumpBackup.php <action> [<options>]
134 Actions:
135 --full Dump complete history of every page.
136 --current Includes only the latest revision of each page.
137 Options:
138 --quiet Don't dump status reports to stderr.
139 --report=n Report position and speed after every n pages processed.
140 (Default: 100)
141 END
142 );
143 }
144
145 ?>