49 private $importSources;
62 parent::__construct(
'Import',
'import' );
64 $this->permManager = $permManager;
65 $this->wikiImporterFactory = $wikiImporterFactory;
86 if ( !is_array( $this->importSources ) ) {
87 throw new UnexpectedValueException(
'$wgImportSources must be an array' );
89 $this->
getHookRunner()->onImportSources( $this->importSources );
92 if ( !$this->permManager->userHasAnyRight( $user,
'import',
'importupload' ) ) {
96 # @todo Allow PermissionManager::getPermissionErrors() to take an array
98 $this->permManager->getPermissionErrors(
99 'import', $user, $this->getPageTitle(),
100 PermissionManager::RIGOR_FULL,
101 [
'ns-specialprotected',
'badaccess-group0',
'badaccess-groups' ]
103 $this->permManager->getPermissionErrors(
104 'importupload', $user, $this->getPageTitle(),
105 PermissionManager::RIGOR_FULL,
106 [
'ns-specialprotected',
'badaccess-group0',
'badaccess-groups' ]
114 $this->
getOutput()->addModules(
'mediawiki.misc-authed-ooui' );
115 $this->
getOutput()->addModuleStyles(
'mediawiki.special.import.styles.ooui' );
120 if ( $request->wasPosted() && $request->getRawVal(
'action' ) ==
'submit' ) {
129 private function doImport() {
132 $sourceName = $request->getVal(
'source' );
133 $assignKnownUsers = $request->getCheck(
'assignKnownUsers' );
135 $logcomment = $request->getText(
'log-comment' );
138 : $request->getIntOrNull(
'pagelink-depth' );
141 $mapping = $request->getVal(
'mapping' );
143 if ( $mapping ===
'namespace' ) {
144 $namespace = $request->getIntOrNull(
'namespace' );
145 } elseif ( $mapping ===
'subpage' ) {
146 $rootpage = $request->getText(
'rootpage' );
151 $fullInterwikiPrefix =
null;
152 if ( !$user->matchEditToken( $request->getVal(
'wpEditToken' ) ) ) {
153 $source = Status::newFatal(
'import-token-mismatch' );
154 } elseif ( $sourceName ===
'upload' ) {
156 $fullInterwikiPrefix = $request->getVal(
'usernamePrefix' );
157 if ( $this->permManager->userHasRight( $user,
'importupload' ) ) {
162 } elseif ( $sourceName ===
'interwiki' ) {
163 if ( !$this->permManager->userHasRight( $user,
'import' ) ) {
166 $interwiki = $fullInterwikiPrefix = $request->getVal(
'interwiki' );
168 $hasSubprojects = array_key_exists( $interwiki, $this->importSources );
169 if ( !$hasSubprojects && !in_array( $interwiki, $this->importSources ) ) {
170 $source = Status::newFatal(
"import-invalid-interwiki" );
173 if ( $hasSubprojects ) {
174 $subproject = $request->getVal(
'subproject' );
176 if ( str_starts_with( $subproject, $interwiki .
'::' ) ) {
177 $subproject = substr( $subproject, strlen( $interwiki .
'::' ) );
179 $fullInterwikiPrefix .=
':' . $subproject;
181 if ( $hasSubprojects &&
182 !in_array( $subproject, $this->importSources[$interwiki] )
184 $source = Status::newFatal(
'import-invalid-interwiki' );
186 $history = $request->getCheck(
'interwikiHistory' );
187 $frompage = $request->getText(
'frompage' );
188 $includeTemplates = $request->getCheck(
'interwikiTemplates' );
191 $fullInterwikiPrefix,
199 $source = Status::newFatal(
"importunknownsource" );
202 if ( (
string)$fullInterwikiPrefix ===
'' ) {
203 $source->fatal(
'importnoprefix' );
208 $out->wrapWikiTextAsInterface(
'error',
209 $this->
msg(
'importfailed',
$source->getWikiText(
false,
false, $this->getLanguage() ) )
213 $importer = $this->wikiImporterFactory->getWikiImporter(
$source->value );
214 if ( $namespace !==
null ) {
215 $importer->setTargetNamespace( $namespace );
216 } elseif ( $rootpage !==
null ) {
217 $statusRootPage = $importer->setTargetRootPage( $rootpage );
218 if ( !$statusRootPage->isGood() ) {
220 "<div class=\"error\">\n$1\n</div>",
222 'import-options-wrong',
223 $statusRootPage->getWikiText(
false,
false, $this->getLanguage() ),
224 count( $statusRootPage->getErrorsArray() )
232 $importer->setUsernamePrefix( $fullInterwikiPrefix, $assignKnownUsers );
234 $out->addWikiMsg(
"importstart" );
240 $fullInterwikiPrefix,
248 $importer->doImport();
249 }
catch ( Exception $e ) {
252 $result = $reporter->close();
255 # No source or XML parse error
257 "<div class=\"error\">\n$1\n</div>",
258 [
'importfailed', $exception->getMessage() ]
260 } elseif ( !$result->isGood() ) {
263 "<div class=\"error\">\n$1\n</div>",
264 [
'importfailed', $result->getWikiText(
false,
false, $this->getLanguage() ) ]
268 $out->addWikiMsg(
'importsuccess' );
270 $out->addHTML(
'<hr />' );
274 private function getMappingFormPart( $sourceName ) {
281 'id' =>
"mw-import-mapping-$sourceName",
282 'options-messages' => [
283 'import-mapping-default' =>
'default',
284 'import-mapping-namespace' =>
'namespace',
285 'import-mapping-subpage' =>
'subpage'
287 'default' => $defaultNamespace !==
null ?
'namespace' :
'default'
290 'type' =>
'namespaceselect',
291 'name' =>
'namespace',
293 'id' =>
"mw-import-namespace-$sourceName",
294 'default' => $defaultNamespace ?:
'',
296 'disable-if' => [
'!==',
'mapping',
'namespace' ],
300 'name' =>
'rootpage',
303 'id' =>
"mw-interwiki-rootpage-$sourceName",
304 'disable-if' => [
'!==',
'mapping',
'subpage' ],
309 private function showForm() {
310 $action = $this->
getPageTitle()->getLocalURL( [
'action' =>
'submit' ] );
313 $this->
addHelpLink(
'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import',
true );
315 $interwikiFormDescriptor = [];
316 $uploadFormDescriptor = [];
318 if ( $this->permManager->userHasRight( $user,
'importupload' ) ) {
319 $mappingSelection = $this->getMappingFormPart(
'upload' );
320 $uploadFormDescriptor += [
324 'default' => $this->
msg(
'importtext' )->parseAsBlock()
328 'name' =>
'xmlimport',
329 'accept' => [
'application/xml',
'text/xml' ],
330 'label-message' =>
'import-upload-filename',
333 'usernamePrefix' => [
335 'name' =>
'usernamePrefix',
336 'label-message' =>
'import-upload-username-prefix',
339 'assignKnownUsers' => [
341 'name' =>
'assignKnownUsers',
342 'label-message' =>
'import-assign-known-users'
346 'name' =>
'log-comment',
347 'label-message' =>
'import-comment'
352 'default' =>
'upload',
357 $uploadFormDescriptor += $mappingSelection;
360 $htmlForm->setAction( $action );
361 $htmlForm->setId(
'mw-import-upload-form' );
362 $htmlForm->setWrapperLegendMsg(
'import-upload' );
363 $htmlForm->setSubmitTextMsg(
'uploadbtn' );
364 $htmlForm->prepareForm()->displayForm(
false );
366 } elseif ( !$this->importSources ) {
367 $out->addWikiMsg(
'importnosources' );
370 if ( $this->permManager->userHasRight( $user,
'import' ) && $this->importSources ) {
373 $needSubprojectField =
false;
374 foreach ( $this->importSources as $key => $value ) {
375 if ( is_int( $key ) ) {
377 } elseif ( $value !== $key ) {
378 $needSubprojectField =
true;
381 $projects[ $key ] = $key;
384 $interwikiFormDescriptor += [
388 'default' => $this->
msg(
'import-interwiki-text' )->parseAsBlock()
392 'name' =>
'interwiki',
393 'label-message' =>
'import-interwiki-sourcewiki',
394 'options' => $projects
398 if ( $needSubprojectField ) {
400 foreach ( $this->importSources as $key => $value ) {
401 if ( is_array( $value ) ) {
402 foreach ( $value as $subproject ) {
403 $subprojects[ $subproject ] = $key .
'::' . $subproject;
408 $interwikiFormDescriptor += [
411 'name' =>
'subproject',
412 'options' => $subprojects
417 $interwikiFormDescriptor += [
420 'name' =>
'frompage',
421 'label-message' =>
'import-interwiki-sourcepage'
423 'interwikiHistory' => [
425 'name' =>
'interwikiHistory',
426 'label-message' =>
'import-interwiki-history'
428 'interwikiTemplates' => [
430 'name' =>
'interwikiTemplates',
431 'label-message' =>
'import-interwiki-templates'
433 'assignKnownUsers' => [
435 'name' =>
'assignKnownUsers',
436 'label-message' =>
'import-assign-known-users'
441 $interwikiFormDescriptor += [
442 'pagelink-depth' => [
444 'name' =>
'pagelink-depth',
445 'label-message' =>
'export-pagelinks',
451 $interwikiFormDescriptor += [
454 'name' =>
'log-comment',
455 'label-message' =>
'import-comment'
460 'default' =>
'interwiki',
464 $mappingSelection = $this->getMappingFormPart(
'interwiki' );
466 $interwikiFormDescriptor += $mappingSelection;
469 $htmlForm->setAction( $action );
470 $htmlForm->setId(
'mw-import-interwiki-form' );
471 $htmlForm->setWrapperLegendMsg(
'importinterwiki' );
472 $htmlForm->setSubmitTextMsg(
'import-interwiki-submit' );
473 $htmlForm->prepareForm()->displayForm(
false );