46 private $importSources;
56 parent::__construct(
'Import',
'import' );
58 $this->wikiImporterFactory = $wikiImporterFactory;
77 if ( !is_array( $this->importSources ) ) {
78 throw new UnexpectedValueException(
'$wgImportSources must be an array' );
80 $this->
getHookRunner()->onImportSources( $this->importSources );
83 $statusImport = PermissionStatus::newEmpty();
84 $authority->isDefinitelyAllowed(
'import', $statusImport );
85 $statusImportUpload = PermissionStatus::newEmpty();
86 $authority->isDefinitelyAllowed(
'importupload', $statusImportUpload );
90 if ( !$statusImport->isGood() && !$statusImportUpload->isGood() ) {
95 $this->
getOutput()->setPageTitleMsg( $this->
msg(
'permissionserrors' ) );
96 $this->
getOutput()->addWikiTextAsInterface( Html::errorBox(
97 $this->
getOutput()->formatPermissionStatus( $statusImport,
'import' )
99 $this->
getOutput()->addWikiTextAsInterface( Html::errorBox(
100 $this->
getOutput()->formatPermissionStatus( $statusImportUpload,
'importupload' )
105 $this->
getOutput()->addModules(
'mediawiki.misc-authed-ooui' );
106 $this->
getOutput()->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 ) ) {
155 } elseif ( $sourceName ===
'interwiki' ) {
156 if ( !$authority->authorizeAction(
'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 ( Exception $e ) {
249 $result = $reporter->close();
252 # No source or XML parse error
254 Html::errorBox(
'$1' ),
257 } elseif ( !$result->isGood() ) {
260 Html::errorBox(
'$1' ),
263 $result->getWikiText(
false,
false, $this->getLanguage() ),
264 count( $result->getMessages() )
269 $out->addWikiMsg(
'importsuccess' );
271 $out->addHTML(
'<hr />' );
275 private function getMappingFormPart( $sourceName ) {
282 'id' =>
"mw-import-mapping-$sourceName",
283 'options-messages' => [
284 'import-mapping-default' =>
'default',
285 'import-mapping-namespace' =>
'namespace',
286 'import-mapping-subpage' =>
'subpage'
288 'default' => $defaultNamespace !==
null ?
'namespace' :
'default'
291 'type' =>
'namespaceselect',
292 'name' =>
'namespace',
294 'id' =>
"mw-import-namespace-$sourceName",
295 'default' => $defaultNamespace ?:
'',
297 'disable-if' => [
'!==',
'mapping',
'namespace' ],
301 'name' =>
'rootpage',
304 'id' =>
"mw-interwiki-rootpage-$sourceName",
305 'disable-if' => [
'!==',
'mapping',
'subpage' ],
310 private function showForm() {
311 $action = $this->
getPageTitle()->getLocalURL( [
'action' =>
'submit' ] );
314 $this->
addHelpLink(
'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Import',
true );
316 $interwikiFormDescriptor = [];
317 $uploadFormDescriptor = [];
319 if ( $authority->isDefinitelyAllowed(
'importupload' ) ) {
320 $mappingSelection = $this->getMappingFormPart(
'upload' );
321 $uploadFormDescriptor += [
325 'default' => $this->
msg(
'importtext' )->parseAsBlock()
329 'name' =>
'xmlimport',
330 'accept' => [
'application/xml',
'text/xml' ],
331 'label-message' =>
'import-upload-filename',
334 'usernamePrefix' => [
336 'name' =>
'usernamePrefix',
337 'label-message' =>
'import-upload-username-prefix',
340 'assignKnownUsers' => [
342 'name' =>
'assignKnownUsers',
343 'label-message' =>
'import-assign-known-users'
347 'name' =>
'log-comment',
348 'label-message' =>
'import-comment'
353 'default' =>
'upload',
358 $uploadFormDescriptor += $mappingSelection;
360 $htmlForm = HTMLForm::factory(
'ooui', $uploadFormDescriptor, $this->
getContext() );
361 $htmlForm->setAction( $action );
362 $htmlForm->setId(
'mw-import-upload-form' );
363 $htmlForm->setWrapperLegendMsg(
'import-upload' );
364 $htmlForm->setSubmitTextMsg(
'uploadbtn' );
365 $htmlForm->prepareForm()->displayForm(
false );
367 } elseif ( !$this->importSources ) {
368 $out->addWikiMsg(
'importnosources' );
371 if ( $authority->isDefinitelyAllowed(
'import' ) && $this->importSources ) {
374 $needSubprojectField =
false;
375 foreach ( $this->importSources as $key => $value ) {
376 if ( is_int( $key ) ) {
378 } elseif ( $value !== $key ) {
379 $needSubprojectField =
true;
382 $projects[ $key ] = $key;
385 $interwikiFormDescriptor += [
389 'default' => $this->
msg(
'import-interwiki-text' )->parseAsBlock()
393 'name' =>
'interwiki',
394 'label-message' =>
'import-interwiki-sourcewiki',
395 'options' => $projects
399 if ( $needSubprojectField ) {
401 foreach ( $this->importSources as $key => $value ) {
402 if ( is_array( $value ) ) {
403 foreach ( $value as $subproject ) {
404 $subprojects[ $subproject ] = $key .
'::' . $subproject;
409 $interwikiFormDescriptor += [
412 'name' =>
'subproject',
413 'options' => $subprojects
418 $interwikiFormDescriptor += [
421 'name' =>
'frompage',
422 'label-message' =>
'import-interwiki-sourcepage'
424 'interwikiHistory' => [
426 'name' =>
'interwikiHistory',
427 'label-message' =>
'import-interwiki-history'
429 'interwikiTemplates' => [
431 'name' =>
'interwikiTemplates',
432 'label-message' =>
'import-interwiki-templates'
434 'assignKnownUsers' => [
436 'name' =>
'assignKnownUsers',
437 'label-message' =>
'import-assign-known-users'
442 $interwikiFormDescriptor += [
443 'pagelink-depth' => [
445 'name' =>
'pagelink-depth',
446 'label-message' =>
'export-pagelinks',
452 $interwikiFormDescriptor += [
455 'name' =>
'log-comment',
456 'label-message' =>
'import-comment'
461 'default' =>
'interwiki',
465 $mappingSelection = $this->getMappingFormPart(
'interwiki' );
467 $interwikiFormDescriptor += $mappingSelection;
469 $htmlForm = HTMLForm::factory(
'ooui', $interwikiFormDescriptor, $this->
getContext() );
470 $htmlForm->setAction( $action );
471 $htmlForm->setId(
'mw-import-interwiki-form' );
472 $htmlForm->setWrapperLegendMsg(
'importinterwiki' );
473 $htmlForm->setSubmitTextMsg(
'import-interwiki-submit' );
474 $htmlForm->prepareForm()->displayForm(
false );