Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / specials / SpecialImport.php
bloba2930fcebe0cd8d7fe6bd74b3c761d084b21edf3
1 <?php
2 /**
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
23 * @file
24 * @ingroup SpecialPage
27 use MediaWiki\MediaWikiServices;
29 /**
30 * MediaWiki page data importer
32 * @ingroup SpecialPage
34 class SpecialImport extends SpecialPage {
35 private $sourceName = false;
36 private $interwiki = false;
37 private $subproject;
38 private $fullInterwikiPrefix;
39 private $mapping = 'default';
40 private $namespace;
41 private $rootpage = '';
42 private $frompage = '';
43 private $logcomment = false;
44 private $history = true;
45 private $includeTemplates = false;
46 private $pageLinkDepth;
47 private $importSources;
49 /**
50 * Constructor
52 public function __construct() {
53 parent::__construct( 'Import', 'import' );
56 public function doesWrites() {
57 return true;
60 /**
61 * Execute
62 * @param string|null $par
63 * @throws PermissionsError
64 * @throws ReadOnlyError
66 function execute( $par ) {
67 $this->useTransactionalTimeLimit();
69 $this->setHeaders();
70 $this->outputHeader();
72 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
74 $this->getOutput()->addModules( 'mediawiki.special.import' );
76 $this->importSources = $this->getConfig()->get( 'ImportSources' );
77 Hooks::run( 'ImportSources', [ &$this->importSources ] );
79 $user = $this->getUser();
80 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
81 throw new PermissionsError( 'import' );
84 # @todo Allow Title::getUserPermissionsErrors() to take an array
85 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
86 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
87 $errors = wfMergeErrorArrays(
88 $this->getPageTitle()->getUserPermissionsErrors(
89 'import', $user, true,
90 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
92 $this->getPageTitle()->getUserPermissionsErrors(
93 'importupload', $user, true,
94 [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
98 if ( $errors ) {
99 throw new PermissionsError( 'import', $errors );
102 $this->checkReadOnly();
104 $request = $this->getRequest();
105 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
106 $this->doImport();
108 $this->showForm();
112 * Do the actual import
114 private function doImport() {
115 $isUpload = false;
116 $request = $this->getRequest();
117 $this->sourceName = $request->getVal( "source" );
119 $this->logcomment = $request->getText( 'log-comment' );
120 $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
122 : $request->getIntOrNull( 'pagelink-depth' );
124 $this->mapping = $request->getVal( 'mapping' );
125 if ( $this->mapping === 'namespace' ) {
126 $this->namespace = $request->getIntOrNull( 'namespace' );
127 } elseif ( $this->mapping === 'subpage' ) {
128 $this->rootpage = $request->getText( 'rootpage' );
129 } else {
130 $this->mapping = 'default';
133 $user = $this->getUser();
134 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
135 $source = Status::newFatal( 'import-token-mismatch' );
136 } elseif ( $this->sourceName === 'upload' ) {
137 $isUpload = true;
138 if ( $user->isAllowed( 'importupload' ) ) {
139 $source = ImportStreamSource::newFromUpload( "xmlimport" );
140 } else {
141 throw new PermissionsError( 'importupload' );
143 } elseif ( $this->sourceName === 'interwiki' ) {
144 if ( !$user->isAllowed( 'import' ) ) {
145 throw new PermissionsError( 'import' );
147 $this->interwiki = $this->fullInterwikiPrefix = $request->getVal( 'interwiki' );
148 // does this interwiki have subprojects?
149 $hasSubprojects = array_key_exists( $this->interwiki, $this->importSources );
150 if ( !$hasSubprojects && !in_array( $this->interwiki, $this->importSources ) ) {
151 $source = Status::newFatal( "import-invalid-interwiki" );
152 } else {
153 if ( $hasSubprojects ) {
154 $this->subproject = $request->getVal( 'subproject' );
155 $this->fullInterwikiPrefix .= ':' . $request->getVal( 'subproject' );
157 if ( $hasSubprojects &&
158 !in_array( $this->subproject, $this->importSources[$this->interwiki] )
160 $source = Status::newFatal( "import-invalid-interwiki" );
161 } else {
162 $this->history = $request->getCheck( 'interwikiHistory' );
163 $this->frompage = $request->getText( "frompage" );
164 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
165 $source = ImportStreamSource::newFromInterwiki(
166 $this->fullInterwikiPrefix,
167 $this->frompage,
168 $this->history,
169 $this->includeTemplates,
170 $this->pageLinkDepth );
173 } else {
174 $source = Status::newFatal( "importunknownsource" );
177 $out = $this->getOutput();
178 if ( !$source->isGood() ) {
179 $out->wrapWikiMsg(
180 "<p class=\"error\">\n$1\n</p>",
181 [ 'importfailed', $source->getWikiText() ]
183 } else {
184 $importer = new WikiImporter( $source->value, $this->getConfig() );
185 if ( !is_null( $this->namespace ) ) {
186 $importer->setTargetNamespace( $this->namespace );
187 } elseif ( !is_null( $this->rootpage ) ) {
188 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
189 if ( !$statusRootPage->isGood() ) {
190 $out->wrapWikiMsg(
191 "<p class=\"error\">\n$1\n</p>",
193 'import-options-wrong',
194 $statusRootPage->getWikiText(),
195 count( $statusRootPage->getErrorsArray() )
199 return;
203 $out->addWikiMsg( "importstart" );
205 $reporter = new ImportReporter(
206 $importer,
207 $isUpload,
208 $this->fullInterwikiPrefix,
209 $this->logcomment
211 $reporter->setContext( $this->getContext() );
212 $exception = false;
214 $reporter->open();
215 try {
216 $importer->doImport();
217 } catch ( Exception $e ) {
218 $exception = $e;
220 $result = $reporter->close();
222 if ( $exception ) {
223 # No source or XML parse error
224 $out->wrapWikiMsg(
225 "<p class=\"error\">\n$1\n</p>",
226 [ 'importfailed', $exception->getMessage() ]
228 } elseif ( !$result->isGood() ) {
229 # Zero revisions
230 $out->wrapWikiMsg(
231 "<p class=\"error\">\n$1\n</p>",
232 [ 'importfailed', $result->getWikiText() ]
234 } else {
235 # Success!
236 $out->addWikiMsg( 'importsuccess' );
238 $out->addHTML( '<hr />' );
242 private function getMappingFormPart( $sourceName ) {
243 $isSameSourceAsBefore = ( $this->sourceName === $sourceName );
244 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
245 return "<tr>
246 <td>
247 </td>
248 <td class='mw-input'>" .
249 Xml::radioLabel(
250 $this->msg( 'import-mapping-default' )->text(),
251 'mapping',
252 'default',
253 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
254 "mw-import-mapping-$sourceName-default",
255 ( $isSameSourceAsBefore ?
256 ( $this->mapping === 'default' ) :
257 is_null( $defaultNamespace ) )
259 "</td>
260 </tr>
261 <tr>
262 <td>
263 </td>
264 <td class='mw-input'>" .
265 Xml::radioLabel(
266 $this->msg( 'import-mapping-namespace' )->text(),
267 'mapping',
268 'namespace',
269 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
270 "mw-import-mapping-$sourceName-namespace",
271 ( $isSameSourceAsBefore ?
272 ( $this->mapping === 'namespace' ) :
273 !is_null( $defaultNamespace ) )
274 ) . ' ' .
275 Html::namespaceSelector(
277 'selected' => ( $isSameSourceAsBefore ?
278 $this->namespace :
279 ( $defaultNamespace || '' ) ),
280 ], [
281 'name' => "namespace",
282 // mw-import-namespace-interwiki, mw-import-namespace-upload
283 'id' => "mw-import-namespace-$sourceName",
284 'class' => 'namespaceselector',
287 "</td>
288 </tr>
289 <tr>
290 <td>
291 </td>
292 <td class='mw-input'>" .
293 Xml::radioLabel(
294 $this->msg( 'import-mapping-subpage' )->text(),
295 'mapping',
296 'subpage',
297 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
298 "mw-import-mapping-$sourceName-subpage",
299 ( $isSameSourceAsBefore ? ( $this->mapping === 'subpage' ) : '' )
300 ) . ' ' .
301 Xml::input( 'rootpage', 50,
302 ( $isSameSourceAsBefore ? $this->rootpage : '' ),
304 // Should be "mw-import-rootpage-...", but we keep this inaccurate
305 // ID for legacy reasons
306 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
307 'id' => "mw-interwiki-rootpage-$sourceName",
308 'type' => 'text'
310 ) . ' ' .
311 "</td>
312 </tr>";
315 private function showForm() {
316 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
317 $user = $this->getUser();
318 $out = $this->getOutput();
319 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
321 if ( $user->isAllowed( 'importupload' ) ) {
322 $mappingSelection = $this->getMappingFormPart( 'upload' );
323 $out->addHTML(
324 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
325 Xml::openElement(
326 'form',
328 'enctype' => 'multipart/form-data',
329 'method' => 'post',
330 'action' => $action,
331 'id' => 'mw-import-upload-form'
334 $this->msg( 'importtext' )->parseAsBlock() .
335 Html::hidden( 'action', 'submit' ) .
336 Html::hidden( 'source', 'upload' ) .
337 Xml::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
338 "<tr>
339 <td class='mw-label'>" .
340 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
341 "</td>
342 <td class='mw-input'>" .
343 Html::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
344 "</td>
345 </tr>
346 <tr>
347 <td class='mw-label'>" .
348 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
349 "</td>
350 <td class='mw-input'>" .
351 Xml::input( 'log-comment', 50,
352 ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
353 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
354 "</td>
355 </tr>
356 $mappingSelection
357 <tr>
358 <td></td>
359 <td class='mw-submit'>" .
360 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
361 "</td>
362 </tr>" .
363 Xml::closeElement( 'table' ) .
364 Html::hidden( 'editToken', $user->getEditToken() ) .
365 Xml::closeElement( 'form' ) .
366 Xml::closeElement( 'fieldset' )
368 } else {
369 if ( empty( $this->importSources ) ) {
370 $out->addWikiMsg( 'importnosources' );
374 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources ) ) {
375 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
376 $importDepth = '';
377 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
378 $importDepth = "<tr>
379 <td class='mw-label'>" .
380 $this->msg( 'export-pagelinks' )->parse() .
381 "</td>
382 <td class='mw-input'>" .
383 Xml::input( 'pagelink-depth', 3, 0 ) .
384 "</td>
385 </tr>";
387 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
389 $out->addHTML(
390 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
391 Xml::openElement(
392 'form',
394 'method' => 'post',
395 'action' => $action,
396 'id' => 'mw-import-interwiki-form'
399 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
400 Html::hidden( 'action', 'submit' ) .
401 Html::hidden( 'source', 'interwiki' ) .
402 Html::hidden( 'editToken', $user->getEditToken() ) .
403 Xml::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
404 "<tr>
405 <td class='mw-label'>" .
406 Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
407 "</td>
408 <td class='mw-input'>" .
409 Xml::openElement(
410 'select',
411 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
415 $needSubprojectField = false;
416 foreach ( $this->importSources as $key => $value ) {
417 if ( is_int( $key ) ) {
418 $key = $value;
419 } elseif ( $value !== $key ) {
420 $needSubprojectField = true;
423 $attribs = [
424 'value' => $key,
426 if ( is_array( $value ) ) {
427 $attribs['data-subprojects'] = implode( ' ', $value );
429 if ( $this->interwiki === $key ) {
430 $attribs['selected'] = 'selected';
432 $out->addHTML( Html::element( 'option', $attribs, $key ) );
435 $out->addHTML(
436 Xml::closeElement( 'select' )
439 if ( $needSubprojectField ) {
440 $out->addHTML(
441 Xml::openElement(
442 'select',
443 [ 'name' => 'subproject', 'id' => 'subproject' ]
447 $subprojectsToAdd = [];
448 foreach ( $this->importSources as $key => $value ) {
449 if ( is_array( $value ) ) {
450 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
453 $subprojectsToAdd = array_unique( $subprojectsToAdd );
454 sort( $subprojectsToAdd );
455 foreach ( $subprojectsToAdd as $subproject ) {
456 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
459 $out->addHTML(
460 Xml::closeElement( 'select' )
464 $out->addHTML(
465 "</td>
466 </tr>
467 <tr>
468 <td class='mw-label'>" .
469 Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
470 "</td>
471 <td class='mw-input'>" .
472 Xml::input( 'frompage', 50, $this->frompage, [ 'id' => 'frompage' ] ) .
473 "</td>
474 </tr>
475 <tr>
476 <td>
477 </td>
478 <td class='mw-input'>" .
479 Xml::checkLabel(
480 $this->msg( 'import-interwiki-history' )->text(),
481 'interwikiHistory',
482 'interwikiHistory',
483 $this->history
485 "</td>
486 </tr>
487 <tr>
488 <td>
489 </td>
490 <td class='mw-input'>" .
491 Xml::checkLabel(
492 $this->msg( 'import-interwiki-templates' )->text(),
493 'interwikiTemplates',
494 'interwikiTemplates',
495 $this->includeTemplates
497 "</td>
498 </tr>
499 $importDepth
500 <tr>
501 <td class='mw-label'>" .
502 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
503 "</td>
504 <td class='mw-input'>" .
505 Xml::input( 'log-comment', 50,
506 ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
507 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
508 "</td>
509 </tr>
510 $mappingSelection
511 <tr>
512 <td>
513 </td>
514 <td class='mw-submit'>" .
515 Xml::submitButton(
516 $this->msg( 'import-interwiki-submit' )->text(),
517 Linker::tooltipAndAccesskeyAttribs( 'import' )
519 "</td>
520 </tr>" .
521 Xml::closeElement( 'table' ) .
522 Xml::closeElement( 'form' ) .
523 Xml::closeElement( 'fieldset' )
528 protected function getGroupName() {
529 return 'pagetools';
534 * Reporting callback
535 * @ingroup SpecialPage
537 class ImportReporter extends ContextSource {
538 private $reason = false;
539 private $logTags = [];
540 private $mOriginalLogCallback = null;
541 private $mOriginalPageOutCallback = null;
542 private $mLogItemCount = 0;
545 * @param WikiImporter $importer
546 * @param bool $upload
547 * @param string $interwiki
548 * @param string|bool $reason
550 function __construct( $importer, $upload, $interwiki, $reason = false ) {
551 $this->mOriginalPageOutCallback =
552 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
553 $this->mOriginalLogCallback =
554 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
555 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
556 $this->mPageCount = 0;
557 $this->mIsUpload = $upload;
558 $this->mInterwiki = $interwiki;
559 $this->reason = $reason;
563 * Sets change tags to apply to the import log entry and null revision.
565 * @param array $tags
566 * @since 1.29
568 public function setChangeTags( array $tags ) {
569 $this->logTags = $tags;
572 function open() {
573 $this->getOutput()->addHTML( "<ul>\n" );
576 function reportNotice( $msg, array $params ) {
577 $this->getOutput()->addHTML(
578 Html::element( 'li', [], $this->msg( $msg, $params )->text() )
582 function reportLogItem( /* ... */ ) {
583 $this->mLogItemCount++;
584 if ( is_callable( $this->mOriginalLogCallback ) ) {
585 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
590 * @param Title $title
591 * @param ForeignTitle $foreignTitle
592 * @param int $revisionCount
593 * @param int $successCount
594 * @param array $pageInfo
595 * @return void
597 public function reportPage( $title, $foreignTitle, $revisionCount,
598 $successCount, $pageInfo ) {
599 $args = func_get_args();
600 call_user_func_array( $this->mOriginalPageOutCallback, $args );
602 if ( $title === null ) {
603 # Invalid or non-importable title; a notice is already displayed
604 return;
607 $this->mPageCount++;
608 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
609 if ( $successCount > 0 ) {
610 // <bdi> prevents jumbling of the versions count
611 // in RTL wikis in case the page title is LTR
612 $this->getOutput()->addHTML(
613 "<li>" . $linkRenderer->makeLink( $title ) . " " .
614 "<bdi>" .
615 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
616 "</bdi>" .
617 "</li>\n"
620 $logParams = [ '4:number:count' => $successCount ];
621 if ( $this->mIsUpload ) {
622 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
623 $successCount )->inContentLanguage()->text();
624 $action = 'upload';
625 } else {
626 $pageTitle = $foreignTitle->getFullText();
627 $fullInterwikiPrefix = $this->mInterwiki;
628 Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
630 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
631 $interwiki = '[[:' . $interwikiTitleStr . ']]';
632 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
633 $successCount )->params( $interwiki )->inContentLanguage()->text();
634 $action = 'interwiki';
635 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
637 if ( $this->reason ) {
638 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
639 . $this->reason;
642 $comment = $detail; // quick
643 $dbw = wfGetDB( DB_MASTER );
644 $latest = $title->getLatestRevID();
645 $nullRevision = Revision::newNullRevision(
646 $dbw,
647 $title->getArticleID(),
648 $comment,
649 true,
650 $this->getUser()
653 $nullRevId = null;
654 if ( !is_null( $nullRevision ) ) {
655 $nullRevId = $nullRevision->insertOn( $dbw );
656 $page = WikiPage::factory( $title );
657 # Update page record
658 $page->updateRevisionOn( $dbw, $nullRevision );
659 Hooks::run(
660 'NewRevisionFromEditComplete',
661 [ $page, $nullRevision, $latest, $this->getUser() ]
665 // Create the import log entry
666 $logEntry = new ManualLogEntry( 'import', $action );
667 $logEntry->setTarget( $title );
668 $logEntry->setComment( $this->reason );
669 $logEntry->setPerformer( $this->getUser() );
670 $logEntry->setParameters( $logParams );
671 $logid = $logEntry->insert();
672 if ( count( $this->logTags ) ) {
673 $logEntry->setTags( $this->logTags );
675 // Make sure the null revision will be tagged as well
676 $logEntry->setAssociatedRevId( $nullRevId );
678 $logEntry->publish( $logid );
680 } else {
681 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
682 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
686 function close() {
687 $out = $this->getOutput();
688 if ( $this->mLogItemCount > 0 ) {
689 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
690 $out->addHTML( Xml::tags( 'li', null, $msg ) );
691 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
692 $out->addHTML( "</ul>\n" );
694 return Status::newFatal( 'importnopages' );
696 $out->addHTML( "</ul>\n" );
698 return Status::newGood( $this->mPageCount );