9ce52ef0130d5a4eb566135726c08cb26b93eedf
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;
47 public function __construct() {
48 parent
::__construct( 'Import', 'import' );
51 public function doesWrites() {
57 * @param string|null $par
58 * @throws PermissionsError
59 * @throws ReadOnlyError
61 function execute( $par ) {
62 $this->useTransactionalTimeLimit();
65 $this->outputHeader();
67 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
69 $this->getOutput()->addModules( 'mediawiki.special.import' );
71 $this->importSources
= $this->getConfig()->get( 'ImportSources' );
72 Hooks
::run( 'ImportSources', [ &$this->importSources
] );
74 $user = $this->getUser();
75 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
76 throw new PermissionsError( 'import' );
79 # @todo Allow Title::getUserPermissionsErrors() to take an array
80 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
81 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
82 $errors = wfMergeErrorArrays(
83 $this->getPageTitle()->getUserPermissionsErrors(
84 'import', $user, true,
85 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
87 $this->getPageTitle()->getUserPermissionsErrors(
88 'importupload', $user, true,
89 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
94 throw new PermissionsError( 'import', $errors );
97 $this->checkReadOnly();
99 $request = $this->getRequest();
100 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
107 * Do the actual import
109 private function doImport() {
111 $request = $this->getRequest();
112 $this->sourceName
= $request->getVal( "source" );
114 $this->logcomment
= $request->getText( 'log-comment' );
115 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
117 : $request->getIntOrNull( 'pagelink-depth' );
119 $this->mapping
= $request->getVal( 'mapping' );
120 if ( $this->mapping
=== 'namespace' ) {
121 $this->namespace = $request->getIntOrNull( 'namespace' );
122 } elseif ( $this->mapping
=== 'subpage' ) {
123 $this->rootpage
= $request->getText( 'rootpage' );
125 $this->mapping
= 'default';
128 $user = $this->getUser();
129 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
130 $source = Status
::newFatal( 'import-token-mismatch' );
131 } elseif ( $this->sourceName
=== 'upload' ) {
133 if ( $user->isAllowed( 'importupload' ) ) {
134 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
136 throw new PermissionsError( 'importupload' );
138 } elseif ( $this->sourceName
=== 'interwiki' ) {
139 if ( !$user->isAllowed( 'import' ) ) {
140 throw new PermissionsError( 'import' );
142 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
143 // does this interwiki have subprojects?
144 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
145 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
146 $source = Status
::newFatal( "import-invalid-interwiki" );
148 if ( $hasSubprojects ) {
149 $this->subproject
= $request->getVal( 'subproject' );
150 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
152 if ( $hasSubprojects &&
153 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
155 $source = Status
::newFatal( "import-invalid-interwiki" );
157 $this->history
= $request->getCheck( 'interwikiHistory' );
158 $this->frompage
= $request->getText( "frompage" );
159 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
160 $source = ImportStreamSource
::newFromInterwiki(
161 $this->fullInterwikiPrefix
,
164 $this->includeTemplates
,
165 $this->pageLinkDepth
);
169 $source = Status
::newFatal( "importunknownsource" );
172 $out = $this->getOutput();
173 if ( !$source->isGood() ) {
174 $out->addWikiText( "<p class=\"error\">\n" .
175 $this->msg( 'importfailed', $source->getWikiText() )->parse() . "\n</p>" );
177 $importer = new WikiImporter( $source->value
, $this->getConfig() );
178 if ( !is_null( $this->namespace ) ) {
179 $importer->setTargetNamespace( $this->namespace );
180 } elseif ( !is_null( $this->rootpage
) ) {
181 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
182 if ( !$statusRootPage->isGood() ) {
184 "<p class=\"error\">\n$1\n</p>",
186 'import-options-wrong',
187 $statusRootPage->getWikiText(),
188 count( $statusRootPage->getErrorsArray() )
196 $out->addWikiMsg( "importstart" );
198 $reporter = new ImportReporter(
201 $this->fullInterwikiPrefix
,
204 $reporter->setContext( $this->getContext() );
209 $importer->doImport();
210 } catch ( Exception
$e ) {
213 $result = $reporter->close();
216 # No source or XML parse error
218 "<p class=\"error\">\n$1\n</p>",
219 [ 'importfailed', $exception->getMessage() ]
221 } elseif ( !$result->isGood() ) {
224 "<p class=\"error\">\n$1\n</p>",
225 [ 'importfailed', $result->getWikiText() ]
229 $out->addWikiMsg( 'importsuccess' );
231 $out->addHTML( '<hr />' );
235 private function getMappingFormPart( $sourceName ) {
236 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
237 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
241 <td class='mw-input'>" .
243 $this->msg( 'import-mapping-default' )->text(),
246 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
247 "mw-import-mapping-$sourceName-default",
248 ( $isSameSourceAsBefore ?
249 ( $this->mapping
=== 'default' ) :
250 is_null( $defaultNamespace ) )
257 <td class='mw-input'>" .
259 $this->msg( 'import-mapping-namespace' )->text(),
262 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
263 "mw-import-mapping-$sourceName-namespace",
264 ( $isSameSourceAsBefore ?
265 ( $this->mapping
=== 'namespace' ) :
266 !is_null( $defaultNamespace ) )
268 Html
::namespaceSelector(
270 'selected' => ( $isSameSourceAsBefore ?
272 ( $defaultNamespace ||
'' ) ),
274 'name' => "namespace",
275 // mw-import-namespace-interwiki, mw-import-namespace-upload
276 'id' => "mw-import-namespace-$sourceName",
277 'class' => 'namespaceselector',
285 <td class='mw-input'>" .
287 $this->msg( 'import-mapping-subpage' )->text(),
290 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
291 "mw-import-mapping-$sourceName-subpage",
292 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
294 Xml
::input( 'rootpage', 50,
295 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
297 // Should be "mw-import-rootpage-...", but we keep this inaccurate
298 // ID for legacy reasons
299 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
300 'id' => "mw-interwiki-rootpage-$sourceName",
308 private function showForm() {
309 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
310 $user = $this->getUser();
311 $out = $this->getOutput();
312 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
314 if ( $user->isAllowed( 'importupload' ) ) {
315 $mappingSelection = $this->getMappingFormPart( 'upload' );
317 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
321 'enctype' => 'multipart/form-data',
324 'id' => 'mw-import-upload-form'
327 $this->msg( 'importtext' )->parseAsBlock() .
328 Html
::hidden( 'action', 'submit' ) .
329 Html
::hidden( 'source', 'upload' ) .
330 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
332 <td class='mw-label'>" .
333 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
335 <td class='mw-input'>" .
336 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
340 <td class='mw-label'>" .
341 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
343 <td class='mw-input'>" .
344 Xml
::input( 'log-comment', 50,
345 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
346 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
352 <td class='mw-submit'>" .
353 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
356 Xml
::closeElement( 'table' ) .
357 Html
::hidden( 'editToken', $user->getEditToken() ) .
358 Xml
::closeElement( 'form' ) .
359 Xml
::closeElement( 'fieldset' )
362 if ( empty( $this->importSources
) ) {
363 $out->addWikiMsg( 'importnosources' );
367 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
368 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
370 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
372 <td class='mw-label'>" .
373 $this->msg( 'export-pagelinks' )->parse() .
375 <td class='mw-input'>" .
376 Xml
::input( 'pagelink-depth', 3, 0 ) .
380 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
383 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
389 'id' => 'mw-import-interwiki-form'
392 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
393 Html
::hidden( 'action', 'submit' ) .
394 Html
::hidden( 'source', 'interwiki' ) .
395 Html
::hidden( 'editToken', $user->getEditToken() ) .
396 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
398 <td class='mw-label'>" .
399 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
401 <td class='mw-input'>" .
404 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
408 $needSubprojectField = false;
409 foreach ( $this->importSources
as $key => $value ) {
410 if ( is_int( $key ) ) {
412 } elseif ( $value !== $key ) {
413 $needSubprojectField = true;
419 if ( is_array( $value ) ) {
420 $attribs['data-subprojects'] = implode( ' ', $value );
422 if ( $this->interwiki
=== $key ) {
423 $attribs['selected'] = 'selected';
425 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
429 Xml
::closeElement( 'select' )
432 if ( $needSubprojectField ) {
436 [ 'name' => 'subproject', 'id' => 'subproject' ]
440 $subprojectsToAdd = [];
441 foreach ( $this->importSources
as $key => $value ) {
442 if ( is_array( $value ) ) {
443 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
446 $subprojectsToAdd = array_unique( $subprojectsToAdd );
447 sort( $subprojectsToAdd );
448 foreach ( $subprojectsToAdd as $subproject ) {
449 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
453 Xml
::closeElement( 'select' )
461 <td class='mw-label'>" .
462 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
464 <td class='mw-input'>" .
465 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
471 <td class='mw-input'>" .
473 $this->msg( 'import-interwiki-history' )->text(),
483 <td class='mw-input'>" .
485 $this->msg( 'import-interwiki-templates' )->text(),
486 'interwikiTemplates',
487 'interwikiTemplates',
488 $this->includeTemplates
494 <td class='mw-label'>" .
495 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
497 <td class='mw-input'>" .
498 Xml
::input( 'log-comment', 50,
499 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
500 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
507 <td class='mw-submit'>" .
509 $this->msg( 'import-interwiki-submit' )->text(),
510 Linker
::tooltipAndAccesskeyAttribs( 'import' )
514 Xml
::closeElement( 'table' ) .
515 Xml
::closeElement( 'form' ) .
516 Xml
::closeElement( 'fieldset' )
521 protected function getGroupName() {