Update git submodules
[mediawiki.git] / includes / api / ApiImport.php
blob4d1de51883b24600f2f5c46794e5d94c24bda2d1
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 use MediaWiki\MainConfigNames;
24 use Wikimedia\ParamValidator\ParamValidator;
26 /**
27 * API module that imports an XML file like Special:Import does
29 * @ingroup API
31 class ApiImport extends ApiBase {
33 private WikiImporterFactory $wikiImporterFactory;
35 /**
36 * @param ApiMain $main
37 * @param string $action
38 * @param WikiImporterFactory $wikiImporterFactory
40 public function __construct(
41 ApiMain $main,
42 $action,
43 WikiImporterFactory $wikiImporterFactory
44 ) {
45 parent::__construct( $main, $action );
47 $this->wikiImporterFactory = $wikiImporterFactory;
50 public function execute() {
51 $this->useTransactionalTimeLimit();
52 $params = $this->extractRequestParams();
54 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
56 $isUpload = false;
57 if ( isset( $params['interwikisource'] ) ) {
58 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
59 $this->dieWithError( 'apierror-cantimport' );
61 if ( !isset( $params['interwikipage'] ) ) {
62 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
64 $source = ImportStreamSource::newFromInterwiki(
65 $params['interwikisource'],
66 $params['interwikipage'],
67 $params['fullhistory'],
68 $params['templates']
70 $usernamePrefix = $params['interwikisource'];
71 } else {
72 $isUpload = true;
73 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
74 $this->dieWithError( 'apierror-cantimport-upload' );
76 $source = ImportStreamSource::newFromUpload( 'xml' );
77 $usernamePrefix = (string)$params['interwikiprefix'];
78 if ( $usernamePrefix === '' ) {
79 $encParamName = $this->encodeParamName( 'interwikiprefix' );
80 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
83 if ( !$source->isOK() ) {
84 $this->dieStatus( $source );
87 // Check if user can add the log entry tags which were requested
88 if ( $params['tags'] ) {
89 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
90 if ( !$ableToTag->isOK() ) {
91 $this->dieStatus( $ableToTag );
95 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value );
96 if ( isset( $params['namespace'] ) ) {
97 $importer->setTargetNamespace( $params['namespace'] );
98 } elseif ( isset( $params['rootpage'] ) ) {
99 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
100 if ( !$statusRootPage->isGood() ) {
101 $this->dieStatus( $statusRootPage );
104 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
105 $reporter = new ApiImportReporter(
106 $importer,
107 $isUpload,
108 $params['interwikisource'],
109 $params['summary']
111 if ( $params['tags'] ) {
112 $reporter->setChangeTags( $params['tags'] );
115 try {
116 $importer->doImport();
117 } catch ( Exception $e ) {
118 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
121 $resultData = $reporter->getData();
122 $result = $this->getResult();
123 ApiResult::setIndexedTagName( $resultData, 'page' );
124 $result->addValue( null, $this->getModuleName(), $resultData );
128 * Returns a list of interwiki prefixes corresponding to each defined import
129 * source.
131 * @return array
132 * @since 1.27
134 public function getAllowedImportSources() {
135 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
136 $this->getHookRunner()->onImportSources( $importSources );
138 $result = [];
139 foreach ( $importSources as $key => $value ) {
140 if ( is_int( $key ) ) {
141 $result[] = $value;
142 } else {
143 foreach ( $value as $subproject ) {
144 $result[] = "$key:$subproject";
148 return $result;
151 public function mustBePosted() {
152 return true;
155 public function isWriteMode() {
156 return true;
159 public function getAllowedParams() {
160 return [
161 'summary' => null,
162 'xml' => [
163 ParamValidator::PARAM_TYPE => 'upload',
165 'interwikiprefix' => [
166 ParamValidator::PARAM_TYPE => 'string',
168 'interwikisource' => [
169 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
171 'interwikipage' => null,
172 'fullhistory' => false,
173 'templates' => false,
174 'namespace' => [
175 ParamValidator::PARAM_TYPE => 'namespace'
177 'assignknownusers' => false,
178 'rootpage' => null,
179 'tags' => [
180 ParamValidator::PARAM_TYPE => 'tags',
181 ParamValidator::PARAM_ISMULTI => true,
186 public function needsToken() {
187 return 'csrf';
190 protected function getExamplesMessages() {
191 return [
192 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
193 'namespace=100&fullhistory=&token=123ABC'
194 => 'apihelp-import-example-import',
198 public function getHelpUrls() {
199 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';