MediaWiki REL1_30
ApiImport.php
Go to the documentation of this file.
1<?php
32class ApiImport extends ApiBase {
33
34 public function execute() {
36
37 $user = $this->getUser();
39
40 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
41
42 $isUpload = false;
43 if ( isset( $params['interwikisource'] ) ) {
44 if ( !$user->isAllowed( 'import' ) ) {
45 $this->dieWithError( 'apierror-cantimport' );
46 }
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
49 }
51 $params['interwikisource'],
52 $params['interwikipage'],
53 $params['fullhistory'],
54 $params['templates']
55 );
56 } else {
57 $isUpload = true;
58 if ( !$user->isAllowed( 'importupload' ) ) {
59 $this->dieWithError( 'apierror-cantimport-upload' );
60 }
62 }
63 if ( !$source->isOK() ) {
64 $this->dieStatus( $source );
65 }
66
67 // Check if user can add the log entry tags which were requested
68 if ( $params['tags'] ) {
69 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
70 if ( !$ableToTag->isOK() ) {
71 $this->dieStatus( $ableToTag );
72 }
73 }
74
75 $importer = new WikiImporter( $source->value, $this->getConfig() );
76 if ( isset( $params['namespace'] ) ) {
77 $importer->setTargetNamespace( $params['namespace'] );
78 } elseif ( isset( $params['rootpage'] ) ) {
79 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
80 if ( !$statusRootPage->isGood() ) {
81 $this->dieStatus( $statusRootPage );
82 }
83 }
84 $reporter = new ApiImportReporter(
85 $importer,
86 $isUpload,
87 $params['interwikisource'],
88 $params['summary']
89 );
90 if ( $params['tags'] ) {
91 $reporter->setChangeTags( $params['tags'] );
92 }
93
94 try {
95 $importer->doImport();
96 } catch ( Exception $e ) {
97 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
98 }
99
100 $resultData = $reporter->getData();
101 $result = $this->getResult();
102 ApiResult::setIndexedTagName( $resultData, 'page' );
103 $result->addValue( null, $this->getModuleName(), $resultData );
104 }
105
113 public function getAllowedImportSources() {
114 $importSources = $this->getConfig()->get( 'ImportSources' );
115 Hooks::run( 'ImportSources', [ &$importSources ] );
116
117 $result = [];
118 foreach ( $importSources as $key => $value ) {
119 if ( is_int( $key ) ) {
120 $result[] = $value;
121 } else {
122 foreach ( $value as $subproject ) {
123 $result[] = "$key:$subproject";
124 }
125 }
126 }
127 return $result;
128 }
129
130 public function mustBePosted() {
131 return true;
132 }
133
134 public function isWriteMode() {
135 return true;
136 }
137
138 public function getAllowedParams() {
139 return [
140 'summary' => null,
141 'xml' => [
142 ApiBase::PARAM_TYPE => 'upload',
143 ],
144 'interwikisource' => [
146 ],
147 'interwikipage' => null,
148 'fullhistory' => false,
149 'templates' => false,
150 'namespace' => [
151 ApiBase::PARAM_TYPE => 'namespace'
152 ],
153 'rootpage' => null,
154 'tags' => [
155 ApiBase::PARAM_TYPE => 'tags',
157 ],
158 ];
159 }
160
161 public function needsToken() {
162 return 'csrf';
163 }
164
165 protected function getExamplesMessages() {
166 return [
167 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
168 'namespace=100&fullhistory=&token=123ABC'
169 => 'apihelp-import-example-import',
170 ];
171 }
172
173 public function getHelpUrls() {
174 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
175 }
176}
177
183 private $mResultArr = [];
184
193 public function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
194 // Add a result entry
195 $r = [];
196
197 if ( $title === null ) {
198 # Invalid or non-importable title
199 $r['title'] = $pageInfo['title'];
200 $r['invalid'] = true;
201 } else {
202 ApiQueryBase::addTitleInfo( $r, $title );
203 $r['revisions'] = intval( $successCount );
204 }
205
206 $this->mResultArr[] = $r;
207
208 // Piggyback on the parent to do the logging
209 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
210 }
211
212 public function getData() {
213 return $this->mResultArr;
214 }
215}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:41
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:1855
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:91
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:740
getResult()
Get the result object.
Definition ApiBase.php:632
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:814
dieWithException( $exception, array $options=[])
Abort execution with an error derived from an exception.
Definition ApiBase.php:1867
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:512
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1920
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:2631
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:55
Import reporter for the API.
reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo)
API module that imports an XML file like Special:Import does.
Definition ApiImport.php:32
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiImport.php:34
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 addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
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...
getUser()
Get the User object.
getConfig()
Get the Config object.
Reporting callback.
static newFromInterwiki( $interwiki, $page, $history=false, $templates=false, $pageLinkDepth=0)
static newFromUpload( $fieldname="xmlimport")
XML file reader for the page data importer.
namespace being checked & $result
Definition hooks.txt:2293
returning false will NOT prevent logging $e
Definition hooks.txt:2146
$source
$params