* changed display function for length to Linker::formatRevisionSize
[mediawiki.git] / includes / api / ApiImport.php
blobf01035cc7629a44b276931d3d984534fa4442d11
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
32 /**
33 * API module that imports an XML file like Special:Import does
35 * @ingroup API
37 class ApiImport extends ApiBase {
39 public function __construct( $main, $action ) {
40 parent::__construct( $main, $action );
43 public function execute() {
44 global $wgUser;
46 $params = $this->extractRequestParams();
48 $isUpload = false;
49 if ( isset( $params['interwikisource'] ) ) {
50 if ( !$wgUser->isAllowed( 'import' ) ) {
51 $this->dieUsageMsg( array( 'cantimport' ) );
53 if ( !isset( $params['interwikipage'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
56 $source = ImportStreamSource::newFromInterwiki(
57 $params['interwikisource'],
58 $params['interwikipage'],
59 $params['fullhistory'],
60 $params['templates']
62 } else {
63 $isUpload = true;
64 if ( !$wgUser->isAllowed( 'importupload' ) ) {
65 $this->dieUsageMsg( array( 'cantimport-upload' ) );
67 $source = ImportStreamSource::newFromUpload( 'xml' );
69 if ( !$source->isOK() ) {
70 $this->dieUsageMsg( $source->getErrorsArray() );
73 $importer = new WikiImporter( $source->value );
74 if ( isset( $params['namespace'] ) ) {
75 $importer->setTargetNamespace( $params['namespace'] );
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 $this->getResult()->setIndexedTagName( $resultData, 'page' );
92 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
95 public function mustBePosted() {
96 return true;
99 public function isWriteMode() {
100 return true;
103 public function getAllowedParams() {
104 global $wgImportSources;
105 return array(
106 'token' => null,
107 'summary' => null,
108 'xml' => null,
109 'interwikisource' => array(
110 ApiBase::PARAM_TYPE => $wgImportSources
112 'interwikipage' => null,
113 'fullhistory' => false,
114 'templates' => false,
115 'namespace' => array(
116 ApiBase::PARAM_TYPE => 'namespace'
121 public function getParamDescription() {
122 return array(
123 'token' => 'Import token obtained through prop=info',
124 'summary' => 'Import summary',
125 'xml' => 'Uploaded XML file',
126 'interwikisource' => 'For interwiki imports: wiki to import from',
127 'interwikipage' => 'For interwiki imports: page to import',
128 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
129 'templates' => 'For interwiki imports: import all included templates as well',
130 'namespace' => 'For interwiki imports: import to this namespace',
134 public function getDescription() {
135 return array(
136 'Import a page from another wiki, or an XML file.' ,
137 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
138 'sending a file for the "xml" parameter.'
142 public function getPossibleErrors() {
143 return array_merge( parent::getPossibleErrors(), array(
144 array( 'cantimport' ),
145 array( 'missingparam', 'interwikipage' ),
146 array( 'cantimport-upload' ),
147 array( 'import-unknownerror', 'source' ),
148 array( 'import-unknownerror', 'result' ),
149 ) );
152 public function needsToken() {
153 return true;
156 public function getTokenSalt() {
157 return '';
160 protected function getExamples() {
161 return array(
162 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
163 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC',
167 public function getVersion() {
168 return __CLASS__ . ': $Id$';
173 * Import reporter for the API
174 * @ingroup API
176 class ApiImportReporter extends ImportReporter {
177 private $mResultArr = array();
179 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
180 // Add a result entry
181 $r = array();
182 ApiQueryBase::addTitleInfo( $r, $title );
183 $r['revisions'] = intval( $successCount );
184 $this->mResultArr[] = $r;
186 // Piggyback on the parent to do the logging
187 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
190 function getData() {
191 return $this->mResultArr;