* Add a nice fieldset to the input form to be consistent with other forms.
[lhc/web/wiklou.git] / maintenance / backup.inc
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @addtogroup SpecialPage
22 */
23
24 class DumpDBZip2Output extends DumpPipeOutput {
25 function DumpDBZip2Output( $file ) {
26 parent::DumpPipeOutput( "dbzip2", $file );
27 }
28 }
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 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
38 var $skipFooter = false; // don't output </mediawiki>
39 var $startId = 0;
40 var $endId = 0;
41 var $sink = null; // Output filters
42 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
43 var $dumpUploads = false;
44
45 function BackupDumper( $args ) {
46 $this->stderr = fopen( "php://stderr", "wt" );
47
48 // Built-in output and filter plugins
49 $this->registerOutput( 'file', 'DumpFileOutput' );
50 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
51 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
52 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
53 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
54
55 $this->registerFilter( 'latest', 'DumpLatestFilter' );
56 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
57 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
58
59 $this->sink = $this->processArgs( $args );
60 }
61
62 /**
63 * @param string $name
64 * @param string $class name of output filter plugin class
65 */
66 function registerOutput( $name, $class ) {
67 $this->outputTypes[$name] = $class;
68 }
69
70 /**
71 * @param string $name
72 * @param string $class name of filter plugin class
73 */
74 function registerFilter( $name, $class ) {
75 $this->filterTypes[$name] = $class;
76 }
77
78 /**
79 * Load a plugin and register it
80 * @param string $class Name of plugin class; must have a static 'register'
81 * method that takes a BackupDumper as a parameter.
82 * @param string $file Full or relative path to the PHP file to load, or empty
83 */
84 function loadPlugin( $class, $file ) {
85 if( $file != '' ) {
86 require_once( $file );
87 }
88 $register = array( $class, 'register' );
89 call_user_func_array( $register, array( &$this ) );
90 }
91
92 /**
93 * @param array $args
94 * @return array
95 * @static
96 */
97 function processArgs( $args ) {
98 $sink = null;
99 $sinks = array();
100 foreach( $args as $arg ) {
101 $matches = array();
102 if( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
103 @list( /* $full */ , $opt, $val, $param ) = $matches;
104 switch( $opt ) {
105 case "plugin":
106 $this->loadPlugin( $val, $param );
107 break;
108 case "output":
109 if( !is_null( $sink ) ) {
110 $sinks[] = $sink;
111 }
112 if( !isset( $this->outputTypes[$val] ) ) {
113 wfDie( "Unrecognized output sink type '$val'\n" );
114 }
115 $type = $this->outputTypes[$val];
116 $sink = new $type( $param );
117 break;
118 case "filter":
119 if( is_null( $sink ) ) {
120 $this->progress( "Warning: assuming stdout for filter output\n" );
121 $sink = new DumpOutput();
122 }
123 if( !isset( $this->filterTypes[$val] ) ) {
124 wfDie( "Unrecognized filter type '$val'\n" );
125 }
126 $type = $this->filterTypes[$val];
127 $filter = new $type( $sink, $param );
128
129 // references are lame in php...
130 unset( $sink );
131 $sink = $filter;
132
133 break;
134 case "report":
135 $this->reportingInterval = intval( $val );
136 break;
137 case "server":
138 $this->server = $val;
139 break;
140 case "force-normal":
141 if( !function_exists( 'utf8_normalize' ) ) {
142 dl( "php_utfnormal.so" );
143 if( !function_exists( 'utf8_normalize' ) ) {
144 wfDie( "Failed to load UTF-8 normalization extension. " .
145 "Install or remove --force-normal parameter to use slower code.\n" );
146 }
147 }
148 break;
149 default:
150 $this->processOption( $opt, $val, $param );
151 }
152 }
153 }
154
155 if( is_null( $sink ) ) {
156 $sink = new DumpOutput();
157 }
158 $sinks[] = $sink;
159
160 if( count( $sinks ) > 1 ) {
161 return new DumpMultiWriter( $sinks );
162 } else {
163 return $sink;
164 }
165 }
166
167 function processOption( $opt, $val, $param ) {
168 // extension point for subclasses to add options
169 }
170
171 function dump( $history, $text = MW_EXPORT_TEXT ) {
172 # Notice messages will foul up your XML output even if they're
173 # relatively harmless.
174 if( ini_get( 'display_errors' ) )
175 ini_set( 'display_errors', 'stderr' );
176
177 $this->initProgress( $history );
178
179 $db = $this->backupDb();
180 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
181 $exporter->dumpUploads = $this->dumpUploads;
182
183 $wrapper = new ExportProgressFilter( $this->sink, $this );
184 $exporter->setOutputSink( $wrapper );
185
186 if( !$this->skipHeader )
187 $exporter->openStream();
188
189 if( is_null( $this->pages ) ) {
190 if( $this->startId || $this->endId ) {
191 $exporter->pagesByRange( $this->startId, $this->endId );
192 } else {
193 $exporter->allPages();
194 }
195 } else {
196 $exporter->pagesByName( $this->pages );
197 }
198
199 if( !$this->skipFooter )
200 $exporter->closeStream();
201
202 $this->report( true );
203 }
204
205 /**
206 * Initialise starting time and maximum revision count.
207 * We'll make ETA calculations based an progress, assuming relatively
208 * constant per-revision rate.
209 * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
210 */
211 function initProgress( $history = WikiExporter::FULL ) {
212 $table = ($history == WikiExporter::CURRENT) ? 'page' : 'revision';
213 $field = ($history == WikiExporter::CURRENT) ? 'page_id' : 'rev_id';
214
215 $dbr = wfGetDB( DB_SLAVE );
216 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', 'BackupDumper::dump' );
217 $this->startTime = wfTime();
218 }
219
220 function backupDb() {
221 global $wgDBadminuser, $wgDBadminpassword;
222 global $wgDBname, $wgDebugDumpSql, $wgDBtype;
223 $flags = ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT; // god-damn hack
224
225 $class = 'Database' . ucfirst($wgDBtype);
226 $db = new $class( $this->backupServer(), $wgDBadminuser, $wgDBadminpassword, $wgDBname, false, $flags );
227
228 // Discourage the server from disconnecting us if it takes a long time
229 // to read out the big ol' batch query.
230 $db->setTimeout( 3600 * 24 );
231
232 return $db;
233 }
234
235 function backupServer() {
236 global $wgDBserver;
237 return $this->server
238 ? $this->server
239 : $wgDBserver;
240 }
241
242 function reportPage() {
243 $this->pageCount++;
244 }
245
246 function revCount() {
247 $this->revCount++;
248 $this->report();
249 }
250
251 function report( $final = false ) {
252 if( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
253 $this->showReport();
254 }
255 }
256
257 function showReport() {
258 if( $this->reporting ) {
259 $delta = wfTime() - $this->startTime;
260 $now = wfTimestamp( TS_DB );
261 if( $delta ) {
262 $rate = $this->pageCount / $delta;
263 $revrate = $this->revCount / $delta;
264 $portion = $this->revCount / $this->maxCount;
265 $eta = $this->startTime + $delta / $portion;
266 $etats = wfTimestamp( TS_DB, intval( $eta ) );
267 } else {
268 $rate = '-';
269 $revrate = '-';
270 $etats = '-';
271 }
272 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
273 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
274 }
275 }
276
277 function progress( $string ) {
278 fwrite( $this->stderr, $string . "\n" );
279 }
280 }
281
282 class ExportProgressFilter extends DumpFilter {
283 function ExportProgressFilter( &$sink, &$progress ) {
284 parent::DumpFilter( $sink );
285 $this->progress = $progress;
286 }
287
288 function writeClosePage( $string ) {
289 parent::writeClosePage( $string );
290 $this->progress->reportPage();
291 }
292
293 function writeRevision( $rev, $string ) {
294 parent::writeRevision( $rev, $string );
295 $this->progress->revCount();
296 }
297 }
298
299 ?>