MediaWiki  REL1_22
importDump.php
Go to the documentation of this file.
00001 <?php
00027 require_once __DIR__ . '/Maintenance.php';
00028 
00034 class BackupReader extends Maintenance {
00035     public $reportingInterval = 100;
00036     public $pageCount = 0;
00037     public $revCount = 0;
00038     public $dryRun = false;
00039     public $uploads = false;
00040     public $imageBasePath = false;
00041     public $nsFilter = false;
00042 
00043     function __construct() {
00044         parent::__construct();
00045         $gz = in_array( 'compress.zlib', stream_get_wrappers() ) ? 'ok' : '(disabled; requires PHP zlib module)';
00046         $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() ) ? 'ok' : '(disabled; requires PHP bzip2 module)';
00047 
00048         $this->mDescription = <<<TEXT
00049 This script reads pages from an XML file as produced from Special:Export or
00050 dumpBackup.php, and saves them into the current wiki.
00051 
00052 Compressed XML files may be read directly:
00053   .gz $gz
00054   .bz2 $bz2
00055   .7z (if 7za executable is in PATH)
00056 
00057 Note that for very large data sets, importDump.php may be slow; there are
00058 alternate methods which can be much faster for full site restoration:
00059 <http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
00060 TEXT;
00061         $this->stderr = fopen( "php://stderr", "wt" );
00062         $this->addOption( 'report',
00063             'Report position and speed after every n pages processed', false, true );
00064         $this->addOption( 'namespaces',
00065             'Import only the pages from namespaces belonging to the list of ' .
00066             'pipe-separated namespace names or namespace indexes', false, true );
00067         $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
00068         $this->addOption( 'debug', 'Output extra verbose debug information' );
00069         $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
00070         $this->addOption( 'no-updates', 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state' );
00071         $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
00072         $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
00073     }
00074 
00075     public function execute() {
00076         if ( wfReadOnly() ) {
00077             $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
00078         }
00079 
00080         $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
00081         if ( !$this->reportingInterval ) {
00082             $this->reportingInterval = 100; // avoid division by zero
00083         }
00084 
00085         $this->dryRun = $this->hasOption( 'dry-run' );
00086         $this->uploads = $this->hasOption( 'uploads' ); // experimental!
00087         if ( $this->hasOption( 'image-base-path' ) ) {
00088             $this->imageBasePath = $this->getOption( 'image-base-path' );
00089         }
00090         if ( $this->hasOption( 'namespaces' ) ) {
00091             $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
00092         }
00093 
00094         if ( $this->hasArg() ) {
00095             $this->importFromFile( $this->getArg() );
00096         } else {
00097             $this->importFromStdin();
00098         }
00099 
00100         $this->output( "Done!\n" );
00101         $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
00102     }
00103 
00104     function setNsfilter( array $namespaces ) {
00105         if ( count( $namespaces ) == 0 ) {
00106             $this->nsFilter = false;
00107             return;
00108         }
00109         $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
00110     }
00111 
00112     private function getNsIndex( $namespace ) {
00113         global $wgContLang;
00114         if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
00115             return $result;
00116         }
00117         $ns = intval( $namespace );
00118         if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
00119             return $ns;
00120         }
00121         $this->error( "Unknown namespace text / index specified: $namespace", true );
00122     }
00123 
00128     private function skippedNamespace( $obj ) {
00129         if ( $obj instanceof Title ) {
00130             $ns = $obj->getNamespace();
00131         } elseif ( $obj instanceof Revision ) {
00132             $ns = $obj->getTitle()->getNamespace();
00133         } elseif ( $obj instanceof WikiRevision ) {
00134             $ns = $obj->title->getNamespace();
00135         } else {
00136             echo wfBacktrace();
00137             $this->error( "Cannot get namespace of object in " . __METHOD__, true );
00138         }
00139         return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
00140     }
00141 
00142     function reportPage( $page ) {
00143         $this->pageCount++;
00144     }
00145 
00150     function handleRevision( $rev ) {
00151         $title = $rev->getTitle();
00152         if ( !$title ) {
00153             $this->progress( "Got bogus revision with null title!" );
00154             return;
00155         }
00156 
00157         if ( $this->skippedNamespace( $title ) ) {
00158             return;
00159         }
00160 
00161         $this->revCount++;
00162         $this->report();
00163 
00164         if ( !$this->dryRun ) {
00165             call_user_func( $this->importCallback, $rev );
00166         }
00167     }
00168 
00173     function handleUpload( $revision ) {
00174         if ( $this->uploads ) {
00175             if ( $this->skippedNamespace( $revision ) ) {
00176                 return;
00177             }
00178             $this->uploadCount++;
00179             // $this->report();
00180             $this->progress( "upload: " . $revision->getFilename() );
00181 
00182             if ( !$this->dryRun ) {
00183                 // bluuuh hack
00184                 // call_user_func( $this->uploadCallback, $revision );
00185                 $dbw = wfGetDB( DB_MASTER );
00186                 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
00187             }
00188         }
00189     }
00190 
00191     function handleLogItem( $rev ) {
00192         if ( $this->skippedNamespace( $rev ) ) {
00193             return;
00194         }
00195         $this->revCount++;
00196         $this->report();
00197 
00198         if ( !$this->dryRun ) {
00199             call_user_func( $this->logItemCallback, $rev );
00200         }
00201     }
00202 
00203     function report( $final = false ) {
00204         if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
00205             $this->showReport();
00206         }
00207     }
00208 
00209     function showReport() {
00210         if ( !$this->mQuiet ) {
00211             $delta = microtime( true ) - $this->startTime;
00212             if ( $delta ) {
00213                 $rate = sprintf( "%.2f", $this->pageCount / $delta );
00214                 $revrate = sprintf( "%.2f", $this->revCount / $delta );
00215             } else {
00216                 $rate = '-';
00217                 $revrate = '-';
00218             }
00219             # Logs dumps don't have page tallies
00220             if ( $this->pageCount ) {
00221                 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
00222             } else {
00223                 $this->progress( "$this->revCount ($revrate revs/sec)" );
00224             }
00225         }
00226         wfWaitForSlaves();
00227         // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
00228         DeferredUpdates::doUpdates( 'commit' );
00229     }
00230 
00231     function progress( $string ) {
00232         fwrite( $this->stderr, $string . "\n" );
00233     }
00234 
00235     function importFromFile( $filename ) {
00236         if ( preg_match( '/\.gz$/', $filename ) ) {
00237             $filename = 'compress.zlib://' . $filename;
00238         } elseif ( preg_match( '/\.bz2$/', $filename ) ) {
00239             $filename = 'compress.bzip2://' . $filename;
00240         } elseif ( preg_match( '/\.7z$/', $filename ) ) {
00241             $filename = 'mediawiki.compress.7z://' . $filename;
00242         }
00243 
00244         $file = fopen( $filename, 'rt' );
00245         return $this->importFromHandle( $file );
00246     }
00247 
00248     function importFromStdin() {
00249         $file = fopen( 'php://stdin', 'rt' );
00250         if ( self::posix_isatty( $file ) ) {
00251             $this->maybeHelp( true );
00252         }
00253         return $this->importFromHandle( $file );
00254     }
00255 
00256     function importFromHandle( $handle ) {
00257         $this->startTime = microtime( true );
00258 
00259         $source = new ImportStreamSource( $handle );
00260         $importer = new WikiImporter( $source );
00261 
00262         if ( $this->hasOption( 'debug' ) ) {
00263             $importer->setDebug( true );
00264         }
00265         if ( $this->hasOption( 'no-updates' ) ) {
00266             $importer->setNoUpdates( true );
00267         }
00268         $importer->setPageCallback( array( &$this, 'reportPage' ) );
00269         $this->importCallback = $importer->setRevisionCallback(
00270             array( &$this, 'handleRevision' ) );
00271         $this->uploadCallback = $importer->setUploadCallback(
00272             array( &$this, 'handleUpload' ) );
00273         $this->logItemCallback = $importer->setLogItemCallback(
00274             array( &$this, 'handleLogItem' ) );
00275         if ( $this->uploads ) {
00276             $importer->setImportUploads( true );
00277         }
00278         if ( $this->imageBasePath ) {
00279             $importer->setImageBasePath( $this->imageBasePath );
00280         }
00281 
00282         if ( $this->dryRun ) {
00283             $importer->setPageOutCallback( null );
00284         }
00285 
00286         return $importer->doImport();
00287     }
00288 }
00289 
00290 $maintClass = 'BackupReader';
00291 require_once RUN_MAINTENANCE_IF_MAIN;