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
28 * MediaWiki page data importer
30 * @ingroup SpecialPage
32 class SpecialImport
extends SpecialPage
{
33 private $sourceName = false;
34 private $interwiki = false;
36 private $fullInterwikiPrefix;
37 private $mapping = 'default';
39 private $rootpage = '';
40 private $frompage = '';
41 private $logcomment = false;
42 private $history = true;
43 private $includeTemplates = false;
44 private $pageLinkDepth;
45 private $importSources;
46 private $assignKnownUsers;
47 private $usernamePrefix;
49 public function __construct() {
50 parent
::__construct( 'Import', 'import' );
53 public function doesWrites() {
59 * @param string|null $par
60 * @throws PermissionsError
61 * @throws ReadOnlyError
63 function execute( $par ) {
64 $this->useTransactionalTimeLimit();
67 $this->outputHeader();
69 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
71 $this->getOutput()->addModules( 'mediawiki.special.import' );
73 $this->importSources
= $this->getConfig()->get( 'ImportSources' );
74 Hooks
::run( 'ImportSources', [ &$this->importSources
] );
76 $user = $this->getUser();
77 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
78 throw new PermissionsError( 'import' );
81 # @todo Allow Title::getUserPermissionsErrors() to take an array
82 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very weird expectation of what
83 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
84 $errors = wfMergeErrorArrays(
85 $this->getPageTitle()->getUserPermissionsErrors(
86 'import', $user, true,
87 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
89 $this->getPageTitle()->getUserPermissionsErrors(
90 'importupload', $user, true,
91 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
96 throw new PermissionsError( 'import', $errors );
99 $this->checkReadOnly();
101 $request = $this->getRequest();
102 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
109 * Do the actual import
111 private function doImport() {
113 $request = $this->getRequest();
114 $this->sourceName
= $request->getVal( "source" );
115 $this->assignKnownUsers
= $request->getCheck( 'assignKnownUsers' );
117 $this->logcomment
= $request->getText( 'log-comment' );
118 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
120 : $request->getIntOrNull( 'pagelink-depth' );
122 $this->mapping
= $request->getVal( 'mapping' );
123 if ( $this->mapping
=== 'namespace' ) {
124 $this->namespace = $request->getIntOrNull( 'namespace' );
125 } elseif ( $this->mapping
=== 'subpage' ) {
126 $this->rootpage
= $request->getText( 'rootpage' );
128 $this->mapping
= 'default';
131 $user = $this->getUser();
132 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
133 $source = Status
::newFatal( 'import-token-mismatch' );
134 } elseif ( $this->sourceName
=== 'upload' ) {
136 $this->usernamePrefix
= $this->fullInterwikiPrefix
= $request->getVal( 'usernamePrefix' );
137 if ( $user->isAllowed( 'importupload' ) ) {
138 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
140 throw new PermissionsError( 'importupload' );
142 } elseif ( $this->sourceName
=== 'interwiki' ) {
143 if ( !$user->isAllowed( 'import' ) ) {
144 throw new PermissionsError( 'import' );
146 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
147 // does this interwiki have subprojects?
148 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
149 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
150 $source = Status
::newFatal( "import-invalid-interwiki" );
152 if ( $hasSubprojects ) {
153 $this->subproject
= $request->getVal( 'subproject' );
154 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
156 if ( $hasSubprojects &&
157 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
159 $source = Status
::newFatal( "import-invalid-interwiki" );
161 $this->history
= $request->getCheck( 'interwikiHistory' );
162 $this->frompage
= $request->getText( "frompage" );
163 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
164 $source = ImportStreamSource
::newFromInterwiki(
165 $this->fullInterwikiPrefix
,
168 $this->includeTemplates
,
169 $this->pageLinkDepth
);
173 $source = Status
::newFatal( "importunknownsource" );
176 if ( (string)$this->fullInterwikiPrefix
=== '' ) {
177 $source->fatal( 'importnoprefix' );
180 $out = $this->getOutput();
181 if ( !$source->isGood() ) {
182 $out->wrapWikiTextAsInterface( 'error',
183 $this->msg( 'importfailed', $source->getWikiText() )->plain()
186 $importer = new WikiImporter( $source->value
, $this->getConfig() );
187 if ( !is_null( $this->namespace ) ) {
188 $importer->setTargetNamespace( $this->namespace );
189 } elseif ( !is_null( $this->rootpage
) ) {
190 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
191 if ( !$statusRootPage->isGood() ) {
193 "<div class=\"error\">\n$1\n</div>",
195 'import-options-wrong',
196 $statusRootPage->getWikiText(),
197 count( $statusRootPage->getErrorsArray() )
204 $importer->setUsernamePrefix( $this->fullInterwikiPrefix
, $this->assignKnownUsers
);
206 $out->addWikiMsg( "importstart" );
208 $reporter = new ImportReporter(
211 $this->fullInterwikiPrefix
,
214 $reporter->setContext( $this->getContext() );
219 $importer->doImport();
220 } catch ( Exception
$e ) {
223 $result = $reporter->close();
226 # No source or XML parse error
228 "<div class=\"error\">\n$1\n</div>",
229 [ 'importfailed', $exception->getMessage() ]
231 } elseif ( !$result->isGood() ) {
234 "<div class=\"error\">\n$1\n</div>",
235 [ 'importfailed', $result->getWikiText() ]
239 $out->addWikiMsg( 'importsuccess' );
241 $out->addHTML( '<hr />' );
245 private function getMappingFormPart( $sourceName ) {
246 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
247 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
251 <td class='mw-input'>" .
253 $this->msg( 'import-mapping-default' )->text(),
256 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
257 "mw-import-mapping-$sourceName-default",
258 ( $isSameSourceAsBefore ?
259 ( $this->mapping
=== 'default' ) :
260 is_null( $defaultNamespace ) )
267 <td class='mw-input'>" .
269 $this->msg( 'import-mapping-namespace' )->text(),
272 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
273 "mw-import-mapping-$sourceName-namespace",
274 ( $isSameSourceAsBefore ?
275 ( $this->mapping
=== 'namespace' ) :
276 !is_null( $defaultNamespace ) )
278 Html
::namespaceSelector(
280 'selected' => ( $isSameSourceAsBefore ?
282 ( $defaultNamespace ||
'' ) ),
284 'name' => "namespace",
285 // mw-import-namespace-interwiki, mw-import-namespace-upload
286 'id' => "mw-import-namespace-$sourceName",
287 'class' => 'namespaceselector',
295 <td class='mw-input'>" .
297 $this->msg( 'import-mapping-subpage' )->text(),
300 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
301 "mw-import-mapping-$sourceName-subpage",
302 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
304 Xml
::input( 'rootpage', 50,
305 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
307 // Should be "mw-import-rootpage-...", but we keep this inaccurate
308 // ID for legacy reasons
309 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
310 'id' => "mw-interwiki-rootpage-$sourceName",
318 private function showForm() {
319 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
320 $user = $this->getUser();
321 $out = $this->getOutput();
322 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
324 if ( $user->isAllowed( 'importupload' ) ) {
325 $mappingSelection = $this->getMappingFormPart( 'upload' );
327 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
331 'enctype' => 'multipart/form-data',
334 'id' => 'mw-import-upload-form'
337 $this->msg( 'importtext' )->parseAsBlock() .
338 Html
::hidden( 'action', 'submit' ) .
339 Html
::hidden( 'source', 'upload' ) .
340 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
342 <td class='mw-label'>" .
343 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
345 <td class='mw-input'>" .
346 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
350 <td class='mw-label'>" .
351 Xml
::label( $this->msg( 'import-upload-username-prefix' )->text(),
352 'mw-import-usernamePrefix' ) .
354 <td class='mw-input'>" .
355 Xml
::input( 'usernamePrefix', 50,
356 $this->usernamePrefix
,
357 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
362 <td class='mw-input'>" .
364 $this->msg( 'import-assign-known-users' )->text(),
367 $this->assignKnownUsers
372 <td class='mw-label'>" .
373 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
375 <td class='mw-input'>" .
376 Xml
::input( 'log-comment', 50,
377 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
378 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
384 <td class='mw-submit'>" .
385 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
388 Xml
::closeElement( 'table' ) .
389 Html
::hidden( 'editToken', $user->getEditToken() ) .
390 Xml
::closeElement( 'form' ) .
391 Xml
::closeElement( 'fieldset' )
394 if ( empty( $this->importSources
) ) {
395 $out->addWikiMsg( 'importnosources' );
399 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
400 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
402 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
404 <td class='mw-label'>" .
405 $this->msg( 'export-pagelinks' )->parse() .
407 <td class='mw-input'>" .
408 Xml
::input( 'pagelink-depth', 3, 0 ) .
412 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
415 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
421 'id' => 'mw-import-interwiki-form'
424 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
425 Html
::hidden( 'action', 'submit' ) .
426 Html
::hidden( 'source', 'interwiki' ) .
427 Html
::hidden( 'editToken', $user->getEditToken() ) .
428 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
430 <td class='mw-label'>" .
431 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
433 <td class='mw-input'>" .
436 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
440 $needSubprojectField = false;
441 foreach ( $this->importSources
as $key => $value ) {
442 if ( is_int( $key ) ) {
444 } elseif ( $value !== $key ) {
445 $needSubprojectField = true;
451 if ( is_array( $value ) ) {
452 $attribs['data-subprojects'] = implode( ' ', $value );
454 if ( $this->interwiki
=== $key ) {
455 $attribs['selected'] = 'selected';
457 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
461 Xml
::closeElement( 'select' )
464 if ( $needSubprojectField ) {
468 [ 'name' => 'subproject', 'id' => 'subproject' ]
472 $subprojectsToAdd = [];
473 foreach ( $this->importSources
as $key => $value ) {
474 if ( is_array( $value ) ) {
475 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
478 $subprojectsToAdd = array_unique( $subprojectsToAdd );
479 sort( $subprojectsToAdd );
480 foreach ( $subprojectsToAdd as $subproject ) {
481 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
485 Xml
::closeElement( 'select' )
493 <td class='mw-label'>" .
494 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
496 <td class='mw-input'>" .
497 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
503 <td class='mw-input'>" .
505 $this->msg( 'import-interwiki-history' )->text(),
515 <td class='mw-input'>" .
517 $this->msg( 'import-interwiki-templates' )->text(),
518 'interwikiTemplates',
519 'interwikiTemplates',
520 $this->includeTemplates
526 <td class='mw-input'>" .
528 $this->msg( 'import-assign-known-users' )->text(),
530 'interwikiAssignKnownUsers',
531 $this->assignKnownUsers
537 <td class='mw-label'>" .
538 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
540 <td class='mw-input'>" .
541 Xml
::input( 'log-comment', 50,
542 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
543 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
550 <td class='mw-submit'>" .
552 $this->msg( 'import-interwiki-submit' )->text(),
553 Linker
::tooltipAndAccesskeyAttribs( 'import' )
557 Xml
::closeElement( 'table' ) .
558 Xml
::closeElement( 'form' ) .
559 Xml
::closeElement( 'fieldset' )
564 protected function getGroupName() {