* Add options to dumpBackup.php for making split/partial dumps by page id
[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 $originalDir = getcwd();
26
27 $optionsWithArgs = array( 'server', 'pagelist', 'start', 'end' );
28
29 require_once( 'commandLine.inc' );
30 require_once( 'SpecialExport.php' );
31
32 class BackupDumper {
33 var $reportingInterval = 100;
34 var $reporting = true;
35 var $pageCount = 0;
36 var $revCount = 0;
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>
41 var $startId = 0;
42 var $endId = 0;
43
44 function BackupDumper() {
45 $this->stderr = fopen( "php://stderr", "wt" );
46 }
47
48 function dump( $history ) {
49 # This shouldn't happen if on console... ;)
50 header( 'Content-type: text/html; charset=UTF-8' );
51
52 # Notice messages will foul up your XML output even if they're
53 # relatively harmless.
54 ini_set( 'display_errors', false );
55
56 $this->startTime = wfTime();
57
58 $dbr =& wfGetDB( DB_SLAVE );
59 $this->maxCount = $dbr->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
60 $this->startTime = wfTime();
61
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' ) );
66
67 if( !$this->skipHeader )
68 $exporter->openStream();
69
70 if( is_null( $this->pages ) ) {
71 if( $this->startId || $this->endId ) {
72 $exporter->pagesByRange( $this->startId, $this->endId );
73 } else {
74 $exporter->allPages();
75 }
76 } else {
77 $exporter->pagesByName( $this->pages );
78 }
79
80 if( !$this->skipFooter )
81 $exporter->closeStream();
82
83 $this->report( true );
84 }
85
86 function &backupDb() {
87 global $wgDBadminuser, $wgDBadminpassword;
88 global $wgDBname;
89 $db =& new Database( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname );
90 $timeout = 3600 * 24;
91 $db->query( "SET net_read_timeout=$timeout" );
92 $db->query( "SET net_write_timeout=$timeout" );
93 return $db;
94 }
95
96 function backupServer() {
97 global $wgDBserver;
98 return $this->server
99 ? $this->server
100 : $wgDBserver;
101 }
102
103 function reportPage( $page ) {
104 $this->pageCount++;
105 $this->report();
106 }
107
108 function revCount( $rev ) {
109 $this->revCount++;
110 }
111
112 function report( $final = false ) {
113 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
114 $this->showReport();
115 }
116 }
117
118 function showReport() {
119 if( $this->reporting ) {
120 $delta = wfTime() - $this->startTime;
121 $now = wfTimestamp( TS_DB );
122 if( $delta ) {
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 ) );
128 } else {
129 $rate = '-';
130 $revrate = '-';
131 $etats = '-';
132 }
133 global $wgDBname;
134 $this->progress( "$now: $wgDBname $this->pageCount, ETA $etats ($rate pages/sec $revrate revs/sec)" );
135 }
136 }
137
138 function progress( $string ) {
139 fwrite( $this->stderr, $string . "\n" );
140 }
141 }
142
143 $dumper = new BackupDumper();
144 if( isset( $options['quiet'] ) ) {
145 $dumper->reporting = false;
146 }
147 if( isset( $options['report'] ) ) {
148 $dumper->reportingInterval = intval( $options['report'] );
149 }
150 if( isset( $options['server'] ) ) {
151 $dumper->server = $options['server'];
152 }
153
154 if ( isset( $options['pagelist'] ) ) {
155 $olddir = getcwd();
156 chdir( $originalDir );
157 $pages = file( $options['pagelist'] );
158 chdir( $olddir );
159 if ( $pages === false ) {
160 print "Unable to open file {$options['pagelist']}\n";
161 exit;
162 }
163 $pages = array_map( 'trim', $pages );
164 $dumper->pages = array_filter( $pages, create_function( '$x', 'return $x !== "";' ) );
165 }
166
167 if( isset( $options['start'] ) ) {
168 $dumper->startId = intval( $options['start'] );
169 }
170 if( isset( $options['end'] ) ) {
171 $dumper->endId = intval( $options['end'] );
172 }
173 $dumper->skipHeader = isset( $options['skip-header'] );
174 $dumper->skipFooter = isset( $options['skip-footer'] );
175
176 if( isset( $options['full'] ) ) {
177 $dumper->dump( MW_EXPORT_FULL );
178 } elseif( isset( $options['current'] ) ) {
179 $dumper->dump( MW_EXPORT_CURRENT );
180 } else {
181 $dumper->progress( <<<END
182 This script dumps the wiki page database into an XML interchange wrapper
183 format for export or backup.
184
185 XML output is sent to stdout; progress reports are sent to stderr.
186
187 Usage: php dumpBackup.php <action> [<options>]
188 Actions:
189 --full Dump complete history of every page.
190 --current Includes only the latest revision of each page.
191
192 Options:
193 --quiet Don't dump status reports to stderr.
194 --report=n Report position and speed after every n pages processed.
195 (Default: 100)
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
201 END
202 );
203 }
204
205 ?>