Merge "TextPassDumper -> backupTextPass.inc"
[lhc/web/wiklou.git] / maintenance / backupTextPass.inc
1 <?php
2 /**
3 * BackupDumper 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
28 /**
29 * @ingroup Maintenance
30 */
31 class TextPassDumper extends BackupDumper {
32 var $prefetch = null;
33 var $input = "php://stdin";
34 var $history = WikiExporter::FULL;
35 var $fetchCount = 0;
36 var $prefetchCount = 0;
37 var $prefetchCountLast = 0;
38 var $fetchCountLast = 0;
39
40 var $maxFailures = 5;
41 var $maxConsecutiveFailedTextRetrievals = 200;
42 var $failureTimeout = 5; // Seconds to sleep after db failure
43
44 var $php = "php";
45 var $spawn = false;
46 var $spawnProc = false;
47 var $spawnWrite = false;
48 var $spawnRead = false;
49 var $spawnErr = false;
50
51 var $xmlwriterobj = false;
52
53 // when we spend more than maxTimeAllowed seconds on this run, we continue
54 // processing until we write out the next complete page, then save output file(s),
55 // rename it/them and open new one(s)
56 var $maxTimeAllowed = 0; // 0 = no limit
57 var $timeExceeded = false;
58 var $firstPageWritten = false;
59 var $lastPageWritten = false;
60 var $checkpointJustWritten = false;
61 var $checkpointFiles = array();
62
63 /**
64 * @var DatabaseBase
65 */
66 protected $db;
67
68
69 /**
70 * Drop the database connection $this->db and try to get a new one.
71 *
72 * This function tries to get a /different/ connection if this is
73 * possible. Hence, (if this is possible) it switches to a different
74 * failover upon each call.
75 *
76 * This function resets $this->lb and closes all connections on it.
77 *
78 * @throws MWException
79 */
80 function rotateDb() {
81 // Cleaning up old connections
82 if ( isset( $this->lb ) ) {
83 $this->lb->closeAll();
84 unset( $this->lb );
85 }
86
87 if ( isset( $this->db ) && $this->db->isOpen() ) {
88 throw new MWException( 'DB is set and has not been closed by the Load Balancer' );
89 }
90
91 unset( $this->db );
92
93 // Trying to set up new connection.
94 // We do /not/ retry upon failure, but delegate to encapsulating logic, to avoid
95 // individually retrying at different layers of code.
96
97 // 1. The LoadBalancer.
98 try {
99 $this->lb = wfGetLBFactory()->newMainLB();
100 } catch ( Exception $e ) {
101 throw new MWException( __METHOD__ . " rotating DB failed to obtain new load balancer (" . $e->getMessage() . ")" );
102 }
103
104
105 // 2. The Connection, through the load balancer.
106 try {
107 $this->db = $this->lb->getConnection( DB_SLAVE, 'backup' );
108 } catch ( Exception $e ) {
109 throw new MWException( __METHOD__ . " rotating DB failed to obtain new database (" . $e->getMessage() . ")" );
110 }
111 }
112
113
114 function initProgress( $history ) {
115 parent::initProgress();
116 $this->timeOfCheckpoint = $this->startTime;
117 }
118
119 function dump( $history, $text = WikiExporter::TEXT ) {
120 // This shouldn't happen if on console... ;)
121 header( 'Content-type: text/html; charset=UTF-8' );
122
123 // Notice messages will foul up your XML output even if they're
124 // relatively harmless.
125 if ( ini_get( 'display_errors' ) )
126 ini_set( 'display_errors', 'stderr' );
127
128 $this->initProgress( $this->history );
129
130 // We are trying to get an initial database connection to avoid that the
131 // first try of this request's first call to getText fails. However, if
132 // obtaining a good DB connection fails it's not a serious issue, as
133 // getText does retry upon failure and can start without having a working
134 // DB connection.
135 try {
136 $this->rotateDb();
137 } catch ( Exception $e ) {
138 // We do not even count this as failure. Just let eventual
139 // watchdogs know.
140 $this->progress( "Getting initial DB connection failed (" .
141 $e->getMessage() . ")" );
142 }
143
144 $this->egress = new ExportProgressFilter( $this->sink, $this );
145
146 // it would be nice to do it in the constructor, oh well. need egress set
147 $this->finalOptionCheck();
148
149 // we only want this so we know how to close a stream :-P
150 $this->xmlwriterobj = new XmlDumpWriter();
151
152 $input = fopen( $this->input, "rt" );
153 $result = $this->readDump( $input );
154
155 if ( WikiError::isError( $result ) ) {
156 throw new MWException( $result->getMessage() );
157 }
158
159 if ( $this->spawnProc ) {
160 $this->closeSpawn();
161 }
162
163 $this->report( true );
164 }
165
166 function processOption( $opt, $val, $param ) {
167 global $IP;
168 $url = $this->processFileOpt( $val, $param );
169
170 switch( $opt ) {
171 case 'prefetch':
172 require_once "$IP/maintenance/backupPrefetch.inc";
173 $this->prefetch = new BaseDump( $url );
174 break;
175 case 'stub':
176 $this->input = $url;
177 break;
178 case 'maxtime':
179 $this->maxTimeAllowed = intval( $val ) * 60;
180 break;
181 case 'checkpointfile':
182 $this->checkpointFiles[] = $val;
183 break;
184 case 'current':
185 $this->history = WikiExporter::CURRENT;
186 break;
187 case 'full':
188 $this->history = WikiExporter::FULL;
189 break;
190 case 'spawn':
191 $this->spawn = true;
192 if ( $val ) {
193 $this->php = $val;
194 }
195 break;
196 }
197 }
198
199 function processFileOpt( $val, $param ) {
200 $fileURIs = explode( ';', $param );
201 foreach ( $fileURIs as $URI ) {
202 switch( $val ) {
203 case "file":
204 $newURI = $URI;
205 break;
206 case "gzip":
207 $newURI = "compress.zlib://$URI";
208 break;
209 case "bzip2":
210 $newURI = "compress.bzip2://$URI";
211 break;
212 case "7zip":
213 $newURI = "mediawiki.compress.7z://$URI";
214 break;
215 default:
216 $newURI = $URI;
217 }
218 $newFileURIs[] = $newURI;
219 }
220 $val = implode( ';', $newFileURIs );
221 return $val;
222 }
223
224 /**
225 * Overridden to include prefetch ratio if enabled.
226 */
227 function showReport() {
228 if ( !$this->prefetch ) {
229 parent::showReport();
230 return;
231 }
232
233 if ( $this->reporting ) {
234 $now = wfTimestamp( TS_DB );
235 $nowts = wfTime();
236 $deltaAll = wfTime() - $this->startTime;
237 $deltaPart = wfTime() - $this->lastTime;
238 $this->pageCountPart = $this->pageCount - $this->pageCountLast;
239 $this->revCountPart = $this->revCount - $this->revCountLast;
240
241 if ( $deltaAll ) {
242 $portion = $this->revCount / $this->maxCount;
243 $eta = $this->startTime + $deltaAll / $portion;
244 $etats = wfTimestamp( TS_DB, intval( $eta ) );
245 if ( $this->fetchCount ) {
246 $fetchRate = 100.0 * $this->prefetchCount / $this->fetchCount;
247 } else {
248 $fetchRate = '-';
249 }
250 $pageRate = $this->pageCount / $deltaAll;
251 $revRate = $this->revCount / $deltaAll;
252 } else {
253 $pageRate = '-';
254 $revRate = '-';
255 $etats = '-';
256 $fetchRate = '-';
257 }
258 if ( $deltaPart ) {
259 if ( $this->fetchCountLast ) {
260 $fetchRatePart = 100.0 * $this->prefetchCountLast / $this->fetchCountLast;
261 } else {
262 $fetchRatePart = '-';
263 }
264 $pageRatePart = $this->pageCountPart / $deltaPart;
265 $revRatePart = $this->revCountPart / $deltaPart;
266
267 } else {
268 $fetchRatePart = '-';
269 $pageRatePart = '-';
270 $revRatePart = '-';
271 }
272 $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]",
273 $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $fetchRate, $fetchRatePart, $etats, $this->maxCount ) );
274 $this->lastTime = $nowts;
275 $this->revCountLast = $this->revCount;
276 $this->prefetchCountLast = $this->prefetchCount;
277 $this->fetchCountLast = $this->fetchCount;
278 }
279 }
280
281 function setTimeExceeded() {
282 $this->timeExceeded = True;
283 }
284
285 function checkIfTimeExceeded() {
286 if ( $this->maxTimeAllowed && ( $this->lastTime - $this->timeOfCheckpoint > $this->maxTimeAllowed ) ) {
287 return true;
288 }
289 return false;
290 }
291
292 function finalOptionCheck() {
293 if ( ( $this->checkpointFiles && ! $this->maxTimeAllowed ) ||
294 ( $this->maxTimeAllowed && !$this->checkpointFiles ) ) {
295 throw new MWException( "Options checkpointfile and maxtime must be specified together.\n" );
296 }
297 foreach ( $this->checkpointFiles as $checkpointFile ) {
298 $count = substr_count ( $checkpointFile, "%s" );
299 if ( $count != 2 ) {
300 throw new MWException( "Option checkpointfile must contain two '%s' for substitution of first and last pageids, count is $count instead, file is $checkpointFile.\n" );
301 }
302 }
303
304 if ( $this->checkpointFiles ) {
305 $filenameList = (array)$this->egress->getFilenames();
306 if ( count( $filenameList ) != count( $this->checkpointFiles ) ) {
307 throw new MWException( "One checkpointfile must be specified for each output option, if maxtime is used.\n" );
308 }
309 }
310 }
311
312 function readDump( $input ) {
313 $this->buffer = "";
314 $this->openElement = false;
315 $this->atStart = true;
316 $this->state = "";
317 $this->lastName = "";
318 $this->thisPage = 0;
319 $this->thisRev = 0;
320
321 $parser = xml_parser_create( "UTF-8" );
322 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
323
324 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
325 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
326
327 $offset = 0; // for context extraction on error reporting
328 $bufferSize = 512 * 1024;
329 do {
330 if ( $this->checkIfTimeExceeded() ) {
331 $this->setTimeExceeded();
332 }
333 $chunk = fread( $input, $bufferSize );
334 if ( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
335 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
336 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
337 }
338 $offset += strlen( $chunk );
339 } while ( $chunk !== false && !feof( $input ) );
340 if ( $this->maxTimeAllowed ) {
341 $filenameList = (array)$this->egress->getFilenames();
342 // we wrote some stuff after last checkpoint that needs renamed
343 if ( file_exists( $filenameList[0] ) ) {
344 $newFilenames = array();
345 # we might have just written the header and footer and had no
346 # pages or revisions written... perhaps they were all deleted
347 # there's no pageID 0 so we use that. the caller is responsible
348 # for deciding what to do with a file containing only the
349 # siteinfo information and the mw tags.
350 if ( ! $this->firstPageWritten ) {
351 $firstPageID = str_pad( 0, 9, "0", STR_PAD_LEFT );
352 $lastPageID = str_pad( 0, 9, "0", STR_PAD_LEFT );
353 }
354 else {
355 $firstPageID = str_pad( $this->firstPageWritten, 9, "0", STR_PAD_LEFT );
356 $lastPageID = str_pad( $this->lastPageWritten, 9, "0", STR_PAD_LEFT );
357 }
358 for ( $i = 0; $i < count( $filenameList ); $i++ ) {
359 $checkpointNameFilledIn = sprintf( $this->checkpointFiles[$i], $firstPageID, $lastPageID );
360 $fileinfo = pathinfo( $filenameList[$i] );
361 $newFilenames[] = $fileinfo['dirname'] . '/' . $checkpointNameFilledIn;
362 }
363 $this->egress->closeAndRename( $newFilenames );
364 }
365 }
366 xml_parser_free( $parser );
367
368 return true;
369 }
370
371 /**
372 * Tries to get the revision text for a revision id.
373 *
374 * Upon errors, retries (Up to $this->maxFailures tries each call).
375 * If still no good revision get could be found even after this retrying, "" is returned.
376 * If no good revision text could be returned for
377 * $this->maxConsecutiveFailedTextRetrievals consecutive calls to getText, MWException
378 * is thrown.
379 *
380 * @param $id string The revision id to get the text for
381 *
382 * @return string The revision text for $id, or ""
383 * @throws MWException
384 */
385 function getText( $id ) {
386 $prefetchNotTried = true; // Whether or not we already tried to get the text via prefetch.
387 $text = false; // The candidate for a good text. false if no proper value.
388 $failures = 0; // The number of times, this invocation of getText already failed.
389
390 static $consecutiveFailedTextRetrievals = 0; // The number of times getText failed without
391 // yielding a good text in between.
392
393 $this->fetchCount++;
394
395 // To allow to simply return on success and do not have to worry about book keeping,
396 // we assume, this fetch works (possible after some retries). Nevertheless, we koop
397 // the old value, so we can restore it, if problems occur (See after the while loop).
398 $oldConsecutiveFailedTextRetrievals = $consecutiveFailedTextRetrievals;
399 $consecutiveFailedTextRetrievals = 0;
400
401 while ( $failures < $this->maxFailures ) {
402
403 // As soon as we found a good text for the $id, we will return immediately.
404 // Hence, if we make it past the try catch block, we know that we did not
405 // find a good text.
406
407 try {
408 // Step 1: Get some text (or reuse from previous iteratuon if checking
409 // for plausibility failed)
410
411 // Trying to get prefetch, if it has not been tried before
412 if ( $text === false && isset( $this->prefetch ) && $prefetchNotTried ) {
413 $prefetchNotTried = false;
414 $tryIsPrefetch = true;
415 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
416 if ( $text === null ) {
417 $text = false;
418 }
419 }
420
421 if ( $text === false ) {
422 // Fallback to asking the database
423 $tryIsPrefetch = false;
424 if ( $this->spawn ) {
425 $text = $this->getTextSpawned( $id );
426 } else {
427 $text = $this->getTextDb( $id );
428 }
429 }
430
431 if ( $text === false ) {
432 throw new MWException( "Generic error while obtaining text for id " . $id );
433 }
434
435 // We received a good candidate for the text of $id via some method
436
437 // Step 2: Checking for plausibility and return the text if it is
438 // plausible
439 $revID = intval( $this->thisRev );
440 if ( ! isset( $this->db ) ) {
441 throw new MWException( "No database available" );
442 }
443 $revLength = $this->db->selectField( 'revision', 'rev_len', array( 'rev_id' => $revID ) );
444 if ( strlen( $text ) == $revLength ) {
445 if ( $tryIsPrefetch ) {
446 $this->prefetchCount++;
447 }
448 return $text;
449 }
450
451 $text = false;
452 throw new MWException( "Received text is unplausible for id " . $id );
453
454 } catch ( Exception $e ) {
455 $msg = "getting/checking text " . $id . " failed (" . $e->getMessage() . ")";
456 if ( $failures + 1 < $this->maxFailures ) {
457 $msg .= " (Will retry " . ( $this->maxFailures - $failures - 1 ) . " more times)";
458 }
459 $this->progress( $msg );
460 }
461
462 // Something went wrong; we did not a text that was plausible :(
463 $failures++;
464
465
466 // After backing off for some time, we try to reboot the whole process as
467 // much as possible to not carry over failures from one part to the other
468 // parts
469 sleep( $this->failureTimeout );
470 try {
471 $this->rotateDb();
472 if ( $this->spawn ) {
473 $this->closeSpawn();
474 $this->openSpawn();
475 }
476 } catch ( Exception $e ) {
477 $this->progress( "Rebooting getText infrastructure failed (" . $e->getMessage() . ")" .
478 " Trying to continue anyways" );
479 }
480 }
481
482 // Retirieving a good text for $id failed (at least) maxFailures times.
483 // We abort for this $id.
484
485 // Restoring the consecutive failures, and maybe aborting, if the dump
486 // is too broken.
487 $consecutiveFailedTextRetrievals = $oldConsecutiveFailedTextRetrievals + 1;
488 if ( $consecutiveFailedTextRetrievals > $this->maxConsecutiveFailedTextRetrievals ) {
489 throw new MWException( "Graceful storage failure" );
490 }
491
492 return "";
493 }
494
495
496 /**
497 * May throw a database error if, say, the server dies during query.
498 * @param $id
499 * @return bool|string
500 * @throws MWException
501 */
502 private function getTextDb( $id ) {
503 global $wgContLang;
504 if ( ! isset( $this->db ) ) {
505 throw new MWException( __METHOD__ . "No database available" );
506 }
507 $row = $this->db->selectRow( 'text',
508 array( 'old_text', 'old_flags' ),
509 array( 'old_id' => $id ),
510 __METHOD__ );
511 $text = Revision::getRevisionText( $row );
512 if ( $text === false ) {
513 return false;
514 }
515 $stripped = str_replace( "\r", "", $text );
516 $normalized = $wgContLang->normalize( $stripped );
517 return $normalized;
518 }
519
520 private function getTextSpawned( $id ) {
521 wfSuppressWarnings();
522 if ( !$this->spawnProc ) {
523 // First time?
524 $this->openSpawn();
525 }
526 $text = $this->getTextSpawnedOnce( $id );
527 wfRestoreWarnings();
528 return $text;
529 }
530
531 function openSpawn() {
532 global $IP;
533
534 if ( file_exists( "$IP/../multiversion/MWScript.php" ) ) {
535 $cmd = implode( " ",
536 array_map( 'wfEscapeShellArg',
537 array(
538 $this->php,
539 "$IP/../multiversion/MWScript.php",
540 "fetchText.php",
541 '--wiki', wfWikiID() ) ) );
542 }
543 else {
544 $cmd = implode( " ",
545 array_map( 'wfEscapeShellArg',
546 array(
547 $this->php,
548 "$IP/maintenance/fetchText.php",
549 '--wiki', wfWikiID() ) ) );
550 }
551 $spec = array(
552 0 => array( "pipe", "r" ),
553 1 => array( "pipe", "w" ),
554 2 => array( "file", "/dev/null", "a" ) );
555 $pipes = array();
556
557 $this->progress( "Spawning database subprocess: $cmd" );
558 $this->spawnProc = proc_open( $cmd, $spec, $pipes );
559 if ( !$this->spawnProc ) {
560 // shit
561 $this->progress( "Subprocess spawn failed." );
562 return false;
563 }
564 list(
565 $this->spawnWrite, // -> stdin
566 $this->spawnRead, // <- stdout
567 ) = $pipes;
568
569 return true;
570 }
571
572 private function closeSpawn() {
573 wfSuppressWarnings();
574 if ( $this->spawnRead )
575 fclose( $this->spawnRead );
576 $this->spawnRead = false;
577 if ( $this->spawnWrite )
578 fclose( $this->spawnWrite );
579 $this->spawnWrite = false;
580 if ( $this->spawnErr )
581 fclose( $this->spawnErr );
582 $this->spawnErr = false;
583 if ( $this->spawnProc )
584 pclose( $this->spawnProc );
585 $this->spawnProc = false;
586 wfRestoreWarnings();
587 }
588
589 private function getTextSpawnedOnce( $id ) {
590 global $wgContLang;
591
592 $ok = fwrite( $this->spawnWrite, "$id\n" );
593 // $this->progress( ">> $id" );
594 if ( !$ok ) return false;
595
596 $ok = fflush( $this->spawnWrite );
597 // $this->progress( ">> [flush]" );
598 if ( !$ok ) return false;
599
600 // check that the text id they are sending is the one we asked for
601 // this avoids out of sync revision text errors we have encountered in the past
602 $newId = fgets( $this->spawnRead );
603 if ( $newId === false ) {
604 return false;
605 }
606 if ( $id != intval( $newId ) ) {
607 return false;
608 }
609
610 $len = fgets( $this->spawnRead );
611 // $this->progress( "<< " . trim( $len ) );
612 if ( $len === false ) return false;
613
614 $nbytes = intval( $len );
615 // actual error, not zero-length text
616 if ( $nbytes < 0 ) return false;
617
618 $text = "";
619
620 // Subprocess may not send everything at once, we have to loop.
621 while ( $nbytes > strlen( $text ) ) {
622 $buffer = fread( $this->spawnRead, $nbytes - strlen( $text ) );
623 if ( $buffer === false ) break;
624 $text .= $buffer;
625 }
626
627 $gotbytes = strlen( $text );
628 if ( $gotbytes != $nbytes ) {
629 $this->progress( "Expected $nbytes bytes from database subprocess, got $gotbytes " );
630 return false;
631 }
632
633 // Do normalization in the dump thread...
634 $stripped = str_replace( "\r", "", $text );
635 $normalized = $wgContLang->normalize( $stripped );
636 return $normalized;
637 }
638
639 function startElement( $parser, $name, $attribs ) {
640 $this->checkpointJustWritten = false;
641
642 $this->clearOpenElement( null );
643 $this->lastName = $name;
644
645 if ( $name == 'revision' ) {
646 $this->state = $name;
647 $this->egress->writeOpenPage( null, $this->buffer );
648 $this->buffer = "";
649 } elseif ( $name == 'page' ) {
650 $this->state = $name;
651 if ( $this->atStart ) {
652 $this->egress->writeOpenStream( $this->buffer );
653 $this->buffer = "";
654 $this->atStart = false;
655 }
656 }
657
658 if ( $name == "text" && isset( $attribs['id'] ) ) {
659 $text = $this->getText( $attribs['id'] );
660 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
661 if ( strlen( $text ) > 0 ) {
662 $this->characterData( $parser, $text );
663 }
664 } else {
665 $this->openElement = array( $name, $attribs );
666 }
667 }
668
669 function endElement( $parser, $name ) {
670 $this->checkpointJustWritten = false;
671
672 if ( $this->openElement ) {
673 $this->clearOpenElement( "" );
674 } else {
675 $this->buffer .= "</$name>";
676 }
677
678 if ( $name == 'revision' ) {
679 $this->egress->writeRevision( null, $this->buffer );
680 $this->buffer = "";
681 $this->thisRev = "";
682 } elseif ( $name == 'page' ) {
683 if ( ! $this->firstPageWritten ) {
684 $this->firstPageWritten = trim( $this->thisPage );
685 }
686 $this->lastPageWritten = trim( $this->thisPage );
687 if ( $this->timeExceeded ) {
688 $this->egress->writeClosePage( $this->buffer );
689 // nasty hack, we can't just write the chardata after the
690 // page tag, it will include leading blanks from the next line
691 $this->egress->sink->write( "\n" );
692
693 $this->buffer = $this->xmlwriterobj->closeStream();
694 $this->egress->writeCloseStream( $this->buffer );
695
696 $this->buffer = "";
697 $this->thisPage = "";
698 // this could be more than one file if we had more than one output arg
699
700 $filenameList = (array)$this->egress->getFilenames();
701 $newFilenames = array();
702 $firstPageID = str_pad( $this->firstPageWritten, 9, "0", STR_PAD_LEFT );
703 $lastPageID = str_pad( $this->lastPageWritten, 9, "0", STR_PAD_LEFT );
704 for ( $i = 0; $i < count( $filenameList ); $i++ ) {
705 $checkpointNameFilledIn = sprintf( $this->checkpointFiles[$i], $firstPageID, $lastPageID );
706 $fileinfo = pathinfo( $filenameList[$i] );
707 $newFilenames[] = $fileinfo['dirname'] . '/' . $checkpointNameFilledIn;
708 }
709 $this->egress->closeRenameAndReopen( $newFilenames );
710 $this->buffer = $this->xmlwriterobj->openStream();
711 $this->timeExceeded = false;
712 $this->timeOfCheckpoint = $this->lastTime;
713 $this->firstPageWritten = false;
714 $this->checkpointJustWritten = true;
715 }
716 else {
717 $this->egress->writeClosePage( $this->buffer );
718 $this->buffer = "";
719 $this->thisPage = "";
720 }
721
722 } elseif ( $name == 'mediawiki' ) {
723 $this->egress->writeCloseStream( $this->buffer );
724 $this->buffer = "";
725 }
726 }
727
728 function characterData( $parser, $data ) {
729 $this->clearOpenElement( null );
730 if ( $this->lastName == "id" ) {
731 if ( $this->state == "revision" ) {
732 $this->thisRev .= $data;
733 } elseif ( $this->state == "page" ) {
734 $this->thisPage .= $data;
735 }
736 }
737 // have to skip the newline left over from closepagetag line of
738 // end of checkpoint files. nasty hack!!
739 if ( $this->checkpointJustWritten ) {
740 if ( $data[0] == "\n" ) {
741 $data = substr( $data, 1 );
742 }
743 $this->checkpointJustWritten = false;
744 }
745 $this->buffer .= htmlspecialchars( $data );
746 }
747
748 function clearOpenElement( $style ) {
749 if ( $this->openElement ) {
750 $this->buffer .= Xml::element( $this->openElement[0], $this->openElement[1], $style );
751 $this->openElement = false;
752 }
753 }
754 }