MediaWiki master
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() {
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, $this->getAuthority() );
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 $this
111 );
112 if ( $params['tags'] ) {
113 $reporter->setChangeTags( $params['tags'] );
114 }
115
116 try {
117 $importer->doImport();
118 } catch ( Exception $e ) {
119 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
120 }
121
122 $resultData = $reporter->getData();
123 $result = $this->getResult();
124 ApiResult::setIndexedTagName( $resultData, 'page' );
125 $result->addValue( null, $this->getModuleName(), $resultData );
126 }
127
135 public function getAllowedImportSources() {
136 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
137 $this->getHookRunner()->onImportSources( $importSources );
138
139 $result = [];
140 foreach ( $importSources as $key => $value ) {
141 if ( is_int( $key ) ) {
142 $result[] = $value;
143 } else {
144 foreach ( $value as $subproject ) {
145 $result[] = "$key:$subproject";
146 }
147 }
148 }
149 return $result;
150 }
151
152 public function mustBePosted() {
153 return true;
154 }
155
156 public function isWriteMode() {
157 return true;
158 }
159
160 public function getAllowedParams() {
161 return [
162 'summary' => null,
163 'xml' => [
164 ParamValidator::PARAM_TYPE => 'upload',
165 ],
166 'interwikiprefix' => [
167 ParamValidator::PARAM_TYPE => 'string',
168 ],
169 'interwikisource' => [
170 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
171 ],
172 'interwikipage' => null,
173 'fullhistory' => false,
174 'templates' => false,
175 'namespace' => [
176 ParamValidator::PARAM_TYPE => 'namespace'
177 ],
178 'assignknownusers' => false,
179 'rootpage' => null,
180 'tags' => [
181 ParamValidator::PARAM_TYPE => 'tags',
182 ParamValidator::PARAM_ISMULTI => true,
183 ],
184 ];
185 }
186
187 public function needsToken() {
188 return 'csrf';
189 }
190
191 protected function getExamplesMessages() {
192 return [
193 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
194 'namespace=100&fullhistory=&token=123ABC'
195 => 'apihelp-import-example-import',
196 ];
197 }
198
199 public function getHelpUrls() {
200 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
201 }
202}
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:64
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1533
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:789
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:985
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1589
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1381
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:756
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1546
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:65
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