MediaWiki master
ApiImport.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
25use ChangeTags;
26use Exception;
31
37class ApiImport extends ApiBase {
38
39 private WikiImporterFactory $wikiImporterFactory;
40
46 public function __construct(
47 ApiMain $main,
48 $action,
49 WikiImporterFactory $wikiImporterFactory
50 ) {
51 parent::__construct( $main, $action );
52
53 $this->wikiImporterFactory = $wikiImporterFactory;
54 }
55
56 public function execute() {
59
60 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
61
62 $isUpload = false;
63 if ( isset( $params['interwikisource'] ) ) {
64 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
65 $this->dieWithError( 'apierror-cantimport' );
66 }
67 if ( !isset( $params['interwikipage'] ) ) {
68 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
69 }
71 $params['interwikisource'],
72 $params['interwikipage'],
73 $params['fullhistory'],
74 $params['templates']
75 );
76 $usernamePrefix = $params['interwikisource'];
77 } else {
78 $isUpload = true;
79 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
80 $this->dieWithError( 'apierror-cantimport-upload' );
81 }
83 $usernamePrefix = (string)$params['interwikiprefix'];
84 if ( $usernamePrefix === '' ) {
85 $encParamName = $this->encodeParamName( 'interwikiprefix' );
86 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
87 }
88 }
89 if ( !$source->isOK() ) {
90 $this->dieStatus( $source );
91 }
92
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 );
98 }
99 }
100
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 );
108 }
109 }
110 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
111 $reporter = new ApiImportReporter(
112 $importer,
113 $isUpload,
114 $params['interwikisource'],
115 $params['summary'],
116 $this
117 );
118 if ( $params['tags'] ) {
119 $reporter->setChangeTags( $params['tags'] );
120 }
121
122 try {
123 $importer->doImport();
124 } catch ( Exception $e ) {
125 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
126 }
127
128 $resultData = $reporter->getData();
129 $result = $this->getResult();
130 ApiResult::setIndexedTagName( $resultData, 'page' );
131 $result->addValue( null, $this->getModuleName(), $resultData );
132 }
133
141 public function getAllowedImportSources() {
142 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
143 $this->getHookRunner()->onImportSources( $importSources );
144
145 $result = [];
146 foreach ( $importSources as $key => $value ) {
147 if ( is_int( $key ) ) {
148 $result[] = $value;
149 } else {
150 foreach ( $value as $subproject ) {
151 $result[] = "$key:$subproject";
152 }
153 }
154 }
155 return $result;
156 }
157
158 public function mustBePosted() {
159 return true;
160 }
161
162 public function isWriteMode() {
163 return true;
164 }
165
166 public function getAllowedParams() {
167 return [
168 'summary' => null,
169 'xml' => [
170 ParamValidator::PARAM_TYPE => 'upload',
171 ],
172 'interwikiprefix' => [
173 ParamValidator::PARAM_TYPE => 'string',
174 ],
175 'interwikisource' => [
176 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
177 ],
178 'interwikipage' => null,
179 'fullhistory' => false,
180 'templates' => false,
181 'namespace' => [
182 ParamValidator::PARAM_TYPE => 'namespace'
183 ],
184 'assignknownusers' => false,
185 'rootpage' => null,
186 'tags' => [
187 ParamValidator::PARAM_TYPE => 'tags',
188 ParamValidator::PARAM_ISMULTI => true,
189 ],
190 ];
191 }
192
193 public function needsToken() {
194 return 'csrf';
195 }
196
197 protected function getExamplesMessages() {
198 return [
199 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
200 'namespace=100&fullhistory=&token=123ABC'
201 => 'apihelp-import-example-import',
202 ];
203 }
204
205 public function getHelpUrls() {
206 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
207 }
208}
209
211class_alias( ApiImport::class, 'ApiImport' );
array $params
The job parameters.
Recent changes tagging.
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...
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromUpload( $fieldname="xmlimport")
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:795
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1425
getResult()
Get the result object.
Definition ApiBase.php:710
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:1025
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1590
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:829
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1632
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
Import reporter for the API.
API module that imports an XML file like Special:Import does.
Definition ApiImport.php:37
needsToken()
Returns the token type this module requires in order to execute.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiMain $main, $action, WikiImporterFactory $wikiImporterFactory)
Definition ApiImport.php:46
mustBePosted()
Indicates whether this module must be called with a POST request.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiImport.php:56
isWriteMode()
Indicates whether this module requires write access to the wiki.
getAllowedImportSources()
Returns a list of interwiki prefixes corresponding to each defined import source.
getExamplesMessages()
Returns usage examples for this module.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
A class containing constants representing the names of configuration variables.
const ImportSources
Name constant for the ImportSources setting, for use with Config::get()
Factory service for WikiImporter instances.
Service for formatting and validating API parameters.
$source