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