Merge "Add some more detailed info about the xslt param of format=xml"
[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 getDescription() {
130 return array(
131 'Import a page from another wiki, or an XML file.' ,
132 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
133 'sending a file for the "xml" parameter.'
134 );
135 }
136
137 public function getPossibleErrors() {
138 return array_merge( parent::getPossibleErrors(), array(
139 array( 'cantimport' ),
140 array( 'missingparam', 'interwikipage' ),
141 array( 'cantimport-upload' ),
142 array( 'import-unknownerror', 'source' ),
143 array( 'import-unknownerror', 'result' ),
144 ) );
145 }
146
147 public function needsToken() {
148 return true;
149 }
150
151 public function getTokenSalt() {
152 return '';
153 }
154
155 public function getExamples() {
156 return array(
157 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC'
158 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
159 );
160 }
161
162 public function getHelpUrls() {
163 return 'https://www.mediawiki.org/wiki/API:Import';
164 }
165
166 public function getVersion() {
167 return __CLASS__ . ': $Id$';
168 }
169 }
170
171 /**
172 * Import reporter for the API
173 * @ingroup API
174 */
175 class ApiImportReporter extends ImportReporter {
176 private $mResultArr = array();
177
178 /**
179 * @param $title Title
180 * @param $origTitle Title
181 * @param $revisionCount int
182 * @param $successCount int
183 * @param $pageInfo
184 * @return void
185 */
186 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
187 // Add a result entry
188 $r = array();
189
190 if ( $title === null ) {
191 # Invalid or non-importable title
192 $r['title'] = $pageInfo['title'];
193 $r['invalid'] = '';
194 } else {
195 ApiQueryBase::addTitleInfo( $r, $title );
196 $r['revisions'] = intval( $successCount );
197 }
198
199 $this->mResultArr[] = $r;
200
201 // Piggyback on the parent to do the logging
202 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
203 }
204
205 function getData() {
206 return $this->mResultArr;
207 }
208 }