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 use MediaWiki\MainConfigNames
;
24 use Wikimedia\ParamValidator\ParamValidator
;
27 * API module that imports an XML file like Special:Import does
31 class ApiImport
extends ApiBase
{
33 private WikiImporterFactory
$wikiImporterFactory;
36 * @param ApiMain $main
37 * @param string $action
38 * @param WikiImporterFactory $wikiImporterFactory
40 public function __construct(
43 WikiImporterFactory
$wikiImporterFactory
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' );
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'],
70 $usernamePrefix = $params['interwikisource'];
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(
108 $params['interwikisource'],
111 if ( $params['tags'] ) {
112 $reporter->setChangeTags( $params['tags'] );
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
134 public function getAllowedImportSources() {
135 $importSources = $this->getConfig()->get( MainConfigNames
::ImportSources
);
136 $this->getHookRunner()->onImportSources( $importSources );
139 foreach ( $importSources as $key => $value ) {
140 if ( is_int( $key ) ) {
143 foreach ( $value as $subproject ) {
144 $result[] = "$key:$subproject";
151 public function mustBePosted() {
155 public function isWriteMode() {
159 public function getAllowedParams() {
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,
175 ParamValidator
::PARAM_TYPE
=> 'namespace'
177 'assignknownusers' => false,
180 ParamValidator
::PARAM_TYPE
=> 'tags',
181 ParamValidator
::PARAM_ISMULTI
=> true,
186 public function needsToken() {
190 protected function getExamplesMessages() {
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';