MediaWiki  REL1_20
backupTextPass.inc
Go to the documentation of this file.
00001 <?php
00027 require_once( __DIR__ . '/backup.inc' );
00028 
00032 class TextPassDumper extends BackupDumper {
00033         var $prefetch = null;
00034         var $input = "php://stdin";
00035         var $history = WikiExporter::FULL;
00036         var $fetchCount = 0;
00037         var $prefetchCount = 0;
00038         var $prefetchCountLast = 0;
00039         var $fetchCountLast = 0;
00040 
00041         var $maxFailures = 5;
00042         var $maxConsecutiveFailedTextRetrievals = 200;
00043         var $failureTimeout = 5; // Seconds to sleep after db failure
00044 
00045         var $php = "php";
00046         var $spawn = false;
00047 
00051         var $spawnProc = false;
00052 
00056         var $spawnWrite = false;
00057 
00061         var $spawnRead = false;
00062 
00066         var $spawnErr = false;
00067 
00068         var $xmlwriterobj = false;
00069 
00070         // when we spend more than maxTimeAllowed seconds on this run, we continue
00071         // processing until we write out the next complete page, then save output file(s),
00072         // rename it/them and open new one(s)
00073         var $maxTimeAllowed = 0;  // 0 = no limit
00074         var $timeExceeded = false;
00075         var $firstPageWritten = false;
00076         var $lastPageWritten = false;
00077         var $checkpointJustWritten = false;
00078         var $checkpointFiles = array();
00079 
00083         protected $db;
00084 
00085 
00097         function rotateDb() {
00098                 // Cleaning up old connections
00099                 if ( isset( $this->lb ) ) {
00100                         $this->lb->closeAll();
00101                         unset( $this->lb );
00102                 }
00103 
00104                 if ( $this->forcedDb !== null ) {
00105                         $this->db = $this->forcedDb;
00106                         return;
00107                 }
00108 
00109                 if ( isset( $this->db ) && $this->db->isOpen() ) {
00110                         throw new MWException( 'DB is set and has not been closed by the Load Balancer' );
00111                 }
00112 
00113                 unset( $this->db );
00114 
00115                 // Trying to set up new connection.
00116                 // We do /not/ retry upon failure, but delegate to encapsulating logic, to avoid
00117                 // individually retrying at different layers of code.
00118 
00119                 // 1. The LoadBalancer.
00120                 try {
00121                         $this->lb = wfGetLBFactory()->newMainLB();
00122                 } catch ( Exception $e ) {
00123                         throw new MWException( __METHOD__ . " rotating DB failed to obtain new load balancer (" . $e->getMessage() . ")" );
00124                 }
00125 
00126 
00127                 // 2. The Connection, through the load balancer.
00128                 try {
00129                         $this->db = $this->lb->getConnection( DB_SLAVE, 'backup' );
00130                 } catch ( Exception $e ) {
00131                         throw new MWException( __METHOD__ . " rotating DB failed to obtain new database (" . $e->getMessage() . ")" );
00132                 }
00133         }
00134 
00135 
00136         function initProgress( $history = WikiExporter::FULL ) {
00137                 parent::initProgress();
00138                 $this->timeOfCheckpoint = $this->startTime;
00139         }
00140 
00141         function dump( $history, $text = WikiExporter::TEXT ) {
00142                 // Notice messages will foul up your XML output even if they're
00143                 // relatively harmless.
00144                 if ( ini_get( 'display_errors' ) )
00145                         ini_set( 'display_errors', 'stderr' );
00146 
00147                 $this->initProgress( $this->history );
00148 
00149                 // We are trying to get an initial database connection to avoid that the
00150                 // first try of this request's first call to getText fails. However, if
00151                 // obtaining a good DB connection fails it's not a serious issue, as
00152                 // getText does retry upon failure and can start without having a working
00153                 // DB connection.
00154                 try {
00155                         $this->rotateDb();
00156                 } catch ( Exception $e ) {
00157                         // We do not even count this as failure. Just let eventual
00158                         // watchdogs know.
00159                         $this->progress( "Getting initial DB connection failed (" .
00160                                 $e->getMessage() . ")" );
00161                 }
00162 
00163                 $this->egress = new ExportProgressFilter( $this->sink, $this );
00164 
00165                 // it would be nice to do it in the constructor, oh well. need egress set
00166                 $this->finalOptionCheck();
00167 
00168                 // we only want this so we know how to close a stream :-P
00169                 $this->xmlwriterobj = new XmlDumpWriter();
00170 
00171                 $input = fopen( $this->input, "rt" );
00172                 $result = $this->readDump( $input );
00173 
00174                 if ( $this->spawnProc ) {
00175                         $this->closeSpawn();
00176                 }
00177 
00178                 $this->report( true );
00179         }
00180 
00181         function processOption( $opt, $val, $param ) {
00182                 global $IP;
00183                 $url = $this->processFileOpt( $val, $param );
00184 
00185                 switch( $opt ) {
00186                 case 'prefetch':
00187                         require_once "$IP/maintenance/backupPrefetch.inc";
00188                         $this->prefetch = new BaseDump( $url );
00189                         break;
00190                 case 'stub':
00191                         $this->input = $url;
00192                         break;
00193                 case 'maxtime':
00194                         $this->maxTimeAllowed = intval( $val ) * 60;
00195                         break;
00196                 case 'checkpointfile':
00197                         $this->checkpointFiles[] = $val;
00198                         break;
00199                 case 'current':
00200                         $this->history = WikiExporter::CURRENT;
00201                         break;
00202                 case 'full':
00203                         $this->history = WikiExporter::FULL;
00204                         break;
00205                 case 'spawn':
00206                         $this->spawn = true;
00207                         if ( $val ) {
00208                                 $this->php = $val;
00209                         }
00210                         break;
00211                 }
00212         }
00213 
00214         function processFileOpt( $val, $param ) {
00215                 $fileURIs = explode( ';', $param );
00216                 foreach ( $fileURIs as $URI ) {
00217                         switch( $val ) {
00218                                 case "file":
00219                                         $newURI = $URI;
00220                                         break;
00221                                 case "gzip":
00222                                         $newURI = "compress.zlib://$URI";
00223                                         break;
00224                                 case "bzip2":
00225                                         $newURI = "compress.bzip2://$URI";
00226                                         break;
00227                                 case "7zip":
00228                                         $newURI = "mediawiki.compress.7z://$URI";
00229                                         break;
00230                                 default:
00231                                         $newURI = $URI;
00232                         }
00233                         $newFileURIs[] = $newURI;
00234                 }
00235                 $val = implode( ';', $newFileURIs );
00236                 return $val;
00237         }
00238 
00242         function showReport() {
00243                 if ( !$this->prefetch ) {
00244                         parent::showReport();
00245                         return;
00246                 }
00247 
00248                 if ( $this->reporting ) {
00249                         $now = wfTimestamp( TS_DB );
00250                         $nowts = microtime( true );
00251                         $deltaAll = $nowts - $this->startTime;
00252                         $deltaPart = $nowts - $this->lastTime;
00253                         $this->pageCountPart = $this->pageCount - $this->pageCountLast;
00254                         $this->revCountPart = $this->revCount - $this->revCountLast;
00255 
00256                         if ( $deltaAll ) {
00257                                 $portion = $this->revCount / $this->maxCount;
00258                                 $eta = $this->startTime + $deltaAll / $portion;
00259                                 $etats = wfTimestamp( TS_DB, intval( $eta ) );
00260                                 if ( $this->fetchCount ) {
00261                                         $fetchRate = 100.0 * $this->prefetchCount / $this->fetchCount;
00262                                 } else {
00263                                         $fetchRate = '-';
00264                                 }
00265                                 $pageRate = $this->pageCount / $deltaAll;
00266                                 $revRate = $this->revCount / $deltaAll;
00267                         } else {
00268                                 $pageRate = '-';
00269                                 $revRate = '-';
00270                                 $etats = '-';
00271                                 $fetchRate = '-';
00272                         }
00273                         if ( $deltaPart ) {
00274                                 if ( $this->fetchCountLast ) {
00275                                         $fetchRatePart = 100.0 * $this->prefetchCountLast / $this->fetchCountLast;
00276                                 } else {
00277                                         $fetchRatePart = '-';
00278                                 }
00279                                 $pageRatePart = $this->pageCountPart / $deltaPart;
00280                                 $revRatePart = $this->revCountPart / $deltaPart;
00281 
00282                         } else {
00283                                 $fetchRatePart = '-';
00284                                 $pageRatePart = '-';
00285                                 $revRatePart = '-';
00286                         }
00287                         $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]",
00288                                         $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $fetchRate, $fetchRatePart, $etats, $this->maxCount ) );
00289                         $this->lastTime = $nowts;
00290                         $this->revCountLast = $this->revCount;
00291                         $this->prefetchCountLast = $this->prefetchCount;
00292                         $this->fetchCountLast = $this->fetchCount;
00293                 }
00294         }
00295 
00296         function setTimeExceeded() {
00297                 $this->timeExceeded = True;
00298         }
00299 
00300         function checkIfTimeExceeded() {
00301                 if ( $this->maxTimeAllowed &&  ( $this->lastTime - $this->timeOfCheckpoint  > $this->maxTimeAllowed ) ) {
00302                         return true;
00303                 }
00304                 return false;
00305         }
00306 
00307         function finalOptionCheck() {
00308                 if ( ( $this->checkpointFiles && ! $this->maxTimeAllowed ) ||
00309                         ( $this->maxTimeAllowed && !$this->checkpointFiles ) ) {
00310                         throw new MWException( "Options checkpointfile and maxtime must be specified together.\n" );
00311                 }
00312                 foreach ( $this->checkpointFiles as $checkpointFile ) {
00313                         $count = substr_count ( $checkpointFile, "%s" );
00314                         if ( $count != 2 ) {
00315                                 throw new MWException( "Option checkpointfile must contain two '%s' for substitution of first and last pageids, count is $count instead, file is $checkpointFile.\n" );
00316                         }
00317                 }
00318 
00319                 if ( $this->checkpointFiles ) {
00320                         $filenameList = (array)$this->egress->getFilenames();
00321                         if ( count( $filenameList ) != count( $this->checkpointFiles ) ) {
00322                                 throw new MWException( "One checkpointfile must be specified for each output option, if maxtime is used.\n" );
00323                         }
00324                 }
00325         }
00326 
00331         function readDump( $input ) {
00332                 $this->buffer = "";
00333                 $this->openElement = false;
00334                 $this->atStart = true;
00335                 $this->state = "";
00336                 $this->lastName = "";
00337                 $this->thisPage = 0;
00338                 $this->thisRev = 0;
00339 
00340                 $parser = xml_parser_create( "UTF-8" );
00341                 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
00342 
00343                 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
00344                 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
00345 
00346                 $offset = 0; // for context extraction on error reporting
00347                 $bufferSize = 512 * 1024;
00348                 do {
00349                         if ( $this->checkIfTimeExceeded() ) {
00350                                 $this->setTimeExceeded();
00351                         }
00352                         $chunk = fread( $input, $bufferSize );
00353                         if ( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
00354                                 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
00355 
00356                                 $byte = xml_get_current_byte_index( $parser );
00357                                 $msg = wfMessage( 'xml-error-string',
00358                                         'XML import parse failure',
00359                                         xml_get_current_line_number( $parser ),
00360                                         xml_get_current_column_number( $parser ),
00361                                         $byte . ( is_null( $chunk ) ? null : ( '; "' . substr( $chunk, $byte -$offset, 16 ) . '"' ) ),
00362                                         xml_error_string( xml_get_error_code( $parser ) ) )->escaped();
00363 
00364                                 xml_parser_free( $parser );
00365 
00366                                 throw new MWException( $msg );
00367                         }
00368                         $offset += strlen( $chunk );
00369                 } while ( $chunk !== false && !feof( $input ) );
00370                 if ( $this->maxTimeAllowed ) {
00371                         $filenameList = (array)$this->egress->getFilenames();
00372                         // we wrote some stuff after last checkpoint that needs renamed
00373                         if ( file_exists( $filenameList[0] ) ) {
00374                                 $newFilenames = array();
00375                                 # we might have just written the header and footer and had no
00376                                 # pages or revisions written... perhaps they were all deleted
00377                                 # there's no pageID 0 so we use that. the caller is responsible
00378                                 # for deciding what to do with a file containing only the
00379                                 # siteinfo information and the mw tags.
00380                                 if ( ! $this->firstPageWritten ) {
00381                                         $firstPageID = str_pad( 0, 9, "0", STR_PAD_LEFT );
00382                                         $lastPageID = str_pad( 0, 9, "0", STR_PAD_LEFT );
00383                                 }
00384                                 else {
00385                                         $firstPageID = str_pad( $this->firstPageWritten, 9, "0", STR_PAD_LEFT );
00386                                         $lastPageID = str_pad( $this->lastPageWritten, 9, "0", STR_PAD_LEFT );
00387                                 }
00388                                 for ( $i = 0; $i < count( $filenameList ); $i++ ) {
00389                                         $checkpointNameFilledIn = sprintf( $this->checkpointFiles[$i], $firstPageID, $lastPageID );
00390                                         $fileinfo = pathinfo( $filenameList[$i] );
00391                                         $newFilenames[] = $fileinfo['dirname'] . '/' . $checkpointNameFilledIn;
00392                                 }
00393                                 $this->egress->closeAndRename( $newFilenames );
00394                         }
00395                 }
00396                 xml_parser_free( $parser );
00397 
00398                 return true;
00399         }
00400 
00415         function getText( $id ) {
00416                 $prefetchNotTried = true; // Whether or not we already tried to get the text via prefetch.
00417                 $text = false; // The candidate for a good text. false if no proper value.
00418                 $failures = 0; // The number of times, this invocation of getText already failed.
00419 
00420                 static $consecutiveFailedTextRetrievals = 0; // The number of times getText failed without
00421                                                              // yielding a good text in between.
00422 
00423                 $this->fetchCount++;
00424 
00425                 // To allow to simply return on success and do not have to worry about book keeping,
00426                 // we assume, this fetch works (possible after some retries). Nevertheless, we koop
00427                 // the old value, so we can restore it, if problems occur (See after the while loop).
00428                 $oldConsecutiveFailedTextRetrievals = $consecutiveFailedTextRetrievals;
00429                 $consecutiveFailedTextRetrievals = 0;
00430 
00431                 while ( $failures < $this->maxFailures ) {
00432 
00433                         // As soon as we found a good text for the $id, we will return immediately.
00434                         // Hence, if we make it past the try catch block, we know that we did not
00435                         // find a good text.
00436 
00437                         try {
00438                                 // Step 1: Get some text (or reuse from previous iteratuon if checking
00439                                 //         for plausibility failed)
00440 
00441                                 // Trying to get prefetch, if it has not been tried before
00442                                 if ( $text === false && isset( $this->prefetch ) && $prefetchNotTried ) {
00443                                         $prefetchNotTried = false;
00444                                         $tryIsPrefetch = true;
00445                                         $text = $this->prefetch->prefetch( intval( $this->thisPage ),
00446                                                 intval( $this->thisRev ) );
00447                                         if ( $text === null ) {
00448                                                 $text = false;
00449                                         }
00450                                 }
00451 
00452                                 if ( $text === false ) {
00453                                         // Fallback to asking the database
00454                                         $tryIsPrefetch = false;
00455                                         if ( $this->spawn ) {
00456                                                 $text = $this->getTextSpawned( $id );
00457                                         } else {
00458                                                 $text = $this->getTextDb( $id );
00459                                         }
00460 
00461                                         // No more checks for texts from DB for now.
00462                                         // If we received something that is not false,
00463                                         // We treat it as good text, regardless of whether it actually is or is not
00464                                         if ( $text !== false ) {
00465                                                 return $text;
00466                                         }
00467                                 }
00468 
00469                                 if ( $text === false ) {
00470                                         throw new MWException( "Generic error while obtaining text for id " . $id );
00471                                 }
00472 
00473                                 // We received a good candidate for the text of $id via some method
00474 
00475                                 // Step 2: Checking for plausibility and return the text if it is
00476                                 //         plausible
00477                                 $revID = intval( $this->thisRev );
00478                                 if ( ! isset( $this->db ) ) {
00479                                         throw new MWException( "No database available" );
00480                                 }
00481                                 $revLength = $this->db->selectField( 'revision', 'rev_len', array( 'rev_id' => $revID ) );
00482                                 if ( strlen( $text ) == $revLength ) {
00483                                         if ( $tryIsPrefetch ) {
00484                                                 $this->prefetchCount++;
00485                                         }
00486                                         return $text;
00487                                 }
00488 
00489                                 $text = false;
00490                                 throw new MWException( "Received text is unplausible for id " . $id );
00491 
00492                         } catch ( Exception $e ) {
00493                                 $msg = "getting/checking text " . $id . " failed (" . $e->getMessage() . ")";
00494                                 if ( $failures + 1 < $this->maxFailures ) {
00495                                         $msg .= " (Will retry " . ( $this->maxFailures - $failures - 1 ) . " more times)";
00496                                 }
00497                                 $this->progress( $msg );
00498                         }
00499 
00500                         // Something went wrong; we did not a text that was plausible :(
00501                         $failures++;
00502 
00503                         // A failure in a prefetch hit does not warrant resetting db connection etc.
00504                         if ( ! $tryIsPrefetch ) {
00505                                 // After backing off for some time, we try to reboot the whole process as
00506                                 // much as possible to not carry over failures from one part to the other
00507                                 // parts
00508                                 sleep( $this->failureTimeout );
00509                                 try {
00510                                         $this->rotateDb();
00511                                         if ( $this->spawn ) {
00512                                                 $this->closeSpawn();
00513                                                 $this->openSpawn();
00514                                         }
00515                                 } catch ( Exception $e ) {
00516                                         $this->progress( "Rebooting getText infrastructure failed (" . $e->getMessage() . ")" .
00517                                                 " Trying to continue anyways" );
00518                                 }
00519                         }
00520                 }
00521 
00522                 // Retirieving a good text for $id failed (at least) maxFailures times.
00523                 // We abort for this $id.
00524 
00525                 // Restoring the consecutive failures, and maybe aborting, if the dump
00526                 // is too broken.
00527                 $consecutiveFailedTextRetrievals = $oldConsecutiveFailedTextRetrievals + 1;
00528                 if ( $consecutiveFailedTextRetrievals > $this->maxConsecutiveFailedTextRetrievals ) {
00529                         throw new MWException( "Graceful storage failure" );
00530                 }
00531 
00532                 return "";
00533         }
00534 
00535 
00542         private function getTextDb( $id ) {
00543                 global $wgContLang;
00544                 if ( ! isset( $this->db ) ) {
00545                         throw new MWException( __METHOD__ . "No database available" );
00546                 }
00547                 $row = $this->db->selectRow( 'text',
00548                         array( 'old_text', 'old_flags' ),
00549                         array( 'old_id' => $id ),
00550                         __METHOD__ );
00551                 $text = Revision::getRevisionText( $row );
00552                 if ( $text === false ) {
00553                         return false;
00554                 }
00555                 $stripped = str_replace( "\r", "", $text );
00556                 $normalized = $wgContLang->normalize( $stripped );
00557                 return $normalized;
00558         }
00559 
00560         private function getTextSpawned( $id ) {
00561                 wfSuppressWarnings();
00562                 if ( !$this->spawnProc ) {
00563                         // First time?
00564                         $this->openSpawn();
00565                 }
00566                 $text = $this->getTextSpawnedOnce( $id );
00567                 wfRestoreWarnings();
00568                 return $text;
00569         }
00570 
00571         function openSpawn() {
00572                 global $IP;
00573 
00574                 if ( file_exists( "$IP/../multiversion/MWScript.php" ) ) {
00575                         $cmd = implode( " ",
00576                                 array_map( 'wfEscapeShellArg',
00577                                         array(
00578                                                 $this->php,
00579                                                 "$IP/../multiversion/MWScript.php",
00580                                                 "fetchText.php",
00581                                                 '--wiki', wfWikiID() ) ) );
00582                 }
00583                 else {
00584                         $cmd = implode( " ",
00585                                 array_map( 'wfEscapeShellArg',
00586                                         array(
00587                                                 $this->php,
00588                                                 "$IP/maintenance/fetchText.php",
00589                                                 '--wiki', wfWikiID() ) ) );
00590                 }
00591                 $spec = array(
00592                         0 => array( "pipe", "r" ),
00593                         1 => array( "pipe", "w" ),
00594                         2 => array( "file", "/dev/null", "a" ) );
00595                 $pipes = array();
00596 
00597                 $this->progress( "Spawning database subprocess: $cmd" );
00598                 $this->spawnProc = proc_open( $cmd, $spec, $pipes );
00599                 if ( !$this->spawnProc ) {
00600                         // shit
00601                         $this->progress( "Subprocess spawn failed." );
00602                         return false;
00603                 }
00604                 list(
00605                         $this->spawnWrite, // -> stdin
00606                         $this->spawnRead,  // <- stdout
00607                 ) = $pipes;
00608 
00609                 return true;
00610         }
00611 
00612         private function closeSpawn() {
00613                 wfSuppressWarnings();
00614                 if ( $this->spawnRead )
00615                         fclose( $this->spawnRead );
00616                 $this->spawnRead = false;
00617                 if ( $this->spawnWrite )
00618                         fclose( $this->spawnWrite );
00619                 $this->spawnWrite = false;
00620                 if ( $this->spawnErr )
00621                         fclose( $this->spawnErr );
00622                 $this->spawnErr = false;
00623                 if ( $this->spawnProc )
00624                         pclose( $this->spawnProc );
00625                 $this->spawnProc = false;
00626                 wfRestoreWarnings();
00627         }
00628 
00629         private function getTextSpawnedOnce( $id ) {
00630                 global $wgContLang;
00631 
00632                 $ok = fwrite( $this->spawnWrite, "$id\n" );
00633                 // $this->progress( ">> $id" );
00634                 if ( !$ok ) return false;
00635 
00636                 $ok = fflush( $this->spawnWrite );
00637                 // $this->progress( ">> [flush]" );
00638                 if ( !$ok ) return false;
00639 
00640                 // check that the text id they are sending is the one we asked for
00641                 // this avoids out of sync revision text errors we have encountered in the past
00642                 $newId = fgets( $this->spawnRead );
00643                 if ( $newId === false ) {
00644                         return false;
00645                 }
00646                 if ( $id != intval( $newId ) ) {
00647                         return false;
00648                 }
00649 
00650                 $len = fgets( $this->spawnRead );
00651                 // $this->progress( "<< " . trim( $len ) );
00652                 if ( $len === false ) return false;
00653 
00654                 $nbytes = intval( $len );
00655                 // actual error, not zero-length text
00656                 if ( $nbytes < 0 ) return false;
00657 
00658                 $text = "";
00659 
00660                 // Subprocess may not send everything at once, we have to loop.
00661                 while ( $nbytes > strlen( $text ) ) {
00662                         $buffer = fread( $this->spawnRead, $nbytes - strlen( $text ) );
00663                         if ( $buffer === false ) break;
00664                         $text .= $buffer;
00665                 }
00666 
00667                 $gotbytes = strlen( $text );
00668                 if ( $gotbytes != $nbytes ) {
00669                         $this->progress( "Expected $nbytes bytes from database subprocess, got $gotbytes " );
00670                         return false;
00671                 }
00672 
00673                 // Do normalization in the dump thread...
00674                 $stripped = str_replace( "\r", "", $text );
00675                 $normalized = $wgContLang->normalize( $stripped );
00676                 return $normalized;
00677         }
00678 
00679         function startElement( $parser, $name, $attribs ) {
00680                 $this->checkpointJustWritten = false;
00681 
00682                 $this->clearOpenElement( null );
00683                 $this->lastName = $name;
00684 
00685                 if ( $name == 'revision' ) {
00686                         $this->state = $name;
00687                         $this->egress->writeOpenPage( null, $this->buffer );
00688                         $this->buffer = "";
00689                 } elseif ( $name == 'page' ) {
00690                         $this->state = $name;
00691                         if ( $this->atStart ) {
00692                                 $this->egress->writeOpenStream( $this->buffer );
00693                                 $this->buffer = "";
00694                                 $this->atStart = false;
00695                         }
00696                 }
00697 
00698                 if ( $name == "text" && isset( $attribs['id'] ) ) {
00699                         $text = $this->getText( $attribs['id'] );
00700                         $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
00701                         if ( strlen( $text ) > 0 ) {
00702                                 $this->characterData( $parser, $text );
00703                         }
00704                 } else {
00705                         $this->openElement = array( $name, $attribs );
00706                 }
00707         }
00708 
00709         function endElement( $parser, $name ) {
00710                 $this->checkpointJustWritten = false;
00711 
00712                 if ( $this->openElement ) {
00713                         $this->clearOpenElement( "" );
00714                 } else {
00715                         $this->buffer .= "</$name>";
00716                 }
00717 
00718                 if ( $name == 'revision' ) {
00719                         $this->egress->writeRevision( null, $this->buffer );
00720                         $this->buffer = "";
00721                         $this->thisRev = "";
00722                 } elseif ( $name == 'page' ) {
00723                         if ( ! $this->firstPageWritten ) {
00724                                 $this->firstPageWritten = trim( $this->thisPage );
00725                         }
00726                         $this->lastPageWritten = trim( $this->thisPage );
00727                         if ( $this->timeExceeded ) {
00728                                 $this->egress->writeClosePage( $this->buffer );
00729                                 // nasty hack, we can't just write the chardata after the
00730                                 // page tag, it will include leading blanks from the next line
00731                                 $this->egress->sink->write( "\n" );
00732 
00733                                 $this->buffer = $this->xmlwriterobj->closeStream();
00734                                 $this->egress->writeCloseStream( $this->buffer );
00735 
00736                                 $this->buffer = "";
00737                                 $this->thisPage = "";
00738                                 // this could be more than one file if we had more than one output arg
00739 
00740                                 $filenameList = (array)$this->egress->getFilenames();
00741                                 $newFilenames = array();
00742                                 $firstPageID = str_pad( $this->firstPageWritten, 9, "0", STR_PAD_LEFT );
00743                                 $lastPageID = str_pad( $this->lastPageWritten, 9, "0", STR_PAD_LEFT );
00744                                 for ( $i = 0; $i < count( $filenameList ); $i++ ) {
00745                                         $checkpointNameFilledIn = sprintf( $this->checkpointFiles[$i], $firstPageID, $lastPageID );
00746                                         $fileinfo = pathinfo( $filenameList[$i] );
00747                                         $newFilenames[] = $fileinfo['dirname'] . '/' . $checkpointNameFilledIn;
00748                                 }
00749                                 $this->egress->closeRenameAndReopen( $newFilenames );
00750                                 $this->buffer = $this->xmlwriterobj->openStream();
00751                                 $this->timeExceeded = false;
00752                                 $this->timeOfCheckpoint = $this->lastTime;
00753                                 $this->firstPageWritten = false;
00754                                 $this->checkpointJustWritten = true;
00755                         }
00756                         else {
00757                                 $this->egress->writeClosePage( $this->buffer );
00758                                 $this->buffer = "";
00759                                 $this->thisPage = "";
00760                         }
00761 
00762                 } elseif ( $name == 'mediawiki' ) {
00763                         $this->egress->writeCloseStream( $this->buffer );
00764                         $this->buffer = "";
00765                 }
00766         }
00767 
00768         function characterData( $parser, $data ) {
00769                 $this->clearOpenElement( null );
00770                 if ( $this->lastName == "id" ) {
00771                         if ( $this->state == "revision" ) {
00772                                 $this->thisRev .= $data;
00773                         } elseif ( $this->state == "page" ) {
00774                                 $this->thisPage .= $data;
00775                         }
00776                 }
00777                 // have to skip the newline left over from closepagetag line of
00778                 // end of checkpoint files. nasty hack!!
00779                 if ( $this->checkpointJustWritten ) {
00780                         if ( $data[0] == "\n" ) {
00781                                 $data = substr( $data, 1 );
00782                         }
00783                         $this->checkpointJustWritten = false;
00784                 }
00785                 $this->buffer .= htmlspecialchars( $data );
00786         }
00787 
00788         function clearOpenElement( $style ) {
00789                 if ( $this->openElement ) {
00790                         $this->buffer .= Xml::element( $this->openElement[0], $this->openElement[1], $style );
00791                         $this->openElement = false;
00792                 }
00793         }
00794 }