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();
47 if ( !isset( $params['token'] ) )
48 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
49 if ( !$wgUser->matchEditToken( $params['token'] ) )
50 $this->dieUsageMsg( array( 'sessionfailure' ) );
54 if ( isset( $params['interwikisource'] ) )
56 if ( !isset( $params['interwikipage'] ) )
57 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
58 $source = ImportStreamSource
::newFromInterwiki(
59 $params['interwikisource'],
60 $params['interwikipage'],
61 $params['fullhistory'],
62 $params['templates'] );
67 if ( !$wgUser->isAllowed( 'importupload' ) )
68 $this->dieUsageMsg( array( 'cantimport-upload' ) );
69 $source = ImportStreamSource
::newFromUpload( 'xml' );
71 if ( $source instanceof WikiErrorMsg
)
72 $this->dieUsageMsg( array_merge(
73 array( $source->getMessageKey() ),
74 $source->getMessageArgs() ) );
75 else if ( WikiError
::isError( $source ) )
76 // This shouldn't happen
77 $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
79 $importer = new WikiImporter( $source );
80 if ( isset( $params['namespace'] ) )
81 $importer->setTargetNamespace( $params['namespace'] );
82 $reporter = new ApiImportReporter( $importer, $isUpload,
83 $params['interwikisource'],
86 $result = $importer->doImport();
87 if ( $result instanceof WikiXmlError
)
88 $this->dieUsageMsg( array( 'import-xml-error',
91 $result->mByte
. $result->mContext
,
92 xml_error_string( $result->mXmlError
) ) );
93 else if ( WikiError
::isError( $result ) )
94 $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) ); // This shouldn't happen
96 $resultData = $reporter->getData();
97 $this->getResult()->setIndexedTagName( $resultData, 'page' );
98 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
101 public function mustBePosted() { return true; }
103 public function isWriteMode() {
107 public function getAllowedParams() {
108 global $wgImportSources;
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'
125 public function getParamDescription() {
127 'token' => 'Import token obtained through prop=info',
128 'summary' => 'Import summary',
129 'xml' => 'Uploaded XML file',
130 'interwikisource' => 'For interwiki imports: wiki to import from',
131 'interwikipage' => 'For interwiki imports: page to import',
132 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
133 'templates' => 'For interwiki imports: import all included templates as well',
134 'namespace' => 'For interwiki imports: import to this namespace',
138 public function getDescription() {
140 'Import a page from another wiki, or an XML file'
144 protected function getExamples() {
146 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
147 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
151 public function getVersion() {
152 return __CLASS__
. ': $Id$';
157 * Import reporter for the API
160 class ApiImportReporter
extends ImportReporter
{
161 private $mResultArr = array();
163 function reportPage( $title, $origTitle, $revisionCount, $successCount )
165 // Add a result entry
167 ApiQueryBase
::addTitleInfo( $r, $title );
168 $r['revisions'] = intval( $successCount );
169 $this->mResultArr
[] = $r;
171 // Piggyback on the parent to do the logging
172 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount );
177 return $this->mResultArr
;