MediaWiki  master
ApiImport.php
Go to the documentation of this file.
1 <?php
28 class 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',
180  ApiBase::PARAM_ISMULTI => true,
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 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
ApiImport\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiImport.php:150
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiImport\getAllowedImportSources
getAllowedImportSources()
Returns a list of interwiki prefixes corresponding to each defined import source.
Definition: ApiImport.php:133
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiImport\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiImport.php:185
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiImport\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiImport.php:197
ImportStreamSource\newFromInterwiki
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Definition: ImportStreamSource.php:149
ApiImport\$wikiImporterFactory
WikiImporterFactory $wikiImporterFactory
Definition: ApiImport.php:31
ChangeTags\canAddTagsAccompanyingChange
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...
Definition: ChangeTags.php:625
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:603
ApiImport\__construct
__construct(ApiMain $main, $action, WikiImporterFactory $wikiImporterFactory)
Definition: ApiImport.php:38
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:879
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:685
ApiImport\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiImport.php:189
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1236
ApiBase\dieWithException
dieWithException(Throwable $exception, array $options=[])
Abort execution with an error derived from a throwable.
Definition: ApiBase.php:1392
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1442
ApiImport\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiImport.php:48
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
$source
$source
Definition: mwdoc-filter.php:34
ApiImport\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiImport.php:154
ApiImport
API module that imports an XML file like Special:Import does.
Definition: ApiImport.php:28
WikiImporterFactory
Factory service for WikiImporter instances.
Definition: WikiImporterFactory.php:33
ApiImport\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiImport.php:158
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:653
ApiImportReporter
Import reporter for the API.
Definition: ApiImportReporter.php:30
ImportStreamSource\newFromUpload
static newFromUpload( $fieldname="xmlimport")
Definition: ImportStreamSource.php:75