MediaWiki REL1_41
ApiImport.php
Go to the documentation of this file.
1<?php
25
31class ApiImport extends ApiBase {
32
33 private WikiImporterFactory $wikiImporterFactory;
34
40 public function __construct(
41 ApiMain $main,
42 $action,
43 WikiImporterFactory $wikiImporterFactory
44 ) {
45 parent::__construct( $main, $action );
46
47 $this->wikiImporterFactory = $wikiImporterFactory;
48 }
49
50 public function execute() {
52 $params = $this->extractRequestParams();
53
54 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
55
56 $isUpload = false;
57 if ( isset( $params['interwikisource'] ) ) {
58 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
59 $this->dieWithError( 'apierror-cantimport' );
60 }
61 if ( !isset( $params['interwikipage'] ) ) {
62 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
63 }
64 $source = ImportStreamSource::newFromInterwiki(
65 $params['interwikisource'],
66 $params['interwikipage'],
67 $params['fullhistory'],
68 $params['templates']
69 );
70 $usernamePrefix = $params['interwikisource'];
71 } else {
72 $isUpload = true;
73 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
74 $this->dieWithError( 'apierror-cantimport-upload' );
75 }
76 $source = ImportStreamSource::newFromUpload( 'xml' );
77 $usernamePrefix = (string)$params['interwikiprefix'];
78 if ( $usernamePrefix === '' ) {
79 $encParamName = $this->encodeParamName( 'interwikiprefix' );
80 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
81 }
82 }
83 if ( !$source->isOK() ) {
84 $this->dieStatus( $source );
85 }
86
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 );
92 }
93 }
94
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 );
102 }
103 }
104 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
105 $reporter = new ApiImportReporter(
106 $importer,
107 $isUpload,
108 $params['interwikisource'],
109 $params['summary']
110 );
111 if ( $params['tags'] ) {
112 $reporter->setChangeTags( $params['tags'] );
113 }
114
115 try {
116 $importer->doImport();
117 } catch ( Exception $e ) {
118 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
119 }
120
121 $resultData = $reporter->getData();
122 $result = $this->getResult();
123 ApiResult::setIndexedTagName( $resultData, 'page' );
124 $result->addValue( null, $this->getModuleName(), $resultData );
125 }
126
134 public function getAllowedImportSources() {
135 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
136 $this->getHookRunner()->onImportSources( $importSources );
137
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";
145 }
146 }
147 }
148 return $result;
149 }
150
151 public function mustBePosted() {
152 return true;
153 }
154
155 public function isWriteMode() {
156 return true;
157 }
158
159 public function getAllowedParams() {
160 return [
161 'summary' => null,
162 'xml' => [
163 ParamValidator::PARAM_TYPE => 'upload',
164 ],
165 'interwikiprefix' => [
166 ParamValidator::PARAM_TYPE => 'string',
167 ],
168 'interwikisource' => [
169 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
170 ],
171 'interwikipage' => null,
172 'fullhistory' => false,
173 'templates' => false,
174 'namespace' => [
175 ParamValidator::PARAM_TYPE => 'namespace'
176 ],
177 'assignknownusers' => false,
178 'rootpage' => null,
179 'tags' => [
180 ParamValidator::PARAM_TYPE => 'tags',
181 ParamValidator::PARAM_ISMULTI => true,
182 ],
183 ];
184 }
185
186 public function needsToken() {
187 return 'csrf';
188 }
189
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',
195 ];
196 }
197
198 public function getHelpUrls() {
199 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
200 }
201}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:62
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1515
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:785
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:981
getResult()
Get the result object.
Definition ApiBase.php:667
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:807
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:528
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1570
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1363
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:752
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1528
Import reporter for the API.
API module that imports an XML file like Special:Import does.
Definition ApiImport.php:31
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiImport.php:50
needsToken()
Returns the token type this module requires in order to execute.
isWriteMode()
Indicates whether this module requires write mode.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
mustBePosted()
Indicates whether this module must be called with a POST request.
__construct(ApiMain $main, $action, WikiImporterFactory $wikiImporterFactory)
Definition ApiImport.php:40
getAllowedImportSources()
Returns a list of interwiki prefixes corresponding to each defined import source.
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:64
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
A class containing constants representing the names of configuration variables.
Factory service for WikiImporter instances.
Service for formatting and validating API parameters.
$source