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