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