48 private $importSources;
55 parent::__construct(
'Import',
'import' );
57 $this->wikiImporterFactory = $wikiImporterFactory;
76 if ( !is_array( $this->importSources ) ) {
77 throw new UnexpectedValueException(
'$wgImportSources must be an array' );
79 $this->
getHookRunner()->onImportSources( $this->importSources );
82 $statusImport = PermissionStatus::newEmpty();
83 $authority->isDefinitelyAllowed(
'import', $statusImport );
84 $statusImportUpload = PermissionStatus::newEmpty();
85 $authority->isDefinitelyAllowed(
'importupload', $statusImportUpload );
90 if ( !$statusImport->isGood() && !$statusImportUpload->isGood() ) {
93 $out->prepareErrorPage();
94 $out->setPageTitleMsg( $this->
msg(
'permissionserrors' ) );
95 $out->addModuleStyles(
'mediawiki.codex.messagebox.styles' );
96 $out->addWikiTextAsInterface( Html::errorBox(
97 $out->formatPermissionStatus( $statusImport,
'import' )
99 $out->addWikiTextAsInterface( Html::errorBox(
100 $out->formatPermissionStatus( $statusImportUpload,
'importupload' )
105 $out->addModules(
'mediawiki.misc-authed-ooui' );
106 $out->addModuleStyles(
'mediawiki.special.import.styles.ooui' );
111 if ( $request->wasPosted() && $request->getRawVal(
'action' ) ==
'submit' ) {
120 private function doImport() {
123 $sourceName = $request->getVal(
'source' );
124 $assignKnownUsers = $request->getCheck(
'assignKnownUsers' );
126 $logcomment = $request->getText(
'log-comment' );
129 : $request->getIntOrNull(
'pagelink-depth' );
132 $mapping = $request->getVal(
'mapping' );
134 if ( $mapping ===
'namespace' ) {
135 $namespace = $request->getIntOrNull(
'namespace' );
136 } elseif ( $mapping ===
'subpage' ) {
137 $rootpage = $request->getText(
'rootpage' );
142 $status = PermissionStatus::newEmpty();
144 $fullInterwikiPrefix =
null;
145 if ( !$user->matchEditToken( $request->getVal(
'wpEditToken' ) ) ) {
146 $source = Status::newFatal(
'import-token-mismatch' );
147 } elseif ( $sourceName ===
'upload' ) {
149 $fullInterwikiPrefix = $request->getVal(
'usernamePrefix' );
150 if ( $authority->authorizeAction(
'importupload', $status ) ) {
153 throw new PermissionsError(
'importupload', $status );
155 } elseif ( $sourceName ===
'interwiki' ) {
156 if ( !$authority->authorizeAction(
'import', $status ) ) {
157 throw new PermissionsError(
'import', $status );
159 $interwiki = $fullInterwikiPrefix = $request->getVal(
'interwiki' );
161 $hasSubprojects = array_key_exists( $interwiki, $this->importSources );
162 if ( !$hasSubprojects && !in_array( $interwiki, $this->importSources ) ) {
163 $source = Status::newFatal(
"import-invalid-interwiki" );
166 if ( $hasSubprojects ) {
167 $subproject = $request->getVal(
'subproject' );
169 if ( str_starts_with( $subproject, $interwiki .
'::' ) ) {
170 $subproject = substr( $subproject, strlen( $interwiki .
'::' ) );
172 $fullInterwikiPrefix .=
':' . $subproject;
174 if ( $hasSubprojects &&
175 !in_array( $subproject, $this->importSources[$interwiki] )
177 $source = Status::newFatal(
'import-invalid-interwiki' );
179 $history = $request->getCheck(
'interwikiHistory' );
180 $frompage = $request->getText(
'frompage' );
181 $includeTemplates = $request->getCheck(
'interwikiTemplates' );
184 $fullInterwikiPrefix,
192 $source = Status::newFatal(
"importunknownsource" );
195 if ( (
string)$fullInterwikiPrefix ===
'' ) {
196 $source->fatal(
'importnoprefix' );
202 Html::errorBox(
'$1' ),
205 $source->getWikiText(
false,
false, $this->getLanguage() ),
206 count(
$source->getMessages() )
210 $importer = $this->wikiImporterFactory->getWikiImporter(
$source->value, $this->getAuthority() );
211 if ( $namespace !==
null ) {
212 $importer->setTargetNamespace( $namespace );
213 } elseif ( $rootpage !==
null ) {
214 $statusRootPage = $importer->setTargetRootPage( $rootpage );
215 if ( !$statusRootPage->isGood() ) {
217 Html::errorBox(
'$1' ),
219 'import-options-wrong',
220 $statusRootPage->getWikiText(
false,
false, $this->getLanguage() ),
221 count( $statusRootPage->getMessages() )
229 $importer->setUsernamePrefix( $fullInterwikiPrefix, $assignKnownUsers );
231 $out->addWikiMsg(
"importstart" );
237 $fullInterwikiPrefix,
245 $importer->doImport();
246 }
catch ( DBError | TimeoutException $e ) {
249 }
catch ( Exception $e ) {
252 $result = $reporter->close();
256 # No source or XML parse error
258 Html::errorBox(
'$1' ),
261 } elseif ( !$result->isGood() ) {
264 Html::errorBox(
'$1' ),
267 $result->getWikiText(
false,
false, $this->getLanguage() ),
268 count( $result->getMessages() )
273 $out->addWikiMsg(
'importsuccess' );
275 $out->addHTML(
'<hr />' );
279 private function getMappingFormPart(
string $sourceName ): array {
280 $defaultNamespace = $this->
getConfig()->get( MainConfigNames::ImportTargetNamespace );
286 'id' =>
"mw-import-mapping-$sourceName",
287 'options-messages' => [
288 'import-mapping-default' =>
'default',
289 'import-mapping-namespace' =>
'namespace',
290 'import-mapping-subpage' =>
'subpage'
292 'default' => $defaultNamespace !==
null ?
'namespace' :
'default'
295 'type' =>
'namespaceselect',
296 'name' =>
'namespace',
298 'id' =>
"mw-import-namespace-$sourceName",
299 'default' => $defaultNamespace ?:
'',
301 'disable-if' => [
'!==',
'mapping',
'namespace' ],
305 'name' =>
'rootpage',
308 'id' =>
"mw-interwiki-rootpage-$sourceName",
309 'disable-if' => [
'!==',
'mapping',
'subpage' ],
314 private function showForm() {
315 $action = $this->getPageTitle()->getLocalURL( [
'action' =>
'submit' ] );
317 $out = $this->getOutput();
318 $this->addHelpLink(
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Import',
true );
320 $interwikiFormDescriptor = [];
321 $uploadFormDescriptor = [];
323 if ( $authority->isDefinitelyAllowed(
'importupload' ) ) {
324 $mappingSelection = $this->getMappingFormPart(
'upload' );
325 $uploadFormDescriptor += [
329 'default' => $this->msg(
'importtext' )->parseAsBlock()
333 'name' =>
'xmlimport',
334 'accept' => [
'application/xml',
'text/xml' ],
335 'label-message' =>
'import-upload-filename',
338 'usernamePrefix' => [
340 'name' =>
'usernamePrefix',
341 'label-message' =>
'import-upload-username-prefix',
344 'assignKnownUsers' => [
346 'name' =>
'assignKnownUsers',
347 'label-message' =>
'import-assign-known-users'
351 'name' =>
'log-comment',
352 'label-message' =>
'import-comment'
357 'default' =>
'upload',
362 $uploadFormDescriptor += $mappingSelection;
364 $htmlForm = HTMLForm::factory(
'ooui', $uploadFormDescriptor, $this->
getContext() );
365 $htmlForm->setAction( $action );
366 $htmlForm->setId(
'mw-import-upload-form' );
367 $htmlForm->setWrapperLegendMsg(
'import-upload' );
368 $htmlForm->setSubmitTextMsg(
'uploadbtn' );
369 $htmlForm->prepareForm()->displayForm(
false );
371 } elseif ( !$this->importSources ) {
372 $out->addWikiMsg(
'importnosources' );
375 if ( $authority->isDefinitelyAllowed(
'import' ) && $this->importSources ) {
378 $needSubprojectField =
false;
379 foreach ( $this->importSources as $key => $value ) {
380 if ( is_int( $key ) ) {
382 } elseif ( $value !== $key ) {
383 $needSubprojectField =
true;
386 $projects[ $key ] = $key;
389 $interwikiFormDescriptor += [
393 'default' => $this->msg(
'import-interwiki-text' )->parseAsBlock()
397 'name' =>
'interwiki',
398 'label-message' =>
'import-interwiki-sourcewiki',
399 'options' => $projects
403 if ( $needSubprojectField ) {
405 foreach ( $this->importSources as $key => $value ) {
406 if ( is_array( $value ) ) {
407 foreach ( $value as $subproject ) {
408 $subprojects[ $subproject ] = $key .
'::' . $subproject;
413 $interwikiFormDescriptor += [
416 'name' =>
'subproject',
417 'options' => $subprojects
422 $interwikiFormDescriptor += [
425 'name' =>
'frompage',
426 'label-message' =>
'import-interwiki-sourcepage'
428 'interwikiHistory' => [
430 'name' =>
'interwikiHistory',
431 'label-message' =>
'import-interwiki-history'
433 'interwikiTemplates' => [
435 'name' =>
'interwikiTemplates',
436 'label-message' =>
'import-interwiki-templates'
438 'assignKnownUsers' => [
440 'name' =>
'assignKnownUsers',
441 'label-message' =>
'import-assign-known-users'
446 $interwikiFormDescriptor += [
447 'pagelink-depth' => [
449 'name' =>
'pagelink-depth',
450 'label-message' =>
'export-pagelinks',
456 $interwikiFormDescriptor += [
459 'name' =>
'log-comment',
460 'label-message' =>
'import-comment'
465 'default' =>
'interwiki',
469 $mappingSelection = $this->getMappingFormPart(
'interwiki' );
471 $interwikiFormDescriptor += $mappingSelection;
473 $htmlForm = HTMLForm::factory(
'ooui', $interwikiFormDescriptor, $this->
getContext() );
474 $htmlForm->setAction( $action );
475 $htmlForm->setId(
'mw-import-interwiki-form' );
476 $htmlForm->setWrapperLegendMsg(
'importinterwiki' );
477 $htmlForm->setSubmitTextMsg(
'import-interwiki-submit' );
478 $htmlForm->prepareForm()->displayForm(
false );