3 * Implements Special:Import
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup SpecialPage
27 use MediaWiki\MediaWikiServices
;
28 use MediaWiki\Permissions\PermissionManager
;
31 * MediaWiki page data importer
33 * @ingroup SpecialPage
35 class SpecialImport
extends SpecialPage
{
36 private $sourceName = false;
37 private $interwiki = false;
39 private $fullInterwikiPrefix;
40 private $mapping = 'default';
42 private $rootpage = '';
43 private $frompage = '';
44 private $logcomment = false;
45 private $history = true;
46 private $includeTemplates = false;
47 private $pageLinkDepth;
48 private $importSources;
49 private $assignKnownUsers;
50 private $usernamePrefix;
52 public function __construct() {
53 parent
::__construct( 'Import', 'import' );
56 public function doesWrites() {
62 * @param string|null $par
63 * @throws PermissionsError
64 * @throws ReadOnlyError
66 function execute( $par ) {
67 $this->useTransactionalTimeLimit();
70 $this->outputHeader();
72 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
74 $this->getOutput()->addModules( 'mediawiki.special.import' );
76 $this->importSources
= $this->getConfig()->get( 'ImportSources' );
77 Hooks
::run( 'ImportSources', [ &$this->importSources
] );
79 $user = $this->getUser();
80 if ( !MediaWikiServices
::getInstance()
81 ->getPermissionManager()
82 ->userHasAnyRight( $user, 'import', 'importupload' )
84 throw new PermissionsError( 'import' );
87 # @todo Allow Title::getUserPermissionsErrors() to take an array
88 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very weird expectation of what
89 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
90 $errors = wfMergeErrorArrays(
91 $this->getPageTitle()->getUserPermissionsErrors(
92 'import', $user, PermissionManager
::RIGOR_FULL
,
93 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
95 $this->getPageTitle()->getUserPermissionsErrors(
96 'importupload', $user, PermissionManager
::RIGOR_FULL
,
97 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
102 throw new PermissionsError( 'import', $errors );
105 $this->checkReadOnly();
107 $request = $this->getRequest();
108 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
115 * Do the actual import
117 private function doImport() {
119 $request = $this->getRequest();
120 $this->sourceName
= $request->getVal( "source" );
121 $this->assignKnownUsers
= $request->getCheck( 'assignKnownUsers' );
123 $this->logcomment
= $request->getText( 'log-comment' );
124 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
126 : $request->getIntOrNull( 'pagelink-depth' );
128 $this->mapping
= $request->getVal( 'mapping' );
129 if ( $this->mapping
=== 'namespace' ) {
130 $this->namespace = $request->getIntOrNull( 'namespace' );
131 } elseif ( $this->mapping
=== 'subpage' ) {
132 $this->rootpage
= $request->getText( 'rootpage' );
134 $this->mapping
= 'default';
137 $user = $this->getUser();
138 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
139 $source = Status
::newFatal( 'import-token-mismatch' );
140 } elseif ( $this->sourceName
=== 'upload' ) {
142 $this->usernamePrefix
= $this->fullInterwikiPrefix
= $request->getVal( 'usernamePrefix' );
143 if ( $user->isAllowed( 'importupload' ) ) {
144 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
146 throw new PermissionsError( 'importupload' );
148 } elseif ( $this->sourceName
=== 'interwiki' ) {
149 if ( !$user->isAllowed( 'import' ) ) {
150 throw new PermissionsError( 'import' );
152 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
153 // does this interwiki have subprojects?
154 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
155 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
156 $source = Status
::newFatal( "import-invalid-interwiki" );
158 if ( $hasSubprojects ) {
159 $this->subproject
= $request->getVal( 'subproject' );
160 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
162 if ( $hasSubprojects &&
163 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
165 $source = Status
::newFatal( "import-invalid-interwiki" );
167 $this->history
= $request->getCheck( 'interwikiHistory' );
168 $this->frompage
= $request->getText( "frompage" );
169 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
170 $source = ImportStreamSource
::newFromInterwiki(
171 $this->fullInterwikiPrefix
,
174 $this->includeTemplates
,
175 $this->pageLinkDepth
);
179 $source = Status
::newFatal( "importunknownsource" );
182 if ( (string)$this->fullInterwikiPrefix
=== '' ) {
183 $source->fatal( 'importnoprefix' );
186 $out = $this->getOutput();
187 if ( !$source->isGood() ) {
188 $out->wrapWikiTextAsInterface( 'error',
189 $this->msg( 'importfailed', $source->getWikiText() )->plain()
192 $importer = new WikiImporter( $source->value
, $this->getConfig() );
193 if ( !is_null( $this->namespace ) ) {
194 $importer->setTargetNamespace( $this->namespace );
195 } elseif ( !is_null( $this->rootpage
) ) {
196 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
197 if ( !$statusRootPage->isGood() ) {
199 "<div class=\"error\">\n$1\n</div>",
201 'import-options-wrong',
202 $statusRootPage->getWikiText(),
203 count( $statusRootPage->getErrorsArray() )
210 $importer->setUsernamePrefix( $this->fullInterwikiPrefix
, $this->assignKnownUsers
);
212 $out->addWikiMsg( "importstart" );
214 $reporter = new ImportReporter(
217 $this->fullInterwikiPrefix
,
220 $reporter->setContext( $this->getContext() );
225 $importer->doImport();
226 } catch ( Exception
$e ) {
229 $result = $reporter->close();
232 # No source or XML parse error
234 "<div class=\"error\">\n$1\n</div>",
235 [ 'importfailed', $exception->getMessage() ]
237 } elseif ( !$result->isGood() ) {
240 "<div class=\"error\">\n$1\n</div>",
241 [ 'importfailed', $result->getWikiText() ]
245 $out->addWikiMsg( 'importsuccess' );
247 $out->addHTML( '<hr />' );
251 private function getMappingFormPart( $sourceName ) {
252 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
253 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
257 <td class='mw-input'>" .
259 $this->msg( 'import-mapping-default' )->text(),
262 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
263 "mw-import-mapping-$sourceName-default",
264 ( $isSameSourceAsBefore ?
265 ( $this->mapping
=== 'default' ) :
266 is_null( $defaultNamespace ) )
273 <td class='mw-input'>" .
275 $this->msg( 'import-mapping-namespace' )->text(),
278 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
279 "mw-import-mapping-$sourceName-namespace",
280 ( $isSameSourceAsBefore ?
281 ( $this->mapping
=== 'namespace' ) :
282 !is_null( $defaultNamespace ) )
284 Html
::namespaceSelector(
286 'selected' => ( $isSameSourceAsBefore ?
288 ( $defaultNamespace ||
'' ) ),
289 'in-user-lang' => true,
291 'name' => "namespace",
292 // mw-import-namespace-interwiki, mw-import-namespace-upload
293 'id' => "mw-import-namespace-$sourceName",
294 'class' => 'namespaceselector',
302 <td class='mw-input'>" .
304 $this->msg( 'import-mapping-subpage' )->text(),
307 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
308 "mw-import-mapping-$sourceName-subpage",
309 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
311 Xml
::input( 'rootpage', 50,
312 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
314 // Should be "mw-import-rootpage-...", but we keep this inaccurate
315 // ID for legacy reasons
316 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
317 'id' => "mw-interwiki-rootpage-$sourceName",
325 private function showForm() {
326 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
327 $user = $this->getUser();
328 $out = $this->getOutput();
329 $this->addHelpLink( 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
331 if ( $user->isAllowed( 'importupload' ) ) {
332 $mappingSelection = $this->getMappingFormPart( 'upload' );
334 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
338 'enctype' => 'multipart/form-data',
341 'id' => 'mw-import-upload-form'
344 $this->msg( 'importtext' )->parseAsBlock() .
345 Html
::hidden( 'action', 'submit' ) .
346 Html
::hidden( 'source', 'upload' ) .
347 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
349 <td class='mw-label'>" .
350 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
352 <td class='mw-input'>" .
353 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
357 <td class='mw-label'>" .
358 Xml
::label( $this->msg( 'import-upload-username-prefix' )->text(),
359 'mw-import-usernamePrefix' ) .
361 <td class='mw-input'>" .
362 Xml
::input( 'usernamePrefix', 50,
363 $this->usernamePrefix
,
364 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
369 <td class='mw-input'>" .
371 $this->msg( 'import-assign-known-users' )->text(),
374 $this->assignKnownUsers
379 <td class='mw-label'>" .
380 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
382 <td class='mw-input'>" .
383 Xml
::input( 'log-comment', 50,
384 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
385 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
391 <td class='mw-submit'>" .
392 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
395 Xml
::closeElement( 'table' ) .
396 Html
::hidden( 'editToken', $user->getEditToken() ) .
397 Xml
::closeElement( 'form' ) .
398 Xml
::closeElement( 'fieldset' )
400 } elseif ( empty( $this->importSources
) ) {
401 $out->addWikiMsg( 'importnosources' );
404 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
405 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
407 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
409 <td class='mw-label'>" .
410 $this->msg( 'export-pagelinks' )->parse() .
412 <td class='mw-input'>" .
413 Xml
::input( 'pagelink-depth', 3, 0 ) .
417 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
420 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
426 'id' => 'mw-import-interwiki-form'
429 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
430 Html
::hidden( 'action', 'submit' ) .
431 Html
::hidden( 'source', 'interwiki' ) .
432 Html
::hidden( 'editToken', $user->getEditToken() ) .
433 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
435 <td class='mw-label'>" .
436 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
438 <td class='mw-input'>" .
441 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
445 $needSubprojectField = false;
446 foreach ( $this->importSources
as $key => $value ) {
447 if ( is_int( $key ) ) {
449 } elseif ( $value !== $key ) {
450 $needSubprojectField = true;
456 if ( is_array( $value ) ) {
457 $attribs['data-subprojects'] = implode( ' ', $value );
459 if ( $this->interwiki
=== $key ) {
460 $attribs['selected'] = 'selected';
462 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
466 Xml
::closeElement( 'select' )
469 if ( $needSubprojectField ) {
473 [ 'name' => 'subproject', 'id' => 'subproject' ]
477 $subprojectsToAdd = [];
478 foreach ( $this->importSources
as $key => $value ) {
479 if ( is_array( $value ) ) {
480 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
483 $subprojectsToAdd = array_unique( $subprojectsToAdd );
484 sort( $subprojectsToAdd );
485 foreach ( $subprojectsToAdd as $subproject ) {
486 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
490 Xml
::closeElement( 'select' )
498 <td class='mw-label'>" .
499 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
501 <td class='mw-input'>" .
502 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
508 <td class='mw-input'>" .
510 $this->msg( 'import-interwiki-history' )->text(),
520 <td class='mw-input'>" .
522 $this->msg( 'import-interwiki-templates' )->text(),
523 'interwikiTemplates',
524 'interwikiTemplates',
525 $this->includeTemplates
531 <td class='mw-input'>" .
533 $this->msg( 'import-assign-known-users' )->text(),
535 'interwikiAssignKnownUsers',
536 $this->assignKnownUsers
542 <td class='mw-label'>" .
543 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
545 <td class='mw-input'>" .
546 Xml
::input( 'log-comment', 50,
547 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
548 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
555 <td class='mw-submit'>" .
557 $this->msg( 'import-interwiki-submit' )->text(),
558 Linker
::tooltipAndAccesskeyAttribs( 'import' )
562 Xml
::closeElement( 'table' ) .
563 Xml
::closeElement( 'form' ) .
564 Xml
::closeElement( 'fieldset' )
569 protected function getGroupName() {