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;
42 * @param ApiMain $main
43 * @param string $action
44 * @param WikiImporterFactory $wikiImporterFactory
46 public function __construct(
49 WikiImporterFactory
$wikiImporterFactory
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' );
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'],
76 $usernamePrefix = $params['interwikisource'];
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(
114 $params['interwikisource'],
118 if ( $params['tags'] ) {
119 $reporter->setChangeTags( $params['tags'] );
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
141 public function getAllowedImportSources() {
142 $importSources = $this->getConfig()->get( MainConfigNames
::ImportSources
);
143 $this->getHookRunner()->onImportSources( $importSources );
146 foreach ( $importSources as $key => $value ) {
147 if ( is_int( $key ) ) {
150 foreach ( $value as $subproject ) {
151 $result[] = "$key:$subproject";
158 public function mustBePosted() {
162 public function isWriteMode() {
166 public function getAllowedParams() {
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,
182 ParamValidator
::PARAM_TYPE
=> 'namespace'
184 'assignknownusers' => false,
187 ParamValidator
::PARAM_TYPE
=> 'tags',
188 ParamValidator
::PARAM_ISMULTI
=> true,
193 public function needsToken() {
197 protected function getExamplesMessages() {
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' );