Merge "Fix ApiCreateAccountTest for $wgEnableEmail = false;"
[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 __construct( $main, $action ) {
35 parent::__construct( $main, $action );
36 }
37
38 public function execute() {
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
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( array( '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->dieUsageMsg( $source->getErrorsArray() );
65 }
66
67 $importer = new WikiImporter( $source->value );
68 if ( isset( $params['namespace'] ) ) {
69 $importer->setTargetNamespace( $params['namespace'] );
70 }
71 if ( isset( $params['rootpage'] ) ) {
72 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
73 if( !$statusRootPage->isGood() ) {
74 $this->dieUsageMsg( $statusRootPage->getErrorsArray() );
75 }
76 }
77 $reporter = new ApiImportReporter(
78 $importer,
79 $isUpload,
80 $params['interwikisource'],
81 $params['summary']
82 );
83
84 try {
85 $importer->doImport();
86 } catch ( MWException $e ) {
87 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
88 }
89
90 $resultData = $reporter->getData();
91 $result = $this->getResult();
92 $result->setIndexedTagName( $resultData, 'page' );
93 $result->addValue( null, $this->getModuleName(), $resultData );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 global $wgImportSources;
106 return array(
107 'token' => array(
108 ApiBase::PARAM_TYPE => 'string',
109 ApiBase::PARAM_REQUIRED => true
110 ),
111 'summary' => null,
112 'xml' => null,
113 'interwikisource' => array(
114 ApiBase::PARAM_TYPE => $wgImportSources
115 ),
116 'interwikipage' => null,
117 'fullhistory' => false,
118 'templates' => false,
119 'namespace' => array(
120 ApiBase::PARAM_TYPE => 'namespace'
121 ),
122 'rootpage' => null,
123 );
124 }
125
126 public function getParamDescription() {
127 return array(
128 'token' => 'Import token obtained through prop=info',
129 'summary' => 'Import summary',
130 'xml' => 'Uploaded XML file',
131 'interwikisource' => 'For interwiki imports: wiki to import from',
132 'interwikipage' => 'For interwiki imports: page to import',
133 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
134 'templates' => 'For interwiki imports: import all included templates as well',
135 'namespace' => 'For interwiki imports: import to this namespace',
136 'rootpage' => 'Import as subpage of this page',
137 );
138 }
139
140 public function getResultProperties() {
141 return array(
142 ApiBase::PROP_LIST => true,
143 '' => array(
144 'ns' => 'namespace',
145 'title' => 'string',
146 'revisions' => 'integer'
147 )
148 );
149 }
150
151 public function getDescription() {
152 return array(
153 'Import a page from another wiki, or an XML file.' ,
154 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
155 'sending a file for the "xml" parameter.'
156 );
157 }
158
159 public function getPossibleErrors() {
160 return array_merge( parent::getPossibleErrors(), array(
161 array( 'cantimport' ),
162 array( 'missingparam', 'interwikipage' ),
163 array( 'cantimport-upload' ),
164 array( 'import-unknownerror', 'source' ),
165 array( 'import-unknownerror', 'result' ),
166 array( 'import-rootpage-nosubpage', 'namespace' ),
167 array( 'import-rootpage-invalid' ),
168 ) );
169 }
170
171 public function needsToken() {
172 return true;
173 }
174
175 public function getTokenSalt() {
176 return '';
177 }
178
179 public function getExamples() {
180 return array(
181 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&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 }