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 $interwiki = false;
35 private $fullInterwikiPrefix;
37 private $rootpage = '';
38 private $frompage = '';
39 private $logcomment = false;
40 private $history = true;
41 private $includeTemplates = false;
42 private $pageLinkDepth;
47 public function __construct() {
48 parent
::__construct( 'Import', 'import' );
49 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
54 * @param string|null $par
56 function execute( $par ) {
58 $this->outputHeader();
60 $this->getOutput()->addModules( 'mediawiki.special.import' );
62 $user = $this->getUser();
63 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
64 throw new PermissionsError( 'import' );
67 # @todo Allow Title::getUserPermissionsErrors() to take an array
68 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
69 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
70 $errors = wfMergeErrorArrays(
71 $this->getPageTitle()->getUserPermissionsErrors(
72 'import', $user, true,
73 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
75 $this->getPageTitle()->getUserPermissionsErrors(
76 'importupload', $user, true,
77 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
82 throw new PermissionsError( 'import', $errors );
85 $this->checkReadOnly();
87 $request = $this->getRequest();
88 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
95 * Do the actual import
97 private function doImport() {
99 $request = $this->getRequest();
100 $this->namespace = $request->getIntOrNull( 'namespace' );
101 $sourceName = $request->getVal( "source" );
103 $this->logcomment
= $request->getText( 'log-comment' );
104 $this->pageLinkDepth
= $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
106 : $request->getIntOrNull( 'pagelink-depth' );
107 $this->rootpage
= $request->getText( 'rootpage' );
109 $user = $this->getUser();
110 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
111 $source = Status
::newFatal( 'import-token-mismatch' );
112 } elseif ( $sourceName == 'upload' ) {
114 if ( $user->isAllowed( 'importupload' ) ) {
115 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
117 throw new PermissionsError( 'importupload' );
119 } elseif ( $sourceName == "interwiki" ) {
120 if ( !$user->isAllowed( 'import' ) ) {
121 throw new PermissionsError( 'import' );
123 $this->interwiki
= $this->fullInterwikiPrefix
= $request->getVal( 'interwiki' );
124 // does this interwiki have subprojects?
125 $importSources = $this->getConfig()->get( 'ImportSources' );
126 $hasSubprojects = array_key_exists( $this->interwiki
, $importSources );
127 if ( !$hasSubprojects && !in_array( $this->interwiki
, $importSources ) ) {
128 $source = Status
::newFatal( "import-invalid-interwiki" );
130 if ( $hasSubprojects ) {
131 $this->subproject
= $request->getVal( 'subproject' );
132 $this->fullInterwikiPrefix
.= ':' . $request->getVal( 'subproject' );
134 if ( $hasSubprojects && !in_array( $this->subproject
, $importSources[$this->interwiki
] ) ) {
135 $source = Status
::newFatal( "import-invalid-interwiki" );
137 $this->history
= $request->getCheck( 'interwikiHistory' );
138 $this->frompage
= $request->getText( "frompage" );
139 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
140 $source = ImportStreamSource
::newFromInterwiki(
141 $this->fullInterwikiPrefix
,
144 $this->includeTemplates
,
145 $this->pageLinkDepth
);
149 $source = Status
::newFatal( "importunknownsource" );
152 $out = $this->getOutput();
153 if ( !$source->isGood() ) {
155 "<p class=\"error\">\n$1\n</p>",
156 array( 'importfailed', $source->getWikiText() )
159 $importer = new WikiImporter( $source->value
);
160 if ( !is_null( $this->namespace ) ) {
161 $importer->setTargetNamespace( $this->namespace );
163 if ( !is_null( $this->rootpage
) ) {
164 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
165 if ( !$statusRootPage->isGood() ) {
167 "<p class=\"error\">\n$1\n</p>",
169 'import-options-wrong',
170 $statusRootPage->getWikiText(),
171 count( $statusRootPage->getErrorsArray() )
179 $out->addWikiMsg( "importstart" );
181 $reporter = new ImportReporter(
184 $this->fullInterwikiPrefix
,
187 $reporter->setContext( $this->getContext() );
192 $importer->doImport();
193 } catch ( MWException
$e ) {
196 $result = $reporter->close();
199 # No source or XML parse error
201 "<p class=\"error\">\n$1\n</p>",
202 array( 'importfailed', $exception->getMessage() )
204 } elseif ( !$result->isGood() ) {
207 "<p class=\"error\">\n$1\n</p>",
208 array( 'importfailed', $result->getWikiText() )
212 $out->addWikiMsg( 'importsuccess' );
214 $out->addHTML( '<hr />' );
218 private function showForm() {
219 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
220 $user = $this->getUser();
221 $out = $this->getOutput();
222 $importSources = $this->getConfig()->get( 'ImportSources' );
224 if ( $user->isAllowed( 'importupload' ) ) {
226 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
230 'enctype' => 'multipart/form-data',
233 'id' => 'mw-import-upload-form'
236 $this->msg( 'importtext' )->parseAsBlock() .
237 Html
::hidden( 'action', 'submit' ) .
238 Html
::hidden( 'source', 'upload' ) .
239 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
241 <td class='mw-label'>" .
242 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
244 <td class='mw-input'>" .
245 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
249 <td class='mw-label'>" .
250 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
252 <td class='mw-input'>" .
253 Xml
::input( 'log-comment', 50, '',
254 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
258 <td class='mw-label'>" .
260 $this->msg( 'import-interwiki-rootpage' )->text(),
261 'mw-interwiki-rootpage-upload'
264 <td class='mw-input'>" .
265 Xml
::input( 'rootpage', 50, $this->rootpage
,
266 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
271 <td class='mw-submit'>" .
272 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
275 Xml
::closeElement( 'table' ) .
276 Html
::hidden( 'editToken', $user->getEditToken() ) .
277 Xml
::closeElement( 'form' ) .
278 Xml
::closeElement( 'fieldset' )
281 if ( empty( $importSources ) ) {
282 $out->addWikiMsg( 'importnosources' );
286 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
287 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
289 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
291 <td class='mw-label'>" .
292 $this->msg( 'export-pagelinks' )->parse() .
294 <td class='mw-input'>" .
295 Xml
::input( 'pagelink-depth', 3, 0 ) .
301 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
307 'id' => 'mw-import-interwiki-form'
310 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
311 Html
::hidden( 'action', 'submit' ) .
312 Html
::hidden( 'source', 'interwiki' ) .
313 Html
::hidden( 'editToken', $user->getEditToken() ) .
314 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
316 <td class='mw-label'>" .
317 Xml
::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
319 <td class='mw-input'>" .
322 array( 'name' => 'interwiki', 'id' => 'interwiki' )
326 $needSubprojectField = false;
327 foreach ( $importSources as $key => $value ) {
328 if ( is_int( $key ) ) {
330 } elseif ( $value !== $key ) {
331 $needSubprojectField = true;
337 if ( is_array( $value ) ) {
338 $attribs['data-subprojects'] = implode( ' ', $value );
340 if ( $this->interwiki
=== $key ) {
341 $attribs['selected'] = 'selected';
343 $out->addHTML( Html
::element( 'option', $attribs, $key ) );
347 Xml
::closeElement( 'select' )
350 if ( $needSubprojectField ) {
354 array( 'name' => 'subproject', 'id' => 'subproject' )
358 $subprojectsToAdd = array();
359 foreach ( $importSources as $key => $value ) {
360 if ( is_array( $value ) ) {
361 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
364 $subprojectsToAdd = array_unique( $subprojectsToAdd );
365 sort( $subprojectsToAdd );
366 foreach ( $subprojectsToAdd as $subproject ) {
367 $out->addHTML( Xml
::option( $subproject, $subproject, $this->subproject
=== $subproject ) );
371 Xml
::closeElement( 'select' )
379 <td class='mw-label'>" .
380 Xml
::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
382 <td class='mw-input'>" .
383 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
389 <td class='mw-input'>" .
391 $this->msg( 'import-interwiki-history' )->text(),
401 <td class='mw-input'>" .
403 $this->msg( 'import-interwiki-templates' )->text(),
404 'interwikiTemplates',
405 'interwikiTemplates',
406 $this->includeTemplates
412 <td class='mw-label'>" .
413 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
415 <td class='mw-input'>" .
416 Html
::namespaceSelector(
418 'selected' => $this->namespace,
421 'name' => 'namespace',
423 'class' => 'namespaceselector',
429 <td class='mw-label'>" .
430 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
432 <td class='mw-input'>" .
433 Xml
::input( 'log-comment', 50, '',
434 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
438 <td class='mw-label'>" .
440 $this->msg( 'import-interwiki-rootpage' )->text(),
441 'mw-interwiki-rootpage-interwiki'
444 <td class='mw-input'>" .
445 Xml
::input( 'rootpage', 50, $this->rootpage
,
446 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
452 <td class='mw-submit'>" .
454 $this->msg( 'import-interwiki-submit' )->text(),
455 Linker
::tooltipAndAccesskeyAttribs( 'import' )
459 Xml
::closeElement( 'table' ) .
460 Xml
::closeElement( 'form' ) .
461 Xml
::closeElement( 'fieldset' )
466 protected function getGroupName() {
473 * @ingroup SpecialPage
475 class ImportReporter
extends ContextSource
{
476 private $reason = false;
477 private $mOriginalLogCallback = null;
478 private $mOriginalPageOutCallback = null;
479 private $mLogItemCount = 0;
482 * @param WikiImporter $importer
483 * @param bool $upload
484 * @param string $interwiki
485 * @param string|bool $reason
487 function __construct( $importer, $upload, $interwiki, $reason = false ) {
488 $this->mOriginalPageOutCallback
=
489 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
490 $this->mOriginalLogCallback
=
491 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
492 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
493 $this->mPageCount
= 0;
494 $this->mIsUpload
= $upload;
495 $this->mInterwiki
= $interwiki;
496 $this->reason
= $reason;
500 $this->getOutput()->addHTML( "<ul>\n" );
503 function reportNotice( $msg, array $params ) {
504 $this->getOutput()->addHTML(
505 Html
::element( 'li', array(), $this->msg( $msg, $params )->text() )
509 function reportLogItem( /* ... */ ) {
510 $this->mLogItemCount++
;
511 if ( is_callable( $this->mOriginalLogCallback
) ) {
512 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
517 * @param Title $title
518 * @param Title $origTitle
519 * @param int $revisionCount
520 * @param int $successCount
521 * @param array $pageInfo
524 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
525 $args = func_get_args();
526 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
528 if ( $title === null ) {
529 # Invalid or non-importable title; a notice is already displayed
535 if ( $successCount > 0 ) {
536 $this->getOutput()->addHTML(
537 "<li>" . Linker
::linkKnown( $title ) . " " .
538 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
542 $log = new LogPage( 'import' );
543 if ( $this->mIsUpload
) {
544 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
545 $successCount )->inContentLanguage()->text();
546 if ( $this->reason
) {
547 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
550 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
552 $interwiki = '[[:' . $this->mInterwiki
. ':' .
553 $origTitle->getPrefixedText() . ']]';
554 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
555 $successCount )->params( $interwiki )->inContentLanguage()->text();
556 if ( $this->reason
) {
557 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
560 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
563 $comment = $detail; // quick
564 $dbw = wfGetDB( DB_MASTER
);
565 $latest = $title->getLatestRevID();
566 $nullRevision = Revision
::newNullRevision(
568 $title->getArticleID(),
574 if ( !is_null( $nullRevision ) ) {
575 $nullRevision->insertOn( $dbw );
576 $page = WikiPage
::factory( $title );
578 $page->updateRevisionOn( $dbw, $nullRevision );
580 'NewRevisionFromEditComplete',
581 array( $page, $nullRevision, $latest, $this->getUser() )
585 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
586 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
591 $out = $this->getOutput();
592 if ( $this->mLogItemCount
> 0 ) {
593 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
594 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
595 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
596 $out->addHTML( "</ul>\n" );
598 return Status
::newFatal( 'importnopages' );
600 $out->addHTML( "</ul>\n" );
602 return Status
::newGood( $this->mPageCount
);