Merge "Add pp_propname_page index to page_props"
[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->dieUsageMsg( $source->getErrorsArray() );
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->dieUsageMsg( $statusRootPage->getErrorsArray() );
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 return array(
103 'token' => array(
104 ApiBase::PARAM_TYPE => 'string',
105 ApiBase::PARAM_REQUIRED => true
106 ),
107 'summary' => null,
108 'xml' => null,
109 'interwikisource' => array(
110 ApiBase::PARAM_TYPE => $wgImportSources
111 ),
112 'interwikipage' => null,
113 'fullhistory' => false,
114 'templates' => false,
115 'namespace' => array(
116 ApiBase::PARAM_TYPE => 'namespace'
117 ),
118 'rootpage' => null,
119 );
120 }
121
122 public function getParamDescription() {
123 return array(
124 'token' => 'Import token obtained through prop=info',
125 'summary' => 'Import summary',
126 'xml' => 'Uploaded XML file',
127 'interwikisource' => 'For interwiki imports: wiki to import from',
128 'interwikipage' => 'For interwiki imports: page to import',
129 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
130 'templates' => 'For interwiki imports: import all included templates as well',
131 'namespace' => 'For interwiki imports: import to this namespace',
132 'rootpage' => 'Import as subpage of this page',
133 );
134 }
135
136 public function getResultProperties() {
137 return array(
138 ApiBase::PROP_LIST => true,
139 '' => array(
140 'ns' => 'namespace',
141 'title' => 'string',
142 'revisions' => 'integer'
143 )
144 );
145 }
146
147 public function getDescription() {
148 return array(
149 'Import a page from another wiki, or an XML file.',
150 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
151 'sending a file for the "xml" parameter.'
152 );
153 }
154
155 public function getPossibleErrors() {
156 return array_merge( parent::getPossibleErrors(), array(
157 array( 'cantimport' ),
158 array( 'missingparam', 'interwikipage' ),
159 array( 'cantimport-upload' ),
160 array( 'import-unknownerror', 'source' ),
161 array( 'import-unknownerror', 'result' ),
162 array( 'import-rootpage-nosubpage', 'namespace' ),
163 array( 'import-rootpage-invalid' ),
164 ) );
165 }
166
167 public function needsToken() {
168 return true;
169 }
170
171 public function getTokenSalt() {
172 return '';
173 }
174
175 public function getExamples() {
176 return array(
177 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC'
178 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
179 );
180 }
181
182 public function getHelpUrls() {
183 return 'https://www.mediawiki.org/wiki/API:Import';
184 }
185 }
186
187 /**
188 * Import reporter for the API
189 * @ingroup API
190 */
191 class ApiImportReporter extends ImportReporter {
192 private $mResultArr = array();
193
194 /**
195 * @param $title Title
196 * @param $origTitle Title
197 * @param $revisionCount int
198 * @param $successCount int
199 * @param $pageInfo
200 * @return void
201 */
202 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
203 // Add a result entry
204 $r = array();
205
206 if ( $title === null ) {
207 # Invalid or non-importable title
208 $r['title'] = $pageInfo['title'];
209 $r['invalid'] = '';
210 } else {
211 ApiQueryBase::addTitleInfo( $r, $title );
212 $r['revisions'] = intval( $successCount );
213 }
214
215 $this->mResultArr[] = $r;
216
217 // Piggyback on the parent to do the logging
218 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
219 }
220
221 function getData() {
222 return $this->mResultArr;
223 }
224 }