fu r95396: register new message keys in maintenacne file
[lhc/web/wiklou.git] / maintenance / backup.inc
1 <?php
2 /**
3 * Base classes for database dumpers
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Dump Maintenance
25 */
26
27 /**
28 * @ingroup Dump Maintenance
29 */
30 class DumpDBZip2Output extends DumpPipeOutput {
31 function DumpDBZip2Output( $file ) {
32 parent::__construct( "dbzip2", $file );
33 }
34 }
35
36 /**
37 * @ingroup Dump Maintenance
38 */
39 class BackupDumper {
40 var $reportingInterval = 100;
41 var $reporting = true;
42 var $pageCount = 0;
43 var $revCount = 0;
44 var $server = null; // use default
45 var $pages = null; // all pages
46 var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
47 var $skipFooter = false; // don't output </mediawiki>
48 var $startId = 0;
49 var $endId = 0;
50 var $sink = null; // Output filters
51 var $stubText = false; // include rev_text_id instead of text; for 2-pass dump
52 var $dumpUploads = false;
53 var $dumpUploadFileContents = false;
54
55 function BackupDumper( $args ) {
56 $this->stderr = fopen( "php://stderr", "wt" );
57
58 // Built-in output and filter plugins
59 $this->registerOutput( 'file', 'DumpFileOutput' );
60 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
61 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
62 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
63 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
64
65 $this->registerFilter( 'latest', 'DumpLatestFilter' );
66 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
67 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
68
69 $this->sink = $this->processArgs( $args );
70 }
71
72 /**
73 * @param $name String
74 * @param $class String: name of output filter plugin class
75 */
76 function registerOutput( $name, $class ) {
77 $this->outputTypes[$name] = $class;
78 }
79
80 /**
81 * @param $name String
82 * @param $class String: name of filter plugin class
83 */
84 function registerFilter( $name, $class ) {
85 $this->filterTypes[$name] = $class;
86 }
87
88 /**
89 * Load a plugin and register it
90 *
91 * @param $class String: name of plugin class; must have a static 'register'
92 * method that takes a BackupDumper as a parameter.
93 * @param $file String: full or relative path to the PHP file to load, or empty
94 */
95 function loadPlugin( $class, $file ) {
96 if ( $file != '' ) {
97 require_once( $file );
98 }
99 $register = array( $class, 'register' );
100 call_user_func_array( $register, array( &$this ) );
101 }
102
103 /**
104 * @param $args Array
105 * @return Array
106 */
107 function processArgs( $args ) {
108 $sink = null;
109 $sinks = array();
110 foreach ( $args as $arg ) {
111 $matches = array();
112 if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
113 @list( /* $full */ , $opt, $val, $param ) = $matches;
114 switch( $opt ) {
115 case "plugin":
116 $this->loadPlugin( $val, $param );
117 break;
118 case "output":
119 if ( !is_null( $sink ) ) {
120 $sinks[] = $sink;
121 }
122 if ( !isset( $this->outputTypes[$val] ) ) {
123 $this->fatalError( "Unrecognized output sink type '$val'" );
124 }
125 $type = $this->outputTypes[$val];
126 $sink = new $type( $param );
127 break;
128 case "filter":
129 if ( is_null( $sink ) ) {
130 $sink = new DumpOutput();
131 }
132 if ( !isset( $this->filterTypes[$val] ) ) {
133 $this->fatalError( "Unrecognized filter type '$val'" );
134 }
135 $type = $this->filterTypes[$val];
136 $filter = new $type( $sink, $param );
137
138 // references are lame in php...
139 unset( $sink );
140 $sink = $filter;
141
142 break;
143 case "report":
144 $this->reportingInterval = intval( $val );
145 break;
146 case "server":
147 $this->server = $val;
148 break;
149 case "force-normal":
150 if ( !function_exists( 'utf8_normalize' ) ) {
151 wfDl( "php_utfnormal.so" );
152 if ( !function_exists( 'utf8_normalize' ) ) {
153 $this->fatalError( "Failed to load UTF-8 normalization extension. " .
154 "Install or remove --force-normal parameter to use slower code." );
155 }
156 }
157 break;
158 default:
159 $this->processOption( $opt, $val, $param );
160 }
161 }
162 }
163
164 if ( is_null( $sink ) ) {
165 $sink = new DumpOutput();
166 }
167 $sinks[] = $sink;
168
169 if ( count( $sinks ) > 1 ) {
170 return new DumpMultiWriter( $sinks );
171 } else {
172 return $sink;
173 }
174 }
175
176 function processOption( $opt, $val, $param ) {
177 // extension point for subclasses to add options
178 }
179
180 function dump( $history, $text = WikiExporter::TEXT ) {
181 # Notice messages will foul up your XML output even if they're
182 # relatively harmless.
183 if ( ini_get( 'display_errors' ) )
184 ini_set( 'display_errors', 'stderr' );
185
186 $this->initProgress( $history );
187
188 $db = $this->backupDb();
189 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
190 $exporter->dumpUploads = $this->dumpUploads;
191 $exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
192
193 $wrapper = new ExportProgressFilter( $this->sink, $this );
194 $exporter->setOutputSink( $wrapper );
195
196 if ( !$this->skipHeader )
197 $exporter->openStream();
198 # Log item dumps: all or by range
199 if ( $history & WikiExporter::LOGS ) {
200 if ( $this->startId || $this->endId ) {
201 $exporter->logsByRange( $this->startId, $this->endId );
202 } else {
203 $exporter->allLogs();
204 }
205 # Page dumps: all or by page ID range
206 } else if ( is_null( $this->pages ) ) {
207 if ( $this->startId || $this->endId ) {
208 $exporter->pagesByRange( $this->startId, $this->endId );
209 } else {
210 $exporter->allPages();
211 }
212 # Dump of specific pages
213 } else {
214 $exporter->pagesByName( $this->pages );
215 }
216
217 if ( !$this->skipFooter )
218 $exporter->closeStream();
219
220 $this->report( true );
221 }
222
223 /**
224 * Initialise starting time and maximum revision count.
225 * We'll make ETA calculations based an progress, assuming relatively
226 * constant per-revision rate.
227 * @param $history Integer: WikiExporter::CURRENT or WikiExporter::FULL
228 */
229 function initProgress( $history = WikiExporter::FULL ) {
230 $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
231 $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
232
233 $dbr = wfGetDB( DB_SLAVE );
234 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
235 $this->startTime = wfTime();
236 }
237
238 /**
239 * @todo Fixme: the --server parameter is currently not respected, as it
240 * doesn't seem terribly easy to ask the load balancer for a particular
241 * connection by name.
242 */
243 function backupDb() {
244 $this->lb = wfGetLBFactory()->newMainLB();
245 $db = $this->lb->getConnection( DB_SLAVE, 'backup' );
246
247 // Discourage the server from disconnecting us if it takes a long time
248 // to read out the big ol' batch query.
249 $db->setTimeout( 3600 * 24 );
250
251 return $db;
252 }
253
254 function __destruct() {
255 if ( isset( $this->lb ) ) {
256 $this->lb->closeAll();
257 }
258 }
259
260 function backupServer() {
261 global $wgDBserver;
262 return $this->server
263 ? $this->server
264 : $wgDBserver;
265 }
266
267 function reportPage() {
268 $this->pageCount++;
269 }
270
271 function revCount() {
272 $this->revCount++;
273 $this->report();
274 }
275
276 function report( $final = false ) {
277 if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
278 $this->showReport();
279 }
280 }
281
282 function showReport() {
283 if ( $this->reporting ) {
284 $delta = wfTime() - $this->startTime;
285 $now = wfTimestamp( TS_DB );
286 if ( $delta ) {
287 $rate = $this->pageCount / $delta;
288 $revrate = $this->revCount / $delta;
289 $portion = $this->revCount / $this->maxCount;
290 $eta = $this->startTime + $delta / $portion;
291 $etats = wfTimestamp( TS_DB, intval( $eta ) );
292 } else {
293 $rate = '-';
294 $revrate = '-';
295 $etats = '-';
296 }
297 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), ETA %s [max %d]",
298 $now, wfWikiID(), $this->pageCount, $rate, $this->revCount, $revrate, $etats, $this->maxCount ) );
299 }
300 }
301
302 function progress( $string ) {
303 fwrite( $this->stderr, $string . "\n" );
304 }
305
306 function fatalError( $msg ) {
307 $this->progress( "$msg\n" );
308 die(1);
309 }
310 }
311
312 class ExportProgressFilter extends DumpFilter {
313 function ExportProgressFilter( &$sink, &$progress ) {
314 parent::__construct( $sink );
315 $this->progress = $progress;
316 }
317
318 function writeClosePage( $string ) {
319 parent::writeClosePage( $string );
320 $this->progress->reportPage();
321 }
322
323 function writeRevision( $rev, $string ) {
324 parent::writeRevision( $rev, $string );
325 $this->progress->revCount();
326 }
327 }