MediaWiki  REL1_19
backup.inc
Go to the documentation of this file.
00001 <?php
00030 class DumpDBZip2Output extends DumpPipeOutput {
00031         function DumpDBZip2Output( $file ) {
00032                 parent::__construct( "dbzip2", $file );
00033         }
00034 }
00035 
00039 class BackupDumper {
00040         var $reportingInterval = 100;
00041         var $reporting = true;
00042         var $pageCount = 0;
00043         var $revCount  = 0;
00044         var $server    = null; // use default
00045         var $pages     = null; // all pages
00046         var $skipHeader = false; // don't output <mediawiki> and <siteinfo>
00047         var $skipFooter = false; // don't output </mediawiki>
00048         var $startId    = 0;
00049         var $endId      = 0;
00050         var $revStartId = 0;
00051         var $revEndId   = 0;
00052         var $sink       = null; // Output filters
00053         var $stubText   = false; // include rev_text_id instead of text; for 2-pass dump
00054         var $dumpUploads = false;
00055         var $dumpUploadFileContents = false;
00056         var $lastTime = 0;
00057         var $pageCountLast = 0;
00058         var $revCountLast = 0;
00059         var $ID = 0;
00060 
00061         var $outputTypes = array(), $filterTypes = array();
00062 
00066         protected $lb;
00067 
00068         function __construct( $args ) {
00069                 $this->stderr = fopen( "php://stderr", "wt" );
00070 
00071                 // Built-in output and filter plugins
00072                 $this->registerOutput( 'file', 'DumpFileOutput' );
00073                 $this->registerOutput( 'gzip', 'DumpGZipOutput' );
00074                 $this->registerOutput( 'bzip2', 'DumpBZip2Output' );
00075                 $this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
00076                 $this->registerOutput( '7zip', 'Dump7ZipOutput' );
00077 
00078                 $this->registerFilter( 'latest', 'DumpLatestFilter' );
00079                 $this->registerFilter( 'notalk', 'DumpNotalkFilter' );
00080                 $this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
00081 
00082                 $this->sink = $this->processArgs( $args );
00083         }
00084 
00089         function registerOutput( $name, $class ) {
00090                 $this->outputTypes[$name] = $class;
00091         }
00092 
00097         function registerFilter( $name, $class ) {
00098                 $this->filterTypes[$name] = $class;
00099         }
00100 
00108         function loadPlugin( $class, $file ) {
00109                 if ( $file != '' ) {
00110                         require_once( $file );
00111                 }
00112                 $register = array( $class, 'register' );
00113                 call_user_func_array( $register, array( &$this ) );
00114         }
00115 
00120         function processArgs( $args ) {
00121                 $sink = null;
00122                 $sinks = array();
00123                 foreach ( $args as $arg ) {
00124                         $matches = array();
00125                         if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) {
00126                                 @list( /* $full */ , $opt, $val, $param ) = $matches;
00127                                 switch( $opt ) {
00128                                 case "plugin":
00129                                         $this->loadPlugin( $val, $param );
00130                                         break;
00131                                 case "output":
00132                                         if ( !is_null( $sink ) ) {
00133                                                 $sinks[] = $sink;
00134                                         }
00135                                         if ( !isset( $this->outputTypes[$val] ) ) {
00136                                                 $this->fatalError( "Unrecognized output sink type '$val'" );
00137                                         }
00138                                         $type = $this->outputTypes[$val];
00139                                         $sink = new $type( $param );
00140                                         break;
00141                                 case "filter":
00142                                         if ( is_null( $sink ) ) {
00143                                                 $sink = new DumpOutput();
00144                                         }
00145                                         if ( !isset( $this->filterTypes[$val] ) ) {
00146                                                 $this->fatalError( "Unrecognized filter type '$val'" );
00147                                         }
00148                                         $type = $this->filterTypes[$val];
00149                                         $filter = new $type( $sink, $param );
00150 
00151                                         // references are lame in php...
00152                                         unset( $sink );
00153                                         $sink = $filter;
00154 
00155                                         break;
00156                                 case "report":
00157                                         $this->reportingInterval = intval( $val );
00158                                         break;
00159                                 case "server":
00160                                         $this->server = $val;
00161                                         break;
00162                                 case "force-normal":
00163                                         if ( !function_exists( 'utf8_normalize' ) ) {
00164                                                 wfDl( "php_utfnormal.so" );
00165                                                 if ( !function_exists( 'utf8_normalize' ) ) {
00166                                                         $this->fatalError( "Failed to load UTF-8 normalization extension. " .
00167                                                                 "Install or remove --force-normal parameter to use slower code." );
00168                                                 }
00169                                         }
00170                                         break;
00171                                 default:
00172                                         $this->processOption( $opt, $val, $param );
00173                                 }
00174                         }
00175                 }
00176 
00177                 if ( is_null( $sink ) ) {
00178                         $sink = new DumpOutput();
00179                 }
00180                 $sinks[] = $sink;
00181 
00182                 if ( count( $sinks ) > 1 ) {
00183                         return new DumpMultiWriter( $sinks );
00184                 } else {
00185                         return $sink;
00186                 }
00187         }
00188 
00189         function processOption( $opt, $val, $param ) {
00190                 // extension point for subclasses to add options
00191         }
00192 
00193         function dump( $history, $text = WikiExporter::TEXT ) {
00194                 # Notice messages will foul up your XML output even if they're
00195                 # relatively harmless.
00196                 if ( ini_get( 'display_errors' ) )
00197                         ini_set( 'display_errors', 'stderr' );
00198 
00199                 $this->initProgress( $history );
00200 
00201                 $db = $this->backupDb();
00202                 $exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
00203                 $exporter->dumpUploads = $this->dumpUploads;
00204                 $exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
00205 
00206                 $wrapper = new ExportProgressFilter( $this->sink, $this );
00207                 $exporter->setOutputSink( $wrapper );
00208 
00209                 if ( !$this->skipHeader )
00210                         $exporter->openStream();
00211                 # Log item dumps: all or by range
00212                 if ( $history & WikiExporter::LOGS ) {
00213                         if ( $this->startId || $this->endId ) {
00214                                 $exporter->logsByRange( $this->startId, $this->endId );
00215                         } else {
00216                                 $exporter->allLogs();
00217                         }
00218                 # Page dumps: all or by page ID range
00219                 } else if ( is_null( $this->pages ) ) {
00220                         if ( $this->startId || $this->endId ) {
00221                                 $exporter->pagesByRange( $this->startId, $this->endId );
00222                         } elseif ( $this->revStartId || $this->revEndId ) {
00223                                 $exporter->revsByRange( $this->revStartId, $this->revEndId );
00224                         } else {
00225                                 $exporter->allPages();
00226                         }
00227                 # Dump of specific pages
00228                 } else {
00229                         $exporter->pagesByName( $this->pages );
00230                 }
00231 
00232                 if ( !$this->skipFooter )
00233                         $exporter->closeStream();
00234 
00235                 $this->report( true );
00236         }
00237 
00244         function initProgress( $history = WikiExporter::FULL ) {
00245                 $table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
00246                 $field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
00247 
00248                 $dbr = wfGetDB( DB_SLAVE );
00249                 $this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
00250                 $this->startTime = wfTime();
00251                 $this->lastTime = $this->startTime;
00252                 $this->ID = getmypid();
00253         }
00254 
00261         function backupDb() {
00262                 $this->lb = wfGetLBFactory()->newMainLB();
00263                 $db = $this->lb->getConnection( DB_SLAVE, 'backup' );
00264 
00265                 // Discourage the server from disconnecting us if it takes a long time
00266                 // to read out the big ol' batch query.
00267                 $db->setTimeout( 3600 * 24 );
00268 
00269                 return $db;
00270         }
00271 
00272         function __destruct() {
00273                 if ( isset( $this->lb ) ) {
00274                         $this->lb->closeAll();
00275                 }
00276         }
00277 
00278         function backupServer() {
00279                 global $wgDBserver;
00280                 return $this->server
00281                         ? $this->server
00282                         : $wgDBserver;
00283         }
00284 
00285         function reportPage() {
00286                 $this->pageCount++;
00287         }
00288 
00289         function revCount() {
00290                 $this->revCount++;
00291                 $this->report();
00292         }
00293 
00294         function report( $final = false ) {
00295                 if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
00296                         $this->showReport();
00297                 }
00298         }
00299 
00300         function showReport() {
00301                 if ( $this->reporting ) {
00302                         $now = wfTimestamp( TS_DB );
00303                         $nowts = wfTime();
00304                         $deltaAll = wfTime() - $this->startTime;
00305                         $deltaPart = wfTime() - $this->lastTime;
00306                         $this->pageCountPart = $this->pageCount - $this->pageCountLast;
00307                         $this->revCountPart = $this->revCount - $this->revCountLast;
00308 
00309                         if ( $deltaAll ) {
00310                                 $portion = $this->revCount / $this->maxCount;
00311                                 $eta = $this->startTime + $deltaAll / $portion;
00312                                 $etats = wfTimestamp( TS_DB, intval( $eta ) );
00313                                 $pageRate = $this->pageCount / $deltaAll;
00314                                 $revRate = $this->revCount / $deltaAll;
00315                         } else {
00316                                 $pageRate = '-';
00317                                 $revRate = '-';
00318                                 $etats = '-';
00319                         }
00320                         if ( $deltaPart ) {
00321                                 $pageRatePart = $this->pageCountPart / $deltaPart;
00322                                 $revRatePart = $this->revCountPart / $deltaPart;
00323                         } else {
00324                                 $pageRatePart = '-';
00325                                 $revRatePart = '-';
00326                         }
00327                         $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), ETA %s [max %d]",
00328                                         $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats, $this->maxCount ) );
00329                         $this->lastTime = $nowts;
00330                         $this->revCountLast = $this->revCount;
00331                 }
00332         }
00333 
00334         function progress( $string ) {
00335                 fwrite( $this->stderr, $string . "\n" );
00336         }
00337 
00338         function fatalError( $msg ) {
00339                 $this->progress( "$msg\n" );
00340                 die(1);
00341         }
00342 }
00343 
00344 class ExportProgressFilter extends DumpFilter {
00345         function __construct( &$sink, &$progress ) {
00346                 parent::__construct( $sink );
00347                 $this->progress = $progress;
00348         }
00349 
00350         function writeClosePage( $string ) {
00351                 parent::writeClosePage( $string );
00352                 $this->progress->reportPage();
00353         }
00354 
00355         function writeRevision( $rev, $string ) {
00356                 parent::writeRevision( $rev, $string );
00357                 $this->progress->revCount();
00358         }
00359 }