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