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
55 * @throws PermissionsError
56 * @throws ReadOnlyError
58 function execute( $par ) {
60 $this->outputHeader();
62 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
64 $this->getOutput()->addModules( 'mediawiki.special.import' );
66 $user = $this->getUser();
67 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
68 throw new PermissionsError( 'import' );
71 # @todo Allow Title::getUserPermissionsErrors() to take an array
72 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
73 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
74 $errors = wfMergeErrorArrays(
75 $this->getPageTitle()->getUserPermissionsErrors(
76 'import', $user, true,
77 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
79 $this->getPageTitle()->getUserPermissionsErrors(
80 'importupload', $user, true,
81 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
86 throw new PermissionsError( 'import', $errors );
89 $this->checkReadOnly();
91 $request = $this->getRequest();
92 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
99 * Do the actual import
101 private function doImport() {
103 $request = $this->getRequest();
104 $this->namespace = $request->getIntOrNull( 'namespace' );
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' );
111 $this->rootpage
= $request->getText( 'rootpage' );
113 $user = $this->getUser();
114 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
115 $source = Status
::newFatal( 'import-token-mismatch' );
116 } elseif ( $this->sourceName
== 'upload' ) {
118 if ( $user->isAllowed( 'importupload' ) ) {
119 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
121 throw new PermissionsError( 'importupload' );
123 } elseif ( $this->sourceName
== "interwiki" ) {
124 if ( !$user->isAllowed( 'import' ) ) {
125 throw new PermissionsError( 'import' );
127 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
128 // does this interwiki have subprojects?
129 $importSources = $this->getConfig()->get( 'ImportSources' );
130 $hasSubprojects = array_key_exists( $this->interwiki
, $importSources );
131 if ( !$hasSubprojects && !in_array( $this->interwiki
, $importSources ) ) {
132 $source = Status
::newFatal( "import-invalid-interwiki" );
134 if ( $hasSubprojects ) {
135 $this->subproject
= $request->getVal( 'subproject' );
136 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
138 if ( $hasSubprojects && !in_array( $this->subproject
, $importSources[$this->interwiki
] ) ) {
139 $source = Status
::newFatal( "import-invalid-interwiki" );
141 $this->history
= $request->getCheck( 'interwikiHistory' );
142 $this->frompage
= $request->getText( "frompage" );
143 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
144 $source = ImportStreamSource
::newFromInterwiki(
145 $this->fullInterwikiPrefix
,
148 $this->includeTemplates
,
149 $this->pageLinkDepth
);
153 $source = Status
::newFatal( "importunknownsource" );
156 $out = $this->getOutput();
157 if ( !$source->isGood() ) {
159 "<p class=\"error\">\n$1\n</p>",
160 array( 'importfailed', $source->getWikiText() )
163 $importer = new WikiImporter( $source->value
, $this->getConfig() );
164 if ( !is_null( $this->namespace ) ) {
165 $importer->setTargetNamespace( $this->namespace );
167 if ( !is_null( $this->rootpage
) ) {
168 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
169 if ( !$statusRootPage->isGood() ) {
171 "<p class=\"error\">\n$1\n</p>",
173 'import-options-wrong',
174 $statusRootPage->getWikiText(),
175 count( $statusRootPage->getErrorsArray() )
183 $out->addWikiMsg( "importstart" );
185 $reporter = new ImportReporter(
188 $this->fullInterwikiPrefix
,
191 $reporter->setContext( $this->getContext() );
196 $importer->doImport();
197 } catch ( Exception
$e ) {
200 $result = $reporter->close();
203 # No source or XML parse error
205 "<p class=\"error\">\n$1\n</p>",
206 array( 'importfailed', $exception->getMessage() )
208 } elseif ( !$result->isGood() ) {
211 "<p class=\"error\">\n$1\n</p>",
212 array( 'importfailed', $result->getWikiText() )
216 $out->addWikiMsg( 'importsuccess' );
218 $out->addHTML( '<hr />' );
222 private function showForm() {
223 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
224 $user = $this->getUser();
225 $out = $this->getOutput();
226 $importSources = $this->getConfig()->get( 'ImportSources' );
228 if ( $user->isAllowed( 'importupload' ) ) {
230 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
234 'enctype' => 'multipart/form-data',
237 'id' => 'mw-import-upload-form'
240 $this->msg( 'importtext' )->parseAsBlock() .
241 Html
::hidden( 'action', 'submit' ) .
242 Html
::hidden( 'source', 'upload' ) .
243 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
245 <td class='mw-label'>" .
246 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
248 <td class='mw-input'>" .
249 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
253 <td class='mw-label'>" .
254 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
256 <td class='mw-input'>" .
257 Xml
::input( 'log-comment', 50,
258 ( $this->sourceName
== 'upload' ?
$this->logcomment
: '' ),
259 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
263 <td class='mw-label'>" .
265 $this->msg( 'import-interwiki-rootpage' )->text(),
266 'mw-interwiki-rootpage-upload'
269 <td class='mw-input'>" .
270 Xml
::input( 'rootpage', 50, $this->rootpage
,
271 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
276 <td class='mw-submit'>" .
277 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
280 Xml
::closeElement( 'table' ) .
281 Html
::hidden( 'editToken', $user->getEditToken() ) .
282 Xml
::closeElement( 'form' ) .
283 Xml
::closeElement( 'fieldset' )
286 if ( empty( $importSources ) ) {
287 $out->addWikiMsg( 'importnosources' );
291 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
292 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
294 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
296 <td class='mw-label'>" .
297 $this->msg( 'export-pagelinks' )->parse() .
299 <td class='mw-input'>" .
300 Xml
::input( 'pagelink-depth', 3, 0 ) .
306 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
312 'id' => 'mw-import-interwiki-form'
315 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
316 Html
::hidden( 'action', 'submit' ) .
317 Html
::hidden( 'source', 'interwiki' ) .
318 Html
::hidden( 'editToken', $user->getEditToken() ) .
319 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
321 <td class='mw-label'>" .
322 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
324 <td class='mw-input'>" .
327 array( 'name' => 'interwiki', 'id' => 'interwiki' )
331 $needSubprojectField = false;
332 foreach ( $importSources as $key => $value ) {
333 if ( is_int( $key ) ) {
335 } elseif ( $value !== $key ) {
336 $needSubprojectField = true;
342 if ( is_array( $value ) ) {
343 $attribs['data-subprojects'] = implode( ' ', $value );
345 if ( $this->interwiki
=== $key ) {
346 $attribs['selected'] = 'selected';
348 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
352 Xml
::closeElement( 'select' )
355 if ( $needSubprojectField ) {
359 array( 'name' => 'subproject', 'id' => 'subproject' )
363 $subprojectsToAdd = array();
364 foreach ( $importSources as $key => $value ) {
365 if ( is_array( $value ) ) {
366 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
369 $subprojectsToAdd = array_unique( $subprojectsToAdd );
370 sort( $subprojectsToAdd );
371 foreach ( $subprojectsToAdd as $subproject ) {
372 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
376 Xml
::closeElement( 'select' )
384 <td class='mw-label'>" .
385 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
387 <td class='mw-input'>" .
388 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
394 <td class='mw-input'>" .
396 $this->msg( 'import-interwiki-history' )->text(),
406 <td class='mw-input'>" .
408 $this->msg( 'import-interwiki-templates' )->text(),
409 'interwikiTemplates',
410 'interwikiTemplates',
411 $this->includeTemplates
417 <td class='mw-label'>" .
418 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
420 <td class='mw-input'>" .
421 Html
::namespaceSelector(
423 'selected' => $this->namespace,
426 'name' => 'namespace',
428 'class' => 'namespaceselector',
434 <td class='mw-label'>" .
435 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
437 <td class='mw-input'>" .
438 Xml
::input( 'log-comment', 50,
439 ( $this->sourceName
== 'interwiki' ?
$this->logcomment
: '' ),
440 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
444 <td class='mw-label'>" .
446 $this->msg( 'import-interwiki-rootpage' )->text(),
447 'mw-interwiki-rootpage-interwiki'
450 <td class='mw-input'>" .
451 Xml
::input( 'rootpage', 50, $this->rootpage
,
452 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
458 <td class='mw-submit'>" .
460 $this->msg( 'import-interwiki-submit' )->text(),
461 Linker
::tooltipAndAccesskeyAttribs( 'import' )
465 Xml
::closeElement( 'table' ) .
466 Xml
::closeElement( 'form' ) .
467 Xml
::closeElement( 'fieldset' )
472 protected function getGroupName() {
479 * @ingroup SpecialPage
481 class ImportReporter
extends ContextSource
{
482 private $reason = false;
483 private $mOriginalLogCallback = null;
484 private $mOriginalPageOutCallback = null;
485 private $mLogItemCount = 0;
488 * @param WikiImporter $importer
489 * @param bool $upload
490 * @param string $interwiki
491 * @param string|bool $reason
493 function __construct( $importer, $upload, $interwiki, $reason = false ) {
494 $this->mOriginalPageOutCallback
=
495 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
496 $this->mOriginalLogCallback
=
497 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
498 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
499 $this->mPageCount
= 0;
500 $this->mIsUpload
= $upload;
501 $this->mInterwiki
= $interwiki;
502 $this->reason
= $reason;
506 $this->getOutput()->addHTML( "<ul>\n" );
509 function reportNotice( $msg, array $params ) {
510 $this->getOutput()->addHTML(
511 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
515 function reportLogItem( /* ... */ ) {
516 $this->mLogItemCount++
;
517 if ( is_callable( $this->mOriginalLogCallback
) ) {
518 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
523 * @param Title $title
524 * @param ForeignTitle $foreignTitle
525 * @param int $revisionCount
526 * @param int $successCount
527 * @param array $pageInfo
530 function reportPage( $title, $foreignTitle, $revisionCount,
531 $successCount, $pageInfo ) {
532 $args = func_get_args();
533 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
535 if ( $title === null ) {
536 # Invalid or non-importable title; a notice is already displayed
542 if ( $successCount > 0 ) {
543 $this->getOutput()->addHTML(
544 "<li>" . Linker
::linkKnown( $title ) . " " .
545 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
549 $log = new LogPage( 'import' );
550 if ( $this->mIsUpload
) {
551 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
552 $successCount )->inContentLanguage()->text();
553 if ( $this->reason
) {
554 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
557 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
559 $interwiki = '[[:' . $this->mInterwiki
. ':' .
560 $foreignTitle->getFullText() . ']]';
561 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
562 $successCount )->params( $interwiki )->inContentLanguage()->text();
563 if ( $this->reason
) {
564 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
567 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
570 $comment = $detail; // quick
571 $dbw = wfGetDB( DB_MASTER
);
572 $latest = $title->getLatestRevID();
573 $nullRevision = Revision
::newNullRevision(
575 $title->getArticleID(),
581 if ( !is_null( $nullRevision ) ) {
582 $nullRevision->insertOn( $dbw );
583 $page = WikiPage
::factory( $title );
585 $page->updateRevisionOn( $dbw, $nullRevision );
587 'NewRevisionFromEditComplete',
588 array( $page, $nullRevision, $latest, $this->getUser() )
592 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
593 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
598 $out = $this->getOutput();
599 if ( $this->mLogItemCount
> 0 ) {
600 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
601 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
602 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
603 $out->addHTML( "</ul>\n" );
605 return Status
::newFatal( 'importnopages' );
607 $out->addHTML( "</ul>\n" );
609 return Status
::newGood( $this->mPageCount
);