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