MediaWiki REL1_37
ApiImport.php
Go to the documentation of this file.
1<?php
28class ApiImport extends ApiBase {
29
32
38 public function __construct(
39 ApiMain $main,
40 $action,
42 ) {
43 parent::__construct( $main, $action );
44
45 $this->wikiImporterFactory = $wikiImporterFactory;
46 }
47
48 public function execute() {
50 $user = $this->getUser();
51 $params = $this->extractRequestParams();
52
53 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
54
55 $isUpload = false;
56 if ( isset( $params['interwikisource'] ) ) {
57 if ( !$this->getAuthority()->isAllowed( 'import' ) ) {
58 $this->dieWithError( 'apierror-cantimport' );
59 }
60 if ( !isset( $params['interwikipage'] ) ) {
61 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
62 }
64 $params['interwikisource'],
65 $params['interwikipage'],
66 $params['fullhistory'],
67 $params['templates']
68 );
69 $usernamePrefix = $params['interwikisource'];
70 } else {
71 $isUpload = true;
72 if ( !$this->getAuthority()->isAllowed( 'importupload' ) ) {
73 $this->dieWithError( 'apierror-cantimport-upload' );
74 }
76 $usernamePrefix = (string)$params['interwikiprefix'];
77 if ( $usernamePrefix === '' ) {
78 $encParamName = $this->encodeParamName( 'interwikiprefix' );
79 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
80 }
81 }
82 if ( !$source->isOK() ) {
83 $this->dieStatus( $source );
84 }
85
86 // Check if user can add the log entry tags which were requested
87 if ( $params['tags'] ) {
88 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
89 if ( !$ableToTag->isOK() ) {
90 $this->dieStatus( $ableToTag );
91 }
92 }
93
94 $importer = $this->wikiImporterFactory->getWikiImporter( $source->value );
95 if ( isset( $params['namespace'] ) ) {
96 $importer->setTargetNamespace( $params['namespace'] );
97 } elseif ( isset( $params['rootpage'] ) ) {
98 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
99 if ( !$statusRootPage->isGood() ) {
100 $this->dieStatus( $statusRootPage );
101 }
102 }
103 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
104 $reporter = new ApiImportReporter(
105 $importer,
106 $isUpload,
107 $params['interwikisource'],
108 $params['summary']
109 );
110 if ( $params['tags'] ) {
111 $reporter->setChangeTags( $params['tags'] );
112 }
113
114 try {
115 $importer->doImport();
116 } catch ( Exception $e ) {
117 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
118 }
119
120 $resultData = $reporter->getData();
121 $result = $this->getResult();
122 ApiResult::setIndexedTagName( $resultData, 'page' );
123 $result->addValue( null, $this->getModuleName(), $resultData );
124 }
125
133 public function getAllowedImportSources() {
134 $importSources = $this->getConfig()->get( 'ImportSources' );
135 $this->getHookRunner()->onImportSources( $importSources );
136
137 $result = [];
138 foreach ( $importSources as $key => $value ) {
139 if ( is_int( $key ) ) {
140 $result[] = $value;
141 } else {
142 foreach ( $value as $subproject ) {
143 $result[] = "$key:$subproject";
144 }
145 }
146 }
147 return $result;
148 }
149
150 public function mustBePosted() {
151 return true;
152 }
153
154 public function isWriteMode() {
155 return true;
156 }
157
158 public function getAllowedParams() {
159 return [
160 'summary' => null,
161 'xml' => [
162 ApiBase::PARAM_TYPE => 'upload',
163 ],
164 'interwikiprefix' => [
165 ApiBase::PARAM_TYPE => 'string',
166 ],
167 'interwikisource' => [
169 ],
170 'interwikipage' => null,
171 'fullhistory' => false,
172 'templates' => false,
173 'namespace' => [
174 ApiBase::PARAM_TYPE => 'namespace'
175 ],
176 'assignknownusers' => false,
177 'rootpage' => null,
178 'tags' => [
179 ApiBase::PARAM_TYPE => 'tags',
181 ],
182 ];
183 }
184
185 public function needsToken() {
186 return 'csrf';
187 }
188
189 protected function getExamplesMessages() {
190 return [
191 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
192 'namespace=100&fullhistory=&token=123ABC'
193 => 'apihelp-import-example-import',
194 ];
195 }
196
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
199 }
200}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:742
const PARAM_TYPE
Definition ApiBase.php:81
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:936
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1293
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:710
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1448
const PARAM_ISMULTI
Definition ApiBase.php:77
Import reporter for the API.
API module that imports an XML file like Special:Import does.
Definition ApiImport.php:28
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiImport.php:48
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:38
getAllowedImportSources()
Returns a list of interwiki prefixes corresponding to each defined import source.
getExamplesMessages()
Returns usage examples for this module.
WikiImporterFactory $wikiImporterFactory
Definition ApiImport.php:31
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:49
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromUpload( $fieldname="xmlimport")
Factory service for WikiImporter instances.
$source