Merge "SpecialBlock [Vue]: add Pinia store for state management"
[mediawiki.git] / includes / api / ApiImport.php
blobcddb81c57c22da1cafdac3c5217b03f2a5595ba9
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 /**
42 * @param ApiMain $main
43 * @param string $action
44 * @param WikiImporterFactory $wikiImporterFactory
46 public function __construct(
47 ApiMain $main,
48 $action,
49 WikiImporterFactory $wikiImporterFactory
50 ) {
51 parent::__construct( $main, $action );
53 $this->wikiImporterFactory = $wikiImporterFactory;
56 public function execute() {
57 $this->useTransactionalTimeLimit();
58 $params = $this->extractRequestParams();
60 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
62 $isUpload = false;
63 if ( isset( $params['interwikisource'] ) ) {
64 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
65 $this->dieWithError( 'apierror-cantimport' );
67 if ( !isset( $params['interwikipage'] ) ) {
68 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
70 $source = ImportStreamSource::newFromInterwiki(
71 $params['interwikisource'],
72 $params['interwikipage'],
73 $params['fullhistory'],
74 $params['templates']
76 $usernamePrefix = $params['interwikisource'];
77 } else {
78 $isUpload = true;
79 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
80 $this->dieWithError( 'apierror-cantimport-upload' );
82 $source = ImportStreamSource::newFromUpload( 'xml' );
83 $usernamePrefix = (string)$params['interwikiprefix'];
84 if ( $usernamePrefix === '' ) {
85 $encParamName = $this->encodeParamName( 'interwikiprefix' );
86 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
89 if ( !$source->isOK() ) {
90 $this->dieStatus( $source );
93 // Check if user can add the log entry tags which were requested
94 if ( $params['tags'] ) {
95 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
96 if ( !$ableToTag->isOK() ) {
97 $this->dieStatus( $ableToTag );
101 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value, $this->getAuthority() );
102 if ( isset( $params['namespace'] ) ) {
103 $importer->setTargetNamespace( $params['namespace'] );
104 } elseif ( isset( $params['rootpage'] ) ) {
105 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
106 if ( !$statusRootPage->isGood() ) {
107 $this->dieStatus( $statusRootPage );
110 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
111 $reporter = new ApiImportReporter(
112 $importer,
113 $isUpload,
114 $params['interwikisource'],
115 $params['summary'],
116 $this
118 if ( $params['tags'] ) {
119 $reporter->setChangeTags( $params['tags'] );
122 try {
123 $importer->doImport();
124 } catch ( Exception $e ) {
125 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
128 $resultData = $reporter->getData();
129 $result = $this->getResult();
130 ApiResult::setIndexedTagName( $resultData, 'page' );
131 $result->addValue( null, $this->getModuleName(), $resultData );
135 * Returns a list of interwiki prefixes corresponding to each defined import
136 * source.
138 * @return array
139 * @since 1.27
141 public function getAllowedImportSources() {
142 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
143 $this->getHookRunner()->onImportSources( $importSources );
145 $result = [];
146 foreach ( $importSources as $key => $value ) {
147 if ( is_int( $key ) ) {
148 $result[] = $value;
149 } else {
150 foreach ( $value as $subproject ) {
151 $result[] = "$key:$subproject";
155 return $result;
158 public function mustBePosted() {
159 return true;
162 public function isWriteMode() {
163 return true;
166 public function getAllowedParams() {
167 return [
168 'summary' => null,
169 'xml' => [
170 ParamValidator::PARAM_TYPE => 'upload',
172 'interwikiprefix' => [
173 ParamValidator::PARAM_TYPE => 'string',
175 'interwikisource' => [
176 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
178 'interwikipage' => null,
179 'fullhistory' => false,
180 'templates' => false,
181 'namespace' => [
182 ParamValidator::PARAM_TYPE => 'namespace'
184 'assignknownusers' => false,
185 'rootpage' => null,
186 'tags' => [
187 ParamValidator::PARAM_TYPE => 'tags',
188 ParamValidator::PARAM_ISMULTI => true,
193 public function needsToken() {
194 return 'csrf';
197 protected function getExamplesMessages() {
198 return [
199 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
200 'namespace=100&fullhistory=&token=123ABC'
201 => 'apihelp-import-example-import',
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
210 /** @deprecated class alias since 1.43 */
211 class_alias( ApiImport::class, 'ApiImport' );