public function execute() { $user = $this->getUser(); $params = $this->extractRequestParams(); $isUpload = false; if ( isset( $params['interwikisource'] ) ) { if ( !$user->isAllowed( 'import' ) ) { $this->dieUsageMsg( 'cantimport' ); } if ( !isset( $params['interwikipage'] ) ) { $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) ); } $source = ImportStreamSource::newFromInterwiki( $params['interwikisource'], $params['interwikipage'], $params['fullhistory'], $params['templates'] ); } else { $isUpload = true; if ( !$user->isAllowed( 'importupload' ) ) { $this->dieUsageMsg( 'cantimport-upload' ); } $source = ImportStreamSource::newFromUpload( 'xml' ); } if ( !$source->isOK() ) { $this->dieStatus( $source ); } $importer = new WikiImporter( $source->value ); if ( isset( $params['namespace'] ) ) { $importer->setTargetNamespace( $params['namespace'] ); } if ( isset( $params['rootpage'] ) ) { $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] ); if ( !$statusRootPage->isGood() ) { $this->dieStatus( $statusRootPage ); } } $reporter = new ApiImportReporter( $importer, $isUpload, $params['interwikisource'], $params['summary'] ); try { $importer->doImport(); } catch ( MWException $e ) { $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) ); } $resultData = $reporter->getData(); $result = $this->getResult(); $result->setIndexedTagName( $resultData, 'page' ); $result->addValue( null, $this->getModuleName(), $resultData ); }
/** * Do the actual import */ private function doImport() { global $wgImportSources, $wgExportMaxLinkDepth; $isUpload = false; $request = $this->getRequest(); $this->namespace = $request->getIntOrNull('namespace'); $sourceName = $request->getVal("source"); $this->logcomment = $request->getText('log-comment'); $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $request->getIntOrNull('pagelink-depth'); $this->rootpage = $request->getText('rootpage'); $user = $this->getUser(); if (!$user->matchEditToken($request->getVal('editToken'))) { $source = Status::newFatal('import-token-mismatch'); } elseif ($sourceName == 'upload') { $isUpload = true; if ($user->isAllowed('importupload')) { $source = ImportStreamSource::newFromUpload("xmlimport"); } else { throw new PermissionsError('importupload'); } } elseif ($sourceName == "interwiki") { if (!$user->isAllowed('import')) { throw new PermissionsError('import'); } $this->interwiki = $request->getVal('interwiki'); if (!in_array($this->interwiki, $wgImportSources)) { $source = Status::newFatal("import-invalid-interwiki"); } else { $this->history = $request->getCheck('interwikiHistory'); $this->frompage = $request->getText("frompage"); $this->includeTemplates = $request->getCheck('interwikiTemplates'); $source = ImportStreamSource::newFromInterwiki($this->interwiki, $this->frompage, $this->history, $this->includeTemplates, $this->pageLinkDepth); } } else { $source = Status::newFatal("importunknownsource"); } $out = $this->getOutput(); if (!$source->isGood()) { $out->wrapWikiMsg("<p class=\"error\">\n\$1\n</p>", array('importfailed', $source->getWikiText())); } else { $importer = new WikiImporter($source->value); if (!is_null($this->namespace)) { $importer->setTargetNamespace($this->namespace); } if (!is_null($this->rootpage)) { $statusRootPage = $importer->setTargetRootPage($this->rootpage); if (!$statusRootPage->isGood()) { $out->wrapWikiMsg("<p class=\"error\">\n\$1\n</p>", array('import-options-wrong', $statusRootPage->getWikiText(), count($statusRootPage->getErrorsArray()))); return; } } $out->addWikiMsg("importstart"); $reporter = new ImportReporter($importer, $isUpload, $this->interwiki, $this->logcomment); $reporter->setContext($this->getContext()); $exception = false; $reporter->open(); try { $importer->doImport(); } catch (MWException $e) { $exception = $e; } $result = $reporter->close(); if ($exception) { # No source or XML parse error $out->wrapWikiMsg("<p class=\"error\">\n\$1\n</p>", array('importfailed', $exception->getMessage())); } elseif (!$result->isGood()) { # Zero revisions $out->wrapWikiMsg("<p class=\"error\">\n\$1\n</p>", array('importfailed', $result->getWikiText())); } else { # Success! $out->addWikiMsg('importsuccess'); } $out->addHTML('<hr />'); } }
function importFromHandle($handle) { $this->startTime = microtime(true); $source = new ImportStreamSource($handle); $importer = new WikiImporter($source, $this->getConfig()); if ($this->hasOption('debug')) { $importer->setDebug(true); } if ($this->hasOption('no-updates')) { $importer->setNoUpdates(true); } if ($this->hasOption('rootpage')) { $statusRootPage = $importer->setTargetRootPage($this->getOption('rootpage')); if (!$statusRootPage->isGood()) { // Die here so that it doesn't print "Done!" $this->error($statusRootPage->getMessage()->text(), 1); return false; } } $importer->setPageCallback([$this, 'reportPage']); $this->importCallback = $importer->setRevisionCallback([$this, 'handleRevision']); $this->uploadCallback = $importer->setUploadCallback([$this, 'handleUpload']); $this->logItemCallback = $importer->setLogItemCallback([$this, 'handleLogItem']); if ($this->uploads) { $importer->setImportUploads(true); } if ($this->imageBasePath) { $importer->setImageBasePath($this->imageBasePath); } if ($this->dryRun) { $importer->setPageOutCallback(null); } return $importer->doImport(); }