Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiImport.php
blob5db21bd84d5ea2bb79392ee042b4a1bfd194b2ee
1 <?php
2 /**
3 * Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use ChangeTags;
26 use Exception;
27 use ImportStreamSource;
28 use MediaWiki\MainConfigNames;
29 use WikiImporterFactory;
30 use Wikimedia\ParamValidator\ParamValidator;
32 /**
33 * API module that imports an XML file like Special:Import does
35 * @ingroup API
37 class ApiImport extends ApiBase {
39 private WikiImporterFactory $wikiImporterFactory;
41 public function __construct(
42 ApiMain $main,
43 string $action,
44 WikiImporterFactory $wikiImporterFactory
45 ) {
46 parent::__construct( $main, $action );
48 $this->wikiImporterFactory = $wikiImporterFactory;
51 public function execute() {
52 $this->useTransactionalTimeLimit();
53 $params = $this->extractRequestParams();
55 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
57 $isUpload = false;
58 if ( isset( $params['interwikisource'] ) ) {
59 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
60 $this->dieWithError( 'apierror-cantimport' );
62 if ( !isset( $params['interwikipage'] ) ) {
63 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
65 $source = ImportStreamSource::newFromInterwiki(
66 $params['interwikisource'],
67 $params['interwikipage'],
68 $params['fullhistory'],
69 $params['templates']
71 $usernamePrefix = $params['interwikisource'];
72 } else {
73 $isUpload = true;
74 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
75 $this->dieWithError( 'apierror-cantimport-upload' );
77 $source = ImportStreamSource::newFromUpload( 'xml' );
78 $usernamePrefix = (string)$params['interwikiprefix'];
79 if ( $usernamePrefix === '' ) {
80 $encParamName = $this->encodeParamName( 'interwikiprefix' );
81 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
84 if ( !$source->isOK() ) {
85 $this->dieStatus( $source );
88 // Check if user can add the log entry tags which were requested
89 if ( $params['tags'] ) {
90 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
91 if ( !$ableToTag->isOK() ) {
92 $this->dieStatus( $ableToTag );
96 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value, $this->getAuthority() );
97 if ( isset( $params['namespace'] ) ) {
98 $importer->setTargetNamespace( $params['namespace'] );
99 } elseif ( isset( $params['rootpage'] ) ) {
100 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
101 if ( !$statusRootPage->isGood() ) {
102 $this->dieStatus( $statusRootPage );
105 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
106 $reporter = new ApiImportReporter(
107 $importer,
108 $isUpload,
109 $params['interwikisource'],
110 $params['summary'],
111 $this
113 if ( $params['tags'] ) {
114 $reporter->setChangeTags( $params['tags'] );
117 try {
118 $importer->doImport();
119 } catch ( Exception $e ) {
120 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
123 $resultData = $reporter->getData();
124 $result = $this->getResult();
125 ApiResult::setIndexedTagName( $resultData, 'page' );
126 $result->addValue( null, $this->getModuleName(), $resultData );
130 * Returns a list of interwiki prefixes corresponding to each defined import
131 * source.
133 * @return array
134 * @since 1.27
136 public function getAllowedImportSources() {
137 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
138 $this->getHookRunner()->onImportSources( $importSources );
140 $result = [];
141 foreach ( $importSources as $key => $value ) {
142 if ( is_int( $key ) ) {
143 $result[] = $value;
144 } else {
145 foreach ( $value as $subproject ) {
146 $result[] = "$key:$subproject";
150 return $result;
153 public function mustBePosted() {
154 return true;
157 public function isWriteMode() {
158 return true;
161 public function getAllowedParams() {
162 return [
163 'summary' => null,
164 'xml' => [
165 ParamValidator::PARAM_TYPE => 'upload',
167 'interwikiprefix' => [
168 ParamValidator::PARAM_TYPE => 'string',
170 'interwikisource' => [
171 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
173 'interwikipage' => null,
174 'fullhistory' => false,
175 'templates' => false,
176 'namespace' => [
177 ParamValidator::PARAM_TYPE => 'namespace'
179 'assignknownusers' => false,
180 'rootpage' => null,
181 'tags' => [
182 ParamValidator::PARAM_TYPE => 'tags',
183 ParamValidator::PARAM_ISMULTI => true,
188 public function needsToken() {
189 return 'csrf';
192 protected function getExamplesMessages() {
193 return [
194 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
195 'namespace=100&fullhistory=&token=123ABC'
196 => 'apihelp-import-example-import',
200 public function getHelpUrls() {
201 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
205 /** @deprecated class alias since 1.43 */
206 class_alias( ApiImport::class, 'ApiImport' );