MediaWiki REL1_34
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 Hooks::run( 'ImportSources', [ &$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:42
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:739
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:710
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:931
dieWithException( $exception, array $options=[])
Abort execution with an error derived from an exception.
Definition ApiBase.php:2026
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:2086
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1871
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:58
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.
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 setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
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...
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromUpload( $fieldname="xmlimport")
XML file reader for the page data importer.
$source