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 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
139 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
140 $source = Status
::newFatal( 'import-token-mismatch' );
141 } elseif ( $this->sourceName
=== 'upload' ) {
143 $this->usernamePrefix
= $this->fullInterwikiPrefix
= $request->getVal( 'usernamePrefix' );
144 if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
145 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
147 throw new PermissionsError( 'importupload' );
149 } elseif ( $this->sourceName
=== 'interwiki' ) {
150 if ( !$permissionManager->userHasRight( $user, 'import' ) ) {
151 throw new PermissionsError( 'import' );
153 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
154 // does this interwiki have subprojects?
155 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
156 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
157 $source = Status
::newFatal( "import-invalid-interwiki" );
159 if ( $hasSubprojects ) {
160 $this->subproject
= $request->getVal( 'subproject' );
161 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
163 if ( $hasSubprojects &&
164 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
166 $source = Status
::newFatal( "import-invalid-interwiki" );
168 $this->history
= $request->getCheck( 'interwikiHistory' );
169 $this->frompage
= $request->getText( "frompage" );
170 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
171 $source = ImportStreamSource
::newFromInterwiki(
172 $this->fullInterwikiPrefix
,
175 $this->includeTemplates
,
176 $this->pageLinkDepth
);
180 $source = Status
::newFatal( "importunknownsource" );
183 if ( (string)$this->fullInterwikiPrefix
=== '' ) {
184 $source->fatal( 'importnoprefix' );
187 $out = $this->getOutput();
188 if ( !$source->isGood() ) {
189 $out->wrapWikiTextAsInterface( 'error',
190 $this->msg( 'importfailed', $source->getWikiText() )->plain()
193 $importer = new WikiImporter( $source->value
, $this->getConfig() );
194 if ( !is_null( $this->namespace ) ) {
195 $importer->setTargetNamespace( $this->namespace );
196 } elseif ( !is_null( $this->rootpage
) ) {
197 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
198 if ( !$statusRootPage->isGood() ) {
200 "<div class=\"error\">\n$1\n</div>",
202 'import-options-wrong',
203 $statusRootPage->getWikiText(),
204 count( $statusRootPage->getErrorsArray() )
211 $importer->setUsernamePrefix( $this->fullInterwikiPrefix
, $this->assignKnownUsers
);
213 $out->addWikiMsg( "importstart" );
215 $reporter = new ImportReporter(
218 $this->fullInterwikiPrefix
,
221 $reporter->setContext( $this->getContext() );
226 $importer->doImport();
227 } catch ( Exception
$e ) {
230 $result = $reporter->close();
233 # No source or XML parse error
235 "<div class=\"error\">\n$1\n</div>",
236 [ 'importfailed', $exception->getMessage() ]
238 } elseif ( !$result->isGood() ) {
241 "<div class=\"error\">\n$1\n</div>",
242 [ 'importfailed', $result->getWikiText() ]
246 $out->addWikiMsg( 'importsuccess' );
248 $out->addHTML( '<hr />' );
252 private function getMappingFormPart( $sourceName ) {
253 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
254 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
258 <td class='mw-input'>" .
260 $this->msg( 'import-mapping-default' )->text(),
263 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
264 "mw-import-mapping-$sourceName-default",
265 ( $isSameSourceAsBefore ?
266 ( $this->mapping
=== 'default' ) :
267 is_null( $defaultNamespace ) )
274 <td class='mw-input'>" .
276 $this->msg( 'import-mapping-namespace' )->text(),
279 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
280 "mw-import-mapping-$sourceName-namespace",
281 ( $isSameSourceAsBefore ?
282 ( $this->mapping
=== 'namespace' ) :
283 !is_null( $defaultNamespace ) )
285 Html
::namespaceSelector(
287 'selected' => ( $isSameSourceAsBefore ?
289 ( $defaultNamespace ||
'' ) ),
290 'in-user-lang' => true,
292 'name' => "namespace",
293 // mw-import-namespace-interwiki, mw-import-namespace-upload
294 'id' => "mw-import-namespace-$sourceName",
295 'class' => 'namespaceselector',
303 <td class='mw-input'>" .
305 $this->msg( 'import-mapping-subpage' )->text(),
308 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
309 "mw-import-mapping-$sourceName-subpage",
310 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
312 Xml
::input( 'rootpage', 50,
313 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
315 // Should be "mw-import-rootpage-...", but we keep this inaccurate
316 // ID for legacy reasons
317 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
318 'id' => "mw-interwiki-rootpage-$sourceName",
326 private function showForm() {
327 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
328 $user = $this->getUser();
329 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
330 $out = $this->getOutput();
331 $this->addHelpLink( 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
333 if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
334 $mappingSelection = $this->getMappingFormPart( 'upload' );
336 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
340 'enctype' => 'multipart/form-data',
343 'id' => 'mw-import-upload-form'
346 $this->msg( 'importtext' )->parseAsBlock() .
347 Html
::hidden( 'action', 'submit' ) .
348 Html
::hidden( 'source', 'upload' ) .
349 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
351 <td class='mw-label'>" .
352 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
354 <td class='mw-input'>" .
355 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
359 <td class='mw-label'>" .
360 Xml
::label( $this->msg( 'import-upload-username-prefix' )->text(),
361 'mw-import-usernamePrefix' ) .
363 <td class='mw-input'>" .
364 Xml
::input( 'usernamePrefix', 50,
365 $this->usernamePrefix
,
366 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
371 <td class='mw-input'>" .
373 $this->msg( 'import-assign-known-users' )->text(),
376 $this->assignKnownUsers
381 <td class='mw-label'>" .
382 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
384 <td class='mw-input'>" .
385 Xml
::input( 'log-comment', 50,
386 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
387 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
393 <td class='mw-submit'>" .
394 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
397 Xml
::closeElement( 'table' ) .
398 Html
::hidden( 'editToken', $user->getEditToken() ) .
399 Xml
::closeElement( 'form' ) .
400 Xml
::closeElement( 'fieldset' )
402 } elseif ( empty( $this->importSources
) ) {
403 $out->addWikiMsg( 'importnosources' );
406 if ( $permissionManager->userHasRight( $user, 'import' ) && !empty( $this->importSources
) ) {
407 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
409 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
411 <td class='mw-label'>" .
412 $this->msg( 'export-pagelinks' )->parse() .
414 <td class='mw-input'>" .
415 Xml
::input( 'pagelink-depth', 3, 0 ) .
419 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
422 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
428 'id' => 'mw-import-interwiki-form'
431 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
432 Html
::hidden( 'action', 'submit' ) .
433 Html
::hidden( 'source', 'interwiki' ) .
434 Html
::hidden( 'editToken', $user->getEditToken() ) .
435 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
437 <td class='mw-label'>" .
438 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
440 <td class='mw-input'>" .
443 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
447 $needSubprojectField = false;
448 foreach ( $this->importSources
as $key => $value ) {
449 if ( is_int( $key ) ) {
451 } elseif ( $value !== $key ) {
452 $needSubprojectField = true;
458 if ( is_array( $value ) ) {
459 $attribs['data-subprojects'] = implode( ' ', $value );
461 if ( $this->interwiki
=== $key ) {
462 $attribs['selected'] = 'selected';
464 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
468 Xml
::closeElement( 'select' )
471 if ( $needSubprojectField ) {
475 [ 'name' => 'subproject', 'id' => 'subproject' ]
479 $subprojectsToAdd = [];
480 foreach ( $this->importSources
as $key => $value ) {
481 if ( is_array( $value ) ) {
482 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
485 $subprojectsToAdd = array_unique( $subprojectsToAdd );
486 sort( $subprojectsToAdd );
487 foreach ( $subprojectsToAdd as $subproject ) {
488 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
492 Xml
::closeElement( 'select' )
500 <td class='mw-label'>" .
501 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
503 <td class='mw-input'>" .
504 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
510 <td class='mw-input'>" .
512 $this->msg( 'import-interwiki-history' )->text(),
522 <td class='mw-input'>" .
524 $this->msg( 'import-interwiki-templates' )->text(),
525 'interwikiTemplates',
526 'interwikiTemplates',
527 $this->includeTemplates
533 <td class='mw-input'>" .
535 $this->msg( 'import-assign-known-users' )->text(),
537 'interwikiAssignKnownUsers',
538 $this->assignKnownUsers
544 <td class='mw-label'>" .
545 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
547 <td class='mw-input'>" .
548 Xml
::input( 'log-comment', 50,
549 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
550 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
557 <td class='mw-submit'>" .
559 $this->msg( 'import-interwiki-submit' )->text(),
560 Linker
::tooltipAndAccesskeyAttribs( 'import' )
564 Xml
::closeElement( 'table' ) .
565 Xml
::closeElement( 'form' ) .
566 Xml
::closeElement( 'fieldset' )
571 protected function getGroupName() {