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