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;
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 wierd 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" );
116 $this->logcomment
= $request->getText( 'log-comment' );
117 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
119 : $request->getIntOrNull( 'pagelink-depth' );
121 $this->mapping
= $request->getVal( 'mapping' );
122 if ( $this->mapping
=== 'namespace' ) {
123 $this->namespace = $request->getIntOrNull( 'namespace' );
124 } elseif ( $this->mapping
=== 'subpage' ) {
125 $this->rootpage
= $request->getText( 'rootpage' );
127 $this->mapping
= 'default';
130 $user = $this->getUser();
131 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
132 $source = Status
::newFatal( 'import-token-mismatch' );
133 } elseif ( $this->sourceName
=== 'upload' ) {
135 if ( $user->isAllowed( 'importupload' ) ) {
136 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
138 throw new PermissionsError( 'importupload' );
140 } elseif ( $this->sourceName
=== 'interwiki' ) {
141 if ( !$user->isAllowed( 'import' ) ) {
142 throw new PermissionsError( 'import' );
144 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
145 // does this interwiki have subprojects?
146 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
147 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
148 $source = Status
::newFatal( "import-invalid-interwiki" );
150 if ( $hasSubprojects ) {
151 $this->subproject
= $request->getVal( 'subproject' );
152 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
154 if ( $hasSubprojects &&
155 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
157 $source = Status
::newFatal( "import-invalid-interwiki" );
159 $this->history
= $request->getCheck( 'interwikiHistory' );
160 $this->frompage
= $request->getText( "frompage" );
161 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
162 $source = ImportStreamSource
::newFromInterwiki(
163 $this->fullInterwikiPrefix
,
166 $this->includeTemplates
,
167 $this->pageLinkDepth
);
171 $source = Status
::newFatal( "importunknownsource" );
174 $out = $this->getOutput();
175 if ( !$source->isGood() ) {
177 "<p class=\"error\">\n$1\n</p>",
178 [ 'importfailed', $source->getWikiText() ]
181 $importer = new WikiImporter( $source->value
, $this->getConfig() );
182 if ( !is_null( $this->namespace ) ) {
183 $importer->setTargetNamespace( $this->namespace );
184 } elseif ( !is_null( $this->rootpage
) ) {
185 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
186 if ( !$statusRootPage->isGood() ) {
188 "<p class=\"error\">\n$1\n</p>",
190 'import-options-wrong',
191 $statusRootPage->getWikiText(),
192 count( $statusRootPage->getErrorsArray() )
200 $out->addWikiMsg( "importstart" );
202 $reporter = new ImportReporter(
205 $this->fullInterwikiPrefix
,
208 $reporter->setContext( $this->getContext() );
213 $importer->doImport();
214 } catch ( Exception
$e ) {
217 $result = $reporter->close();
220 # No source or XML parse error
222 "<p class=\"error\">\n$1\n</p>",
223 [ 'importfailed', $exception->getMessage() ]
225 } elseif ( !$result->isGood() ) {
228 "<p class=\"error\">\n$1\n</p>",
229 [ 'importfailed', $result->getWikiText() ]
233 $out->addWikiMsg( 'importsuccess' );
235 $out->addHTML( '<hr />' );
239 private function getMappingFormPart( $sourceName ) {
240 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
241 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
245 <td class='mw-input'>" .
247 $this->msg( 'import-mapping-default' )->text(),
250 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
251 "mw-import-mapping-$sourceName-default",
252 ( $isSameSourceAsBefore ?
253 ( $this->mapping
=== 'default' ) :
254 is_null( $defaultNamespace ) )
261 <td class='mw-input'>" .
263 $this->msg( 'import-mapping-namespace' )->text(),
266 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
267 "mw-import-mapping-$sourceName-namespace",
268 ( $isSameSourceAsBefore ?
269 ( $this->mapping
=== 'namespace' ) :
270 !is_null( $defaultNamespace ) )
272 Html
::namespaceSelector(
274 'selected' => ( $isSameSourceAsBefore ?
276 ( $defaultNamespace ||
'' ) ),
278 'name' => "namespace",
279 // mw-import-namespace-interwiki, mw-import-namespace-upload
280 'id' => "mw-import-namespace-$sourceName",
281 'class' => 'namespaceselector',
289 <td class='mw-input'>" .
291 $this->msg( 'import-mapping-subpage' )->text(),
294 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
295 "mw-import-mapping-$sourceName-subpage",
296 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
298 Xml
::input( 'rootpage', 50,
299 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
301 // Should be "mw-import-rootpage-...", but we keep this inaccurate
302 // ID for legacy reasons
303 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
304 'id' => "mw-interwiki-rootpage-$sourceName",
312 private function showForm() {
313 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
314 $user = $this->getUser();
315 $out = $this->getOutput();
316 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
318 if ( $user->isAllowed( 'importupload' ) ) {
319 $mappingSelection = $this->getMappingFormPart( 'upload' );
321 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
325 'enctype' => 'multipart/form-data',
328 'id' => 'mw-import-upload-form'
331 $this->msg( 'importtext' )->parseAsBlock() .
332 Html
::hidden( 'action', 'submit' ) .
333 Html
::hidden( 'source', 'upload' ) .
334 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
336 <td class='mw-label'>" .
337 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
339 <td class='mw-input'>" .
340 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
344 <td class='mw-label'>" .
345 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
347 <td class='mw-input'>" .
348 Xml
::input( 'log-comment', 50,
349 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
350 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
356 <td class='mw-submit'>" .
357 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
360 Xml
::closeElement( 'table' ) .
361 Html
::hidden( 'editToken', $user->getEditToken() ) .
362 Xml
::closeElement( 'form' ) .
363 Xml
::closeElement( 'fieldset' )
366 if ( empty( $this->importSources
) ) {
367 $out->addWikiMsg( 'importnosources' );
371 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
372 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
374 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
376 <td class='mw-label'>" .
377 $this->msg( 'export-pagelinks' )->parse() .
379 <td class='mw-input'>" .
380 Xml
::input( 'pagelink-depth', 3, 0 ) .
384 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
387 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
393 'id' => 'mw-import-interwiki-form'
396 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
397 Html
::hidden( 'action', 'submit' ) .
398 Html
::hidden( 'source', 'interwiki' ) .
399 Html
::hidden( 'editToken', $user->getEditToken() ) .
400 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
402 <td class='mw-label'>" .
403 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
405 <td class='mw-input'>" .
408 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
412 $needSubprojectField = false;
413 foreach ( $this->importSources
as $key => $value ) {
414 if ( is_int( $key ) ) {
416 } elseif ( $value !== $key ) {
417 $needSubprojectField = true;
423 if ( is_array( $value ) ) {
424 $attribs['data-subprojects'] = implode( ' ', $value );
426 if ( $this->interwiki
=== $key ) {
427 $attribs['selected'] = 'selected';
429 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
433 Xml
::closeElement( 'select' )
436 if ( $needSubprojectField ) {
440 [ 'name' => 'subproject', 'id' => 'subproject' ]
444 $subprojectsToAdd = [];
445 foreach ( $this->importSources
as $key => $value ) {
446 if ( is_array( $value ) ) {
447 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
450 $subprojectsToAdd = array_unique( $subprojectsToAdd );
451 sort( $subprojectsToAdd );
452 foreach ( $subprojectsToAdd as $subproject ) {
453 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
457 Xml
::closeElement( 'select' )
465 <td class='mw-label'>" .
466 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
468 <td class='mw-input'>" .
469 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
475 <td class='mw-input'>" .
477 $this->msg( 'import-interwiki-history' )->text(),
487 <td class='mw-input'>" .
489 $this->msg( 'import-interwiki-templates' )->text(),
490 'interwikiTemplates',
491 'interwikiTemplates',
492 $this->includeTemplates
498 <td class='mw-label'>" .
499 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
501 <td class='mw-input'>" .
502 Xml
::input( 'log-comment', 50,
503 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
504 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
511 <td class='mw-submit'>" .
513 $this->msg( 'import-interwiki-submit' )->text(),
514 Linker
::tooltipAndAccesskeyAttribs( 'import' )
518 Xml
::closeElement( 'table' ) .
519 Xml
::closeElement( 'form' ) .
520 Xml
::closeElement( 'fieldset' )
525 protected function getGroupName() {
532 * @ingroup SpecialPage
534 class ImportReporter
extends ContextSource
{
535 private $reason = false;
536 private $logTags = [];
537 private $mOriginalLogCallback = null;
538 private $mOriginalPageOutCallback = null;
539 private $mLogItemCount = 0;
542 * @param WikiImporter $importer
543 * @param bool $upload
544 * @param string $interwiki
545 * @param string|bool $reason
547 function __construct( $importer, $upload, $interwiki, $reason = false ) {
548 $this->mOriginalPageOutCallback
=
549 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
550 $this->mOriginalLogCallback
=
551 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
552 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
553 $this->mPageCount
= 0;
554 $this->mIsUpload
= $upload;
555 $this->mInterwiki
= $interwiki;
556 $this->reason
= $reason;
560 * Sets change tags to apply to the import log entry and null revision.
565 public function setChangeTags( array $tags ) {
566 $this->logTags
= $tags;
570 $this->getOutput()->addHTML( "<ul>\n" );
573 function reportNotice( $msg, array $params ) {
574 $this->getOutput()->addHTML(
575 Html
::element( 'li', [], $this->msg( $msg, $params )->text() )
579 function reportLogItem( /* ... */ ) {
580 $this->mLogItemCount++
;
581 if ( is_callable( $this->mOriginalLogCallback
) ) {
582 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
587 * @param Title $title
588 * @param ForeignTitle $foreignTitle
589 * @param int $revisionCount
590 * @param int $successCount
591 * @param array $pageInfo
594 public function reportPage( $title, $foreignTitle, $revisionCount,
595 $successCount, $pageInfo ) {
596 $args = func_get_args();
597 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
599 if ( $title === null ) {
600 # Invalid or non-importable title; a notice is already displayed
605 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
606 if ( $successCount > 0 ) {
607 // <bdi> prevents jumbling of the versions count
608 // in RTL wikis in case the page title is LTR
609 $this->getOutput()->addHTML(
610 "<li>" . $linkRenderer->makeLink( $title ) . " " .
612 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
617 $logParams = [ '4:number:count' => $successCount ];
618 if ( $this->mIsUpload
) {
619 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
620 $successCount )->inContentLanguage()->text();
623 $pageTitle = $foreignTitle->getFullText();
624 $fullInterwikiPrefix = $this->mInterwiki
;
625 Hooks
::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
627 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
628 $interwiki = '[[:' . $interwikiTitleStr . ']]';
629 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
630 $successCount )->params( $interwiki )->inContentLanguage()->text();
631 $action = 'interwiki';
632 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
634 if ( $this->reason
) {
635 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
639 $comment = $detail; // quick
640 $dbw = wfGetDB( DB_MASTER
);
641 $latest = $title->getLatestRevID();
642 $nullRevision = Revision
::newNullRevision(
644 $title->getArticleID(),
651 if ( !is_null( $nullRevision ) ) {
652 $nullRevId = $nullRevision->insertOn( $dbw );
653 $page = WikiPage
::factory( $title );
655 $page->updateRevisionOn( $dbw, $nullRevision );
657 'NewRevisionFromEditComplete',
658 [ $page, $nullRevision, $latest, $this->getUser() ]
662 // Create the import log entry
663 $logEntry = new ManualLogEntry( 'import', $action );
664 $logEntry->setTarget( $title );
665 $logEntry->setComment( $this->reason
);
666 $logEntry->setPerformer( $this->getUser() );
667 $logEntry->setParameters( $logParams );
668 $logid = $logEntry->insert();
669 if ( count( $this->logTags
) ) {
670 $logEntry->setTags( $this->logTags
);
672 // Make sure the null revision will be tagged as well
673 $logEntry->setAssociatedRevId( $nullRevId );
675 $logEntry->publish( $logid );
678 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
679 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
684 $out = $this->getOutput();
685 if ( $this->mLogItemCount
> 0 ) {
686 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
687 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
688 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
689 $out->addHTML( "</ul>\n" );
691 return Status
::newFatal( 'importnopages' );
693 $out->addHTML( "</ul>\n" );
695 return Status
::newGood( $this->mPageCount
);