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() ) {
176 $out->addWikiText( "<p class=\"error\">\n" .
177 $this->msg( 'importfailed', $source->getWikiText() )->parse() . "\n</p>" );
179 $importer = new WikiImporter( $source->value
, $this->getConfig() );
180 if ( !is_null( $this->namespace ) ) {
181 $importer->setTargetNamespace( $this->namespace );
182 } elseif ( !is_null( $this->rootpage
) ) {
183 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
184 if ( !$statusRootPage->isGood() ) {
186 "<p class=\"error\">\n$1\n</p>",
188 'import-options-wrong',
189 $statusRootPage->getWikiText(),
190 count( $statusRootPage->getErrorsArray() )
198 $out->addWikiMsg( "importstart" );
200 $reporter = new ImportReporter(
203 $this->fullInterwikiPrefix
,
206 $reporter->setContext( $this->getContext() );
211 $importer->doImport();
212 } catch ( Exception
$e ) {
215 $result = $reporter->close();
218 # No source or XML parse error
220 "<p class=\"error\">\n$1\n</p>",
221 [ 'importfailed', $exception->getMessage() ]
223 } elseif ( !$result->isGood() ) {
226 "<p class=\"error\">\n$1\n</p>",
227 [ 'importfailed', $result->getWikiText() ]
231 $out->addWikiMsg( 'importsuccess' );
233 $out->addHTML( '<hr />' );
237 private function getMappingFormPart( $sourceName ) {
238 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
239 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
243 <td class='mw-input'>" .
245 $this->msg( 'import-mapping-default' )->text(),
248 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
249 "mw-import-mapping-$sourceName-default",
250 ( $isSameSourceAsBefore ?
251 ( $this->mapping
=== 'default' ) :
252 is_null( $defaultNamespace ) )
259 <td class='mw-input'>" .
261 $this->msg( 'import-mapping-namespace' )->text(),
264 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
265 "mw-import-mapping-$sourceName-namespace",
266 ( $isSameSourceAsBefore ?
267 ( $this->mapping
=== 'namespace' ) :
268 !is_null( $defaultNamespace ) )
270 Html
::namespaceSelector(
272 'selected' => ( $isSameSourceAsBefore ?
274 ( $defaultNamespace ||
'' ) ),
276 'name' => "namespace",
277 // mw-import-namespace-interwiki, mw-import-namespace-upload
278 'id' => "mw-import-namespace-$sourceName",
279 'class' => 'namespaceselector',
287 <td class='mw-input'>" .
289 $this->msg( 'import-mapping-subpage' )->text(),
292 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
293 "mw-import-mapping-$sourceName-subpage",
294 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
296 Xml
::input( 'rootpage', 50,
297 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
299 // Should be "mw-import-rootpage-...", but we keep this inaccurate
300 // ID for legacy reasons
301 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
302 'id' => "mw-interwiki-rootpage-$sourceName",
310 private function showForm() {
311 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
312 $user = $this->getUser();
313 $out = $this->getOutput();
314 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
316 if ( $user->isAllowed( 'importupload' ) ) {
317 $mappingSelection = $this->getMappingFormPart( 'upload' );
319 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
323 'enctype' => 'multipart/form-data',
326 'id' => 'mw-import-upload-form'
329 $this->msg( 'importtext' )->parseAsBlock() .
330 Html
::hidden( 'action', 'submit' ) .
331 Html
::hidden( 'source', 'upload' ) .
332 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
334 <td class='mw-label'>" .
335 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
337 <td class='mw-input'>" .
338 Html
::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
342 <td class='mw-label'>" .
343 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
345 <td class='mw-input'>" .
346 Xml
::input( 'log-comment', 50,
347 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
348 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
354 <td class='mw-submit'>" .
355 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
358 Xml
::closeElement( 'table' ) .
359 Html
::hidden( 'editToken', $user->getEditToken() ) .
360 Xml
::closeElement( 'form' ) .
361 Xml
::closeElement( 'fieldset' )
364 if ( empty( $this->importSources
) ) {
365 $out->addWikiMsg( 'importnosources' );
369 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
370 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
372 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
374 <td class='mw-label'>" .
375 $this->msg( 'export-pagelinks' )->parse() .
377 <td class='mw-input'>" .
378 Xml
::input( 'pagelink-depth', 3, 0 ) .
382 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
385 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
391 'id' => 'mw-import-interwiki-form'
394 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
395 Html
::hidden( 'action', 'submit' ) .
396 Html
::hidden( 'source', 'interwiki' ) .
397 Html
::hidden( 'editToken', $user->getEditToken() ) .
398 Xml
::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
400 <td class='mw-label'>" .
401 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
403 <td class='mw-input'>" .
406 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
410 $needSubprojectField = false;
411 foreach ( $this->importSources
as $key => $value ) {
412 if ( is_int( $key ) ) {
414 } elseif ( $value !== $key ) {
415 $needSubprojectField = true;
421 if ( is_array( $value ) ) {
422 $attribs['data-subprojects'] = implode( ' ', $value );
424 if ( $this->interwiki
=== $key ) {
425 $attribs['selected'] = 'selected';
427 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
431 Xml
::closeElement( 'select' )
434 if ( $needSubprojectField ) {
438 [ 'name' => 'subproject', 'id' => 'subproject' ]
442 $subprojectsToAdd = [];
443 foreach ( $this->importSources
as $key => $value ) {
444 if ( is_array( $value ) ) {
445 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
448 $subprojectsToAdd = array_unique( $subprojectsToAdd );
449 sort( $subprojectsToAdd );
450 foreach ( $subprojectsToAdd as $subproject ) {
451 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
455 Xml
::closeElement( 'select' )
463 <td class='mw-label'>" .
464 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
466 <td class='mw-input'>" .
467 Xml
::input( 'frompage', 50, $this->frompage
, [ 'id' => 'frompage' ] ) .
473 <td class='mw-input'>" .
475 $this->msg( 'import-interwiki-history' )->text(),
485 <td class='mw-input'>" .
487 $this->msg( 'import-interwiki-templates' )->text(),
488 'interwikiTemplates',
489 'interwikiTemplates',
490 $this->includeTemplates
496 <td class='mw-label'>" .
497 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
499 <td class='mw-input'>" .
500 Xml
::input( 'log-comment', 50,
501 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
502 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
509 <td class='mw-submit'>" .
511 $this->msg( 'import-interwiki-submit' )->text(),
512 Linker
::tooltipAndAccesskeyAttribs( 'import' )
516 Xml
::closeElement( 'table' ) .
517 Xml
::closeElement( 'form' ) .
518 Xml
::closeElement( 'fieldset' )
523 protected function getGroupName() {
530 * @ingroup SpecialPage
532 class ImportReporter
extends ContextSource
{
533 private $reason = false;
534 private $logTags = [];
535 private $mOriginalLogCallback = null;
536 private $mOriginalPageOutCallback = null;
537 private $mLogItemCount = 0;
540 * @param WikiImporter $importer
541 * @param bool $upload
542 * @param string $interwiki
543 * @param string|bool $reason
545 function __construct( $importer, $upload, $interwiki, $reason = false ) {
546 $this->mOriginalPageOutCallback
=
547 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
548 $this->mOriginalLogCallback
=
549 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
550 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
551 $this->mPageCount
= 0;
552 $this->mIsUpload
= $upload;
553 $this->mInterwiki
= $interwiki;
554 $this->reason
= $reason;
558 * Sets change tags to apply to the import log entry and null revision.
563 public function setChangeTags( array $tags ) {
564 $this->logTags
= $tags;
568 $this->getOutput()->addHTML( "<ul>\n" );
571 function reportNotice( $msg, array $params ) {
572 $this->getOutput()->addHTML(
573 Html
::element( 'li', [], $this->msg( $msg, $params )->text() )
577 function reportLogItem( /* ... */ ) {
578 $this->mLogItemCount++
;
579 if ( is_callable( $this->mOriginalLogCallback
) ) {
580 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
585 * @param Title $title
586 * @param ForeignTitle $foreignTitle
587 * @param int $revisionCount
588 * @param int $successCount
589 * @param array $pageInfo
592 public function reportPage( $title, $foreignTitle, $revisionCount,
593 $successCount, $pageInfo ) {
594 $args = func_get_args();
595 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
597 if ( $title === null ) {
598 # Invalid or non-importable title; a notice is already displayed
603 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
604 if ( $successCount > 0 ) {
605 // <bdi> prevents jumbling of the versions count
606 // in RTL wikis in case the page title is LTR
607 $this->getOutput()->addHTML(
608 "<li>" . $linkRenderer->makeLink( $title ) . " " .
610 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
615 $logParams = [ '4:number:count' => $successCount ];
616 if ( $this->mIsUpload
) {
617 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
618 $successCount )->inContentLanguage()->text();
621 $pageTitle = $foreignTitle->getFullText();
622 $fullInterwikiPrefix = $this->mInterwiki
;
623 Hooks
::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
625 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
626 $interwiki = '[[:' . $interwikiTitleStr . ']]';
627 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
628 $successCount )->params( $interwiki )->inContentLanguage()->text();
629 $action = 'interwiki';
630 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
632 if ( $this->reason
) {
633 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
637 $comment = $detail; // quick
638 $dbw = wfGetDB( DB_MASTER
);
639 $latest = $title->getLatestRevID();
640 $nullRevision = Revision
::newNullRevision(
642 $title->getArticleID(),
649 if ( !is_null( $nullRevision ) ) {
650 $nullRevId = $nullRevision->insertOn( $dbw );
651 $page = WikiPage
::factory( $title );
653 $page->updateRevisionOn( $dbw, $nullRevision );
655 'NewRevisionFromEditComplete',
656 [ $page, $nullRevision, $latest, $this->getUser() ]
660 // Create the import log entry
661 $logEntry = new ManualLogEntry( 'import', $action );
662 $logEntry->setTarget( $title );
663 $logEntry->setComment( $this->reason
);
664 $logEntry->setPerformer( $this->getUser() );
665 $logEntry->setParameters( $logParams );
666 $logid = $logEntry->insert();
667 if ( count( $this->logTags
) ) {
668 $logEntry->setTags( $this->logTags
);
670 // Make sure the null revision will be tagged as well
671 $logEntry->setAssociatedRevId( $nullRevId );
673 $logEntry->publish( $logid );
676 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
677 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
682 $out = $this->getOutput();
683 if ( $this->mLogItemCount
> 0 ) {
684 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
685 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
686 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
687 $out->addHTML( "</ul>\n" );
689 return Status
::newFatal( 'importnopages' );
691 $out->addHTML( "</ul>\n" );
693 return Status
::newGood( $this->mPageCount
);