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;
38 private $rootpage = '';
39 private $frompage = '';
40 private $logcomment = false;
41 private $history = true;
42 private $includeTemplates = false;
43 private $pageLinkDepth;
48 public function __construct() {
49 parent
::__construct( 'Import', 'import' );
54 * @param string|null $par
56 function execute( $par ) {
58 $this->outputHeader();
60 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
62 $this->getOutput()->addModules( 'mediawiki.special.import' );
64 $user = $this->getUser();
65 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
66 throw new PermissionsError( 'import' );
69 # @todo Allow Title::getUserPermissionsErrors() to take an array
70 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
71 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
72 $errors = wfMergeErrorArrays(
73 $this->getPageTitle()->getUserPermissionsErrors(
74 'import', $user, true,
75 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
77 $this->getPageTitle()->getUserPermissionsErrors(
78 'importupload', $user, true,
79 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
84 throw new PermissionsError( 'import', $errors );
87 $this->checkReadOnly();
89 $request = $this->getRequest();
90 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
97 * Do the actual import
99 private function doImport() {
101 $request = $this->getRequest();
102 $this->namespace = $request->getIntOrNull( 'namespace' );
103 $this->sourceName
= $request->getVal( "source" );
105 $this->logcomment
= $request->getText( 'log-comment' );
106 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
108 : $request->getIntOrNull( 'pagelink-depth' );
109 $this->rootpage
= $request->getText( 'rootpage' );
111 $user = $this->getUser();
112 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
113 $source = Status
::newFatal( 'import-token-mismatch' );
114 } elseif ( $this->sourceName
== 'upload' ) {
116 if ( $user->isAllowed( 'importupload' ) ) {
117 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
119 throw new PermissionsError( 'importupload' );
121 } elseif ( $this->sourceName
== "interwiki" ) {
122 if ( !$user->isAllowed( 'import' ) ) {
123 throw new PermissionsError( 'import' );
125 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
126 // does this interwiki have subprojects?
127 $importSources = $this->getConfig()->get( 'ImportSources' );
128 $hasSubprojects = array_key_exists( $this->interwiki
, $importSources );
129 if ( !$hasSubprojects && !in_array( $this->interwiki
, $importSources ) ) {
130 $source = Status
::newFatal( "import-invalid-interwiki" );
132 if ( $hasSubprojects ) {
133 $this->subproject
= $request->getVal( 'subproject' );
134 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
136 if ( $hasSubprojects && !in_array( $this->subproject
, $importSources[$this->interwiki
] ) ) {
137 $source = Status
::newFatal( "import-invalid-interwiki" );
139 $this->history
= $request->getCheck( 'interwikiHistory' );
140 $this->frompage
= $request->getText( "frompage" );
141 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
142 $source = ImportStreamSource
::newFromInterwiki(
143 $this->fullInterwikiPrefix
,
146 $this->includeTemplates
,
147 $this->pageLinkDepth
);
151 $source = Status
::newFatal( "importunknownsource" );
154 $out = $this->getOutput();
155 if ( !$source->isGood() ) {
157 "<p class=\"error\">\n$1\n</p>",
158 array( 'importfailed', $source->getWikiText() )
161 $importer = new WikiImporter( $source->value
, $this->getConfig() );
162 if ( !is_null( $this->namespace ) ) {
163 $importer->setTargetNamespace( $this->namespace );
165 if ( !is_null( $this->rootpage
) ) {
166 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
167 if ( !$statusRootPage->isGood() ) {
169 "<p class=\"error\">\n$1\n</p>",
171 'import-options-wrong',
172 $statusRootPage->getWikiText(),
173 count( $statusRootPage->getErrorsArray() )
181 $out->addWikiMsg( "importstart" );
183 $reporter = new ImportReporter(
186 $this->fullInterwikiPrefix
,
189 $reporter->setContext( $this->getContext() );
194 $importer->doImport();
195 } catch ( MWException
$e ) {
198 $result = $reporter->close();
201 # No source or XML parse error
203 "<p class=\"error\">\n$1\n</p>",
204 array( 'importfailed', $exception->getMessage() )
206 } elseif ( !$result->isGood() ) {
209 "<p class=\"error\">\n$1\n</p>",
210 array( 'importfailed', $result->getWikiText() )
214 $out->addWikiMsg( 'importsuccess' );
216 $out->addHTML( '<hr />' );
220 private function showForm() {
221 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
222 $user = $this->getUser();
223 $out = $this->getOutput();
224 $importSources = $this->getConfig()->get( 'ImportSources' );
226 if ( $user->isAllowed( 'importupload' ) ) {
228 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
232 'enctype' => 'multipart/form-data',
235 'id' => 'mw-import-upload-form'
238 $this->msg( 'importtext' )->parseAsBlock() .
239 Html
::hidden( 'action', 'submit' ) .
240 Html
::hidden( 'source', 'upload' ) .
241 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
243 <td class='mw-label'>" .
244 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
246 <td class='mw-input'>" .
247 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
251 <td class='mw-label'>" .
252 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
254 <td class='mw-input'>" .
255 Xml
::input( 'log-comment', 50,
256 ( $this->sourceName
== 'upload' ?
$this->logcomment
: '' ),
257 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
261 <td class='mw-label'>" .
263 $this->msg( 'import-interwiki-rootpage' )->text(),
264 'mw-interwiki-rootpage-upload'
267 <td class='mw-input'>" .
268 Xml
::input( 'rootpage', 50, $this->rootpage
,
269 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
274 <td class='mw-submit'>" .
275 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
278 Xml
::closeElement( 'table' ) .
279 Html
::hidden( 'editToken', $user->getEditToken() ) .
280 Xml
::closeElement( 'form' ) .
281 Xml
::closeElement( 'fieldset' )
284 if ( empty( $importSources ) ) {
285 $out->addWikiMsg( 'importnosources' );
289 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
290 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
292 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
294 <td class='mw-label'>" .
295 $this->msg( 'export-pagelinks' )->parse() .
297 <td class='mw-input'>" .
298 Xml
::input( 'pagelink-depth', 3, 0 ) .
304 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
310 'id' => 'mw-import-interwiki-form'
313 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
314 Html
::hidden( 'action', 'submit' ) .
315 Html
::hidden( 'source', 'interwiki' ) .
316 Html
::hidden( 'editToken', $user->getEditToken() ) .
317 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
319 <td class='mw-label'>" .
320 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
322 <td class='mw-input'>" .
325 array( 'name' => 'interwiki', 'id' => 'interwiki' )
329 $needSubprojectField = false;
330 foreach ( $importSources as $key => $value ) {
331 if ( is_int( $key ) ) {
333 } elseif ( $value !== $key ) {
334 $needSubprojectField = true;
340 if ( is_array( $value ) ) {
341 $attribs['data-subprojects'] = implode( ' ', $value );
343 if ( $this->interwiki
=== $key ) {
344 $attribs['selected'] = 'selected';
346 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
350 Xml
::closeElement( 'select' )
353 if ( $needSubprojectField ) {
357 array( 'name' => 'subproject', 'id' => 'subproject' )
361 $subprojectsToAdd = array();
362 foreach ( $importSources as $key => $value ) {
363 if ( is_array( $value ) ) {
364 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
367 $subprojectsToAdd = array_unique( $subprojectsToAdd );
368 sort( $subprojectsToAdd );
369 foreach ( $subprojectsToAdd as $subproject ) {
370 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
374 Xml
::closeElement( 'select' )
382 <td class='mw-label'>" .
383 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
385 <td class='mw-input'>" .
386 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
392 <td class='mw-input'>" .
394 $this->msg( 'import-interwiki-history' )->text(),
404 <td class='mw-input'>" .
406 $this->msg( 'import-interwiki-templates' )->text(),
407 'interwikiTemplates',
408 'interwikiTemplates',
409 $this->includeTemplates
415 <td class='mw-label'>" .
416 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
418 <td class='mw-input'>" .
419 Html
::namespaceSelector(
421 'selected' => $this->namespace,
424 'name' => 'namespace',
426 'class' => 'namespaceselector',
432 <td class='mw-label'>" .
433 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
435 <td class='mw-input'>" .
436 Xml
::input( 'log-comment', 50,
437 ( $this->sourceName
== 'interwiki' ?
$this->logcomment
: '' ),
438 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
442 <td class='mw-label'>" .
444 $this->msg( 'import-interwiki-rootpage' )->text(),
445 'mw-interwiki-rootpage-interwiki'
448 <td class='mw-input'>" .
449 Xml
::input( 'rootpage', 50, $this->rootpage
,
450 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
456 <td class='mw-submit'>" .
458 $this->msg( 'import-interwiki-submit' )->text(),
459 Linker
::tooltipAndAccesskeyAttribs( 'import' )
463 Xml
::closeElement( 'table' ) .
464 Xml
::closeElement( 'form' ) .
465 Xml
::closeElement( 'fieldset' )
470 protected function getGroupName() {
477 * @ingroup SpecialPage
479 class ImportReporter
extends ContextSource
{
480 private $reason = false;
481 private $mOriginalLogCallback = null;
482 private $mOriginalPageOutCallback = null;
483 private $mLogItemCount = 0;
486 * @param WikiImporter $importer
487 * @param bool $upload
488 * @param string $interwiki
489 * @param string|bool $reason
491 function __construct( $importer, $upload, $interwiki, $reason = false ) {
492 $this->mOriginalPageOutCallback
=
493 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
494 $this->mOriginalLogCallback
=
495 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
496 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
497 $this->mPageCount
= 0;
498 $this->mIsUpload
= $upload;
499 $this->mInterwiki
= $interwiki;
500 $this->reason
= $reason;
504 $this->getOutput()->addHTML( "<ul>\n" );
507 function reportNotice( $msg, array $params ) {
508 $this->getOutput()->addHTML(
509 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
513 function reportLogItem( /* ... */ ) {
514 $this->mLogItemCount++
;
515 if ( is_callable( $this->mOriginalLogCallback
) ) {
516 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
521 * @param Title $title
522 * @param Title $origTitle
523 * @param int $revisionCount
524 * @param int $successCount
525 * @param array $pageInfo
528 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
529 $args = func_get_args();
530 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
532 if ( $title === null ) {
533 # Invalid or non-importable title; a notice is already displayed
539 if ( $successCount > 0 ) {
540 $this->getOutput()->addHTML(
541 "<li>" . Linker
::linkKnown( $title ) . " " .
542 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
546 $log = new LogPage( 'import' );
547 if ( $this->mIsUpload
) {
548 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
549 $successCount )->inContentLanguage()->text();
550 if ( $this->reason
) {
551 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
554 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
556 $interwiki = '[[:' . $this->mInterwiki
. ':' .
557 $origTitle->getPrefixedText() . ']]';
558 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
559 $successCount )->params( $interwiki )->inContentLanguage()->text();
560 if ( $this->reason
) {
561 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
564 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
567 $comment = $detail; // quick
568 $dbw = wfGetDB( DB_MASTER
);
569 $latest = $title->getLatestRevID();
570 $nullRevision = Revision
::newNullRevision(
572 $title->getArticleID(),
578 if ( !is_null( $nullRevision ) ) {
579 $nullRevision->insertOn( $dbw );
580 $page = WikiPage
::factory( $title );
582 $page->updateRevisionOn( $dbw, $nullRevision );
584 'NewRevisionFromEditComplete',
585 array( $page, $nullRevision, $latest, $this->getUser() )
589 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
590 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
595 $out = $this->getOutput();
596 if ( $this->mLogItemCount
> 0 ) {
597 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
598 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
599 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
600 $out->addHTML( "</ul>\n" );
602 return Status
::newFatal( 'importnopages' );
604 $out->addHTML( "</ul>\n" );
606 return Status
::newGood( $this->mPageCount
);