Merge "Typo fix"
[mediawiki.git] / includes / api / ApiImport.php
blobf48a822e365329b22fd50f4b03fe32b47bf448c7
1 <?php
2 /**
5 * Created on Feb 4, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * API module that imports an XML file like Special:Import does
30 * @ingroup API
32 class ApiImport extends ApiBase {
34 public function execute() {
35 $user = $this->getUser();
36 $params = $this->extractRequestParams();
38 $isUpload = false;
39 if ( isset( $params['interwikisource'] ) ) {
40 if ( !$user->isAllowed( 'import' ) ) {
41 $this->dieUsageMsg( 'cantimport' );
43 if ( !isset( $params['interwikipage'] ) ) {
44 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
46 $source = ImportStreamSource::newFromInterwiki(
47 $params['interwikisource'],
48 $params['interwikipage'],
49 $params['fullhistory'],
50 $params['templates']
52 } else {
53 $isUpload = true;
54 if ( !$user->isAllowed( 'importupload' ) ) {
55 $this->dieUsageMsg( 'cantimport-upload' );
57 $source = ImportStreamSource::newFromUpload( 'xml' );
59 if ( !$source->isOK() ) {
60 $this->dieStatus( $source );
63 $importer = new WikiImporter( $source->value );
64 if ( isset( $params['namespace'] ) ) {
65 $importer->setTargetNamespace( $params['namespace'] );
67 if ( isset( $params['rootpage'] ) ) {
68 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
69 if ( !$statusRootPage->isGood() ) {
70 $this->dieStatus( $statusRootPage );
73 $reporter = new ApiImportReporter(
74 $importer,
75 $isUpload,
76 $params['interwikisource'],
77 $params['summary']
80 try {
81 $importer->doImport();
82 } catch ( MWException $e ) {
83 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
86 $resultData = $reporter->getData();
87 $result = $this->getResult();
88 $result->setIndexedTagName( $resultData, 'page' );
89 $result->addValue( null, $this->getModuleName(), $resultData );
92 public function mustBePosted() {
93 return true;
96 public function isWriteMode() {
97 return true;
100 public function getAllowedParams() {
101 global $wgImportSources;
102 return array(
103 'token' => array(
104 ApiBase::PARAM_TYPE => 'string',
105 ApiBase::PARAM_REQUIRED => true
107 'summary' => null,
108 'xml' => array(
109 ApiBase::PARAM_TYPE => 'upload',
111 'interwikisource' => array(
112 ApiBase::PARAM_TYPE => $wgImportSources
114 'interwikipage' => null,
115 'fullhistory' => false,
116 'templates' => false,
117 'namespace' => array(
118 ApiBase::PARAM_TYPE => 'namespace'
120 'rootpage' => null,
124 public function getParamDescription() {
125 return array(
126 'token' => 'Import token obtained through prop=info',
127 'summary' => 'Import summary',
128 'xml' => 'Uploaded XML file',
129 'interwikisource' => 'For interwiki imports: wiki to import from',
130 'interwikipage' => 'For interwiki imports: page to import',
131 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
132 'templates' => 'For interwiki imports: import all included templates as well',
133 'namespace' => 'For interwiki imports: import to this namespace',
134 'rootpage' => 'Import as subpage of this page',
138 public function getResultProperties() {
139 return array(
140 ApiBase::PROP_LIST => true,
141 '' => array(
142 'ns' => 'namespace',
143 'title' => 'string',
144 'revisions' => 'integer'
149 public function getDescription() {
150 return array(
151 'Import a page from another wiki, or an XML file.',
152 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
153 'sending a file for the "xml" parameter.'
157 public function getPossibleErrors() {
158 return array_merge( parent::getPossibleErrors(), array(
159 array( 'cantimport' ),
160 array( 'missingparam', 'interwikipage' ),
161 array( 'cantimport-upload' ),
162 array( 'import-unknownerror', 'source' ),
163 array( 'import-unknownerror', 'result' ),
164 array( 'import-rootpage-nosubpage', 'namespace' ),
165 array( 'import-rootpage-invalid' ),
166 ) );
169 public function needsToken() {
170 return true;
173 public function getTokenSalt() {
174 return '';
177 public function getExamples() {
178 return array(
179 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC'
180 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
184 public function getHelpUrls() {
185 return 'https://www.mediawiki.org/wiki/API:Import';
190 * Import reporter for the API
191 * @ingroup API
193 class ApiImportReporter extends ImportReporter {
194 private $mResultArr = array();
197 * @param $title Title
198 * @param $origTitle Title
199 * @param $revisionCount int
200 * @param $successCount int
201 * @param $pageInfo
202 * @return void
204 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
205 // Add a result entry
206 $r = array();
208 if ( $title === null ) {
209 # Invalid or non-importable title
210 $r['title'] = $pageInfo['title'];
211 $r['invalid'] = '';
212 } else {
213 ApiQueryBase::addTitleInfo( $r, $title );
214 $r['revisions'] = intval( $successCount );
217 $this->mResultArr[] = $r;
219 // Piggyback on the parent to do the logging
220 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
223 function getData() {
224 return $this->mResultArr;