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' );
50 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
55 * @param string|null $par
57 function execute( $par ) {
59 $this->outputHeader();
61 $this->getOutput()->addModules( 'mediawiki.special.import' );
63 $user = $this->getUser();
64 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
65 throw new PermissionsError( 'import' );
68 # @todo Allow Title::getUserPermissionsErrors() to take an array
69 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
70 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
71 $errors = wfMergeErrorArrays(
72 $this->getPageTitle()->getUserPermissionsErrors(
73 'import', $user, true,
74 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
76 $this->getPageTitle()->getUserPermissionsErrors(
77 'importupload', $user, true,
78 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
83 throw new PermissionsError( 'import', $errors );
86 $this->checkReadOnly();
88 $request = $this->getRequest();
89 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
96 * Do the actual import
98 private function doImport() {
100 $request = $this->getRequest();
101 $this->namespace = $request->getIntOrNull( 'namespace' );
102 $this->sourceName
= $request->getVal( "source" );
104 $this->logcomment
= $request->getText( 'log-comment' );
105 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
107 : $request->getIntOrNull( 'pagelink-depth' );
108 $this->rootpage
= $request->getText( 'rootpage' );
110 $user = $this->getUser();
111 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
112 $source = Status
::newFatal( 'import-token-mismatch' );
113 } elseif ( $this->sourceName
== 'upload' ) {
115 if ( $user->isAllowed( 'importupload' ) ) {
116 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
118 throw new PermissionsError( 'importupload' );
120 } elseif ( $this->sourceName
== "interwiki" ) {
121 if ( !$user->isAllowed( 'import' ) ) {
122 throw new PermissionsError( 'import' );
124 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
125 // does this interwiki have subprojects?
126 $importSources = $this->getConfig()->get( 'ImportSources' );
127 $hasSubprojects = array_key_exists( $this->interwiki
, $importSources );
128 if ( !$hasSubprojects && !in_array( $this->interwiki
, $importSources ) ) {
129 $source = Status
::newFatal( "import-invalid-interwiki" );
131 if ( $hasSubprojects ) {
132 $this->subproject
= $request->getVal( 'subproject' );
133 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
135 if ( $hasSubprojects && !in_array( $this->subproject
, $importSources[$this->interwiki
] ) ) {
136 $source = Status
::newFatal( "import-invalid-interwiki" );
138 $this->history
= $request->getCheck( 'interwikiHistory' );
139 $this->frompage
= $request->getText( "frompage" );
140 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
141 $source = ImportStreamSource
::newFromInterwiki(
142 $this->fullInterwikiPrefix
,
145 $this->includeTemplates
,
146 $this->pageLinkDepth
);
150 $source = Status
::newFatal( "importunknownsource" );
153 $out = $this->getOutput();
154 if ( !$source->isGood() ) {
156 "<p class=\"error\">\n$1\n</p>",
157 array( 'importfailed', $source->getWikiText() )
160 $importer = new WikiImporter( $source->value
);
161 if ( !is_null( $this->namespace ) ) {
162 $importer->setTargetNamespace( $this->namespace );
164 if ( !is_null( $this->rootpage
) ) {
165 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
166 if ( !$statusRootPage->isGood() ) {
168 "<p class=\"error\">\n$1\n</p>",
170 'import-options-wrong',
171 $statusRootPage->getWikiText(),
172 count( $statusRootPage->getErrorsArray() )
180 $out->addWikiMsg( "importstart" );
182 $reporter = new ImportReporter(
185 $this->fullInterwikiPrefix
,
188 $reporter->setContext( $this->getContext() );
193 $importer->doImport();
194 } catch ( MWException
$e ) {
197 $result = $reporter->close();
200 # No source or XML parse error
202 "<p class=\"error\">\n$1\n</p>",
203 array( 'importfailed', $exception->getMessage() )
205 } elseif ( !$result->isGood() ) {
208 "<p class=\"error\">\n$1\n</p>",
209 array( 'importfailed', $result->getWikiText() )
213 $out->addWikiMsg( 'importsuccess' );
215 $out->addHTML( '<hr />' );
219 private function showForm() {
220 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
221 $user = $this->getUser();
222 $out = $this->getOutput();
223 $importSources = $this->getConfig()->get( 'ImportSources' );
225 if ( $user->isAllowed( 'importupload' ) ) {
227 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
231 'enctype' => 'multipart/form-data',
234 'id' => 'mw-import-upload-form'
237 $this->msg( 'importtext' )->parseAsBlock() .
238 Html
::hidden( 'action', 'submit' ) .
239 Html
::hidden( 'source', 'upload' ) .
240 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
242 <td class='mw-label'>" .
243 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
245 <td class='mw-input'>" .
246 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
250 <td class='mw-label'>" .
251 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
253 <td class='mw-input'>" .
254 Xml
::input( 'log-comment', 50,
255 ( $this->sourceName
== 'upload' ?
$this->logcomment
: '' ),
256 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
260 <td class='mw-label'>" .
262 $this->msg( 'import-interwiki-rootpage' )->text(),
263 'mw-interwiki-rootpage-upload'
266 <td class='mw-input'>" .
267 Xml
::input( 'rootpage', 50, $this->rootpage
,
268 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
273 <td class='mw-submit'>" .
274 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
277 Xml
::closeElement( 'table' ) .
278 Html
::hidden( 'editToken', $user->getEditToken() ) .
279 Xml
::closeElement( 'form' ) .
280 Xml
::closeElement( 'fieldset' )
283 if ( empty( $importSources ) ) {
284 $out->addWikiMsg( 'importnosources' );
288 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
289 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
291 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
293 <td class='mw-label'>" .
294 $this->msg( 'export-pagelinks' )->parse() .
296 <td class='mw-input'>" .
297 Xml
::input( 'pagelink-depth', 3, 0 ) .
303 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
309 'id' => 'mw-import-interwiki-form'
312 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
313 Html
::hidden( 'action', 'submit' ) .
314 Html
::hidden( 'source', 'interwiki' ) .
315 Html
::hidden( 'editToken', $user->getEditToken() ) .
316 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
318 <td class='mw-label'>" .
319 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
321 <td class='mw-input'>" .
324 array( 'name' => 'interwiki', 'id' => 'interwiki' )
328 $needSubprojectField = false;
329 foreach ( $importSources as $key => $value ) {
330 if ( is_int( $key ) ) {
332 } elseif ( $value !== $key ) {
333 $needSubprojectField = true;
339 if ( is_array( $value ) ) {
340 $attribs['data-subprojects'] = implode( ' ', $value );
342 if ( $this->interwiki
=== $key ) {
343 $attribs['selected'] = 'selected';
345 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
349 Xml
::closeElement( 'select' )
352 if ( $needSubprojectField ) {
356 array( 'name' => 'subproject', 'id' => 'subproject' )
360 $subprojectsToAdd = array();
361 foreach ( $importSources as $key => $value ) {
362 if ( is_array( $value ) ) {
363 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
366 $subprojectsToAdd = array_unique( $subprojectsToAdd );
367 sort( $subprojectsToAdd );
368 foreach ( $subprojectsToAdd as $subproject ) {
369 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
373 Xml
::closeElement( 'select' )
381 <td class='mw-label'>" .
382 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
384 <td class='mw-input'>" .
385 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
391 <td class='mw-input'>" .
393 $this->msg( 'import-interwiki-history' )->text(),
403 <td class='mw-input'>" .
405 $this->msg( 'import-interwiki-templates' )->text(),
406 'interwikiTemplates',
407 'interwikiTemplates',
408 $this->includeTemplates
414 <td class='mw-label'>" .
415 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
417 <td class='mw-input'>" .
418 Html
::namespaceSelector(
420 'selected' => $this->namespace,
423 'name' => 'namespace',
425 'class' => 'namespaceselector',
431 <td class='mw-label'>" .
432 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
434 <td class='mw-input'>" .
435 Xml
::input( 'log-comment', 50,
436 ( $this->sourceName
== 'interwiki' ?
$this->logcomment
: '' ),
437 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
441 <td class='mw-label'>" .
443 $this->msg( 'import-interwiki-rootpage' )->text(),
444 'mw-interwiki-rootpage-interwiki'
447 <td class='mw-input'>" .
448 Xml
::input( 'rootpage', 50, $this->rootpage
,
449 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
455 <td class='mw-submit'>" .
457 $this->msg( 'import-interwiki-submit' )->text(),
458 Linker
::tooltipAndAccesskeyAttribs( 'import' )
462 Xml
::closeElement( 'table' ) .
463 Xml
::closeElement( 'form' ) .
464 Xml
::closeElement( 'fieldset' )
469 protected function getGroupName() {
476 * @ingroup SpecialPage
478 class ImportReporter
extends ContextSource
{
479 private $reason = false;
480 private $mOriginalLogCallback = null;
481 private $mOriginalPageOutCallback = null;
482 private $mLogItemCount = 0;
485 * @param WikiImporter $importer
486 * @param bool $upload
487 * @param string $interwiki
488 * @param string|bool $reason
490 function __construct( $importer, $upload, $interwiki, $reason = false ) {
491 $this->mOriginalPageOutCallback
=
492 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
493 $this->mOriginalLogCallback
=
494 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
495 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
496 $this->mPageCount
= 0;
497 $this->mIsUpload
= $upload;
498 $this->mInterwiki
= $interwiki;
499 $this->reason
= $reason;
503 $this->getOutput()->addHTML( "<ul>\n" );
506 function reportNotice( $msg, array $params ) {
507 $this->getOutput()->addHTML(
508 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
512 function reportLogItem( /* ... */ ) {
513 $this->mLogItemCount++
;
514 if ( is_callable( $this->mOriginalLogCallback
) ) {
515 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
520 * @param Title $title
521 * @param Title $origTitle
522 * @param int $revisionCount
523 * @param int $successCount
524 * @param array $pageInfo
527 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
528 $args = func_get_args();
529 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
531 if ( $title === null ) {
532 # Invalid or non-importable title; a notice is already displayed
538 if ( $successCount > 0 ) {
539 $this->getOutput()->addHTML(
540 "<li>" . Linker
::linkKnown( $title ) . " " .
541 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
545 $log = new LogPage( 'import' );
546 if ( $this->mIsUpload
) {
547 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
548 $successCount )->inContentLanguage()->text();
549 if ( $this->reason
) {
550 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
553 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
555 $interwiki = '[[:' . $this->mInterwiki
. ':' .
556 $origTitle->getPrefixedText() . ']]';
557 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
558 $successCount )->params( $interwiki )->inContentLanguage()->text();
559 if ( $this->reason
) {
560 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
563 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
566 $comment = $detail; // quick
567 $dbw = wfGetDB( DB_MASTER
);
568 $latest = $title->getLatestRevID();
569 $nullRevision = Revision
::newNullRevision(
571 $title->getArticleID(),
577 if ( !is_null( $nullRevision ) ) {
578 $nullRevision->insertOn( $dbw );
579 $page = WikiPage
::factory( $title );
581 $page->updateRevisionOn( $dbw, $nullRevision );
583 'NewRevisionFromEditComplete',
584 array( $page, $nullRevision, $latest, $this->getUser() )
588 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
589 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
594 $out = $this->getOutput();
595 if ( $this->mLogItemCount
> 0 ) {
596 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
597 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
598 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
599 $out->addHTML( "</ul>\n" );
601 return Status
::newFatal( 'importnopages' );
603 $out->addHTML( "</ul>\n" );
605 return Status
::newGood( $this->mPageCount
);