Merge "Re-remove experiment code for moduleStorage"
[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 $user = $this->getUser();
36 $params = $this->extractRequestParams();
37
38 $isUpload = false;
39 if ( isset( $params['interwikisource'] ) ) {
40 if ( !$user->isAllowed( 'import' ) ) {
41 $this->dieUsageMsg( 'cantimport' );
42 }
43 if ( !isset( $params['interwikipage'] ) ) {
44 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
45 }
46 $source = ImportStreamSource::newFromInterwiki(
47 $params['interwikisource'],
48 $params['interwikipage'],
49 $params['fullhistory'],
50 $params['templates']
51 );
52 } else {
53 $isUpload = true;
54 if ( !$user->isAllowed( 'importupload' ) ) {
55 $this->dieUsageMsg( 'cantimport-upload' );
56 }
57 $source = ImportStreamSource::newFromUpload( 'xml' );
58 }
59 if ( !$source->isOK() ) {
60 $this->dieStatus( $source );
61 }
62
63 $importer = new WikiImporter( $source->value );
64 if ( isset( $params['namespace'] ) ) {
65 $importer->setTargetNamespace( $params['namespace'] );
66 }
67 if ( isset( $params['rootpage'] ) ) {
68 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
69 if ( !$statusRootPage->isGood() ) {
70 $this->dieStatus( $statusRootPage );
71 }
72 }
73 $reporter = new ApiImportReporter(
74 $importer,
75 $isUpload,
76 $params['interwikisource'],
77 $params['summary']
78 );
79
80 try {
81 $importer->doImport();
82 } catch ( MWException $e ) {
83 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
84 }
85
86 $resultData = $reporter->getData();
87 $result = $this->getResult();
88 $result->setIndexedTagName( $resultData, 'page' );
89 $result->addValue( null, $this->getModuleName(), $resultData );
90 }
91
92 public function mustBePosted() {
93 return true;
94 }
95
96 public function isWriteMode() {
97 return true;
98 }
99
100 public function getAllowedParams() {
101 global $wgImportSources;
102
103 return array(
104 'token' => array(
105 ApiBase::PARAM_TYPE => 'string',
106 ApiBase::PARAM_REQUIRED => true
107 ),
108 'summary' => null,
109 'xml' => array(
110 ApiBase::PARAM_TYPE => 'upload',
111 ),
112 'interwikisource' => array(
113 ApiBase::PARAM_TYPE => $wgImportSources
114 ),
115 'interwikipage' => null,
116 'fullhistory' => false,
117 'templates' => false,
118 'namespace' => array(
119 ApiBase::PARAM_TYPE => 'namespace'
120 ),
121 'rootpage' => null,
122 );
123 }
124
125 public function getParamDescription() {
126 return array(
127 'token' => 'Import token obtained through prop=info',
128 'summary' => 'Import summary',
129 'xml' => 'Uploaded XML file',
130 'interwikisource' => 'For interwiki imports: wiki to import from',
131 'interwikipage' => 'For interwiki imports: page to import',
132 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
133 'templates' => 'For interwiki imports: import all included templates as well',
134 'namespace' => 'For interwiki imports: import to this namespace',
135 'rootpage' => 'Import as subpage of this page',
136 );
137 }
138
139 public function getResultProperties() {
140 return array(
141 ApiBase::PROP_LIST => true,
142 '' => array(
143 'ns' => 'namespace',
144 'title' => 'string',
145 'revisions' => 'integer'
146 )
147 );
148 }
149
150 public function getDescription() {
151 return array(
152 'Import a page from another wiki, or an XML file.',
153 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
154 'sending a file for the "xml" parameter.'
155 );
156 }
157
158 public function getPossibleErrors() {
159 return array_merge( parent::getPossibleErrors(), array(
160 array( 'cantimport' ),
161 array( 'missingparam', 'interwikipage' ),
162 array( 'cantimport-upload' ),
163 array( 'import-unknownerror', 'source' ),
164 array( 'import-unknownerror', 'result' ),
165 array( 'import-rootpage-nosubpage', 'namespace' ),
166 array( 'import-rootpage-invalid' ),
167 ) );
168 }
169
170 public function needsToken() {
171 return true;
172 }
173
174 public function getTokenSalt() {
175 return '';
176 }
177
178 public function getExamples() {
179 return array(
180 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
181 'namespace=100&fullhistory=&token=123ABC'
182 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
183 );
184 }
185
186 public function getHelpUrls() {
187 return 'https://www.mediawiki.org/wiki/API:Import';
188 }
189 }
190
191 /**
192 * Import reporter for the API
193 * @ingroup API
194 */
195 class ApiImportReporter extends ImportReporter {
196 private $mResultArr = array();
197
198 /**
199 * @param $title Title
200 * @param $origTitle Title
201 * @param $revisionCount int
202 * @param $successCount int
203 * @param $pageInfo
204 * @return void
205 */
206 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
207 // Add a result entry
208 $r = array();
209
210 if ( $title === null ) {
211 # Invalid or non-importable title
212 $r['title'] = $pageInfo['title'];
213 $r['invalid'] = '';
214 } else {
215 ApiQueryBase::addTitleInfo( $r, $title );
216 $r['revisions'] = intval( $successCount );
217 }
218
219 $this->mResultArr[] = $r;
220
221 // Piggyback on the parent to do the logging
222 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
223 }
224
225 function getData() {
226 return $this->mResultArr;
227 }
228 }