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