00001 <?php
00025 $optionsWithArgs = array( 'report' );
00026
00027 require_once( dirname(__FILE__) . '/commandLine.inc' );
00028
00032 class BackupReader {
00033 var $reportingInterval = 100;
00034 var $reporting = true;
00035 var $pageCount = 0;
00036 var $revCount = 0;
00037 var $dryRun = false;
00038 var $debug = false;
00039 var $uploads = false;
00040
00041 function BackupReader() {
00042 $this->stderr = fopen( "php://stderr", "wt" );
00043 }
00044
00045 function reportPage( $page ) {
00046 $this->pageCount++;
00047 }
00048
00049 function handleRevision( $rev ) {
00050 $title = $rev->getTitle();
00051 if( !$title ) {
00052 $this->progress( "Got bogus revision with null title!" );
00053 return;
00054 }
00055
00056 $this->revCount++;
00057 $this->report();
00058
00059 if( !$this->dryRun ) {
00060 call_user_func( $this->importCallback, $rev );
00061 }
00062 }
00063
00064 function handleUpload( $revision ) {
00065 if( $this->uploads ) {
00066 $this->uploadCount++;
00067
00068 $this->progress( "upload: " . $revision->getFilename() );
00069
00070 if( !$this->dryRun ) {
00071
00072
00073 $dbw = wfGetDB( DB_MASTER );
00074 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
00075 }
00076 }
00077 }
00078
00079 function handleLogItem( $rev ) {
00080 $this->revCount++;
00081 $this->report();
00082
00083 if( !$this->dryRun ) {
00084 call_user_func( $this->logItemCallback, $rev );
00085 }
00086 }
00087
00088 function report( $final = false ) {
00089 if( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
00090 $this->showReport();
00091 }
00092 }
00093
00094 function showReport() {
00095 if( $this->reporting ) {
00096 $delta = wfTime() - $this->startTime;
00097 if( $delta ) {
00098 $rate = sprintf("%.2f", $this->pageCount / $delta);
00099 $revrate = sprintf("%.2f", $this->revCount / $delta);
00100 } else {
00101 $rate = '-';
00102 $revrate = '-';
00103 }
00104 # Logs dumps don't have page tallies
00105 if( $this->pageCount )
00106 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
00107 else
00108 $this->progress( "$this->revCount ($revrate revs/sec)" );
00109 }
00110 wfWaitForSlaves(5);
00111 }
00112
00113 function progress( $string ) {
00114 fwrite( $this->stderr, $string . "\n" );
00115 }
00116
00117 function importFromFile( $filename ) {
00118 $t = true;
00119 if( preg_match( '/\.gz$/', $filename ) ) {
00120 $filename = 'compress.zlib://' . $filename;
00121 }
00122 elseif( preg_match( '/\.bz2$/', $filename ) ) {
00123 $filename = 'compress.bzip2://' . $filename;
00124 }
00125 elseif( preg_match( '/\.7z$/', $filename ) ) {
00126 $filename = 'mediawiki.compress.7z://' . $filename;
00127 $t = false;
00128 }
00129
00130 $file = fopen( $filename, $t ? 'rt' : 't' );
00131 return $this->importFromHandle( $file );
00132 }
00133
00134 function importFromStdin() {
00135 $file = fopen( 'php://stdin', 'rt' );
00136 return $this->importFromHandle( $file );
00137 }
00138
00139 function importFromHandle( $handle ) {
00140 $this->startTime = wfTime();
00141
00142 $source = new ImportStreamSource( $handle );
00143 $importer = new WikiImporter( $source );
00144
00145 $importer->setDebug( $this->debug );
00146 $importer->setPageCallback( array( &$this, 'reportPage' ) );
00147 $this->importCallback = $importer->setRevisionCallback(
00148 array( &$this, 'handleRevision' ) );
00149 $this->uploadCallback = $importer->setUploadCallback(
00150 array( &$this, 'handleUpload' ) );
00151 $this->logItemCallback = $importer->setLogItemCallback(
00152 array( &$this, 'handleLogItem' ) );
00153
00154 return $importer->doImport();
00155 }
00156 }
00157
00158 if( wfReadOnly() ) {
00159 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
00160 }
00161
00162 $reader = new BackupReader();
00163 if( isset( $options['quiet'] ) ) {
00164 $reader->reporting = false;
00165 }
00166 if( isset( $options['report'] ) ) {
00167 $reader->reportingInterval = intval( $options['report'] );
00168 }
00169 if( isset( $options['dry-run'] ) ) {
00170 $reader->dryRun = true;
00171 }
00172 if( isset( $options['debug'] ) ) {
00173 $reader->debug = true;
00174 }
00175 if( isset( $options['uploads'] ) ) {
00176 $reader->uploads = true;
00177 }
00178
00179 if( isset( $args[0] ) ) {
00180 $result = $reader->importFromFile( $args[0] );
00181 } else {
00182 $result = $reader->importFromStdin();
00183 }
00184
00185 if( WikiError::isError( $result ) ) {
00186 echo $result->getMessage() . "\n";
00187 } else {
00188 echo "Done!\n";
00189 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
00190 echo "the recentchanges page.\n";
00191 }
00192
00193