MediaWiki  1.34.0
ApiImport.php
Go to the documentation of this file.
1 <?php
28 class 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',
162  ApiBase::PARAM_ISMULTI => true,
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 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
WikiImporter
XML file reader for the page data importer.
Definition: WikiImporter.php:35
ApiImport\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiImport.php:132
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiImport\getAllowedImportSources
getAllowedImportSources()
Returns a list of interwiki prefixes corresponding to each defined import source.
Definition: ApiImport.php:115
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiImport\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiImport.php:167
ApiBase\dieWithException
dieWithException( $exception, array $options=[])
Abort execution with an error derived from an exception.
Definition: ApiBase.php:2026
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiImport\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiImport.php:179
ImportStreamSource\newFromInterwiki
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
Definition: ImportStreamSource.php:149
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:616
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:739
ApiImport\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiImport.php:171
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:710
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:931
ChangeTags\canAddTagsAccompanyingChange
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...
Definition: ChangeTags.php:521
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
ApiImport\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiImport.php:30
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
$source
$source
Definition: mwdoc-filter.php:34
ApiImport\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiImport.php:136
ApiImport
API module that imports an XML file like Special:Import does.
Definition: ApiImport.php:28
ApiImport\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiImport.php:140
WikiImporter\setTargetNamespace
setTargetNamespace( $namespace)
Set a target namespace to override the defaults.
Definition: WikiImporter.php:254
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ApiImportReporter
Import reporter for the API.
Definition: ApiImportReporter.php:27
ImportStreamSource\newFromUpload
static newFromUpload( $fieldname="xmlimport")
Definition: ImportStreamSource.php:75