wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialImport.php
blob5ca90ed786159c904caed9b3100a80524e7b406a
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 /**
28 * MediaWiki page data importer
30 * @ingroup SpecialPage
32 class SpecialImport extends SpecialPage {
33 private $sourceName = false;
34 private $interwiki = false;
35 private $subproject;
36 private $fullInterwikiPrefix;
37 private $mapping = 'default';
38 private $namespace;
39 private $rootpage = '';
40 private $frompage = '';
41 private $logcomment = false;
42 private $history = true;
43 private $includeTemplates = false;
44 private $pageLinkDepth;
45 private $importSources;
47 /**
48 * Constructor
50 public function __construct() {
51 parent::__construct( 'Import', 'import' );
54 /**
55 * Execute
56 * @param string|null $par
57 * @throws PermissionsError
58 * @throws ReadOnlyError
60 function execute( $par ) {
61 $this->useTransactionalTimeLimit();
63 $this->setHeaders();
64 $this->outputHeader();
66 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
68 $this->getOutput()->addModules( 'mediawiki.special.import' );
70 $this->importSources = $this->getConfig()->get( 'ImportSources' );
71 Hooks::run( 'ImportSources', array( &$this->importSources ) );
73 $user = $this->getUser();
74 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
75 throw new PermissionsError( 'import' );
78 # @todo Allow Title::getUserPermissionsErrors() to take an array
79 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
80 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
81 $errors = wfMergeErrorArrays(
82 $this->getPageTitle()->getUserPermissionsErrors(
83 'import', $user, true,
84 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
86 $this->getPageTitle()->getUserPermissionsErrors(
87 'importupload', $user, true,
88 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
92 if ( $errors ) {
93 throw new PermissionsError( 'import', $errors );
96 $this->checkReadOnly();
98 $request = $this->getRequest();
99 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
100 $this->doImport();
102 $this->showForm();
106 * Do the actual import
108 private function doImport() {
109 $isUpload = false;
110 $request = $this->getRequest();
111 $this->sourceName = $request->getVal( "source" );
113 $this->logcomment = $request->getText( 'log-comment' );
114 $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
116 : $request->getIntOrNull( 'pagelink-depth' );
118 $this->mapping = $request->getVal( 'mapping' );
119 if ( $this->mapping === 'namespace' ) {
120 $this->namespace = $request->getIntOrNull( 'namespace' );
121 } elseif ( $this->mapping === 'subpage' ) {
122 $this->rootpage = $request->getText( 'rootpage' );
123 } else {
124 $this->mapping = 'default';
127 $user = $this->getUser();
128 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
129 $source = Status::newFatal( 'import-token-mismatch' );
130 } elseif ( $this->sourceName === 'upload' ) {
131 $isUpload = true;
132 if ( $user->isAllowed( 'importupload' ) ) {
133 $source = ImportStreamSource::newFromUpload( "xmlimport" );
134 } else {
135 throw new PermissionsError( 'importupload' );
137 } elseif ( $this->sourceName === 'interwiki' ) {
138 if ( !$user->isAllowed( 'import' ) ) {
139 throw new PermissionsError( 'import' );
141 $this->interwiki = $this->fullInterwikiPrefix = $request->getVal( 'interwiki' );
142 // does this interwiki have subprojects?
143 $hasSubprojects = array_key_exists( $this->interwiki, $this->importSources );
144 if ( !$hasSubprojects && !in_array( $this->interwiki, $this->importSources ) ) {
145 $source = Status::newFatal( "import-invalid-interwiki" );
146 } else {
147 if ( $hasSubprojects ) {
148 $this->subproject = $request->getVal( 'subproject' );
149 $this->fullInterwikiPrefix .= ':' . $request->getVal( 'subproject' );
151 if ( $hasSubprojects &&
152 !in_array( $this->subproject, $this->importSources[$this->interwiki] )
154 $source = Status::newFatal( "import-invalid-interwiki" );
155 } else {
156 $this->history = $request->getCheck( 'interwikiHistory' );
157 $this->frompage = $request->getText( "frompage" );
158 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
159 $source = ImportStreamSource::newFromInterwiki(
160 $this->fullInterwikiPrefix,
161 $this->frompage,
162 $this->history,
163 $this->includeTemplates,
164 $this->pageLinkDepth );
167 } else {
168 $source = Status::newFatal( "importunknownsource" );
171 $out = $this->getOutput();
172 if ( !$source->isGood() ) {
173 $out->wrapWikiMsg(
174 "<p class=\"error\">\n$1\n</p>",
175 array( 'importfailed', $source->getWikiText() )
177 } else {
178 $importer = new WikiImporter( $source->value, $this->getConfig() );
179 if ( !is_null( $this->namespace ) ) {
180 $importer->setTargetNamespace( $this->namespace );
181 } elseif ( !is_null( $this->rootpage ) ) {
182 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
183 if ( !$statusRootPage->isGood() ) {
184 $out->wrapWikiMsg(
185 "<p class=\"error\">\n$1\n</p>",
186 array(
187 'import-options-wrong',
188 $statusRootPage->getWikiText(),
189 count( $statusRootPage->getErrorsArray() )
193 return;
197 $out->addWikiMsg( "importstart" );
199 $reporter = new ImportReporter(
200 $importer,
201 $isUpload,
202 $this->fullInterwikiPrefix,
203 $this->logcomment
205 $reporter->setContext( $this->getContext() );
206 $exception = false;
208 $reporter->open();
209 try {
210 $importer->doImport();
211 } catch ( Exception $e ) {
212 $exception = $e;
214 $result = $reporter->close();
216 if ( $exception ) {
217 # No source or XML parse error
218 $out->wrapWikiMsg(
219 "<p class=\"error\">\n$1\n</p>",
220 array( 'importfailed', $exception->getMessage() )
222 } elseif ( !$result->isGood() ) {
223 # Zero revisions
224 $out->wrapWikiMsg(
225 "<p class=\"error\">\n$1\n</p>",
226 array( 'importfailed', $result->getWikiText() )
228 } else {
229 # Success!
230 $out->addWikiMsg( 'importsuccess' );
232 $out->addHTML( '<hr />' );
236 private function getMappingFormPart( $sourceName ) {
237 $isSameSourceAsBefore = ( $this->sourceName === $sourceName );
238 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
239 return "<tr>
240 <td>
241 </td>
242 <td class='mw-input'>" .
243 Xml::radioLabel(
244 $this->msg( 'import-mapping-default' )->text(),
245 'mapping',
246 'default',
247 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
248 "mw-import-mapping-$sourceName-default",
249 ( $isSameSourceAsBefore ?
250 ( $this->mapping === 'default' ) :
251 is_null( $defaultNamespace ) )
253 "</td>
254 </tr>
255 <tr>
256 <td>
257 </td>
258 <td class='mw-input'>" .
259 Xml::radioLabel(
260 $this->msg( 'import-mapping-namespace' )->text(),
261 'mapping',
262 'namespace',
263 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
264 "mw-import-mapping-$sourceName-namespace",
265 ( $isSameSourceAsBefore ?
266 ( $this->mapping === 'namespace' ) :
267 !is_null( $defaultNamespace ) )
268 ) . ' ' .
269 Html::namespaceSelector(
270 array(
271 'selected' => ( $isSameSourceAsBefore ?
272 $this->namespace :
273 ( $defaultNamespace || '' ) ),
274 ), array(
275 'name' => "namespace",
276 // mw-import-namespace-interwiki, mw-import-namespace-upload
277 'id' => "mw-import-namespace-$sourceName",
278 'class' => 'namespaceselector',
281 "</td>
282 </tr>
283 <tr>
284 <td>
285 </td>
286 <td class='mw-input'>" .
287 Xml::radioLabel(
288 $this->msg( 'import-mapping-subpage' )->text(),
289 'mapping',
290 'subpage',
291 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
292 "mw-import-mapping-$sourceName-subpage",
293 ( $isSameSourceAsBefore ? ( $this->mapping === 'subpage' ) : '' )
294 ) . ' ' .
295 Xml::input( 'rootpage', 50,
296 ( $isSameSourceAsBefore ? $this->rootpage : '' ),
297 array(
298 // Should be "mw-import-rootpage-...", but we keep this inaccurate
299 // ID for legacy reasons
300 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
301 'id' => "mw-interwiki-rootpage-$sourceName",
302 'type' => 'text'
304 ) . ' ' .
305 "</td>
306 </tr>";
309 private function showForm() {
310 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
311 $user = $this->getUser();
312 $out = $this->getOutput();
313 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
315 if ( $user->isAllowed( 'importupload' ) ) {
316 $mappingSelection = $this->getMappingFormPart( 'upload' );
317 $out->addHTML(
318 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
319 Xml::openElement(
320 'form',
321 array(
322 'enctype' => 'multipart/form-data',
323 'method' => 'post',
324 'action' => $action,
325 'id' => 'mw-import-upload-form'
328 $this->msg( 'importtext' )->parseAsBlock() .
329 Html::hidden( 'action', 'submit' ) .
330 Html::hidden( 'source', 'upload' ) .
331 Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
332 "<tr>
333 <td class='mw-label'>" .
334 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
335 "</td>
336 <td class='mw-input'>" .
337 Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
338 "</td>
339 </tr>
340 <tr>
341 <td class='mw-label'>" .
342 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
343 "</td>
344 <td class='mw-input'>" .
345 Xml::input( 'log-comment', 50,
346 ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
347 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
348 "</td>
349 </tr>
350 $mappingSelection
351 <tr>
352 <td></td>
353 <td class='mw-submit'>" .
354 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
355 "</td>
356 </tr>" .
357 Xml::closeElement( 'table' ) .
358 Html::hidden( 'editToken', $user->getEditToken() ) .
359 Xml::closeElement( 'form' ) .
360 Xml::closeElement( 'fieldset' )
362 } else {
363 if ( empty( $this->importSources ) ) {
364 $out->addWikiMsg( 'importnosources' );
368 if ( $user->isAllowed( 'import' ) && !empty( $this->importSources ) ) {
369 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
370 $importDepth = '';
371 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
372 $importDepth = "<tr>
373 <td class='mw-label'>" .
374 $this->msg( 'export-pagelinks' )->parse() .
375 "</td>
376 <td class='mw-input'>" .
377 Xml::input( 'pagelink-depth', 3, 0 ) .
378 "</td>
379 </tr>";
381 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
383 $out->addHTML(
384 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
385 Xml::openElement(
386 'form',
387 array(
388 'method' => 'post',
389 'action' => $action,
390 'id' => 'mw-import-interwiki-form'
393 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
394 Html::hidden( 'action', 'submit' ) .
395 Html::hidden( 'source', 'interwiki' ) .
396 Html::hidden( 'editToken', $user->getEditToken() ) .
397 Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
398 "<tr>
399 <td class='mw-label'>" .
400 Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
401 "</td>
402 <td class='mw-input'>" .
403 Xml::openElement(
404 'select',
405 array( 'name' => 'interwiki', 'id' => 'interwiki' )
409 $needSubprojectField = false;
410 foreach ( $this->importSources as $key => $value ) {
411 if ( is_int( $key ) ) {
412 $key = $value;
413 } elseif ( $value !== $key ) {
414 $needSubprojectField = true;
417 $attribs = array(
418 'value' => $key,
420 if ( is_array( $value ) ) {
421 $attribs['data-subprojects'] = implode( ' ', $value );
423 if ( $this->interwiki === $key ) {
424 $attribs['selected'] = 'selected';
426 $out->addHTML( Html::element( 'option', $attribs, $key ) );
429 $out->addHTML(
430 Xml::closeElement( 'select' )
433 if ( $needSubprojectField ) {
434 $out->addHTML(
435 Xml::openElement(
436 'select',
437 array( 'name' => 'subproject', 'id' => 'subproject' )
441 $subprojectsToAdd = array();
442 foreach ( $this->importSources as $key => $value ) {
443 if ( is_array( $value ) ) {
444 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
447 $subprojectsToAdd = array_unique( $subprojectsToAdd );
448 sort( $subprojectsToAdd );
449 foreach ( $subprojectsToAdd as $subproject ) {
450 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
453 $out->addHTML(
454 Xml::closeElement( 'select' )
458 $out->addHTML(
459 "</td>
460 </tr>
461 <tr>
462 <td class='mw-label'>" .
463 Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
464 "</td>
465 <td class='mw-input'>" .
466 Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
467 "</td>
468 </tr>
469 <tr>
470 <td>
471 </td>
472 <td class='mw-input'>" .
473 Xml::checkLabel(
474 $this->msg( 'import-interwiki-history' )->text(),
475 'interwikiHistory',
476 'interwikiHistory',
477 $this->history
479 "</td>
480 </tr>
481 <tr>
482 <td>
483 </td>
484 <td class='mw-input'>" .
485 Xml::checkLabel(
486 $this->msg( 'import-interwiki-templates' )->text(),
487 'interwikiTemplates',
488 'interwikiTemplates',
489 $this->includeTemplates
491 "</td>
492 </tr>
493 $importDepth
494 <tr>
495 <td class='mw-label'>" .
496 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
497 "</td>
498 <td class='mw-input'>" .
499 Xml::input( 'log-comment', 50,
500 ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
501 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
502 "</td>
503 </tr>
504 $mappingSelection
505 <tr>
506 <td>
507 </td>
508 <td class='mw-submit'>" .
509 Xml::submitButton(
510 $this->msg( 'import-interwiki-submit' )->text(),
511 Linker::tooltipAndAccesskeyAttribs( 'import' )
513 "</td>
514 </tr>" .
515 Xml::closeElement( 'table' ) .
516 Xml::closeElement( 'form' ) .
517 Xml::closeElement( 'fieldset' )
522 protected function getGroupName() {
523 return 'pagetools';
528 * Reporting callback
529 * @ingroup SpecialPage
531 class ImportReporter extends ContextSource {
532 private $reason = false;
533 private $mOriginalLogCallback = null;
534 private $mOriginalPageOutCallback = null;
535 private $mLogItemCount = 0;
538 * @param WikiImporter $importer
539 * @param bool $upload
540 * @param string $interwiki
541 * @param string|bool $reason
543 function __construct( $importer, $upload, $interwiki, $reason = false ) {
544 $this->mOriginalPageOutCallback =
545 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
546 $this->mOriginalLogCallback =
547 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
548 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
549 $this->mPageCount = 0;
550 $this->mIsUpload = $upload;
551 $this->mInterwiki = $interwiki;
552 $this->reason = $reason;
555 function open() {
556 $this->getOutput()->addHTML( "<ul>\n" );
559 function reportNotice( $msg, array $params ) {
560 $this->getOutput()->addHTML(
561 Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
565 function reportLogItem( /* ... */ ) {
566 $this->mLogItemCount++;
567 if ( is_callable( $this->mOriginalLogCallback ) ) {
568 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
573 * @param Title $title
574 * @param ForeignTitle $foreignTitle
575 * @param int $revisionCount
576 * @param int $successCount
577 * @param array $pageInfo
578 * @return void
580 function reportPage( $title, $foreignTitle, $revisionCount,
581 $successCount, $pageInfo ) {
582 $args = func_get_args();
583 call_user_func_array( $this->mOriginalPageOutCallback, $args );
585 if ( $title === null ) {
586 # Invalid or non-importable title; a notice is already displayed
587 return;
590 $this->mPageCount++;
592 if ( $successCount > 0 ) {
593 $this->getOutput()->addHTML(
594 "<li>" . Linker::linkKnown( $title ) . " " .
595 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
596 "</li>\n"
599 $logParams = array( '4:number:count' => $successCount );
600 if ( $this->mIsUpload ) {
601 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
602 $successCount )->inContentLanguage()->text();
603 $action = 'upload';
604 } else {
605 $interwikiTitleStr = $this->mInterwiki . ':' . $foreignTitle->getFullText();
606 $interwiki = '[[:' . $interwikiTitleStr . ']]';
607 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
608 $successCount )->params( $interwiki )->inContentLanguage()->text();
609 $action = 'interwiki';
610 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
612 if ( $this->reason ) {
613 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
614 . $this->reason;
617 $logEntry = new ManualLogEntry( 'import', $action );
618 $logEntry->setTarget( $title );
619 $logEntry->setComment( $this->reason );
620 $logEntry->setPerformer( $this->getUser() );
621 $logEntry->setParameters( $logParams );
622 $logid = $logEntry->insert();
623 $logEntry->publish( $logid );
625 $comment = $detail; // quick
626 $dbw = wfGetDB( DB_MASTER );
627 $latest = $title->getLatestRevID();
628 $nullRevision = Revision::newNullRevision(
629 $dbw,
630 $title->getArticleID(),
631 $comment,
632 true,
633 $this->getUser()
636 if ( !is_null( $nullRevision ) ) {
637 $nullRevision->insertOn( $dbw );
638 $page = WikiPage::factory( $title );
639 # Update page record
640 $page->updateRevisionOn( $dbw, $nullRevision );
641 Hooks::run(
642 'NewRevisionFromEditComplete',
643 array( $page, $nullRevision, $latest, $this->getUser() )
646 } else {
647 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
648 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
652 function close() {
653 $out = $this->getOutput();
654 if ( $this->mLogItemCount > 0 ) {
655 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
656 $out->addHTML( Xml::tags( 'li', null, $msg ) );
657 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
658 $out->addHTML( "</ul>\n" );
660 return Status::newFatal( 'importnopages' );
662 $out->addHTML( "</ul>\n" );
664 return Status::newGood( $this->mPageCount );