Merge "Fix method declaration in UploadFromStash"
[mediawiki.git] / includes / api / ApiImport.php
blob637c1fff7f0af88095ff9a7b6c58dd158b0ccb1e
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 __construct( $main, $action ) {
35 parent::__construct( $main, $action );
38 public function execute() {
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
42 $isUpload = false;
43 if ( isset( $params['interwikisource'] ) ) {
44 if ( !$user->isAllowed( 'import' ) ) {
45 $this->dieUsageMsg( 'cantimport' );
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
50 $source = ImportStreamSource::newFromInterwiki(
51 $params['interwikisource'],
52 $params['interwikipage'],
53 $params['fullhistory'],
54 $params['templates']
56 } else {
57 $isUpload = true;
58 if ( !$user->isAllowed( 'importupload' ) ) {
59 $this->dieUsageMsg( 'cantimport-upload' );
61 $source = ImportStreamSource::newFromUpload( 'xml' );
63 if ( !$source->isOK() ) {
64 $this->dieUsageMsg( $source->getErrorsArray() );
67 $importer = new WikiImporter( $source->value );
68 if ( isset( $params['namespace'] ) ) {
69 $importer->setTargetNamespace( $params['namespace'] );
71 if ( isset( $params['rootpage'] ) ) {
72 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
73 if( !$statusRootPage->isGood() ) {
74 $this->dieUsageMsg( $statusRootPage->getErrorsArray() );
77 $reporter = new ApiImportReporter(
78 $importer,
79 $isUpload,
80 $params['interwikisource'],
81 $params['summary']
84 try {
85 $importer->doImport();
86 } catch ( MWException $e ) {
87 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
90 $resultData = $reporter->getData();
91 $result = $this->getResult();
92 $result->setIndexedTagName( $resultData, 'page' );
93 $result->addValue( null, $this->getModuleName(), $resultData );
96 public function mustBePosted() {
97 return true;
100 public function isWriteMode() {
101 return true;
104 public function getAllowedParams() {
105 global $wgImportSources;
106 return array(
107 'token' => array(
108 ApiBase::PARAM_TYPE => 'string',
109 ApiBase::PARAM_REQUIRED => true
111 'summary' => null,
112 'xml' => null,
113 'interwikisource' => array(
114 ApiBase::PARAM_TYPE => $wgImportSources
116 'interwikipage' => null,
117 'fullhistory' => false,
118 'templates' => false,
119 'namespace' => array(
120 ApiBase::PARAM_TYPE => 'namespace'
122 'rootpage' => null,
126 public function getParamDescription() {
127 return array(
128 'token' => 'Import token obtained through prop=info',
129 'summary' => 'Import summary',
130 'xml' => 'Uploaded XML file',
131 'interwikisource' => 'For interwiki imports: wiki to import from',
132 'interwikipage' => 'For interwiki imports: page to import',
133 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
134 'templates' => 'For interwiki imports: import all included templates as well',
135 'namespace' => 'For interwiki imports: import to this namespace',
136 'rootpage' => 'Import as subpage of this page',
140 public function getResultProperties() {
141 return array(
142 ApiBase::PROP_LIST => true,
143 '' => array(
144 'ns' => 'namespace',
145 'title' => 'string',
146 'revisions' => 'integer'
151 public function getDescription() {
152 return array(
153 'Import a page from another wiki, or an XML file.' ,
154 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
155 'sending a file for the "xml" parameter.'
159 public function getPossibleErrors() {
160 return array_merge( parent::getPossibleErrors(), array(
161 array( 'cantimport' ),
162 array( 'missingparam', 'interwikipage' ),
163 array( 'cantimport-upload' ),
164 array( 'import-unknownerror', 'source' ),
165 array( 'import-unknownerror', 'result' ),
166 array( 'import-rootpage-nosubpage', 'namespace' ),
167 array( 'import-rootpage-invalid' ),
168 ) );
171 public function needsToken() {
172 return true;
175 public function getTokenSalt() {
176 return '';
179 public function getExamples() {
180 return array(
181 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC'
182 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
186 public function getHelpUrls() {
187 return 'https://www.mediawiki.org/wiki/API:Import';
190 public function getVersion() {
191 return __CLASS__ . ': $Id$';
196 * Import reporter for the API
197 * @ingroup API
199 class ApiImportReporter extends ImportReporter {
200 private $mResultArr = array();
203 * @param $title Title
204 * @param $origTitle Title
205 * @param $revisionCount int
206 * @param $successCount int
207 * @param $pageInfo
208 * @return void
210 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
211 // Add a result entry
212 $r = array();
214 if ( $title === null ) {
215 # Invalid or non-importable title
216 $r['title'] = $pageInfo['title'];
217 $r['invalid'] = '';
218 } else {
219 ApiQueryBase::addTitleInfo( $r, $title );
220 $r['revisions'] = intval( $successCount );
223 $this->mResultArr[] = $r;
225 // Piggyback on the parent to do the logging
226 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
229 function getData() {
230 return $this->mResultArr;