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( false, false, $this->getLanguage() ) )
194 $importer = new WikiImporter( $source->value
, $this->getConfig() );
195 if ( !is_null( $this->namespace ) ) {
196 $importer->setTargetNamespace( $this->namespace );
197 } elseif ( !is_null( $this->rootpage
) ) {
198 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
199 if ( !$statusRootPage->isGood() ) {
201 "<div class=\"error\">\n$1\n</div>",
203 'import-options-wrong',
204 $statusRootPage->getWikiText( false, false, $this->getLanguage() ),
205 count( $statusRootPage->getErrorsArray() )
212 $importer->setUsernamePrefix( $this->fullInterwikiPrefix
, $this->assignKnownUsers
);
214 $out->addWikiMsg( "importstart" );
216 $reporter = new ImportReporter(
219 $this->fullInterwikiPrefix
,
222 $reporter->setContext( $this->getContext() );
227 $importer->doImport();
228 } catch ( Exception
$e ) {
231 $result = $reporter->close();
234 # No source or XML parse error
236 "<div class=\"error\">\n$1\n</div>",
237 [ 'importfailed', $exception->getMessage() ]
239 } elseif ( !$result->isGood() ) {
242 "<div class=\"error\">\n$1\n</div>",
243 [ 'importfailed', $result->getWikiText( false, false, $this->getLanguage() ) ]
247 $out->addWikiMsg( 'importsuccess' );
249 $out->addHTML( '<hr />' );
253 private function getMappingFormPart( $sourceName ) {
254 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
255 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
259 <td class='mw-input'>" .
261 $this->msg( 'import-mapping-default' )->text(),
264 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
265 "mw-import-mapping-$sourceName-default",
266 ( $isSameSourceAsBefore ?
267 ( $this->mapping
=== 'default' ) :
268 is_null( $defaultNamespace ) )
275 <td class='mw-input'>" .
277 $this->msg( 'import-mapping-namespace' )->text(),
280 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
281 "mw-import-mapping-$sourceName-namespace",
282 ( $isSameSourceAsBefore ?
283 ( $this->mapping
=== 'namespace' ) :
284 !is_null( $defaultNamespace ) )
286 Html
::namespaceSelector(
288 'selected' => ( $isSameSourceAsBefore ?
290 ( $defaultNamespace ||
'' ) ),
291 'in-user-lang' => true,
293 'name' => "namespace",
294 // mw-import-namespace-interwiki, mw-import-namespace-upload
295 'id' => "mw-import-namespace-$sourceName",
296 'class' => 'namespaceselector',
304 <td class='mw-input'>" .
306 $this->msg( 'import-mapping-subpage' )->text(),
309 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
310 "mw-import-mapping-$sourceName-subpage",
311 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
313 Xml
::input( 'rootpage', 50,
314 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
316 // Should be "mw-import-rootpage-...", but we keep this inaccurate
317 // ID for legacy reasons
318 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
319 'id' => "mw-interwiki-rootpage-$sourceName",
327 private function showForm() {
328 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
329 $user = $this->getUser();
330 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
331 $out = $this->getOutput();
332 $this->addHelpLink( 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
334 if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
335 $mappingSelection = $this->getMappingFormPart( 'upload' );
337 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
341 'enctype' => 'multipart/form-data',
344 'id' => 'mw-import-upload-form'
347 $this->msg( 'importtext' )->parseAsBlock() .
348 Html
::hidden( 'action', 'submit' ) .
349 Html
::hidden( 'source', 'upload' ) .
350 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
352 <td class='mw-label'>" .
353 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
355 <td class='mw-input'>" .
356 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
360 <td class='mw-label'>" .
361 Xml
::label( $this->msg( 'import-upload-username-prefix' )->text(),
362 'mw-import-usernamePrefix' ) .
364 <td class='mw-input'>" .
365 Xml
::input( 'usernamePrefix', 50,
366 $this->usernamePrefix
,
367 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
372 <td class='mw-input'>" .
374 $this->msg( 'import-assign-known-users' )->text(),
377 $this->assignKnownUsers
382 <td class='mw-label'>" .
383 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
385 <td class='mw-input'>" .
386 Xml
::input( 'log-comment', 50,
387 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
388 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
394 <td class='mw-submit'>" .
395 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
398 Xml
::closeElement( 'table' ) .
399 Html
::hidden( 'editToken', $user->getEditToken() ) .
400 Xml
::closeElement( 'form' ) .
401 Xml
::closeElement( 'fieldset' )
403 } elseif ( empty( $this->importSources
) ) {
404 $out->addWikiMsg( 'importnosources' );
407 if ( $permissionManager->userHasRight( $user, 'import' ) && !empty( $this->importSources
) ) {
408 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
410 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
412 <td class='mw-label'>" .
413 $this->msg( 'export-pagelinks' )->parse() .
415 <td class='mw-input'>" .
416 Xml
::input( 'pagelink-depth', 3, 0 ) .
420 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
423 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
429 'id' => 'mw-import-interwiki-form'
432 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
433 Html
::hidden( 'action', 'submit' ) .
434 Html
::hidden( 'source', 'interwiki' ) .
435 Html
::hidden( 'editToken', $user->getEditToken() ) .
436 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
438 <td class='mw-label'>" .
439 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
441 <td class='mw-input'>" .
444 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
448 $needSubprojectField = false;
449 foreach ( $this->importSources
as $key => $value ) {
450 if ( is_int( $key ) ) {
452 } elseif ( $value !== $key ) {
453 $needSubprojectField = true;
459 if ( is_array( $value ) ) {
460 $attribs['data-subprojects'] = implode( ' ', $value );
462 if ( $this->interwiki
=== $key ) {
463 $attribs['selected'] = 'selected';
465 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
469 Xml
::closeElement( 'select' )
472 if ( $needSubprojectField ) {
476 [ 'name' => 'subproject', 'id' => 'subproject' ]
480 $subprojectsToAdd = [];
481 foreach ( $this->importSources
as $key => $value ) {
482 if ( is_array( $value ) ) {
483 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
486 $subprojectsToAdd = array_unique( $subprojectsToAdd );
487 sort( $subprojectsToAdd );
488 foreach ( $subprojectsToAdd as $subproject ) {
489 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
493 Xml
::closeElement( 'select' )
501 <td class='mw-label'>" .
502 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
504 <td class='mw-input'>" .
505 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
511 <td class='mw-input'>" .
513 $this->msg( 'import-interwiki-history' )->text(),
523 <td class='mw-input'>" .
525 $this->msg( 'import-interwiki-templates' )->text(),
526 'interwikiTemplates',
527 'interwikiTemplates',
528 $this->includeTemplates
534 <td class='mw-input'>" .
536 $this->msg( 'import-assign-known-users' )->text(),
538 'interwikiAssignKnownUsers',
539 $this->assignKnownUsers
545 <td class='mw-label'>" .
546 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
548 <td class='mw-input'>" .
549 Xml
::input( 'log-comment', 50,
550 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
551 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
558 <td class='mw-submit'>" .
560 $this->msg( 'import-interwiki-submit' )->text(),
561 Linker
::tooltipAndAccesskeyAttribs( 'import' )
565 Xml
::closeElement( 'table' ) .
566 Xml
::closeElement( 'form' ) .
567 Xml
::closeElement( 'fieldset' )
572 protected function getGroupName() {