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
;
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;
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 ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
81 throw new PermissionsError( 'import' );
84 # @todo Allow Title::getUserPermissionsErrors() to take an array
85 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
86 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
87 $errors = wfMergeErrorArrays(
88 $this->getPageTitle()->getUserPermissionsErrors(
89 'import', $user, true,
90 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
92 $this->getPageTitle()->getUserPermissionsErrors(
93 'importupload', $user, true,
94 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
99 throw new PermissionsError( 'import', $errors );
102 $this->checkReadOnly();
104 $request = $this->getRequest();
105 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
112 * Do the actual import
114 private function doImport() {
116 $request = $this->getRequest();
117 $this->sourceName
= $request->getVal( "source" );
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 if ( $user->isAllowed( 'importupload' ) ) {
139 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
141 throw new PermissionsError( 'importupload' );
143 } elseif ( $this->sourceName
=== 'interwiki' ) {
144 if ( !$user->isAllowed( 'import' ) ) {
145 throw new PermissionsError( 'import' );
147 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
148 // does this interwiki have subprojects?
149 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
150 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
151 $source = Status
::newFatal( "import-invalid-interwiki" );
153 if ( $hasSubprojects ) {
154 $this->subproject
= $request->getVal( 'subproject' );
155 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
157 if ( $hasSubprojects &&
158 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
160 $source = Status
::newFatal( "import-invalid-interwiki" );
162 $this->history
= $request->getCheck( 'interwikiHistory' );
163 $this->frompage
= $request->getText( "frompage" );
164 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
165 $source = ImportStreamSource
::newFromInterwiki(
166 $this->fullInterwikiPrefix
,
169 $this->includeTemplates
,
170 $this->pageLinkDepth
);
174 $source = Status
::newFatal( "importunknownsource" );
177 $out = $this->getOutput();
178 if ( !$source->isGood() ) {
180 "<p class=\"error\">\n$1\n</p>",
181 [ 'importfailed', $source->getWikiText() ]
184 $importer = new WikiImporter( $source->value
, $this->getConfig() );
185 if ( !is_null( $this->namespace ) ) {
186 $importer->setTargetNamespace( $this->namespace );
187 } elseif ( !is_null( $this->rootpage
) ) {
188 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
189 if ( !$statusRootPage->isGood() ) {
191 "<p class=\"error\">\n$1\n</p>",
193 'import-options-wrong',
194 $statusRootPage->getWikiText(),
195 count( $statusRootPage->getErrorsArray() )
203 $out->addWikiMsg( "importstart" );
205 $reporter = new ImportReporter(
208 $this->fullInterwikiPrefix
,
211 $reporter->setContext( $this->getContext() );
216 $importer->doImport();
217 } catch ( Exception
$e ) {
220 $result = $reporter->close();
223 # No source or XML parse error
225 "<p class=\"error\">\n$1\n</p>",
226 [ 'importfailed', $exception->getMessage() ]
228 } elseif ( !$result->isGood() ) {
231 "<p class=\"error\">\n$1\n</p>",
232 [ 'importfailed', $result->getWikiText() ]
236 $out->addWikiMsg( 'importsuccess' );
238 $out->addHTML( '<hr />' );
242 private function getMappingFormPart( $sourceName ) {
243 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
244 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
248 <td class='mw-input'>" .
250 $this->msg( 'import-mapping-default' )->text(),
253 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
254 "mw-import-mapping-$sourceName-default",
255 ( $isSameSourceAsBefore ?
256 ( $this->mapping
=== 'default' ) :
257 is_null( $defaultNamespace ) )
264 <td class='mw-input'>" .
266 $this->msg( 'import-mapping-namespace' )->text(),
269 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
270 "mw-import-mapping-$sourceName-namespace",
271 ( $isSameSourceAsBefore ?
272 ( $this->mapping
=== 'namespace' ) :
273 !is_null( $defaultNamespace ) )
275 Html
::namespaceSelector(
277 'selected' => ( $isSameSourceAsBefore ?
279 ( $defaultNamespace ||
'' ) ),
281 'name' => "namespace",
282 // mw-import-namespace-interwiki, mw-import-namespace-upload
283 'id' => "mw-import-namespace-$sourceName",
284 'class' => 'namespaceselector',
292 <td class='mw-input'>" .
294 $this->msg( 'import-mapping-subpage' )->text(),
297 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
298 "mw-import-mapping-$sourceName-subpage",
299 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
301 Xml
::input( 'rootpage', 50,
302 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
304 // Should be "mw-import-rootpage-...", but we keep this inaccurate
305 // ID for legacy reasons
306 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
307 'id' => "mw-interwiki-rootpage-$sourceName",
315 private function showForm() {
316 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
317 $user = $this->getUser();
318 $out = $this->getOutput();
319 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
321 if ( $user->isAllowed( 'importupload' ) ) {
322 $mappingSelection = $this->getMappingFormPart( 'upload' );
324 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
328 'enctype' => 'multipart/form-data',
331 'id' => 'mw-import-upload-form'
334 $this->msg( 'importtext' )->parseAsBlock() .
335 Html
::hidden( 'action', 'submit' ) .
336 Html
::hidden( 'source', 'upload' ) .
337 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
339 <td class='mw-label'>" .
340 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
342 <td class='mw-input'>" .
343 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
347 <td class='mw-label'>" .
348 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
350 <td class='mw-input'>" .
351 Xml
::input( 'log-comment', 50,
352 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
353 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
359 <td class='mw-submit'>" .
360 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
363 Xml
::closeElement( 'table' ) .
364 Html
::hidden( 'editToken', $user->getEditToken() ) .
365 Xml
::closeElement( 'form' ) .
366 Xml
::closeElement( 'fieldset' )
369 if ( empty( $this->importSources
) ) {
370 $out->addWikiMsg( 'importnosources' );
374 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
375 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
377 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
379 <td class='mw-label'>" .
380 $this->msg( 'export-pagelinks' )->parse() .
382 <td class='mw-input'>" .
383 Xml
::input( 'pagelink-depth', 3, 0 ) .
387 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
390 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
396 'id' => 'mw-import-interwiki-form'
399 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
400 Html
::hidden( 'action', 'submit' ) .
401 Html
::hidden( 'source', 'interwiki' ) .
402 Html
::hidden( 'editToken', $user->getEditToken() ) .
403 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
405 <td class='mw-label'>" .
406 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
408 <td class='mw-input'>" .
411 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
415 $needSubprojectField = false;
416 foreach ( $this->importSources
as $key => $value ) {
417 if ( is_int( $key ) ) {
419 } elseif ( $value !== $key ) {
420 $needSubprojectField = true;
426 if ( is_array( $value ) ) {
427 $attribs['data-subprojects'] = implode( ' ', $value );
429 if ( $this->interwiki
=== $key ) {
430 $attribs['selected'] = 'selected';
432 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
436 Xml
::closeElement( 'select' )
439 if ( $needSubprojectField ) {
443 [ 'name' => 'subproject', 'id' => 'subproject' ]
447 $subprojectsToAdd = [];
448 foreach ( $this->importSources
as $key => $value ) {
449 if ( is_array( $value ) ) {
450 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
453 $subprojectsToAdd = array_unique( $subprojectsToAdd );
454 sort( $subprojectsToAdd );
455 foreach ( $subprojectsToAdd as $subproject ) {
456 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
460 Xml
::closeElement( 'select' )
468 <td class='mw-label'>" .
469 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
471 <td class='mw-input'>" .
472 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
478 <td class='mw-input'>" .
480 $this->msg( 'import-interwiki-history' )->text(),
490 <td class='mw-input'>" .
492 $this->msg( 'import-interwiki-templates' )->text(),
493 'interwikiTemplates',
494 'interwikiTemplates',
495 $this->includeTemplates
501 <td class='mw-label'>" .
502 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
504 <td class='mw-input'>" .
505 Xml
::input( 'log-comment', 50,
506 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
507 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
514 <td class='mw-submit'>" .
516 $this->msg( 'import-interwiki-submit' )->text(),
517 Linker
::tooltipAndAccesskeyAttribs( 'import' )
521 Xml
::closeElement( 'table' ) .
522 Xml
::closeElement( 'form' ) .
523 Xml
::closeElement( 'fieldset' )
528 protected function getGroupName() {
535 * @ingroup SpecialPage
537 class ImportReporter
extends ContextSource
{
538 private $reason = false;
539 private $logTags = [];
540 private $mOriginalLogCallback = null;
541 private $mOriginalPageOutCallback = null;
542 private $mLogItemCount = 0;
545 * @param WikiImporter $importer
546 * @param bool $upload
547 * @param string $interwiki
548 * @param string|bool $reason
550 function __construct( $importer, $upload, $interwiki, $reason = false ) {
551 $this->mOriginalPageOutCallback
=
552 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
553 $this->mOriginalLogCallback
=
554 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
555 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
556 $this->mPageCount
= 0;
557 $this->mIsUpload
= $upload;
558 $this->mInterwiki
= $interwiki;
559 $this->reason
= $reason;
563 * Sets change tags to apply to the import log entry and null revision.
568 public function setChangeTags( array $tags ) {
569 $this->logTags
= $tags;
573 $this->getOutput()->addHTML( "<ul>\n" );
576 function reportNotice( $msg, array $params ) {
577 $this->getOutput()->addHTML(
578 Html
::element( 'li', [], $this->msg( $msg, $params )->text() )
582 function reportLogItem( /* ... */ ) {
583 $this->mLogItemCount++
;
584 if ( is_callable( $this->mOriginalLogCallback
) ) {
585 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
590 * @param Title $title
591 * @param ForeignTitle $foreignTitle
592 * @param int $revisionCount
593 * @param int $successCount
594 * @param array $pageInfo
597 public function reportPage( $title, $foreignTitle, $revisionCount,
598 $successCount, $pageInfo ) {
599 $args = func_get_args();
600 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
602 if ( $title === null ) {
603 # Invalid or non-importable title; a notice is already displayed
608 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
609 if ( $successCount > 0 ) {
610 // <bdi> prevents jumbling of the versions count
611 // in RTL wikis in case the page title is LTR
612 $this->getOutput()->addHTML(
613 "<li>" . $linkRenderer->makeLink( $title ) . " " .
615 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
620 $logParams = [ '4:number:count' => $successCount ];
621 if ( $this->mIsUpload
) {
622 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
623 $successCount )->inContentLanguage()->text();
626 $pageTitle = $foreignTitle->getFullText();
627 $fullInterwikiPrefix = $this->mInterwiki
;
628 Hooks
::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
630 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
631 $interwiki = '[[:' . $interwikiTitleStr . ']]';
632 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
633 $successCount )->params( $interwiki )->inContentLanguage()->text();
634 $action = 'interwiki';
635 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
637 if ( $this->reason
) {
638 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
642 $comment = $detail; // quick
643 $dbw = wfGetDB( DB_MASTER
);
644 $latest = $title->getLatestRevID();
645 $nullRevision = Revision
::newNullRevision(
647 $title->getArticleID(),
654 if ( !is_null( $nullRevision ) ) {
655 $nullRevId = $nullRevision->insertOn( $dbw );
656 $page = WikiPage
::factory( $title );
658 $page->updateRevisionOn( $dbw, $nullRevision );
660 'NewRevisionFromEditComplete',
661 [ $page, $nullRevision, $latest, $this->getUser() ]
665 // Create the import log entry
666 $logEntry = new ManualLogEntry( 'import', $action );
667 $logEntry->setTarget( $title );
668 $logEntry->setComment( $this->reason
);
669 $logEntry->setPerformer( $this->getUser() );
670 $logEntry->setParameters( $logParams );
671 $logid = $logEntry->insert();
672 if ( count( $this->logTags
) ) {
673 $logEntry->setTags( $this->logTags
);
675 // Make sure the null revision will be tagged as well
676 $logEntry->setAssociatedRevId( $nullRevId );
678 $logEntry->publish( $logid );
681 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
682 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
687 $out = $this->getOutput();
688 if ( $this->mLogItemCount
> 0 ) {
689 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
690 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
691 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
692 $out->addHTML( "</ul>\n" );
694 return Status
::newFatal( 'importnopages' );
696 $out->addHTML( "</ul>\n" );
698 return Status
::newGood( $this->mPageCount
);