MediaWiki master
ApiImport.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
11use Exception;
17
23class ApiImport extends ApiBase {
24
25 private WikiImporterFactory $wikiImporterFactory;
26
27 public function __construct(
28 ApiMain $main,
29 string $action,
30 WikiImporterFactory $wikiImporterFactory
31 ) {
32 parent::__construct( $main, $action );
33
34 $this->wikiImporterFactory = $wikiImporterFactory;
35 }
36
37 public function execute() {
39 $params = $this->extractRequestParams();
40
41 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
42
43 $isUpload = false;
44 if ( isset( $params['interwikisource'] ) ) {
45 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
46 $this->dieWithError( 'apierror-cantimport' );
47 }
48 if ( !isset( $params['interwikipage'] ) ) {
49 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
50 }
52 $params['interwikisource'],
53 $params['interwikipage'],
54 $params['fullhistory'],
55 $params['templates']
56 );
57 $usernamePrefix = $params['interwikisource'];
58 } else {
59 $isUpload = true;
60 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
61 $this->dieWithError( 'apierror-cantimport-upload' );
62 }
64 $usernamePrefix = (string)$params['interwikiprefix'];
65 if ( $usernamePrefix === '' ) {
66 $encParamName = $this->encodeParamName( 'interwikiprefix' );
67 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
68 }
69 }
70 if ( !$source->isOK() ) {
71 $this->dieStatus( $source );
72 }
73
74 // Check if user can add the log entry tags which were requested
75 if ( $params['tags'] ) {
76 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
77 if ( !$ableToTag->isOK() ) {
78 $this->dieStatus( $ableToTag );
79 }
80 }
81
82 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value, $this->getAuthority() );
83 if ( isset( $params['namespace'] ) ) {
84 $importer->setTargetNamespace( $params['namespace'] );
85 } elseif ( isset( $params['rootpage'] ) ) {
86 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
87 if ( !$statusRootPage->isGood() ) {
88 $this->dieStatus( $statusRootPage );
89 }
90 }
91 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
92 $reporter = new ApiImportReporter(
93 $importer,
94 $isUpload,
95 $params['interwikisource'],
96 $params['summary'],
97 $this
98 );
99 if ( $params['tags'] ) {
100 $reporter->setChangeTags( $params['tags'] );
101 }
102
103 try {
104 $importer->doImport();
105 } catch ( Exception $e ) {
106 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
107 }
108
109 $resultData = $reporter->getData();
110 $result = $this->getResult();
111 ApiResult::setIndexedTagName( $resultData, 'page' );
112 $result->addValue( null, $this->getModuleName(), $resultData );
113 }
114
122 public function getAllowedImportSources() {
123 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
124 $this->getHookRunner()->onImportSources( $importSources );
125
126 $result = [];
127 foreach ( $importSources as $key => $value ) {
128 if ( is_int( $key ) ) {
129 $result[] = $value;
130 } else {
131 foreach ( $value as $subproject ) {
132 $result[] = "$key:$subproject";
133 }
134 }
135 }
136 return $result;
137 }
138
140 public function mustBePosted() {
141 return true;
142 }
143
145 public function isWriteMode() {
146 return true;
147 }
148
150 public function getAllowedParams() {
151 return [
152 'summary' => null,
153 'xml' => [
154 ParamValidator::PARAM_TYPE => 'upload',
155 ],
156 'interwikiprefix' => [
157 ParamValidator::PARAM_TYPE => 'string',
158 ],
159 'interwikisource' => [
160 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
161 ],
162 'interwikipage' => null,
163 'fullhistory' => false,
164 'templates' => false,
165 'namespace' => [
166 ParamValidator::PARAM_TYPE => 'namespace'
167 ],
168 'assignknownusers' => false,
169 'rootpage' => null,
170 'tags' => [
171 ParamValidator::PARAM_TYPE => 'tags',
172 ParamValidator::PARAM_ISMULTI => true,
173 ],
174 ];
175 }
176
178 public function needsToken() {
179 return 'csrf';
180 }
181
183 protected function getExamplesMessages() {
184 return [
185 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
186 'namespace=100&fullhistory=&token=123ABC'
187 => 'apihelp-import-example-import',
188 ];
189 }
190
192 public function getHelpUrls() {
193 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
194 }
195}
196
198class_alias( ApiImport::class, 'ApiImport' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:767
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1359
getResult()
Get the result object.
Definition ApiBase.php:682
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:998
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1524
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:801
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
Import reporter for the API.
API module that imports an XML file like Special:Import does.
Definition ApiImport.php:23
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
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:37
__construct(ApiMain $main, string $action, WikiImporterFactory $wikiImporterFactory)
Definition ApiImport.php:27
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
getAllowedImportSources()
Returns a list of interwiki prefixes corresponding to each defined import source.
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Recent changes tagging.
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
static newFromUpload( $fieldname="xmlimport")
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Factory service for WikiImporter instances.
A class containing constants representing the names of configuration variables.
const ImportSources
Name constant for the ImportSources setting, for use with Config::get()
Service for formatting and validating API parameters.
$source