Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / SpecialImport.php
blobd7424af73b867f09b97d0a8d5ae1a98e3a9475cb
1 <?php
2 /**
3 * Copyright © 2003,2005 Brooke Vibber <bvibber@wikimedia.org>
4 * https://www.mediawiki.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 namespace MediaWiki\Specials;
26 use Exception;
27 use ImportReporter;
28 use ImportStreamSource;
29 use MediaWiki\Html\Html;
30 use MediaWiki\HTMLForm\HTMLForm;
31 use MediaWiki\MainConfigNames;
32 use MediaWiki\Permissions\PermissionStatus;
33 use MediaWiki\SpecialPage\SpecialPage;
34 use MediaWiki\Status\Status;
35 use PermissionsError;
36 use UnexpectedValueException;
37 use WikiImporterFactory;
39 /**
40 * MediaWiki page data importer
42 * @ingroup SpecialPage
44 class SpecialImport extends SpecialPage {
45 /** @var array */
46 private $importSources;
48 private WikiImporterFactory $wikiImporterFactory;
50 /**
51 * @param WikiImporterFactory $wikiImporterFactory
53 public function __construct(
54 WikiImporterFactory $wikiImporterFactory
55 ) {
56 parent::__construct( 'Import', 'import' );
58 $this->wikiImporterFactory = $wikiImporterFactory;
61 public function doesWrites() {
62 return true;
65 /**
66 * Execute
67 * @param string|null $par
69 public function execute( $par ) {
70 $this->useTransactionalTimeLimit();
72 $this->setHeaders();
73 $this->outputHeader();
75 $this->importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
76 // Avoid phan error by checking the type
77 if ( !is_array( $this->importSources ) ) {
78 throw new UnexpectedValueException( '$wgImportSources must be an array' );
80 $this->getHookRunner()->onImportSources( $this->importSources );
82 $authority = $this->getAuthority();
83 $statusImport = PermissionStatus::newEmpty();
84 $authority->isDefinitelyAllowed( 'import', $statusImport );
85 $statusImportUpload = PermissionStatus::newEmpty();
86 $authority->isDefinitelyAllowed( 'importupload', $statusImportUpload );
87 // Only show an error here if the user can't import using either method.
88 // If they can use at least one of the methods, allow access, and checks elsewhere
89 // will ensure that we only show the form(s) they can use.
90 if ( !$statusImport->isGood() && !$statusImportUpload->isGood() ) {
91 // Show separate messages for each check. There isn't a good way to merge them into a single
92 // message if the checks failed for different reasons.
94 $this->getOutput()->prepareErrorPage();
95 $this->getOutput()->setPageTitleMsg( $this->msg( 'permissionserrors' ) );
96 $this->getOutput()->addWikiTextAsInterface( Html::errorBox(
97 $this->getOutput()->formatPermissionStatus( $statusImport, 'import' )
98 ) );
99 $this->getOutput()->addWikiTextAsInterface( Html::errorBox(
100 $this->getOutput()->formatPermissionStatus( $statusImportUpload, 'importupload' )
101 ) );
102 return;
105 $this->getOutput()->addModules( 'mediawiki.misc-authed-ooui' );
106 $this->getOutput()->addModuleStyles( 'mediawiki.special.import.styles.ooui' );
108 $this->checkReadOnly();
110 $request = $this->getRequest();
111 if ( $request->wasPosted() && $request->getRawVal( 'action' ) == 'submit' ) {
112 $this->doImport();
114 $this->showForm();
118 * Do the actual import
120 private function doImport() {
121 $isUpload = false;
122 $request = $this->getRequest();
123 $sourceName = $request->getVal( 'source' );
124 $assignKnownUsers = $request->getCheck( 'assignKnownUsers' );
126 $logcomment = $request->getText( 'log-comment' );
127 $pageLinkDepth = $this->getConfig()->get( MainConfigNames::ExportMaxLinkDepth ) == 0
129 : $request->getIntOrNull( 'pagelink-depth' );
131 $rootpage = '';
132 $mapping = $request->getVal( 'mapping' );
133 $namespace = $this->getConfig()->get( MainConfigNames::ImportTargetNamespace );
134 if ( $mapping === 'namespace' ) {
135 $namespace = $request->getIntOrNull( 'namespace' );
136 } elseif ( $mapping === 'subpage' ) {
137 $rootpage = $request->getText( 'rootpage' );
140 $user = $this->getUser();
141 $authority = $this->getAuthority();
142 $status = PermissionStatus::newEmpty();
144 $fullInterwikiPrefix = null;
145 if ( !$user->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
146 $source = Status::newFatal( 'import-token-mismatch' );
147 } elseif ( $sourceName === 'upload' ) {
148 $isUpload = true;
149 $fullInterwikiPrefix = $request->getVal( 'usernamePrefix' );
150 if ( $authority->authorizeAction( 'importupload', $status ) ) {
151 $source = ImportStreamSource::newFromUpload( "xmlimport" );
152 } else {
153 throw new PermissionsError( 'importupload', $status );
155 } elseif ( $sourceName === 'interwiki' ) {
156 if ( !$authority->authorizeAction( 'import', $status ) ) {
157 throw new PermissionsError( 'import', $status );
159 $interwiki = $fullInterwikiPrefix = $request->getVal( 'interwiki' );
160 // does this interwiki have subprojects?
161 $hasSubprojects = array_key_exists( $interwiki, $this->importSources );
162 if ( !$hasSubprojects && !in_array( $interwiki, $this->importSources ) ) {
163 $source = Status::newFatal( "import-invalid-interwiki" );
164 } else {
165 $subproject = null;
166 if ( $hasSubprojects ) {
167 $subproject = $request->getVal( 'subproject' );
168 // Trim "project::" prefix added for JS
169 if ( str_starts_with( $subproject, $interwiki . '::' ) ) {
170 $subproject = substr( $subproject, strlen( $interwiki . '::' ) );
172 $fullInterwikiPrefix .= ':' . $subproject;
174 if ( $hasSubprojects &&
175 !in_array( $subproject, $this->importSources[$interwiki] )
177 $source = Status::newFatal( 'import-invalid-interwiki' );
178 } else {
179 $history = $request->getCheck( 'interwikiHistory' );
180 $frompage = $request->getText( 'frompage' );
181 $includeTemplates = $request->getCheck( 'interwikiTemplates' );
182 $source = ImportStreamSource::newFromInterwiki(
183 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
184 $fullInterwikiPrefix,
185 $frompage,
186 $history,
187 $includeTemplates,
188 $pageLinkDepth );
191 } else {
192 $source = Status::newFatal( "importunknownsource" );
195 if ( (string)$fullInterwikiPrefix === '' ) {
196 $source->fatal( 'importnoprefix' );
199 $out = $this->getOutput();
200 if ( !$source->isGood() ) {
201 $out->wrapWikiMsg(
202 Html::errorBox( '$1' ),
204 'importfailed',
205 $source->getWikiText( false, false, $this->getLanguage() ),
206 count( $source->getMessages() )
209 } else {
210 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value, $this->getAuthority() );
211 if ( $namespace !== null ) {
212 $importer->setTargetNamespace( $namespace );
213 } elseif ( $rootpage !== null ) {
214 $statusRootPage = $importer->setTargetRootPage( $rootpage );
215 if ( !$statusRootPage->isGood() ) {
216 $out->wrapWikiMsg(
217 Html::errorBox( '$1' ),
219 'import-options-wrong',
220 $statusRootPage->getWikiText( false, false, $this->getLanguage() ),
221 count( $statusRootPage->getMessages() )
225 return;
228 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
229 $importer->setUsernamePrefix( $fullInterwikiPrefix, $assignKnownUsers );
231 $out->addWikiMsg( "importstart" );
233 $reporter = new ImportReporter(
234 $importer,
235 $isUpload,
236 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
237 $fullInterwikiPrefix,
238 $logcomment,
239 $this->getContext()
241 $exception = false;
243 $reporter->open();
244 try {
245 $importer->doImport();
246 } catch ( Exception $e ) {
247 $exception = $e;
249 $result = $reporter->close();
251 if ( $exception ) {
252 # No source or XML parse error
253 $out->wrapWikiMsg(
254 Html::errorBox( '$1' ),
255 [ 'importfailed', wfEscapeWikiText( $exception->getMessage() ), 1 ]
257 } elseif ( !$result->isGood() ) {
258 # Zero revisions
259 $out->wrapWikiMsg(
260 Html::errorBox( '$1' ),
262 'importfailed',
263 $result->getWikiText( false, false, $this->getLanguage() ),
264 count( $result->getMessages() )
267 } else {
268 # Success!
269 $out->addWikiMsg( 'importsuccess' );
271 $out->addHTML( '<hr />' );
275 private function getMappingFormPart( $sourceName ) {
276 $defaultNamespace = $this->getConfig()->get( MainConfigNames::ImportTargetNamespace );
277 return [
278 'mapping' => [
279 'type' => 'radio',
280 'name' => 'mapping',
281 // IDs: mw-import-mapping-interwiki, mw-import-mapping-upload
282 'id' => "mw-import-mapping-$sourceName",
283 'options-messages' => [
284 'import-mapping-default' => 'default',
285 'import-mapping-namespace' => 'namespace',
286 'import-mapping-subpage' => 'subpage'
288 'default' => $defaultNamespace !== null ? 'namespace' : 'default'
290 'namespace' => [
291 'type' => 'namespaceselect',
292 'name' => 'namespace',
293 // IDs: mw-import-namespace-interwiki, mw-import-namespace-upload
294 'id' => "mw-import-namespace-$sourceName",
295 'default' => $defaultNamespace ?: '',
296 'all' => null,
297 'disable-if' => [ '!==', 'mapping', 'namespace' ],
299 'rootpage' => [
300 'type' => 'text',
301 'name' => 'rootpage',
302 // Should be "mw-import-...", but we keep the inaccurate ID for compat
303 // IDs: mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
304 'id' => "mw-interwiki-rootpage-$sourceName",
305 'disable-if' => [ '!==', 'mapping', 'subpage' ],
310 private function showForm() {
311 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
312 $authority = $this->getAuthority();
313 $out = $this->getOutput();
314 $this->addHelpLink( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Import', true );
316 $interwikiFormDescriptor = [];
317 $uploadFormDescriptor = [];
319 if ( $authority->isDefinitelyAllowed( 'importupload' ) ) {
320 $mappingSelection = $this->getMappingFormPart( 'upload' );
321 $uploadFormDescriptor += [
322 'intro' => [
323 'type' => 'info',
324 'raw' => true,
325 'default' => $this->msg( 'importtext' )->parseAsBlock()
327 'xmlimport' => [
328 'type' => 'file',
329 'name' => 'xmlimport',
330 'accept' => [ 'application/xml', 'text/xml' ],
331 'label-message' => 'import-upload-filename',
332 'required' => true,
334 'usernamePrefix' => [
335 'type' => 'text',
336 'name' => 'usernamePrefix',
337 'label-message' => 'import-upload-username-prefix',
338 'required' => true,
340 'assignKnownUsers' => [
341 'type' => 'check',
342 'name' => 'assignKnownUsers',
343 'label-message' => 'import-assign-known-users'
345 'log-comment' => [
346 'type' => 'text',
347 'name' => 'log-comment',
348 'label-message' => 'import-comment'
350 'source' => [
351 'type' => 'hidden',
352 'name' => 'source',
353 'default' => 'upload',
354 'id' => '',
358 $uploadFormDescriptor += $mappingSelection;
360 $htmlForm = HTMLForm::factory( 'ooui', $uploadFormDescriptor, $this->getContext() );
361 $htmlForm->setAction( $action );
362 $htmlForm->setId( 'mw-import-upload-form' );
363 $htmlForm->setWrapperLegendMsg( 'import-upload' );
364 $htmlForm->setSubmitTextMsg( 'uploadbtn' );
365 $htmlForm->prepareForm()->displayForm( false );
367 } elseif ( !$this->importSources ) {
368 $out->addWikiMsg( 'importnosources' );
371 if ( $authority->isDefinitelyAllowed( 'import' ) && $this->importSources ) {
373 $projects = [];
374 $needSubprojectField = false;
375 foreach ( $this->importSources as $key => $value ) {
376 if ( is_int( $key ) ) {
377 $key = $value;
378 } elseif ( $value !== $key ) {
379 $needSubprojectField = true;
382 $projects[ $key ] = $key;
385 $interwikiFormDescriptor += [
386 'intro' => [
387 'type' => 'info',
388 'raw' => true,
389 'default' => $this->msg( 'import-interwiki-text' )->parseAsBlock()
391 'interwiki' => [
392 'type' => 'select',
393 'name' => 'interwiki',
394 'label-message' => 'import-interwiki-sourcewiki',
395 'options' => $projects
399 if ( $needSubprojectField ) {
400 $subprojects = [];
401 foreach ( $this->importSources as $key => $value ) {
402 if ( is_array( $value ) ) {
403 foreach ( $value as $subproject ) {
404 $subprojects[ $subproject ] = $key . '::' . $subproject;
409 $interwikiFormDescriptor += [
410 'subproject' => [
411 'type' => 'select',
412 'name' => 'subproject',
413 'options' => $subprojects
418 $interwikiFormDescriptor += [
419 'frompage' => [
420 'type' => 'text',
421 'name' => 'frompage',
422 'label-message' => 'import-interwiki-sourcepage'
424 'interwikiHistory' => [
425 'type' => 'check',
426 'name' => 'interwikiHistory',
427 'label-message' => 'import-interwiki-history'
429 'interwikiTemplates' => [
430 'type' => 'check',
431 'name' => 'interwikiTemplates',
432 'label-message' => 'import-interwiki-templates'
434 'assignKnownUsers' => [
435 'type' => 'check',
436 'name' => 'assignKnownUsers',
437 'label-message' => 'import-assign-known-users'
441 if ( $this->getConfig()->get( MainConfigNames::ExportMaxLinkDepth ) > 0 ) {
442 $interwikiFormDescriptor += [
443 'pagelink-depth' => [
444 'type' => 'int',
445 'name' => 'pagelink-depth',
446 'label-message' => 'export-pagelinks',
447 'default' => 0
452 $interwikiFormDescriptor += [
453 'log-comment' => [
454 'type' => 'text',
455 'name' => 'log-comment',
456 'label-message' => 'import-comment'
458 'source' => [
459 'type' => 'hidden',
460 'name' => 'source',
461 'default' => 'interwiki',
462 'id' => '',
465 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
467 $interwikiFormDescriptor += $mappingSelection;
469 $htmlForm = HTMLForm::factory( 'ooui', $interwikiFormDescriptor, $this->getContext() );
470 $htmlForm->setAction( $action );
471 $htmlForm->setId( 'mw-import-interwiki-form' );
472 $htmlForm->setWrapperLegendMsg( 'importinterwiki' );
473 $htmlForm->setSubmitTextMsg( 'import-interwiki-submit' );
474 $htmlForm->prepareForm()->displayForm( false );
478 protected function getGroupName() {
479 return 'pagetools';
483 /** @deprecated class alias since 1.41 */
484 class_alias( SpecialImport::class, 'SpecialImport' );