187a38c49d8b37395aa2195ce7fb0ef91537d13a
[lhc/web/wiklou.git] / maintenance / dumpTextPass.php
1 <?php
2 /**
3 * Script that postprocesses XML dumps from dumpBackup.php to add page text
4 *
5 * Copyright (C) 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 Maintenance
25 */
26
27 $originalDir = getcwd();
28
29 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
30 require_once( 'backup.inc' );
31
32 /**
33 * @ingroup Maintenance
34 */
35 class TextPassDumper extends BackupDumper {
36 var $prefetch = null;
37 var $input = "php://stdin";
38 var $history = WikiExporter::FULL;
39 var $fetchCount = 0;
40 var $prefetchCount = 0;
41 var $lastTime = 0;
42 var $pageCountLast = 0;
43 var $revCountLast = 0;
44 var $prefetchCountLast = 0;
45 var $fetchCountLast = 0;
46
47 var $failures = 0;
48 var $maxFailures = 5;
49 var $failedTextRetrievals = 0;
50 var $maxConsecutiveFailedTextRetrievals = 200;
51 var $failureTimeout = 5; // Seconds to sleep after db failure
52
53 var $php = "php";
54 var $spawn = false;
55 var $spawnProc = false;
56 var $spawnWrite = false;
57 var $spawnRead = false;
58 var $spawnErr = false;
59
60 var $ID = 0;
61
62 function initProgress( $history ) {
63 parent::initProgress();
64 $this->ID = getmypid();
65 $this->lastTime = $this->startTime;
66 }
67
68 function dump( $history, $text = WikiExporter::TEXT ) {
69 # This shouldn't happen if on console... ;)
70 header( 'Content-type: text/html; charset=UTF-8' );
71
72 # Notice messages will foul up your XML output even if they're
73 # relatively harmless.
74 if ( ini_get( 'display_errors' ) )
75 ini_set( 'display_errors', 'stderr' );
76
77 $this->initProgress( $this->history );
78
79 $this->db = $this->backupDb();
80
81 $this->egress = new ExportProgressFilter( $this->sink, $this );
82
83 $input = fopen( $this->input, "rt" );
84 $result = $this->readDump( $input );
85
86 if ( WikiError::isError( $result ) ) {
87 wfDie( $result->getMessage() );
88 }
89
90 if ( $this->spawnProc ) {
91 $this->closeSpawn();
92 }
93
94 $this->report( true );
95 }
96
97 function processOption( $opt, $val, $param ) {
98 global $IP;
99 $url = $this->processFileOpt( $val, $param );
100
101 switch( $opt ) {
102 case 'prefetch':
103 require_once "$IP/maintenance/backupPrefetch.inc";
104 $this->prefetch = new BaseDump( $url );
105 break;
106 case 'stub':
107 $this->input = $url;
108 break;
109 case 'current':
110 $this->history = WikiExporter::CURRENT;
111 break;
112 case 'full':
113 $this->history = WikiExporter::FULL;
114 break;
115 case 'spawn':
116 $this->spawn = true;
117 if ( $val ) {
118 $this->php = $val;
119 }
120 break;
121 }
122 }
123
124 function processFileOpt( $val, $param ) {
125 $fileURIs = explode(';',$param);
126 foreach ( $fileURIs as $URI ) {
127 switch( $val ) {
128 case "file":
129 $newURI = $URI;
130 break;
131 case "gzip":
132 $newURI = "compress.zlib://$URI";
133 break;
134 case "bzip2":
135 $newURI = "compress.bzip2://$URI";
136 break;
137 case "7zip":
138 $newURI = "mediawiki.compress.7z://$URI";
139 break;
140 default:
141 $newURI = $URI;
142 }
143 $newFileURIs[] = $newURI;
144 }
145 $val = implode( ';', $newFileURIs );
146 return $val;
147 }
148
149 /**
150 * Overridden to include prefetch ratio if enabled.
151 */
152 function showReport() {
153 if ( !$this->prefetch ) {
154 return parent::showReport();
155 }
156
157 if ( $this->reporting ) {
158 $now = wfTimestamp( TS_DB );
159 $deltaAll = wfTime() - $this->startTime;
160 $deltaPart = wfTime() - $this->lastTime;
161 $this->pageCountPart = $this->pageCount - $this->pageCountLast;
162 $this->revCountPart = $this->revCount - $this->revCountLast;
163
164 if ( $deltaAll ) {
165 $portion = $this->revCount / $this->maxCount;
166 $eta = $this->startTime + $deltaAll / $portion;
167 $etats = wfTimestamp( TS_DB, intval( $eta ) );
168 if ( $this->fetchCount ) {
169 $fetchRate = 100.0 * $this->prefetchCount / $this->fetchCount;
170 }
171 else {
172 $fetchRate = '-';
173 }
174 $pageRate = $this->pageCount / $deltaAll;
175 $revRate = $this->revCount / $deltaAll;
176 } else {
177 $pageRate = '-';
178 $revRate = '-';
179 $etats = '-';
180 $fetchRate = '-';
181 }
182 if ( $deltaPart ) {
183 if ( $this->fetchCountLast ) {
184 $fetchRatePart = 100.0 * $this->prefetchCountLast / $this->fetchCountLast;
185 }
186 else {
187 $fetchRatePart = '-';
188 }
189 $pageRatePart = $this->pageCountPart / $deltaPart;
190 $revRatePart = $this->revCountPart / $deltaPart;
191
192 } else {
193 $fetchRatePart = '-';
194 $pageRatePart = '-';
195 $revRatePart = '-';
196 }
197 $this->progress( sprintf( "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), %d revs (%0.1f|%0.1f/sec all|curr), %0.1f%%|%0.1f%% prefetched (all|curr), ETA %s [max %d]",-
198 $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $fetchRate, $fetchRatePart, $etats, $this->maxCount ) );
199 $this->lastTime = $now;
200 $this->partCountLast = $this->partCount;
201 $this->revCountLast = $this->revCount;
202 $this->prefetchCountLast = $this->prefetchCount;
203 $this->fetchCountLast = $this->fetchCount;
204 }
205 }
206
207 function readDump( $input ) {
208 $this->buffer = "";
209 $this->openElement = false;
210 $this->atStart = true;
211 $this->state = "";
212 $this->lastName = "";
213 $this->thisPage = 0;
214 $this->thisRev = 0;
215
216 $parser = xml_parser_create( "UTF-8" );
217 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
218
219 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
220 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
221
222 $offset = 0; // for context extraction on error reporting
223 $bufferSize = 512 * 1024;
224 do {
225 $chunk = fread( $input, $bufferSize );
226 if ( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
227 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
228 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
229 }
230 $offset += strlen( $chunk );
231 } while ( $chunk !== false && !feof( $input ) );
232 xml_parser_free( $parser );
233
234 return true;
235 }
236
237 function getText( $id ) {
238 $this->fetchCount++;
239 if ( isset( $this->prefetch ) ) {
240 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
241 if ( $text !== null ) { // Entry missing from prefetch dump
242 $dbr = wfGetDB( DB_SLAVE );
243 $revID = intval( $this->thisRev );
244 $revLength = $dbr->selectField( 'revision', 'rev_len', array( 'rev_id' => $revID ) );
245 // if length of rev text in file doesn't match length in db, we reload
246 // this avoids carrying forward broken data from previous xml dumps
247 if( strlen( $text ) == $revLength ) {
248 $this->prefetchCount++;
249 return $text;
250 }
251 }
252 }
253 return $this->doGetText( $id );
254 }
255
256 private function doGetText( $id ) {
257
258 $id = intval( $id );
259 $this->failures = 0;
260 $ex = new MWException( "Graceful storage failure" );
261 while (true) {
262 if ( $this->spawn ) {
263 if ($this->failures) {
264 // we don't know why it failed, could be the child process
265 // borked, could be db entry busted, could be db server out to lunch,
266 // so cover all bases
267 $this->closeSpawn();
268 $this->openSpawn();
269 }
270 $text = $this->getTextSpawned( $id );
271 } else {
272 $text = $this->getTextDbSafe( $id );
273 }
274 if ( $text === false ) {
275 $this->failures++;
276 if ( $this->failures > $this->maxFailures) {
277 $this->progress( "Failed to retrieve revision text for text id ".
278 "$id after $this->maxFailures tries, giving up" );
279 // were there so many bad retrievals in a row we want to bail?
280 // at some point we have to declare the dump irretrievably broken
281 $this->failedTextRetrievals++;
282 if ($this->failedTextRetrievals > $this->maxConsecutiveFailedTextRetrievals) {
283 throw $ex;
284 }
285 else {
286 // would be nice to return something better to the caller someday,
287 // log what we know about the failure and about the revision
288 return("");
289 }
290 } else {
291 $this->progress( "Error $this->failures " .
292 "of allowed $this->maxFailures retrieving revision text for text id $id! " .
293 "Pausing $this->failureTimeout seconds before retry..." );
294 sleep( $this->failureTimeout );
295 }
296 } else {
297 $this->failedTextRetrievals= 0;
298 return( $text );
299 }
300 }
301
302 }
303
304 /**
305 * Fetch a text revision from the database, retrying in case of failure.
306 * This may survive some transitory errors by reconnecting, but
307 * may not survive a long-term server outage.
308 */
309 private function getTextDbSafe( $id ) {
310 while ( true ) {
311 try {
312 $text = $this->getTextDb( $id );
313 } catch ( DBQueryError $ex ) {
314 $text = false;
315 }
316 return $text;
317 }
318 }
319
320 /**
321 * May throw a database error if, say, the server dies during query.
322 */
323 private function getTextDb( $id ) {
324 global $wgContLang;
325 $row = $this->db->selectRow( 'text',
326 array( 'old_text', 'old_flags' ),
327 array( 'old_id' => $id ),
328 __METHOD__ );
329 $text = Revision::getRevisionText( $row );
330 if ( $text === false ) {
331 return false;
332 }
333 $stripped = str_replace( "\r", "", $text );
334 $normalized = $wgContLang->normalize( $stripped );
335 return $normalized;
336 }
337
338 private function getTextSpawned( $id ) {
339 wfSuppressWarnings();
340 if ( !$this->spawnProc ) {
341 // First time?
342 $this->openSpawn();
343 }
344 $text = $this->getTextSpawnedOnce( $id );
345 wfRestoreWarnings();
346 return $text;
347 }
348
349 function openSpawn() {
350 global $IP;
351
352 $cmd = implode( " ",
353 array_map( 'wfEscapeShellArg',
354 array(
355 $this->php,
356 "$IP/maintenance/fetchText.php",
357 '--wiki', wfWikiID() ) ) );
358 $spec = array(
359 0 => array( "pipe", "r" ),
360 1 => array( "pipe", "w" ),
361 2 => array( "file", "/dev/null", "a" ) );
362 $pipes = array();
363
364 $this->progress( "Spawning database subprocess: $cmd" );
365 $this->spawnProc = proc_open( $cmd, $spec, $pipes );
366 if ( !$this->spawnProc ) {
367 // shit
368 $this->progress( "Subprocess spawn failed." );
369 return false;
370 }
371 list(
372 $this->spawnWrite, // -> stdin
373 $this->spawnRead, // <- stdout
374 ) = $pipes;
375
376 return true;
377 }
378
379 private function closeSpawn() {
380 wfSuppressWarnings();
381 if ( $this->spawnRead )
382 fclose( $this->spawnRead );
383 $this->spawnRead = false;
384 if ( $this->spawnWrite )
385 fclose( $this->spawnWrite );
386 $this->spawnWrite = false;
387 if ( $this->spawnErr )
388 fclose( $this->spawnErr );
389 $this->spawnErr = false;
390 if ( $this->spawnProc )
391 pclose( $this->spawnProc );
392 $this->spawnProc = false;
393 wfRestoreWarnings();
394 }
395
396 private function getTextSpawnedOnce( $id ) {
397 global $wgContLang;
398
399 $ok = fwrite( $this->spawnWrite, "$id\n" );
400 // $this->progress( ">> $id" );
401 if ( !$ok ) return false;
402
403 $ok = fflush( $this->spawnWrite );
404 // $this->progress( ">> [flush]" );
405 if ( !$ok ) return false;
406
407 // check that the text id they are sending is the one we asked for
408 // this avoids out of sync revision text errors we have encountered in the past
409 $newId = fgets( $this->spawnRead );
410 if ( $newId === false ) {
411 return false;
412 }
413 if ( $id != intval( $newId ) ) {
414 return false;
415 }
416
417 $len = fgets( $this->spawnRead );
418 // $this->progress( "<< " . trim( $len ) );
419 if ( $len === false ) return false;
420
421 $nbytes = intval( $len );
422 // actual error, not zero-length text
423 if ($nbytes < 0 ) return false;
424
425 $text = "";
426
427 // Subprocess may not send everything at once, we have to loop.
428 while ( $nbytes > strlen( $text ) ) {
429 $buffer = fread( $this->spawnRead, $nbytes - strlen( $text ) );
430 if ( $buffer === false ) break;
431 $text .= $buffer;
432 }
433
434 $gotbytes = strlen( $text );
435 if ( $gotbytes != $nbytes ) {
436 $this->progress( "Expected $nbytes bytes from database subprocess, got $gotbytes " );
437 return false;
438 }
439
440 // Do normalization in the dump thread...
441 $stripped = str_replace( "\r", "", $text );
442 $normalized = $wgContLang->normalize( $stripped );
443 return $normalized;
444 }
445
446 function startElement( $parser, $name, $attribs ) {
447 $this->clearOpenElement( null );
448 $this->lastName = $name;
449
450 if ( $name == 'revision' ) {
451 $this->state = $name;
452 $this->egress->writeOpenPage( null, $this->buffer );
453 $this->buffer = "";
454 } elseif ( $name == 'page' ) {
455 $this->state = $name;
456 if ( $this->atStart ) {
457 $this->egress->writeOpenStream( $this->buffer );
458 $this->buffer = "";
459 $this->atStart = false;
460 }
461 }
462
463 if ( $name == "text" && isset( $attribs['id'] ) ) {
464 $text = $this->getText( $attribs['id'] );
465 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
466 if ( strlen( $text ) > 0 ) {
467 $this->characterData( $parser, $text );
468 }
469 } else {
470 $this->openElement = array( $name, $attribs );
471 }
472 }
473
474 function endElement( $parser, $name ) {
475 if ( $this->openElement ) {
476 $this->clearOpenElement( "" );
477 } else {
478 $this->buffer .= "</$name>";
479 }
480
481 if ( $name == 'revision' ) {
482 $this->egress->writeRevision( null, $this->buffer );
483 $this->buffer = "";
484 $this->thisRev = "";
485 } elseif ( $name == 'page' ) {
486 $this->egress->writeClosePage( $this->buffer );
487 $this->buffer = "";
488 $this->thisPage = "";
489 } elseif ( $name == 'mediawiki' ) {
490 $this->egress->writeCloseStream( $this->buffer );
491 $this->buffer = "";
492 }
493 }
494
495 function characterData( $parser, $data ) {
496 $this->clearOpenElement( null );
497 if ( $this->lastName == "id" ) {
498 if ( $this->state == "revision" ) {
499 $this->thisRev .= $data;
500 } elseif ( $this->state == "page" ) {
501 $this->thisPage .= $data;
502 }
503 }
504 $this->buffer .= htmlspecialchars( $data );
505 }
506
507 function clearOpenElement( $style ) {
508 if ( $this->openElement ) {
509 $this->buffer .= Xml::element( $this->openElement[0], $this->openElement[1], $style );
510 $this->openElement = false;
511 }
512 }
513 }
514
515
516 $dumper = new TextPassDumper( $argv );
517
518 if ( !isset( $options['help'] ) ) {
519 $dumper->dump( true );
520 } else {
521 $dumper->progress( <<<ENDS
522 This script postprocesses XML dumps from dumpBackup.php to add
523 page text which was stubbed out (using --stub).
524
525 XML input is accepted on stdin.
526 XML output is sent to stdout; progress reports are sent to stderr.
527
528 Usage: php dumpTextPass.php [<options>]
529 Options:
530 --stub=<type>:<file> To load a compressed stub dump instead of stdin
531 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
532 pressure on the database.
533 (Requires the XMLReader extension)
534 --quiet Don't dump status reports to stderr.
535 --report=n Report position and speed after every n pages processed.
536 (Default: 100)
537 --server=h Force reading from MySQL server h
538 --current Base ETA on number of pages in database instead of all revisions
539 --spawn Spawn a subprocess for loading text records
540 --help Display this help message
541 ENDS
542 );
543 }
544
545