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;
49 public function __construct() {
50 parent
::__construct( 'Import', 'import' );
55 * @param string|null $par
56 * @throws PermissionsError
57 * @throws ReadOnlyError
59 function execute( $par ) {
61 $this->outputHeader();
63 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
65 $this->getOutput()->addModules( 'mediawiki.special.import' );
67 $user = $this->getUser();
68 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
69 throw new PermissionsError( 'import' );
72 # @todo Allow Title::getUserPermissionsErrors() to take an array
73 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
74 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
75 $errors = wfMergeErrorArrays(
76 $this->getPageTitle()->getUserPermissionsErrors(
77 'import', $user, true,
78 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
80 $this->getPageTitle()->getUserPermissionsErrors(
81 'importupload', $user, true,
82 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
87 throw new PermissionsError( 'import', $errors );
90 $this->checkReadOnly();
92 $request = $this->getRequest();
93 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
100 * Do the actual import
102 private function doImport() {
104 $request = $this->getRequest();
105 $this->sourceName
= $request->getVal( "source" );
107 $this->logcomment
= $request->getText( 'log-comment' );
108 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
110 : $request->getIntOrNull( 'pagelink-depth' );
112 $this->mapping
= $request->getVal( 'mapping' );
113 if ( $this->mapping
=== 'namespace' ) {
114 $this->namespace = $request->getIntOrNull( 'namespace' );
115 } elseif ( $this->mapping
=== 'subpage' ) {
116 $this->rootpage
= $request->getText( 'rootpage' );
118 $this->mapping
= 'default';
121 $user = $this->getUser();
122 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
123 $source = Status
::newFatal( 'import-token-mismatch' );
124 } elseif ( $this->sourceName
=== 'upload' ) {
126 if ( $user->isAllowed( 'importupload' ) ) {
127 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
129 throw new PermissionsError( 'importupload' );
131 } elseif ( $this->sourceName
=== 'interwiki' ) {
132 if ( !$user->isAllowed( 'import' ) ) {
133 throw new PermissionsError( 'import' );
135 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
136 // does this interwiki have subprojects?
137 $importSources = $this->getConfig()->get( 'ImportSources' );
138 $hasSubprojects = array_key_exists( $this->interwiki
, $importSources );
139 if ( !$hasSubprojects && !in_array( $this->interwiki
, $importSources ) ) {
140 $source = Status
::newFatal( "import-invalid-interwiki" );
142 if ( $hasSubprojects ) {
143 $this->subproject
= $request->getVal( 'subproject' );
144 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
146 if ( $hasSubprojects && !in_array( $this->subproject
, $importSources[$this->interwiki
] ) ) {
147 $source = Status
::newFatal( "import-invalid-interwiki" );
149 $this->history
= $request->getCheck( 'interwikiHistory' );
150 $this->frompage
= $request->getText( "frompage" );
151 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
152 $source = ImportStreamSource
::newFromInterwiki(
153 $this->fullInterwikiPrefix
,
156 $this->includeTemplates
,
157 $this->pageLinkDepth
);
161 $source = Status
::newFatal( "importunknownsource" );
164 $out = $this->getOutput();
165 if ( !$source->isGood() ) {
167 "<p class=\"error\">\n$1\n</p>",
168 array( 'importfailed', $source->getWikiText() )
171 $importer = new WikiImporter( $source->value
, $this->getConfig() );
172 if ( !is_null( $this->namespace ) ) {
173 $importer->setTargetNamespace( $this->namespace );
174 } elseif ( !is_null( $this->rootpage
) ) {
175 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
176 if ( !$statusRootPage->isGood() ) {
178 "<p class=\"error\">\n$1\n</p>",
180 'import-options-wrong',
181 $statusRootPage->getWikiText(),
182 count( $statusRootPage->getErrorsArray() )
190 $out->addWikiMsg( "importstart" );
192 $reporter = new ImportReporter(
195 $this->fullInterwikiPrefix
,
198 $reporter->setContext( $this->getContext() );
203 $importer->doImport();
204 } catch ( Exception
$e ) {
207 $result = $reporter->close();
210 # No source or XML parse error
212 "<p class=\"error\">\n$1\n</p>",
213 array( 'importfailed', $exception->getMessage() )
215 } elseif ( !$result->isGood() ) {
218 "<p class=\"error\">\n$1\n</p>",
219 array( 'importfailed', $result->getWikiText() )
223 $out->addWikiMsg( 'importsuccess' );
225 $out->addHTML( '<hr />' );
229 private function getMappingFormPart( $sourceName ) {
230 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
231 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
235 <td class='mw-input'>" .
237 $this->msg( 'import-mapping-default' )->text(),
240 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
241 "mw-import-mapping-$sourceName-default",
242 ( $isSameSourceAsBefore ?
243 ( $this->mapping
=== 'default' ) :
244 is_null( $defaultNamespace ) )
251 <td class='mw-input'>" .
253 $this->msg( 'import-mapping-namespace' )->text(),
256 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
257 "mw-import-mapping-$sourceName-namespace",
258 ( $isSameSourceAsBefore ?
259 ( $this->mapping
=== 'namespace' ) :
260 !is_null( $defaultNamespace ) )
262 Html
::namespaceSelector(
264 'selected' => ( $isSameSourceAsBefore ?
266 ( $defaultNamespace ||
'' ) ),
268 'name' => "namespace",
269 // mw-import-namespace-interwiki, mw-import-namespace-upload
270 'id' => "mw-import-namespace-$sourceName",
271 'class' => 'namespaceselector',
279 <td class='mw-input'>" .
281 $this->msg( 'import-mapping-subpage' )->text(),
284 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
285 "mw-import-mapping-$sourceName-subpage",
286 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
288 Xml
::input( 'rootpage', 50,
289 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
291 // Should be "mw-import-rootpage-...", but we keep this inaccurate
292 // ID for legacy reasons
293 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
294 'id' => "mw-interwiki-rootpage-$sourceName",
302 private function showForm() {
303 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
304 $user = $this->getUser();
305 $out = $this->getOutput();
306 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
307 $importSources = $this->getConfig()->get( 'ImportSources' );
309 if ( $user->isAllowed( 'importupload' ) ) {
310 $mappingSelection = $this->getMappingFormPart( 'upload' );
312 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
316 'enctype' => 'multipart/form-data',
319 'id' => 'mw-import-upload-form'
322 $this->msg( 'importtext' )->parseAsBlock() .
323 Html
::hidden( 'action', 'submit' ) .
324 Html
::hidden( 'source', 'upload' ) .
325 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
327 <td class='mw-label'>" .
328 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
330 <td class='mw-input'>" .
331 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
335 <td class='mw-label'>" .
336 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
338 <td class='mw-input'>" .
339 Xml
::input( 'log-comment', 50,
340 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
341 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
347 <td class='mw-submit'>" .
348 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
351 Xml
::closeElement( 'table' ) .
352 Html
::hidden( 'editToken', $user->getEditToken() ) .
353 Xml
::closeElement( 'form' ) .
354 Xml
::closeElement( 'fieldset' )
357 if ( empty( $importSources ) ) {
358 $out->addWikiMsg( 'importnosources' );
362 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
363 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
365 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
367 <td class='mw-label'>" .
368 $this->msg( 'export-pagelinks' )->parse() .
370 <td class='mw-input'>" .
371 Xml
::input( 'pagelink-depth', 3, 0 ) .
375 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
378 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
384 'id' => 'mw-import-interwiki-form'
387 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
388 Html
::hidden( 'action', 'submit' ) .
389 Html
::hidden( 'source', 'interwiki' ) .
390 Html
::hidden( 'editToken', $user->getEditToken() ) .
391 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
393 <td class='mw-label'>" .
394 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
396 <td class='mw-input'>" .
399 array( 'name' => 'interwiki', 'id' => 'interwiki' )
403 $needSubprojectField = false;
404 foreach ( $importSources as $key => $value ) {
405 if ( is_int( $key ) ) {
407 } elseif ( $value !== $key ) {
408 $needSubprojectField = true;
414 if ( is_array( $value ) ) {
415 $attribs['data-subprojects'] = implode( ' ', $value );
417 if ( $this->interwiki
=== $key ) {
418 $attribs['selected'] = 'selected';
420 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
424 Xml
::closeElement( 'select' )
427 if ( $needSubprojectField ) {
431 array( 'name' => 'subproject', 'id' => 'subproject' )
435 $subprojectsToAdd = array();
436 foreach ( $importSources as $key => $value ) {
437 if ( is_array( $value ) ) {
438 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
441 $subprojectsToAdd = array_unique( $subprojectsToAdd );
442 sort( $subprojectsToAdd );
443 foreach ( $subprojectsToAdd as $subproject ) {
444 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
448 Xml
::closeElement( 'select' )
456 <td class='mw-label'>" .
457 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
459 <td class='mw-input'>" .
460 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
466 <td class='mw-input'>" .
468 $this->msg( 'import-interwiki-history' )->text(),
478 <td class='mw-input'>" .
480 $this->msg( 'import-interwiki-templates' )->text(),
481 'interwikiTemplates',
482 'interwikiTemplates',
483 $this->includeTemplates
489 <td class='mw-label'>" .
490 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
492 <td class='mw-input'>" .
493 Xml
::input( 'log-comment', 50,
494 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
495 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
502 <td class='mw-submit'>" .
504 $this->msg( 'import-interwiki-submit' )->text(),
505 Linker
::tooltipAndAccesskeyAttribs( 'import' )
509 Xml
::closeElement( 'table' ) .
510 Xml
::closeElement( 'form' ) .
511 Xml
::closeElement( 'fieldset' )
516 protected function getGroupName() {
523 * @ingroup SpecialPage
525 class ImportReporter
extends ContextSource
{
526 private $reason = false;
527 private $mOriginalLogCallback = null;
528 private $mOriginalPageOutCallback = null;
529 private $mLogItemCount = 0;
532 * @param WikiImporter $importer
533 * @param bool $upload
534 * @param string $interwiki
535 * @param string|bool $reason
537 function __construct( $importer, $upload, $interwiki, $reason = false ) {
538 $this->mOriginalPageOutCallback
=
539 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
540 $this->mOriginalLogCallback
=
541 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
542 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
543 $this->mPageCount
= 0;
544 $this->mIsUpload
= $upload;
545 $this->mInterwiki
= $interwiki;
546 $this->reason
= $reason;
550 $this->getOutput()->addHTML( "<ul>\n" );
553 function reportNotice( $msg, array $params ) {
554 $this->getOutput()->addHTML(
555 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
559 function reportLogItem( /* ... */ ) {
560 $this->mLogItemCount++
;
561 if ( is_callable( $this->mOriginalLogCallback
) ) {
562 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
567 * @param Title $title
568 * @param ForeignTitle $foreignTitle
569 * @param int $revisionCount
570 * @param int $successCount
571 * @param array $pageInfo
574 function reportPage( $title, $foreignTitle, $revisionCount,
575 $successCount, $pageInfo ) {
576 $args = func_get_args();
577 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
579 if ( $title === null ) {
580 # Invalid or non-importable title; a notice is already displayed
586 if ( $successCount > 0 ) {
587 $this->getOutput()->addHTML(
588 "<li>" . Linker
::linkKnown( $title ) . " " .
589 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
593 if ( $this->mIsUpload
) {
594 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
595 $successCount )->inContentLanguage()->text();
596 if ( $this->reason
) {
597 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
602 $interwiki = '[[:' . $this->mInterwiki
. ':' .
603 $foreignTitle->getFullText() . ']]';
604 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
605 $successCount )->params( $interwiki )->inContentLanguage()->text();
606 if ( $this->reason
) {
607 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
610 $action = 'interwiki';
613 $logEntry = new ManualLogEntry( 'import', $action );
614 $logEntry->setTarget( $title );
615 $logEntry->setComment( $detail );
616 $logEntry->setPerformer( $this->getUser() );
617 $logid = $logEntry->insert();
618 $logEntry->publish( $logid );
620 $comment = $detail; // quick
621 $dbw = wfGetDB( DB_MASTER
);
622 $latest = $title->getLatestRevID();
623 $nullRevision = Revision
::newNullRevision(
625 $title->getArticleID(),
631 if ( !is_null( $nullRevision ) ) {
632 $nullRevision->insertOn( $dbw );
633 $page = WikiPage
::factory( $title );
635 $page->updateRevisionOn( $dbw, $nullRevision );
637 'NewRevisionFromEditComplete',
638 array( $page, $nullRevision, $latest, $this->getUser() )
642 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
643 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
648 $out = $this->getOutput();
649 if ( $this->mLogItemCount
> 0 ) {
650 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
651 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
652 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
653 $out->addHTML( "</ul>\n" );
655 return Status
::newFatal( 'importnopages' );
657 $out->addHTML( "</ul>\n" );
659 return Status
::newGood( $this->mPageCount
);