74 $this->
getOutput()->addModules(
'mediawiki.special.import' );
75 $this->
getOutput()->addModuleStyles(
'mediawiki.special.import.styles.ooui' );
77 $this->importSources = $this->
getConfig()->get(
'ImportSources' );
79 if ( !is_array( $this->importSources ) ) {
80 throw new UnexpectedValueException(
'$wgImportSources must be an array' );
82 $this->
getHookRunner()->onImportSources( $this->importSources );
85 if ( !$this->permManager->userHasAnyRight( $user,
'import',
'importupload' ) ) {
89 # @todo Allow PermissionManager::getPermissionErrors() to take an array
91 $this->permManager->getPermissionErrors(
92 'import', $user, $this->getPageTitle(),
93 PermissionManager::RIGOR_FULL,
94 [
'ns-specialprotected',
'badaccess-group0',
'badaccess-groups' ]
96 $this->permManager->getPermissionErrors(
97 'importupload', $user, $this->getPageTitle(),
98 PermissionManager::RIGOR_FULL,
99 [
'ns-specialprotected',
'badaccess-group0',
'badaccess-groups' ]
110 if ( $request->wasPosted() && $request->getRawVal(
'action' ) ==
'submit' ) {
122 $sourceName = $request->getVal(
'source' );
123 $assignKnownUsers = $request->getCheck(
'assignKnownUsers' );
125 $logcomment = $request->getText(
'log-comment' );
126 $pageLinkDepth = $this->
getConfig()->get(
'ExportMaxLinkDepth' ) == 0
128 : $request->getIntOrNull(
'pagelink-depth' );
131 $mapping = $request->getVal(
'mapping' );
132 $namespace = $this->
getConfig()->get(
'ImportTargetNamespace' );
133 if ( $mapping ===
'namespace' ) {
134 $namespace = $request->getIntOrNull(
'namespace' );
135 } elseif ( $mapping ===
'subpage' ) {
136 $rootpage = $request->getText(
'rootpage' );
141 $fullInterwikiPrefix =
null;
142 if ( !$user->matchEditToken( $request->getVal(
'wpEditToken' ) ) ) {
143 $source = Status::newFatal(
'import-token-mismatch' );
144 } elseif ( $sourceName ===
'upload' ) {
146 $fullInterwikiPrefix = $request->getVal(
'usernamePrefix' );
147 if ( $this->permManager->userHasRight( $user,
'importupload' ) ) {
152 } elseif ( $sourceName ===
'interwiki' ) {
153 if ( !$this->permManager->userHasRight( $user,
'import' ) ) {
156 $interwiki = $fullInterwikiPrefix = $request->getVal(
'interwiki' );
158 $hasSubprojects = array_key_exists( $interwiki, $this->importSources );
159 if ( !$hasSubprojects && !in_array( $interwiki, $this->importSources ) ) {
160 $source = Status::newFatal(
"import-invalid-interwiki" );
163 if ( $hasSubprojects ) {
164 $subproject = $request->getVal(
'subproject' );
166 if ( strpos( $subproject, $interwiki .
'::' ) === 0 ) {
167 $subproject = substr( $subproject, strlen( $interwiki .
'::' ) );
169 $fullInterwikiPrefix .=
':' . $subproject;
171 if ( $hasSubprojects &&
172 !in_array( $subproject, $this->importSources[$interwiki] )
174 $source = Status::newFatal(
'import-invalid-interwiki' );
176 $history = $request->getCheck(
'interwikiHistory' );
177 $frompage = $request->getText(
'frompage' );
178 $includeTemplates = $request->getCheck(
'interwikiTemplates' );
180 $fullInterwikiPrefix,
188 $source = Status::newFatal(
"importunknownsource" );
191 if ( (
string)$fullInterwikiPrefix ===
'' ) {
192 $source->fatal(
'importnoprefix' );
197 $out->wrapWikiTextAsInterface(
'error',
198 $this->
msg(
'importfailed',
$source->getWikiText(
false,
false, $this->getLanguage() ) )
202 $importer = $this->wikiImporterFactory->getWikiImporter(
$source->value );
203 if ( $namespace !==
null ) {
204 $importer->setTargetNamespace( $namespace );
205 } elseif ( $rootpage !==
null ) {
206 $statusRootPage = $importer->setTargetRootPage( $rootpage );
207 if ( !$statusRootPage->isGood() ) {
209 "<div class=\"error\">\n$1\n</div>",
211 'import-options-wrong',
212 $statusRootPage->getWikiText(
false,
false, $this->getLanguage() ),
213 count( $statusRootPage->getErrorsArray() )
220 $importer->setUsernamePrefix( $fullInterwikiPrefix, $assignKnownUsers );
222 $out->addWikiMsg(
"importstart" );
227 $fullInterwikiPrefix,
235 $importer->doImport();
236 }
catch ( Exception $e ) {
239 $result = $reporter->close();
242 # No source or XML parse error
244 "<div class=\"error\">\n$1\n</div>",
245 [
'importfailed', $exception->getMessage() ]
247 } elseif ( !$result->isGood() ) {
250 "<div class=\"error\">\n$1\n</div>",
251 [
'importfailed', $result->getWikiText(
false,
false, $this->getLanguage() ) ]
255 $out->addWikiMsg(
'importsuccess' );
257 $out->addHTML(
'<hr />' );
296 $action = $this->
getPageTitle()->getLocalURL( [
'action' =>
'submit' ] );
299 $this->
addHelpLink(
'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import',
true );
301 $interwikiFormDescriptor = [];
302 $uploadFormDescriptor = [];
304 if ( $this->permManager->userHasRight( $user,
'importupload' ) ) {
306 $uploadFormDescriptor += [
310 'default' => $this->
msg(
'importtext' )->parseAsBlock()
314 'name' =>
'xmlimport',
315 'accept' => [
'application/xml',
'text/xml' ],
316 'label-message' =>
'import-upload-filename',
319 'usernamePrefix' => [
321 'name' =>
'usernamePrefix',
322 'label-message' =>
'import-upload-username-prefix',
325 'assignKnownUsers' => [
327 'name' =>
'assignKnownUsers',
328 'label-message' =>
'import-assign-known-users'
332 'name' =>
'log-comment',
333 'label-message' =>
'import-comment'
338 'default' =>
'upload',
343 $uploadFormDescriptor += $mappingSelection;
345 $htmlForm = HTMLForm::factory(
'ooui', $uploadFormDescriptor, $this->
getContext() );
346 $htmlForm->setAction( $action );
347 $htmlForm->setId(
'mw-import-upload-form' );
348 $htmlForm->setWrapperLegendMsg(
'import-upload' );
349 $htmlForm->setSubmitTextMsg(
'uploadbtn' );
350 $htmlForm->prepareForm()->displayForm(
false );
352 } elseif ( empty( $this->importSources ) ) {
353 $out->addWikiMsg(
'importnosources' );
356 if ( $this->permManager->userHasRight( $user,
'import' ) && !empty( $this->importSources ) ) {
359 $needSubprojectField =
false;
360 foreach ( $this->importSources as $key => $value ) {
361 if ( is_int( $key ) ) {
363 } elseif ( $value !== $key ) {
364 $needSubprojectField =
true;
367 $projects[ $key ] = $key;
370 $interwikiFormDescriptor += [
374 'default' => $this->
msg(
'import-interwiki-text' )->parseAsBlock()
378 'name' =>
'interwiki',
379 'label-message' =>
'import-interwiki-sourcewiki',
380 'options' => $projects
384 if ( $needSubprojectField ) {
386 foreach ( $this->importSources as $key => $value ) {
387 if ( is_array( $value ) ) {
388 foreach ( $value as $subproject ) {
389 $subprojects[ $subproject ] = $key .
'::' . $subproject;
394 $interwikiFormDescriptor += [
397 'name' =>
'subproject',
398 'options' => $subprojects
403 $interwikiFormDescriptor += [
406 'name' =>
'frompage',
407 'label-message' =>
'import-interwiki-sourcepage'
409 'interwikiHistory' => [
411 'name' =>
'interwikiHistory',
412 'label-message' =>
'import-interwiki-history'
414 'interwikiTemplates' => [
416 'name' =>
'interwikiTemplates',
417 'label-message' =>
'import-interwiki-templates'
419 'assignKnownUsers' => [
421 'name' =>
'assignKnownUsers',
422 'label-message' =>
'import-assign-known-users'
426 if ( $this->
getConfig()->
get(
'ExportMaxLinkDepth' ) > 0 ) {
427 $interwikiFormDescriptor += [
428 'pagelink-depth' => [
430 'name' =>
'pagelink-depth',
431 'label-message' =>
'export-pagelinks',
437 $interwikiFormDescriptor += [
440 'name' =>
'log-comment',
441 'label-message' =>
'import-comment'
446 'default' =>
'interwiki',
452 $interwikiFormDescriptor += $mappingSelection;
454 $htmlForm = HTMLForm::factory(
'ooui', $interwikiFormDescriptor, $this->
getContext() );
455 $htmlForm->setAction( $action );
456 $htmlForm->setId(
'mw-import-interwiki-form' );
457 $htmlForm->setWrapperLegendMsg(
'importinterwiki' );
458 $htmlForm->setSubmitTextMsg(
'import-interwiki-submit' );
459 $htmlForm->prepareForm()->displayForm(
false );