37 private $importSources;
43 private $wikiImporterFactory;
53 parent::__construct(
'Import',
'import' );
55 $this->permManager = $permManager;
56 $this->wikiImporterFactory = $wikiImporterFactory;
75 $this->importSources = $this->
getConfig()->get( MainConfigNames::ImportSources );
77 if ( !is_array( $this->importSources ) ) {
78 throw new UnexpectedValueException(
'$wgImportSources must be an array' );
80 $this->
getHookRunner()->onImportSources( $this->importSources );
83 if ( !$this->permManager->userHasAnyRight( $user,
'import',
'importupload' ) ) {
87 # @todo Allow PermissionManager::getPermissionErrors() to take an array
89 $this->permManager->getPermissionErrors(
90 'import', $user, $this->getPageTitle(),
91 PermissionManager::RIGOR_FULL,
92 [
'ns-specialprotected',
'badaccess-group0',
'badaccess-groups' ]
94 $this->permManager->getPermissionErrors(
95 'importupload', $user, $this->getPageTitle(),
96 PermissionManager::RIGOR_FULL,
97 [
'ns-specialprotected',
'badaccess-group0',
'badaccess-groups' ]
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' );
127 $pageLinkDepth = $this->
getConfig()->get( MainConfigNames::ExportMaxLinkDepth ) == 0
129 : $request->getIntOrNull(
'pagelink-depth' );
132 $mapping = $request->getVal(
'mapping' );
133 $namespace = $this->
getConfig()->get( MainConfigNames::ImportTargetNamespace );
134 if ( $mapping ===
'namespace' ) {
135 $namespace = $request->getIntOrNull(
'namespace' );
136 } elseif ( $mapping ===
'subpage' ) {
137 $rootpage = $request->getText(
'rootpage' );
142 $fullInterwikiPrefix =
null;
143 if ( !$user->matchEditToken( $request->getVal(
'wpEditToken' ) ) ) {
145 } elseif ( $sourceName ===
'upload' ) {
147 $fullInterwikiPrefix = $request->getVal(
'usernamePrefix' );
148 if ( $this->permManager->userHasRight( $user,
'importupload' ) ) {
153 } elseif ( $sourceName ===
'interwiki' ) {
154 if ( !$this->permManager->userHasRight( $user,
'import' ) ) {
157 $interwiki = $fullInterwikiPrefix = $request->getVal(
'interwiki' );
159 $hasSubprojects = array_key_exists( $interwiki, $this->importSources );
160 if ( !$hasSubprojects && !in_array( $interwiki, $this->importSources ) ) {
164 if ( $hasSubprojects ) {
165 $subproject = $request->getVal(
'subproject' );
167 if ( strpos( $subproject, $interwiki .
'::' ) === 0 ) {
168 $subproject = substr( $subproject, strlen( $interwiki .
'::' ) );
170 $fullInterwikiPrefix .=
':' . $subproject;
172 if ( $hasSubprojects &&
173 !in_array( $subproject, $this->importSources[$interwiki] )
177 $history = $request->getCheck(
'interwikiHistory' );
178 $frompage = $request->getText(
'frompage' );
179 $includeTemplates = $request->getCheck(
'interwikiTemplates' );
182 $fullInterwikiPrefix,
193 if ( (
string)$fullInterwikiPrefix ===
'' ) {
194 $source->fatal(
'importnoprefix' );
199 $out->wrapWikiTextAsInterface(
'error',
200 $this->
msg(
'importfailed',
$source->getWikiText(
false,
false, $this->getLanguage() ) )
204 $importer = $this->wikiImporterFactory->getWikiImporter(
$source->value );
205 if ( $namespace !==
null ) {
206 $importer->setTargetNamespace( $namespace );
207 } elseif ( $rootpage !==
null ) {
208 $statusRootPage = $importer->setTargetRootPage( $rootpage );
209 if ( !$statusRootPage->isGood() ) {
211 "<div class=\"error\">\n$1\n</div>",
213 'import-options-wrong',
214 $statusRootPage->getWikiText(
false,
false, $this->getLanguage() ),
215 count( $statusRootPage->getErrorsArray() )
223 $importer->setUsernamePrefix( $fullInterwikiPrefix, $assignKnownUsers );
225 $out->addWikiMsg(
"importstart" );
231 $fullInterwikiPrefix,
239 $importer->doImport();
240 }
catch ( Exception $e ) {
243 $result = $reporter->close();
246 # No source or XML parse error
248 "<div class=\"error\">\n$1\n</div>",
249 [
'importfailed', $exception->getMessage() ]
251 } elseif ( !$result->isGood() ) {
254 "<div class=\"error\">\n$1\n</div>",
255 [
'importfailed', $result->getWikiText(
false,
false, $this->getLanguage() ) ]
259 $out->addWikiMsg(
'importsuccess' );
261 $out->addHTML(
'<hr />' );
265 private function getMappingFormPart( $sourceName ) {
266 $defaultNamespace = $this->
getConfig()->get( MainConfigNames::ImportTargetNamespace );
272 'id' =>
"mw-import-mapping-$sourceName",
273 'options-messages' => [
274 'import-mapping-default' =>
'default',
275 'import-mapping-namespace' =>
'namespace',
276 'import-mapping-subpage' =>
'subpage'
278 'default' => $defaultNamespace !==
null ?
'namespace' :
'default'
281 'type' =>
'namespaceselect',
282 'name' =>
'namespace',
284 'id' =>
"mw-import-namespace-$sourceName",
285 'default' => $defaultNamespace ?:
'',
287 'disable-if' => [
'!==',
'mapping',
'namespace' ],
291 'name' =>
'rootpage',
294 'id' =>
"mw-interwiki-rootpage-$sourceName",
295 'disable-if' => [
'!==',
'mapping',
'subpage' ],
300 private function showForm() {
301 $action = $this->
getPageTitle()->getLocalURL( [
'action' =>
'submit' ] );
304 $this->
addHelpLink(
'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import',
true );
306 $interwikiFormDescriptor = [];
307 $uploadFormDescriptor = [];
309 if ( $this->permManager->userHasRight( $user,
'importupload' ) ) {
310 $mappingSelection = $this->getMappingFormPart(
'upload' );
311 $uploadFormDescriptor += [
315 'default' => $this->
msg(
'importtext' )->parseAsBlock()
319 'name' =>
'xmlimport',
320 'accept' => [
'application/xml',
'text/xml' ],
321 'label-message' =>
'import-upload-filename',
324 'usernamePrefix' => [
326 'name' =>
'usernamePrefix',
327 'label-message' =>
'import-upload-username-prefix',
330 'assignKnownUsers' => [
332 'name' =>
'assignKnownUsers',
333 'label-message' =>
'import-assign-known-users'
337 'name' =>
'log-comment',
338 'label-message' =>
'import-comment'
343 'default' =>
'upload',
348 $uploadFormDescriptor += $mappingSelection;
351 $htmlForm->setAction( $action );
352 $htmlForm->setId(
'mw-import-upload-form' );
353 $htmlForm->setWrapperLegendMsg(
'import-upload' );
354 $htmlForm->setSubmitTextMsg(
'uploadbtn' );
355 $htmlForm->prepareForm()->displayForm(
false );
357 } elseif ( empty( $this->importSources ) ) {
358 $out->addWikiMsg(
'importnosources' );
361 if ( $this->permManager->userHasRight( $user,
'import' ) && !empty( $this->importSources ) ) {
364 $needSubprojectField =
false;
365 foreach ( $this->importSources as $key => $value ) {
366 if ( is_int( $key ) ) {
368 } elseif ( $value !== $key ) {
369 $needSubprojectField =
true;
372 $projects[ $key ] = $key;
375 $interwikiFormDescriptor += [
379 'default' => $this->
msg(
'import-interwiki-text' )->parseAsBlock()
383 'name' =>
'interwiki',
384 'label-message' =>
'import-interwiki-sourcewiki',
385 'options' => $projects
389 if ( $needSubprojectField ) {
391 foreach ( $this->importSources as $key => $value ) {
392 if ( is_array( $value ) ) {
393 foreach ( $value as $subproject ) {
394 $subprojects[ $subproject ] = $key .
'::' . $subproject;
399 $interwikiFormDescriptor += [
402 'name' =>
'subproject',
403 'options' => $subprojects
408 $interwikiFormDescriptor += [
411 'name' =>
'frompage',
412 'label-message' =>
'import-interwiki-sourcepage'
414 'interwikiHistory' => [
416 'name' =>
'interwikiHistory',
417 'label-message' =>
'import-interwiki-history'
419 'interwikiTemplates' => [
421 'name' =>
'interwikiTemplates',
422 'label-message' =>
'import-interwiki-templates'
424 'assignKnownUsers' => [
426 'name' =>
'assignKnownUsers',
427 'label-message' =>
'import-assign-known-users'
431 if ( $this->
getConfig()->
get( MainConfigNames::ExportMaxLinkDepth ) > 0 ) {
432 $interwikiFormDescriptor += [
433 'pagelink-depth' => [
435 'name' =>
'pagelink-depth',
436 'label-message' =>
'export-pagelinks',
442 $interwikiFormDescriptor += [
445 'name' =>
'log-comment',
446 'label-message' =>
'import-comment'
451 'default' =>
'interwiki',
455 $mappingSelection = $this->getMappingFormPart(
'interwiki' );
457 $interwikiFormDescriptor += $mappingSelection;
460 $htmlForm->setAction( $action );
461 $htmlForm->setId(
'mw-import-interwiki-form' );
462 $htmlForm->setWrapperLegendMsg(
'importinterwiki' );
463 $htmlForm->setSubmitTextMsg(
'import-interwiki-submit' );
464 $htmlForm->prepareForm()->displayForm(
false );
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.