3 * Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
4 * https://www.mediawiki.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Specials
;
28 use ImportStreamSource
;
29 use MediaWiki\Html\Html
;
30 use MediaWiki\HTMLForm\HTMLForm
;
31 use MediaWiki\MainConfigNames
;
32 use MediaWiki\Permissions\PermissionStatus
;
33 use MediaWiki\SpecialPage\SpecialPage
;
34 use MediaWiki\Status\Status
;
36 use UnexpectedValueException
;
37 use WikiImporterFactory
;
40 * MediaWiki page data importer
42 * @ingroup SpecialPage
44 class SpecialImport
extends SpecialPage
{
46 private $importSources;
48 private WikiImporterFactory
$wikiImporterFactory;
51 * @param WikiImporterFactory $wikiImporterFactory
53 public function __construct(
54 WikiImporterFactory
$wikiImporterFactory
56 parent
::__construct( 'Import', 'import' );
58 $this->wikiImporterFactory
= $wikiImporterFactory;
61 public function doesWrites() {
67 * @param string|null $par
69 public function execute( $par ) {
70 $this->useTransactionalTimeLimit();
73 $this->outputHeader();
75 $this->importSources
= $this->getConfig()->get( MainConfigNames
::ImportSources
);
76 // Avoid phan error by checking the type
77 if ( !is_array( $this->importSources
) ) {
78 throw new UnexpectedValueException( '$wgImportSources must be an array' );
80 $this->getHookRunner()->onImportSources( $this->importSources
);
82 $authority = $this->getAuthority();
83 $statusImport = PermissionStatus
::newEmpty();
84 $authority->isDefinitelyAllowed( 'import', $statusImport );
85 $statusImportUpload = PermissionStatus
::newEmpty();
86 $authority->isDefinitelyAllowed( 'importupload', $statusImportUpload );
87 // Only show an error here if the user can't import using either method.
88 // If they can use at least one of the methods, allow access, and checks elsewhere
89 // will ensure that we only show the form(s) they can use.
90 if ( !$statusImport->isGood() && !$statusImportUpload->isGood() ) {
91 // Show separate messages for each check. There isn't a good way to merge them into a single
92 // message if the checks failed for different reasons.
94 $this->getOutput()->prepareErrorPage();
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' );
108 $this->checkReadOnly();
110 $request = $this->getRequest();
111 if ( $request->wasPosted() && $request->getRawVal( 'action' ) == 'submit' ) {
118 * Do the actual import
120 private function doImport() {
122 $request = $this->getRequest();
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' );
140 $user = $this->getUser();
141 $authority = $this->getAuthority();
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 ) ) {
151 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
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' );
160 // does this interwiki have subprojects?
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' );
168 // Trim "project::" prefix added for JS
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' );
182 $source = ImportStreamSource
::newFromInterwiki(
183 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
184 $fullInterwikiPrefix,
192 $source = Status
::newFatal( "importunknownsource" );
195 if ( (string)$fullInterwikiPrefix === '' ) {
196 $source->fatal( 'importnoprefix' );
199 $out = $this->getOutput();
200 if ( !$source->isGood() ) {
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() )
228 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
229 $importer->setUsernamePrefix( $fullInterwikiPrefix, $assignKnownUsers );
231 $out->addWikiMsg( "importstart" );
233 $reporter = new ImportReporter(
236 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
237 $fullInterwikiPrefix,
245 $importer->doImport();
246 } catch ( Exception
$e ) {
249 $result = $reporter->close();
252 # No source or XML parse error
254 Html
::errorBox( '$1' ),
255 [ 'importfailed', wfEscapeWikiText( $exception->getMessage() ), 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 ) {
276 $defaultNamespace = $this->getConfig()->get( MainConfigNames
::ImportTargetNamespace
);
281 // IDs: mw-import-mapping-interwiki, mw-import-mapping-upload
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',
293 // IDs: mw-import-namespace-interwiki, mw-import-namespace-upload
294 'id' => "mw-import-namespace-$sourceName",
295 'default' => $defaultNamespace ?
: '',
297 'disable-if' => [ '!==', 'mapping', 'namespace' ],
301 'name' => 'rootpage',
302 // Should be "mw-import-...", but we keep the inaccurate ID for compat
303 // IDs: mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
304 'id' => "mw-interwiki-rootpage-$sourceName",
305 'disable-if' => [ '!==', 'mapping', 'subpage' ],
310 private function showForm() {
311 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
312 $authority = $this->getAuthority();
313 $out = $this->getOutput();
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'
441 if ( $this->getConfig()->get( MainConfigNames
::ExportMaxLinkDepth
) > 0 ) {
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 );
478 protected function getGroupName() {
483 /** @deprecated class alias since 1.41 */
484 class_alias( SpecialImport
::class, 'SpecialImport' );