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
23 namespace MediaWiki\Api
;
27 use ImportStreamSource
;
28 use MediaWiki\MainConfigNames
;
29 use WikiImporterFactory
;
30 use Wikimedia\ParamValidator\ParamValidator
;
33 * API module that imports an XML file like Special:Import does
37 class ApiImport
extends ApiBase
{
39 private WikiImporterFactory
$wikiImporterFactory;
41 public function __construct(
44 WikiImporterFactory
$wikiImporterFactory
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' );
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'],
71 $usernamePrefix = $params['interwikisource'];
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(
109 $params['interwikisource'],
113 if ( $params['tags'] ) {
114 $reporter->setChangeTags( $params['tags'] );
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
136 public function getAllowedImportSources() {
137 $importSources = $this->getConfig()->get( MainConfigNames
::ImportSources
);
138 $this->getHookRunner()->onImportSources( $importSources );
141 foreach ( $importSources as $key => $value ) {
142 if ( is_int( $key ) ) {
145 foreach ( $value as $subproject ) {
146 $result[] = "$key:$subproject";
153 public function mustBePosted() {
157 public function isWriteMode() {
161 public function getAllowedParams() {
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,
177 ParamValidator
::PARAM_TYPE
=> 'namespace'
179 'assignknownusers' => false,
182 ParamValidator
::PARAM_TYPE
=> 'tags',
183 ParamValidator
::PARAM_ISMULTI
=> true,
188 public function needsToken() {
192 protected function getExamplesMessages() {
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' );