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\Permissions\PermissionManager
;
30 * MediaWiki page data importer
32 * @ingroup SpecialPage
34 class SpecialImport
extends SpecialPage
{
35 private $sourceName = false;
36 private $interwiki = false;
38 private $fullInterwikiPrefix;
39 private $mapping = 'default';
41 private $rootpage = '';
42 private $frompage = '';
43 private $logcomment = false;
44 private $history = true;
45 private $includeTemplates = false;
46 private $pageLinkDepth;
47 private $importSources;
48 private $assignKnownUsers;
49 private $usernamePrefix;
51 public function __construct() {
52 parent
::__construct( 'Import', 'import' );
55 public function doesWrites() {
61 * @param string|null $par
62 * @throws PermissionsError
63 * @throws ReadOnlyError
65 function execute( $par ) {
66 $this->useTransactionalTimeLimit();
69 $this->outputHeader();
71 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
73 $this->getOutput()->addModules( 'mediawiki.special.import' );
75 $this->importSources
= $this->getConfig()->get( 'ImportSources' );
76 Hooks
::run( 'ImportSources', [ &$this->importSources
] );
78 $user = $this->getUser();
79 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
80 throw new PermissionsError( 'import' );
83 # @todo Allow Title::getUserPermissionsErrors() to take an array
84 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very weird expectation of what
85 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
86 $errors = wfMergeErrorArrays(
87 $this->getPageTitle()->getUserPermissionsErrors(
88 'import', $user, PermissionManager
::RIGOR_FULL
,
89 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
91 $this->getPageTitle()->getUserPermissionsErrors(
92 'importupload', $user, PermissionManager
::RIGOR_FULL
,
93 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
98 throw new PermissionsError( 'import', $errors );
101 $this->checkReadOnly();
103 $request = $this->getRequest();
104 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
111 * Do the actual import
113 private function doImport() {
115 $request = $this->getRequest();
116 $this->sourceName
= $request->getVal( "source" );
117 $this->assignKnownUsers
= $request->getCheck( 'assignKnownUsers' );
119 $this->logcomment
= $request->getText( 'log-comment' );
120 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
122 : $request->getIntOrNull( 'pagelink-depth' );
124 $this->mapping
= $request->getVal( 'mapping' );
125 if ( $this->mapping
=== 'namespace' ) {
126 $this->namespace = $request->getIntOrNull( 'namespace' );
127 } elseif ( $this->mapping
=== 'subpage' ) {
128 $this->rootpage
= $request->getText( 'rootpage' );
130 $this->mapping
= 'default';
133 $user = $this->getUser();
134 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
135 $source = Status
::newFatal( 'import-token-mismatch' );
136 } elseif ( $this->sourceName
=== 'upload' ) {
138 $this->usernamePrefix
= $this->fullInterwikiPrefix
= $request->getVal( 'usernamePrefix' );
139 if ( $user->isAllowed( 'importupload' ) ) {
140 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
142 throw new PermissionsError( 'importupload' );
144 } elseif ( $this->sourceName
=== 'interwiki' ) {
145 if ( !$user->isAllowed( 'import' ) ) {
146 throw new PermissionsError( 'import' );
148 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
149 // does this interwiki have subprojects?
150 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
151 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
152 $source = Status
::newFatal( "import-invalid-interwiki" );
154 if ( $hasSubprojects ) {
155 $this->subproject
= $request->getVal( 'subproject' );
156 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
158 if ( $hasSubprojects &&
159 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
161 $source = Status
::newFatal( "import-invalid-interwiki" );
163 $this->history
= $request->getCheck( 'interwikiHistory' );
164 $this->frompage
= $request->getText( "frompage" );
165 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
166 $source = ImportStreamSource
::newFromInterwiki(
167 $this->fullInterwikiPrefix
,
170 $this->includeTemplates
,
171 $this->pageLinkDepth
);
175 $source = Status
::newFatal( "importunknownsource" );
178 if ( (string)$this->fullInterwikiPrefix
=== '' ) {
179 $source->fatal( 'importnoprefix' );
182 $out = $this->getOutput();
183 if ( !$source->isGood() ) {
184 $out->wrapWikiTextAsInterface( 'error',
185 $this->msg( 'importfailed', $source->getWikiText() )->plain()
188 $importer = new WikiImporter( $source->value
, $this->getConfig() );
189 if ( !is_null( $this->namespace ) ) {
190 $importer->setTargetNamespace( $this->namespace );
191 } elseif ( !is_null( $this->rootpage
) ) {
192 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
193 if ( !$statusRootPage->isGood() ) {
195 "<div class=\"error\">\n$1\n</div>",
197 'import-options-wrong',
198 $statusRootPage->getWikiText(),
199 count( $statusRootPage->getErrorsArray() )
206 $importer->setUsernamePrefix( $this->fullInterwikiPrefix
, $this->assignKnownUsers
);
208 $out->addWikiMsg( "importstart" );
210 $reporter = new ImportReporter(
213 $this->fullInterwikiPrefix
,
216 $reporter->setContext( $this->getContext() );
221 $importer->doImport();
222 } catch ( Exception
$e ) {
225 $result = $reporter->close();
228 # No source or XML parse error
230 "<div class=\"error\">\n$1\n</div>",
231 [ 'importfailed', $exception->getMessage() ]
233 } elseif ( !$result->isGood() ) {
236 "<div class=\"error\">\n$1\n</div>",
237 [ 'importfailed', $result->getWikiText() ]
241 $out->addWikiMsg( 'importsuccess' );
243 $out->addHTML( '<hr />' );
247 private function getMappingFormPart( $sourceName ) {
248 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
249 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
253 <td class='mw-input'>" .
255 $this->msg( 'import-mapping-default' )->text(),
258 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
259 "mw-import-mapping-$sourceName-default",
260 ( $isSameSourceAsBefore ?
261 ( $this->mapping
=== 'default' ) :
262 is_null( $defaultNamespace ) )
269 <td class='mw-input'>" .
271 $this->msg( 'import-mapping-namespace' )->text(),
274 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
275 "mw-import-mapping-$sourceName-namespace",
276 ( $isSameSourceAsBefore ?
277 ( $this->mapping
=== 'namespace' ) :
278 !is_null( $defaultNamespace ) )
280 Html
::namespaceSelector(
282 'selected' => ( $isSameSourceAsBefore ?
284 ( $defaultNamespace ||
'' ) ),
285 'in-user-lang' => true,
287 'name' => "namespace",
288 // mw-import-namespace-interwiki, mw-import-namespace-upload
289 'id' => "mw-import-namespace-$sourceName",
290 'class' => 'namespaceselector',
298 <td class='mw-input'>" .
300 $this->msg( 'import-mapping-subpage' )->text(),
303 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
304 "mw-import-mapping-$sourceName-subpage",
305 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
307 Xml
::input( 'rootpage', 50,
308 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
310 // Should be "mw-import-rootpage-...", but we keep this inaccurate
311 // ID for legacy reasons
312 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
313 'id' => "mw-interwiki-rootpage-$sourceName",
321 private function showForm() {
322 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
323 $user = $this->getUser();
324 $out = $this->getOutput();
325 $this->addHelpLink( 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
327 if ( $user->isAllowed( 'importupload' ) ) {
328 $mappingSelection = $this->getMappingFormPart( 'upload' );
330 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
334 'enctype' => 'multipart/form-data',
337 'id' => 'mw-import-upload-form'
340 $this->msg( 'importtext' )->parseAsBlock() .
341 Html
::hidden( 'action', 'submit' ) .
342 Html
::hidden( 'source', 'upload' ) .
343 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
345 <td class='mw-label'>" .
346 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
348 <td class='mw-input'>" .
349 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
353 <td class='mw-label'>" .
354 Xml
::label( $this->msg( 'import-upload-username-prefix' )->text(),
355 'mw-import-usernamePrefix' ) .
357 <td class='mw-input'>" .
358 Xml
::input( 'usernamePrefix', 50,
359 $this->usernamePrefix
,
360 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
365 <td class='mw-input'>" .
367 $this->msg( 'import-assign-known-users' )->text(),
370 $this->assignKnownUsers
375 <td class='mw-label'>" .
376 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
378 <td class='mw-input'>" .
379 Xml
::input( 'log-comment', 50,
380 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
381 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
387 <td class='mw-submit'>" .
388 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
391 Xml
::closeElement( 'table' ) .
392 Html
::hidden( 'editToken', $user->getEditToken() ) .
393 Xml
::closeElement( 'form' ) .
394 Xml
::closeElement( 'fieldset' )
396 } elseif ( empty( $this->importSources
) ) {
397 $out->addWikiMsg( 'importnosources' );
400 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
401 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
403 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
405 <td class='mw-label'>" .
406 $this->msg( 'export-pagelinks' )->parse() .
408 <td class='mw-input'>" .
409 Xml
::input( 'pagelink-depth', 3, 0 ) .
413 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
416 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
422 'id' => 'mw-import-interwiki-form'
425 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
426 Html
::hidden( 'action', 'submit' ) .
427 Html
::hidden( 'source', 'interwiki' ) .
428 Html
::hidden( 'editToken', $user->getEditToken() ) .
429 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
431 <td class='mw-label'>" .
432 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
434 <td class='mw-input'>" .
437 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
441 $needSubprojectField = false;
442 foreach ( $this->importSources
as $key => $value ) {
443 if ( is_int( $key ) ) {
445 } elseif ( $value !== $key ) {
446 $needSubprojectField = true;
452 if ( is_array( $value ) ) {
453 $attribs['data-subprojects'] = implode( ' ', $value );
455 if ( $this->interwiki
=== $key ) {
456 $attribs['selected'] = 'selected';
458 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
462 Xml
::closeElement( 'select' )
465 if ( $needSubprojectField ) {
469 [ 'name' => 'subproject', 'id' => 'subproject' ]
473 $subprojectsToAdd = [];
474 foreach ( $this->importSources
as $key => $value ) {
475 if ( is_array( $value ) ) {
476 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
479 $subprojectsToAdd = array_unique( $subprojectsToAdd );
480 sort( $subprojectsToAdd );
481 foreach ( $subprojectsToAdd as $subproject ) {
482 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
486 Xml
::closeElement( 'select' )
494 <td class='mw-label'>" .
495 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
497 <td class='mw-input'>" .
498 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
504 <td class='mw-input'>" .
506 $this->msg( 'import-interwiki-history' )->text(),
516 <td class='mw-input'>" .
518 $this->msg( 'import-interwiki-templates' )->text(),
519 'interwikiTemplates',
520 'interwikiTemplates',
521 $this->includeTemplates
527 <td class='mw-input'>" .
529 $this->msg( 'import-assign-known-users' )->text(),
531 'interwikiAssignKnownUsers',
532 $this->assignKnownUsers
538 <td class='mw-label'>" .
539 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
541 <td class='mw-input'>" .
542 Xml
::input( 'log-comment', 50,
543 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
544 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
551 <td class='mw-submit'>" .
553 $this->msg( 'import-interwiki-submit' )->text(),
554 Linker
::tooltipAndAccesskeyAttribs( 'import' )
558 Xml
::closeElement( 'table' ) .
559 Xml
::closeElement( 'form' ) .
560 Xml
::closeElement( 'fieldset' )
565 protected function getGroupName() {