| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use ChangeTags; |
| 26 |
use Exception; |
| 27 |
use ImportStreamSource; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use WikiImporterFactory; |
| 30 |
use Wikimedia\ParamValidator\ParamValidator; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ApiImport extends ApiBase { |
| 38 |
|
| 39 |
private WikiImporterFactory $wikiImporterFactory; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
ApiMain $main, |
| 43 |
string $action, |
| 44 |
WikiImporterFactory $wikiImporterFactory |
| 45 |
) { |
| 46 |
parent::__construct( $main, $action ); |
| 47 |
|
| 48 |
$this->wikiImporterFactory = $wikiImporterFactory; |
| 49 |
} |
| 50 |
|
| 51 |
public function execute() { |
| 52 |
$this->useTransactionalTimeLimit(); |
| 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 |
} |
| 65 |
$source = ImportStreamSource::newFromInterwiki( |
| 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 |
} |
| 77 |
$source = ImportStreamSource::newFromUpload( 'xml' ); |
| 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 |
|
| 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, $this->getAuthority() ); |
| 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 |
$this |
| 112 |
); |
| 113 |
if ( $params['tags'] ) { |
| 114 |
$reporter->setChangeTags( $params['tags'] ); |
| 115 |
} |
| 116 |
|
| 117 |
try { |
| 118 |
$importer->doImport(); |
| 119 |
} catch ( Exception $e ) { |
| 120 |
$this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] ); |
| 121 |
} |
| 122 |
|
| 123 |
$resultData = $reporter->getData(); |
| 124 |
$result = $this->getResult(); |
| 125 |
ApiResult::setIndexedTagName( $resultData, 'page' ); |
| 126 |
$result->addValue( null, $this->getModuleName(), $resultData ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function getAllowedImportSources() { |
| 137 |
$importSources = $this->getConfig()->get( MainConfigNames::ImportSources ); |
| 138 |
$this->getHookRunner()->onImportSources( $importSources ); |
| 139 |
|
| 140 |
$result = []; |
| 141 |
foreach ( $importSources as $key => $value ) { |
| 142 |
if ( is_int( $key ) ) { |
| 143 |
$result[] = $value; |
| 144 |
} else { |
| 145 |
foreach ( $value as $subproject ) { |
| 146 |
$result[] = "$key:$subproject"; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
return $result; |
| 151 |
} |
| 152 |
|
| 153 |
public function mustBePosted() { |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
public function isWriteMode() { |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
public function getAllowedParams() { |
| 162 |
return [ |
| 163 |
'summary' => null, |
| 164 |
'xml' => [ |
| 165 |
ParamValidator::PARAM_TYPE => 'upload', |
| 166 |
], |
| 167 |
'interwikiprefix' => [ |
| 168 |
ParamValidator::PARAM_TYPE => 'string', |
| 169 |
], |
| 170 |
'interwikisource' => [ |
| 171 |
ParamValidator::PARAM_TYPE => $this->getAllowedImportSources(), |
| 172 |
], |
| 173 |
'interwikipage' => null, |
| 174 |
'fullhistory' => false, |
| 175 |
'templates' => false, |
| 176 |
'namespace' => [ |
| 177 |
ParamValidator::PARAM_TYPE => 'namespace' |
| 178 |
], |
| 179 |
'assignknownusers' => false, |
| 180 |
'rootpage' => null, |
| 181 |
'tags' => [ |
| 182 |
ParamValidator::PARAM_TYPE => 'tags', |
| 183 |
ParamValidator::PARAM_ISMULTI => true, |
| 184 |
], |
| 185 |
]; |
| 186 |
} |
| 187 |
|
| 188 |
public function needsToken() { |
| 189 |
return 'csrf'; |
| 190 |
} |
| 191 |
|
| 192 |
protected function getExamplesMessages() { |
| 193 |
return [ |
| 194 |
'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' . |
| 195 |
'namespace=100&fullhistory=&token=123ABC' |
| 196 |
=> 'apihelp-import-example-import', |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
public function getHelpUrls() { |
| 201 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import'; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
class_alias( ApiImport::class, 'ApiImport' ); |