00001 <?php
00025 $optionsWithArgs = array( 'report' );
00026
00027 require_once( '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 if( preg_match( '/\.gz$/', $filename ) ) {
00119 $filename = 'compress.zlib://' . $filename;
00120 }
00121 $file = fopen( $filename, 'rt' );
00122 return $this->importFromHandle( $file );
00123 }
00124
00125 function importFromStdin() {
00126 $file = fopen( 'php://stdin', 'rt' );
00127 return $this->importFromHandle( $file );
00128 }
00129
00130 function importFromHandle( $handle ) {
00131 $this->startTime = wfTime();
00132
00133 $source = new ImportStreamSource( $handle );
00134 $importer = new WikiImporter( $source );
00135
00136 $importer->setDebug( $this->debug );
00137 $importer->setPageCallback( array( &$this, 'reportPage' ) );
00138 $this->importCallback = $importer->setRevisionCallback(
00139 array( &$this, 'handleRevision' ) );
00140 $this->uploadCallback = $importer->setUploadCallback(
00141 array( &$this, 'handleUpload' ) );
00142 $this->logItemCallback = $importer->setLogItemCallback(
00143 array( &$this, 'handleLogItem' ) );
00144
00145 return $importer->doImport();
00146 }
00147 }
00148
00149 if( wfReadOnly() ) {
00150 wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
00151 }
00152
00153 $reader = new BackupReader();
00154 if( isset( $options['quiet'] ) ) {
00155 $reader->reporting = false;
00156 }
00157 if( isset( $options['report'] ) ) {
00158 $reader->reportingInterval = intval( $options['report'] );
00159 }
00160 if( isset( $options['dry-run'] ) ) {
00161 $reader->dryRun = true;
00162 }
00163 if( isset( $options['debug'] ) ) {
00164 $reader->debug = true;
00165 }
00166 if( isset( $options['uploads'] ) ) {
00167 $reader->uploads = true;
00168 }
00169
00170 if( isset( $args[0] ) ) {
00171 $result = $reader->importFromFile( $args[0] );
00172 } else {
00173 $result = $reader->importFromStdin();
00174 }
00175
00176 if( WikiError::isError( $result ) ) {
00177 echo $result->getMessage() . "\n";
00178 } else {
00179 echo "Done!\n";
00180 echo "You might want to run rebuildrecentchanges.php to regenerate\n";
00181 echo "the recentchanges page.\n";
00182 }
00183
00184