Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiImport.php
blobbf5e4ce3b288bed0a793492e2b516ffc5f7db39c
1 <?php
2 /**
5 * Created on Feb 4, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API module that imports an XML file like Special:Import does
30 * @ingroup API
32 class ApiImport extends ApiBase {
34 public function execute() {
35 $this->useTransactionalTimeLimit();
37 $user = $this->getUser();
38 $params = $this->extractRequestParams();
40 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
42 $isUpload = false;
43 if ( isset( $params['interwikisource'] ) ) {
44 if ( !$user->isAllowed( 'import' ) ) {
45 $this->dieWithError( 'apierror-cantimport' );
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
50 $source = ImportStreamSource::newFromInterwiki(
51 $params['interwikisource'],
52 $params['interwikipage'],
53 $params['fullhistory'],
54 $params['templates']
56 } else {
57 $isUpload = true;
58 if ( !$user->isAllowed( 'importupload' ) ) {
59 $this->dieWithError( 'apierror-cantimport-upload' );
61 $source = ImportStreamSource::newFromUpload( 'xml' );
63 if ( !$source->isOK() ) {
64 $this->dieStatus( $source );
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 );
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 );
84 $reporter = new ApiImportReporter(
85 $importer,
86 $isUpload,
87 $params['interwikisource'],
88 $params['summary']
90 if ( $params['tags'] ) {
91 $reporter->setChangeTags( $params['tags'] );
94 try {
95 $importer->doImport();
96 } catch ( Exception $e ) {
97 $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
100 $resultData = $reporter->getData();
101 $result = $this->getResult();
102 ApiResult::setIndexedTagName( $resultData, 'page' );
103 $result->addValue( null, $this->getModuleName(), $resultData );
107 * Returns a list of interwiki prefixes corresponding to each defined import
108 * source.
110 * @return array
111 * @since 1.27
113 public function getAllowedImportSources() {
114 $importSources = $this->getConfig()->get( 'ImportSources' );
115 Hooks::run( 'ImportSources', [ &$importSources ] );
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";
127 return $result;
130 public function mustBePosted() {
131 return true;
134 public function isWriteMode() {
135 return true;
138 public function getAllowedParams() {
139 return [
140 'summary' => null,
141 'xml' => [
142 ApiBase::PARAM_TYPE => 'upload',
144 'interwikisource' => [
145 ApiBase::PARAM_TYPE => $this->getAllowedImportSources(),
147 'interwikipage' => null,
148 'fullhistory' => false,
149 'templates' => false,
150 'namespace' => [
151 ApiBase::PARAM_TYPE => 'namespace'
153 'rootpage' => null,
154 'tags' => [
155 ApiBase::PARAM_TYPE => 'tags',
156 ApiBase::PARAM_ISMULTI => true,
161 public function needsToken() {
162 return 'csrf';
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',
173 public function getHelpUrls() {
174 return 'https://www.mediawiki.org/wiki/API:Import';
179 * Import reporter for the API
180 * @ingroup API
182 class ApiImportReporter extends ImportReporter {
183 private $mResultArr = [];
186 * @param Title $title
187 * @param Title $origTitle
188 * @param int $revisionCount
189 * @param int $successCount
190 * @param array $pageInfo
191 * @return void
193 public function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
194 // Add a result entry
195 $r = [];
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 );
206 $this->mResultArr[] = $r;
208 // Piggyback on the parent to do the logging
209 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
212 public function getData() {
213 return $this->mResultArr;