4 * Created on Feb 4, 2009
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiBase.php' );
32 * API module that imports an XML file like Special:Import does
36 class ApiImport extends ApiBase {
38 public function __construct( $main, $action ) {
39 parent :: __construct( $main, $action );
42 public function execute() {
44 if ( !$wgUser->isAllowed( 'import' ) )
45 $this->dieUsageMsg( array( 'cantimport' ) );
46 $params = $this->extractRequestParams();
50 if ( isset( $params['interwikisource'] ) )
52 if ( !isset( $params['interwikipage'] ) )
53 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
54 $source = ImportStreamSource::newFromInterwiki(
55 $params['interwikisource'],
56 $params['interwikipage'],
57 $params['fullhistory'],
58 $params['templates'] );
63 if ( !$wgUser->isAllowed( 'importupload' ) )
64 $this->dieUsageMsg( array( 'cantimport-upload' ) );
65 $source = ImportStreamSource::newFromUpload( 'xml' );
67 if ( $source instanceof WikiErrorMsg )
68 $this->dieUsageMsg( array_merge(
69 array( $source->getMessageKey() ),
70 $source->getMessageArgs() ) );
71 else if ( WikiError::isError( $source ) )
72 // This shouldn't happen
73 $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
75 $importer = new WikiImporter( $source );
76 if ( isset( $params['namespace'] ) )
77 $importer->setTargetNamespace( $params['namespace'] );
78 $reporter = new ApiImportReporter( $importer, $isUpload,
79 $params['interwikisource'],
82 $result = $importer->doImport();
83 if ( $result instanceof WikiXmlError )
84 $this->dieUsageMsg( array( 'import-xml-error',
87 $result->mByte . $result->mContext,
88 xml_error_string( $result->mXmlError ) ) );
89 else if ( WikiError::isError( $result ) )
90 $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) ); // This shouldn't happen
92 $resultData = $reporter->getData();
93 $this->getResult()->setIndexedTagName( $resultData, 'page' );
94 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
97 public function mustBePosted() {
101 public function isWriteMode() {
105 public function getAllowedParams() {
106 global $wgImportSources;
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'
123 public function getParamDescription() {
125 'token' => 'Import token obtained through prop=info',
126 'summary' => 'Import summary',
127 'xml' => 'Uploaded XML file',
128 'interwikisource' => 'For interwiki imports: wiki to import from',
129 'interwikipage' => 'For interwiki imports: page to import',
130 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
131 'templates' => 'For interwiki imports: import all included templates as well',
132 'namespace' => 'For interwiki imports: import to this namespace',
136 public function getDescription() {
138 'Import a page from another wiki, or an XML file'
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' ),
152 public function needsToken() {
156 public function getTokenSalt() {
160 protected function getExamples() {
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: ApiImport.php 74217 2010-10-03 15:53:07Z reedy $';
173 * Import reporter for the API
176 class ApiImportReporter extends ImportReporter {
177 private $mResultArr = array();
179 function reportPage( $title, $origTitle, $revisionCount, $successCount )
181 // Add a result entry
183 ApiQueryBase::addTitleInfo( $r, $title );
184 $r['revisions'] = intval( $successCount );
185 $this->mResultArr[] = $r;
187 // Piggyback on the parent to do the logging
188 parent::reportPage( $title, $origTitle, $revisionCount, $successCount );
193 return $this->mResultArr;