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