MediaWiki REL1_35
ApiImport.php
Go to the documentation of this file.
1<?php
28class ApiImport extends ApiBase {
29
30 public function execute() {
32 $user = $this->getUser();
33 $params = $this->extractRequestParams();
34
35 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
36
37 $isUpload = false;
38 if ( isset( $params['interwikisource'] ) ) {
39 if ( !$this->getPermissionManager()->userHasRight( $user, 'import' ) ) {
40 $this->dieWithError( 'apierror-cantimport' );
41 }
42 if ( !isset( $params['interwikipage'] ) ) {
43 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
44 }
46 $params['interwikisource'],
47 $params['interwikipage'],
48 $params['fullhistory'],
49 $params['templates']
50 );
51 $usernamePrefix = $params['interwikisource'];
52 } else {
53 $isUpload = true;
54 if ( !$this->getPermissionManager()->userHasRight( $user, 'importupload' ) ) {
55 $this->dieWithError( 'apierror-cantimport-upload' );
56 }
58 $usernamePrefix = (string)$params['interwikiprefix'];
59 if ( $usernamePrefix === '' ) {
60 $encParamName = $this->encodeParamName( 'interwikiprefix' );
61 $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
62 }
63 }
64 if ( !$source->isOK() ) {
65 $this->dieStatus( $source );
66 }
67
68 // Check if user can add the log entry tags which were requested
69 if ( $params['tags'] ) {
70 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
71 if ( !$ableToTag->isOK() ) {
72 $this->dieStatus( $ableToTag );
73 }
74 }
75
76 $importer = new WikiImporter( $source->value, $this->getConfig() );
77 if ( isset( $params['namespace'] ) ) {
78 $importer->setTargetNamespace( $params['namespace'] );
79 } elseif ( isset( $params['rootpage'] ) ) {
80 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
81 if ( !$statusRootPage->isGood() ) {
82 $this->dieStatus( $statusRootPage );
83 }
84 }
85 $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
86 $reporter = new ApiImportReporter(
87 $importer,
88 $isUpload,
89 $params['interwikisource'],
90 $params['summary']
91 );
92 if ( $params['tags'] ) {
93 $reporter->setChangeTags( $params['tags'] );
94 }
95
96 try {
97 $importer->doImport();
98 } catch ( Exception $e ) {
99 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
100 }
101
102 $resultData = $reporter->getData();
103 $result = $this->getResult();
104 ApiResult::setIndexedTagName( $resultData, 'page' );
105 $result->addValue( null, $this->getModuleName(), $resultData );
106 }
107
115 public function getAllowedImportSources() {
116 $importSources = $this->getConfig()->get( 'ImportSources' );
117 $this->getHookRunner()->onImportSources( $importSources );
118
119 $result = [];
120 foreach ( $importSources as $key => $value ) {
121 if ( is_int( $key ) ) {
122 $result[] = $value;
123 } else {
124 foreach ( $value as $subproject ) {
125 $result[] = "$key:$subproject";
126 }
127 }
128 }
129 return $result;
130 }
131
132 public function mustBePosted() {
133 return true;
134 }
135
136 public function isWriteMode() {
137 return true;
138 }
139
140 public function getAllowedParams() {
141 return [
142 'summary' => null,
143 'xml' => [
144 ApiBase::PARAM_TYPE => 'upload',
145 ],
146 'interwikiprefix' => [
147 ApiBase::PARAM_TYPE => 'string',
148 ],
149 'interwikisource' => [
151 ],
152 'interwikipage' => null,
153 'fullhistory' => false,
154 'templates' => false,
155 'namespace' => [
156 ApiBase::PARAM_TYPE => 'namespace'
157 ],
158 'assignknownusers' => false,
159 'rootpage' => null,
160 'tags' => [
161 ApiBase::PARAM_TYPE => 'tags',
163 ],
164 ];
165 }
166
167 public function needsToken() {
168 return 'csrf';
169 }
170
171 protected function getExamplesMessages() {
172 return [
173 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
174 'namespace=100&fullhistory=&token=123ABC'
175 => 'apihelp-import-example-import',
176 ];
177 }
178
179 public function getHelpUrls() {
180 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
181 }
182}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:750
const PARAM_TYPE
Definition ApiBase.php:78
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:692
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:944
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
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:1294
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:717
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition ApiBase.php:1449
const PARAM_ISMULTI
Definition ApiBase.php:74
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:30
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 Stable to override.
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.
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
getUser()
Stable to override.
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromUpload( $fieldname="xmlimport")
XML file reader for the page data importer.
$source