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 public function __construct(
26 ApiMain $main,
27 string $action,
28 private readonly WikiImporterFactory $wikiImporterFactory,
29 ) {
30 parent::__construct( $main, $action );
31 }
32
33 public function execute() {
35 $params = $this->extractRequestParams();
36
37 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
38
39 $isUpload = false;
40 if ( isset( $params['interwikisource'] ) ) {
41 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
42 $this->dieWithError( 'apierror-cantimport' );
43 }
44 if ( !isset( $params['interwikipage'] ) ) {
45 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
46 }
48 $params['interwikisource'],
49 $params['interwikipage'],
50 $params['fullhistory'],
51 $params['templates']
52 );
53 $usernamePrefix = $params['interwikisource'];
54 } else {
55 $isUpload = true;
56 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
57 $this->dieWithError( 'apierror-cantimport-upload' );
58 }
60 $usernamePrefix = (string)$params['interwikiprefix'];
61 if ( $usernamePrefix === '' ) {
62 $encParamName = $this->encodeParamName( 'interwikiprefix' );
63 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
64 }
65 }
66 if ( !$source->isOK() ) {
67 $this->dieStatus( $source );
68 }
69
70 // Check if user can add the log entry tags which were requested
71 if ( $params['tags'] ) {
72 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
73 if ( !$ableToTag->isOK() ) {
74 $this->dieStatus( $ableToTag );
75 }
76 }
77
78 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value, $this->getAuthority() );
79 if ( isset( $params['namespace'] ) ) {
80 $importer->setTargetNamespace( $params['namespace'] );
81 } elseif ( isset( $params['rootpage'] ) ) {
82 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
83 if ( !$statusRootPage->isGood() ) {
84 $this->dieStatus( $statusRootPage );
85 }
86 }
87 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
88 $reporter = new ApiImportReporter(
89 $importer,
90 $isUpload,
91 $params['interwikisource'],
92 $params['summary'],
93 $this
94 );
95 if ( $params['tags'] ) {
96 $reporter->setChangeTags( $params['tags'] );
97 }
98
99 try {
100 $importer->doImport();
101 } catch ( Exception $e ) {
102 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
103 }
104
105 $resultData = $reporter->getData();
106 $result = $this->getResult();
107 ApiResult::setIndexedTagName( $resultData, 'page' );
108 $result->addValue( null, $this->getModuleName(), $resultData );
109 }
110
118 public function getAllowedImportSources() {
119 $importSources = $this->getConfig()->get( MainConfigNames::ImportSources );
120 $this->getHookRunner()->onImportSources( $importSources );
121
122 $result = [];
123 foreach ( $importSources as $key => $value ) {
124 if ( is_int( $key ) ) {
125 $result[] = $value;
126 } else {
127 foreach ( $value as $subproject ) {
128 $result[] = "$key:$subproject";
129 }
130 }
131 }
132 return $result;
133 }
134
136 public function mustBePosted() {
137 return true;
138 }
139
141 public function isWriteMode() {
142 return true;
143 }
144
146 public function getAllowedParams() {
147 return [
148 'summary' => null,
149 'xml' => [
150 ParamValidator::PARAM_TYPE => 'upload',
151 ],
152 'interwikiprefix' => [
153 ParamValidator::PARAM_TYPE => 'string',
154 ],
155 'interwikisource' => [
156 ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(),
157 ],
158 'interwikipage' => null,
159 'fullhistory' => false,
160 'templates' => false,
161 'namespace' => [
162 ParamValidator::PARAM_TYPE => 'namespace'
163 ],
164 'assignknownusers' => false,
165 'rootpage' => null,
166 'tags' => [
167 ParamValidator::PARAM_TYPE => 'tags',
168 ParamValidator::PARAM_ISMULTI => true,
169 ],
170 ];
171 }
172
174 public function needsToken() {
175 return 'csrf';
176 }
177
179 protected function getExamplesMessages() {
180 return [
181 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
182 'namespace=100&fullhistory=&token=123ABC'
183 => 'apihelp-import-example-import',
184 ];
185 }
186
188 public function getHelpUrls() {
189 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
190 }
191}
192
194class_alias( ApiImport::class, 'ApiImport' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:781
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1369
getResult()
Get the result object.
Definition ApiBase.php:696
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:1012
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1535
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:815
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
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...
__construct(ApiMain $main, string $action, private readonly WikiImporterFactory $wikiImporterFactory,)
Definition ApiImport.php:25
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:33
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