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;
50 public function __construct() {
51 parent
::__construct( 'Import', 'import' );
56 * @param string|null $par
57 * @throws PermissionsError
58 * @throws ReadOnlyError
60 function execute( $par ) {
61 $this->useTransactionalTimeLimit();
64 $this->outputHeader();
66 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
68 $this->getOutput()->addModules( 'mediawiki.special.import' );
70 $this->importSources
= $this->getConfig()->get( 'ImportSources' );
71 Hooks
::run( 'ImportSources', array( &$this->importSources
) );
73 $user = $this->getUser();
74 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
75 throw new PermissionsError( 'import' );
78 # @todo Allow Title::getUserPermissionsErrors() to take an array
79 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
80 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
81 $errors = wfMergeErrorArrays(
82 $this->getPageTitle()->getUserPermissionsErrors(
83 'import', $user, true,
84 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
86 $this->getPageTitle()->getUserPermissionsErrors(
87 'importupload', $user, true,
88 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
93 throw new PermissionsError( 'import', $errors );
96 $this->checkReadOnly();
98 $request = $this->getRequest();
99 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
106 * Do the actual import
108 private function doImport() {
110 $request = $this->getRequest();
111 $this->sourceName
= $request->getVal( "source" );
113 $this->logcomment
= $request->getText( 'log-comment' );
114 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
116 : $request->getIntOrNull( 'pagelink-depth' );
118 $this->mapping
= $request->getVal( 'mapping' );
119 if ( $this->mapping
=== 'namespace' ) {
120 $this->namespace = $request->getIntOrNull( 'namespace' );
121 } elseif ( $this->mapping
=== 'subpage' ) {
122 $this->rootpage
= $request->getText( 'rootpage' );
124 $this->mapping
= 'default';
127 $user = $this->getUser();
128 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
129 $source = Status
::newFatal( 'import-token-mismatch' );
130 } elseif ( $this->sourceName
=== 'upload' ) {
132 if ( $user->isAllowed( 'importupload' ) ) {
133 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
135 throw new PermissionsError( 'importupload' );
137 } elseif ( $this->sourceName
=== 'interwiki' ) {
138 if ( !$user->isAllowed( 'import' ) ) {
139 throw new PermissionsError( 'import' );
141 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
142 // does this interwiki have subprojects?
143 $hasSubprojects = array_key_exists( $this->interwiki
, $this->importSources
);
144 if ( !$hasSubprojects && !in_array( $this->interwiki
, $this->importSources
) ) {
145 $source = Status
::newFatal( "import-invalid-interwiki" );
147 if ( $hasSubprojects ) {
148 $this->subproject
= $request->getVal( 'subproject' );
149 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
151 if ( $hasSubprojects &&
152 !in_array( $this->subproject
, $this->importSources
[$this->interwiki
] )
154 $source = Status
::newFatal( "import-invalid-interwiki" );
156 $this->history
= $request->getCheck( 'interwikiHistory' );
157 $this->frompage
= $request->getText( "frompage" );
158 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
159 $source = ImportStreamSource
::newFromInterwiki(
160 $this->fullInterwikiPrefix
,
163 $this->includeTemplates
,
164 $this->pageLinkDepth
);
168 $source = Status
::newFatal( "importunknownsource" );
171 $out = $this->getOutput();
172 if ( !$source->isGood() ) {
174 "<p class=\"error\">\n$1\n</p>",
175 array( 'importfailed', $source->getWikiText() )
178 $importer = new WikiImporter( $source->value
, $this->getConfig() );
179 if ( !is_null( $this->namespace ) ) {
180 $importer->setTargetNamespace( $this->namespace );
181 } elseif ( !is_null( $this->rootpage
) ) {
182 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
183 if ( !$statusRootPage->isGood() ) {
185 "<p class=\"error\">\n$1\n</p>",
187 'import-options-wrong',
188 $statusRootPage->getWikiText(),
189 count( $statusRootPage->getErrorsArray() )
197 $out->addWikiMsg( "importstart" );
199 $reporter = new ImportReporter(
202 $this->fullInterwikiPrefix
,
205 $reporter->setContext( $this->getContext() );
210 $importer->doImport();
211 } catch ( Exception
$e ) {
214 $result = $reporter->close();
217 # No source or XML parse error
219 "<p class=\"error\">\n$1\n</p>",
220 array( 'importfailed', $exception->getMessage() )
222 } elseif ( !$result->isGood() ) {
225 "<p class=\"error\">\n$1\n</p>",
226 array( 'importfailed', $result->getWikiText() )
230 $out->addWikiMsg( 'importsuccess' );
232 $out->addHTML( '<hr />' );
236 private function getMappingFormPart( $sourceName ) {
237 $isSameSourceAsBefore = ( $this->sourceName
=== $sourceName );
238 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
242 <td class='mw-input'>" .
244 $this->msg( 'import-mapping-default' )->text(),
247 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
248 "mw-import-mapping-$sourceName-default",
249 ( $isSameSourceAsBefore ?
250 ( $this->mapping
=== 'default' ) :
251 is_null( $defaultNamespace ) )
258 <td class='mw-input'>" .
260 $this->msg( 'import-mapping-namespace' )->text(),
263 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
264 "mw-import-mapping-$sourceName-namespace",
265 ( $isSameSourceAsBefore ?
266 ( $this->mapping
=== 'namespace' ) :
267 !is_null( $defaultNamespace ) )
269 Html
::namespaceSelector(
271 'selected' => ( $isSameSourceAsBefore ?
273 ( $defaultNamespace ||
'' ) ),
275 'name' => "namespace",
276 // mw-import-namespace-interwiki, mw-import-namespace-upload
277 'id' => "mw-import-namespace-$sourceName",
278 'class' => 'namespaceselector',
286 <td class='mw-input'>" .
288 $this->msg( 'import-mapping-subpage' )->text(),
291 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
292 "mw-import-mapping-$sourceName-subpage",
293 ( $isSameSourceAsBefore ?
( $this->mapping
=== 'subpage' ) : '' )
295 Xml
::input( 'rootpage', 50,
296 ( $isSameSourceAsBefore ?
$this->rootpage
: '' ),
298 // Should be "mw-import-rootpage-...", but we keep this inaccurate
299 // ID for legacy reasons
300 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
301 'id' => "mw-interwiki-rootpage-$sourceName",
309 private function showForm() {
310 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
311 $user = $this->getUser();
312 $out = $this->getOutput();
313 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
315 if ( $user->isAllowed( 'importupload' ) ) {
316 $mappingSelection = $this->getMappingFormPart( 'upload' );
318 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
322 'enctype' => 'multipart/form-data',
325 'id' => 'mw-import-upload-form'
328 $this->msg( 'importtext' )->parseAsBlock() .
329 Html
::hidden( 'action', 'submit' ) .
330 Html
::hidden( 'source', 'upload' ) .
331 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
333 <td class='mw-label'>" .
334 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
336 <td class='mw-input'>" .
337 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
341 <td class='mw-label'>" .
342 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
344 <td class='mw-input'>" .
345 Xml
::input( 'log-comment', 50,
346 ( $this->sourceName
=== 'upload' ?
$this->logcomment
: '' ),
347 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
353 <td class='mw-submit'>" .
354 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
357 Xml
::closeElement( 'table' ) .
358 Html
::hidden( 'editToken', $user->getEditToken() ) .
359 Xml
::closeElement( 'form' ) .
360 Xml
::closeElement( 'fieldset' )
363 if ( empty( $this->importSources
) ) {
364 $out->addWikiMsg( 'importnosources' );
368 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources
) ) {
369 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
371 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
373 <td class='mw-label'>" .
374 $this->msg( 'export-pagelinks' )->parse() .
376 <td class='mw-input'>" .
377 Xml
::input( 'pagelink-depth', 3, 0 ) .
381 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
384 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
390 'id' => 'mw-import-interwiki-form'
393 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
394 Html
::hidden( 'action', 'submit' ) .
395 Html
::hidden( 'source', 'interwiki' ) .
396 Html
::hidden( 'editToken', $user->getEditToken() ) .
397 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
399 <td class='mw-label'>" .
400 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
402 <td class='mw-input'>" .
405 array( 'name' => 'interwiki', 'id' => 'interwiki' )
409 $needSubprojectField = false;
410 foreach ( $this->importSources
as $key => $value ) {
411 if ( is_int( $key ) ) {
413 } elseif ( $value !== $key ) {
414 $needSubprojectField = true;
420 if ( is_array( $value ) ) {
421 $attribs['data-subprojects'] = implode( ' ', $value );
423 if ( $this->interwiki
=== $key ) {
424 $attribs['selected'] = 'selected';
426 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
430 Xml
::closeElement( 'select' )
433 if ( $needSubprojectField ) {
437 array( 'name' => 'subproject', 'id' => 'subproject' )
441 $subprojectsToAdd = array();
442 foreach ( $this->importSources
as $key => $value ) {
443 if ( is_array( $value ) ) {
444 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
447 $subprojectsToAdd = array_unique( $subprojectsToAdd );
448 sort( $subprojectsToAdd );
449 foreach ( $subprojectsToAdd as $subproject ) {
450 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
454 Xml
::closeElement( 'select' )
462 <td class='mw-label'>" .
463 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
465 <td class='mw-input'>" .
466 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
472 <td class='mw-input'>" .
474 $this->msg( 'import-interwiki-history' )->text(),
484 <td class='mw-input'>" .
486 $this->msg( 'import-interwiki-templates' )->text(),
487 'interwikiTemplates',
488 'interwikiTemplates',
489 $this->includeTemplates
495 <td class='mw-label'>" .
496 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
498 <td class='mw-input'>" .
499 Xml
::input( 'log-comment', 50,
500 ( $this->sourceName
=== 'interwiki' ?
$this->logcomment
: '' ),
501 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
508 <td class='mw-submit'>" .
510 $this->msg( 'import-interwiki-submit' )->text(),
511 Linker
::tooltipAndAccesskeyAttribs( 'import' )
515 Xml
::closeElement( 'table' ) .
516 Xml
::closeElement( 'form' ) .
517 Xml
::closeElement( 'fieldset' )
522 protected function getGroupName() {
529 * @ingroup SpecialPage
531 class ImportReporter
extends ContextSource
{
532 private $reason = false;
533 private $mOriginalLogCallback = null;
534 private $mOriginalPageOutCallback = null;
535 private $mLogItemCount = 0;
538 * @param WikiImporter $importer
539 * @param bool $upload
540 * @param string $interwiki
541 * @param string|bool $reason
543 function __construct( $importer, $upload, $interwiki, $reason = false ) {
544 $this->mOriginalPageOutCallback
=
545 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
546 $this->mOriginalLogCallback
=
547 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
548 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
549 $this->mPageCount
= 0;
550 $this->mIsUpload
= $upload;
551 $this->mInterwiki
= $interwiki;
552 $this->reason
= $reason;
556 $this->getOutput()->addHTML( "<ul>\n" );
559 function reportNotice( $msg, array $params ) {
560 $this->getOutput()->addHTML(
561 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
565 function reportLogItem( /* ... */ ) {
566 $this->mLogItemCount++
;
567 if ( is_callable( $this->mOriginalLogCallback
) ) {
568 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
573 * @param Title $title
574 * @param ForeignTitle $foreignTitle
575 * @param int $revisionCount
576 * @param int $successCount
577 * @param array $pageInfo
580 function reportPage( $title, $foreignTitle, $revisionCount,
581 $successCount, $pageInfo ) {
582 $args = func_get_args();
583 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
585 if ( $title === null ) {
586 # Invalid or non-importable title; a notice is already displayed
592 if ( $successCount > 0 ) {
593 $this->getOutput()->addHTML(
594 "<li>" . Linker
::linkKnown( $title ) . " " .
595 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
599 $logParams = array( '4:number:count' => $successCount );
600 if ( $this->mIsUpload
) {
601 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
602 $successCount )->inContentLanguage()->text();
605 $interwikiTitleStr = $this->mInterwiki
. ':' . $foreignTitle->getFullText();
606 $interwiki = '[[:' . $interwikiTitleStr . ']]';
607 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
608 $successCount )->params( $interwiki )->inContentLanguage()->text();
609 $action = 'interwiki';
610 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
612 if ( $this->reason
) {
613 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
617 $logEntry = new ManualLogEntry( 'import', $action );
618 $logEntry->setTarget( $title );
619 $logEntry->setComment( $this->reason
);
620 $logEntry->setPerformer( $this->getUser() );
621 $logEntry->setParameters( $logParams );
622 $logid = $logEntry->insert();
623 $logEntry->publish( $logid );
625 $comment = $detail; // quick
626 $dbw = wfGetDB( DB_MASTER
);
627 $latest = $title->getLatestRevID();
628 $nullRevision = Revision
::newNullRevision(
630 $title->getArticleID(),
636 if ( !is_null( $nullRevision ) ) {
637 $nullRevision->insertOn( $dbw );
638 $page = WikiPage
::factory( $title );
640 $page->updateRevisionOn( $dbw, $nullRevision );
642 'NewRevisionFromEditComplete',
643 array( $page, $nullRevision, $latest, $this->getUser() )
647 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
648 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
653 $out = $this->getOutput();
654 if ( $this->mLogItemCount
> 0 ) {
655 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
656 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
657 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
658 $out->addHTML( "</ul>\n" );
660 return Status
::newFatal( 'importnopages' );
662 $out->addHTML( "</ul>\n" );
664 return Status
::newGood( $this->mPageCount
);