Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / api / ApiImport.php
1 <?php
2 /**
3 *
4 *
5 * Created on Feb 4, 2009
6 *
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API module that imports an XML file like Special:Import does
29 *
30 * @ingroup API
31 */
32 class ApiImport extends ApiBase {
33
34 public function execute() {
35 $this->useTransactionalTimeLimit();
36
37 $user = $this->getUser();
38 $params = $this->extractRequestParams();
39
40 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
41
42 $isUpload = false;
43 if ( isset( $params['interwikisource'] ) ) {
44 if ( !$user->isAllowed( 'import' ) ) {
45 $this->dieUsageMsg( 'cantimport' );
46 }
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieUsageMsg( [ 'missingparam', 'interwikipage' ] );
49 }
50 $source = ImportStreamSource::newFromInterwiki(
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->dieUsageMsg( 'cantimport-upload' );
60 }
61 $source = ImportStreamSource::newFromUpload( 'xml' );
62 }
63 if ( !$source->isOK() ) {
64 $this->dieStatus( $source );
65 }
66
67 $importer = new WikiImporter( $source->value, $this->getConfig() );
68 if ( isset( $params['namespace'] ) ) {
69 $importer->setTargetNamespace( $params['namespace'] );
70 } elseif ( isset( $params['rootpage'] ) ) {
71 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
72 if ( !$statusRootPage->isGood() ) {
73 $this->dieStatus( $statusRootPage );
74 }
75 }
76 $reporter = new ApiImportReporter(
77 $importer,
78 $isUpload,
79 $params['interwikisource'],
80 $params['summary']
81 );
82
83 try {
84 $importer->doImport();
85 } catch ( Exception $e ) {
86 $this->dieUsageMsg( [ 'import-unknownerror', $e->getMessage() ] );
87 }
88
89 $resultData = $reporter->getData();
90 $result = $this->getResult();
91 ApiResult::setIndexedTagName( $resultData, 'page' );
92 $result->addValue( null, $this->getModuleName(), $resultData );
93 }
94
95 /**
96 * Returns a list of interwiki prefixes corresponding to each defined import
97 * source.
98 *
99 * @return array
100 * @since 1.27
101 */
102 public function getAllowedImportSources() {
103 $importSources = $this->getConfig()->get( 'ImportSources' );
104 Hooks::run( 'ImportSources', [ &$importSources ] );
105
106 $result = [];
107 foreach ( $importSources as $key => $value ) {
108 if ( is_int( $key ) ) {
109 $result[] = $value;
110 } else {
111 foreach ( $value as $subproject ) {
112 $result[] = "$key:$subproject";
113 }
114 }
115 }
116 return $result;
117 }
118
119 public function mustBePosted() {
120 return true;
121 }
122
123 public function isWriteMode() {
124 return true;
125 }
126
127 public function getAllowedParams() {
128 return [
129 'summary' => null,
130 'xml' => [
131 ApiBase::PARAM_TYPE => 'upload',
132 ],
133 'interwikisource' => [
134 ApiBase::PARAM_TYPE => $this->getAllowedImportSources(),
135 ],
136 'interwikipage' => null,
137 'fullhistory' => false,
138 'templates' => false,
139 'namespace' => [
140 ApiBase::PARAM_TYPE => 'namespace'
141 ],
142 'rootpage' => null,
143 ];
144 }
145
146 public function needsToken() {
147 return 'csrf';
148 }
149
150 protected function getExamplesMessages() {
151 return [
152 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
153 'namespace=100&fullhistory=&token=123ABC'
154 => 'apihelp-import-example-import',
155 ];
156 }
157
158 public function getHelpUrls() {
159 return 'https://www.mediawiki.org/wiki/API:Import';
160 }
161 }
162
163 /**
164 * Import reporter for the API
165 * @ingroup API
166 */
167 class ApiImportReporter extends ImportReporter {
168 private $mResultArr = [];
169
170 /**
171 * @param Title $title
172 * @param Title $origTitle
173 * @param int $revisionCount
174 * @param int $successCount
175 * @param array $pageInfo
176 * @return void
177 */
178 public function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
179 // Add a result entry
180 $r = [];
181
182 if ( $title === null ) {
183 # Invalid or non-importable title
184 $r['title'] = $pageInfo['title'];
185 $r['invalid'] = true;
186 } else {
187 ApiQueryBase::addTitleInfo( $r, $title );
188 $r['revisions'] = intval( $successCount );
189 }
190
191 $this->mResultArr[] = $r;
192
193 // Piggyback on the parent to do the logging
194 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
195 }
196
197 public function getData() {
198 return $this->mResultArr;
199 }
200 }