Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 99
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiImport
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 9
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
210
 getAllowedImportSources
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23namespace MediaWiki\Api;
24
25use ChangeTags;
26use Exception;
27use ImportStreamSource;
28use MediaWiki\MainConfigNames;
29use WikiImporterFactory;
30use Wikimedia\ParamValidator\ParamValidator;
31
32/**
33 * API module that imports an XML file like Special:Import does
34 *
35 * @ingroup API
36 */
37class ApiImport extends ApiBase {
38
39    private WikiImporterFactory $wikiImporterFactory;
40
41    public function __construct(
42        ApiMain $main,
43        string $action,
44        WikiImporterFactory $wikiImporterFactory
45    ) {
46        parent::__construct( $main, $action );
47
48        $this->wikiImporterFactory = $wikiImporterFactory;
49    }
50
51    public function execute() {
52        $this->useTransactionalTimeLimit();
53        $params = $this->extractRequestParams();
54
55        $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
56
57        $isUpload = false;
58        if ( isset( $params['interwikisource'] ) ) {
59            if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
60                $this->dieWithError( 'apierror-cantimport' );
61            }
62            if ( !isset( $params['interwikipage'] ) ) {
63                $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
64            }
65            $source = ImportStreamSource::newFromInterwiki(
66                $params['interwikisource'],
67                $params['interwikipage'],
68                $params['fullhistory'],
69                $params['templates']
70            );
71            $usernamePrefix = $params['interwikisource'];
72        } else {
73            $isUpload = true;
74            if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
75                $this->dieWithError( 'apierror-cantimport-upload' );
76            }
77            $source = ImportStreamSource::newFromUpload( 'xml' );
78            $usernamePrefix = (string)$params['interwikiprefix'];
79            if ( $usernamePrefix === '' ) {
80                $encParamName = $this->encodeParamName( 'interwikiprefix' );
81                $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
82            }
83        }
84        if ( !$source->isOK() ) {
85            $this->dieStatus( $source );
86        }
87
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 );
93            }
94        }
95
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 );
103            }
104        }
105        $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
106        $reporter = new ApiImportReporter(
107            $importer,
108            $isUpload,
109            $params['interwikisource'],
110            $params['summary'],
111            $this
112        );
113        if ( $params['tags'] ) {
114            $reporter->setChangeTags( $params['tags'] );
115        }
116
117        try {
118            $importer->doImport();
119        } catch ( Exception $e ) {
120            $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
121        }
122
123        $resultData = $reporter->getData();
124        $result = $this->getResult();
125        ApiResult::setIndexedTagName( $resultData, 'page' );
126        $result->addValue( null, $this->getModuleName(), $resultData );
127    }
128
129    /**
130     * Returns a list of interwiki prefixes corresponding to each defined import
131     * source.
132     *
133     * @return array
134     * @since 1.27
135     */
136    public function getAllowedImportSources() {
137        $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
138        $this->getHookRunner()->onImportSources( $importSources );
139
140        $result = [];
141        foreach ( $importSources as $key => $value ) {
142            if ( is_int( $key ) ) {
143                $result[] = $value;
144            } else {
145                foreach ( $value as $subproject ) {
146                    $result[] = "$key:$subproject";
147                }
148            }
149        }
150        return $result;
151    }
152
153    public function mustBePosted() {
154        return true;
155    }
156
157    public function isWriteMode() {
158        return true;
159    }
160
161    public function getAllowedParams() {
162        return [
163            'summary' => null,
164            'xml' => [
165                ParamValidator::PARAM_TYPE => 'upload',
166            ],
167            'interwikiprefix' => [
168                ParamValidator::PARAM_TYPE => 'string',
169            ],
170            'interwikisource' => [
171                ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
172            ],
173            'interwikipage' => null,
174            'fullhistory' => false,
175            'templates' => false,
176            'namespace' => [
177                ParamValidator::PARAM_TYPE => 'namespace'
178            ],
179            'assignknownusers' => false,
180            'rootpage' => null,
181            'tags' => [
182                ParamValidator::PARAM_TYPE => 'tags',
183                ParamValidator::PARAM_ISMULTI => true,
184            ],
185        ];
186    }
187
188    public function needsToken() {
189        return 'csrf';
190    }
191
192    protected function getExamplesMessages() {
193        return [
194            'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
195                'namespace=100&fullhistory=&token=123ABC'
196                => 'apihelp-import-example-import',
197        ];
198    }
199
200    public function getHelpUrls() {
201        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
202    }
203}
204
205/** @deprecated class alias since 1.43 */
206class_alias( ApiImport::class, 'ApiImport' );