3 * API for MediaWiki 1.8+
5 * Created on Feb 4, 2009
7 * Copyright © 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
33 * API module that imports an XML file like Special:Import does
37 class ApiImport
extends ApiBase
{
39 public function __construct( $main, $action ) {
40 parent
::__construct( $main, $action );
43 public function execute() {
45 if ( !$wgUser->isAllowed( 'import' ) ) {
46 $this->dieUsageMsg( array( 'cantimport' ) );
48 $params = $this->extractRequestParams();
51 if ( isset( $params['interwikisource'] ) ) {
52 if ( !isset( $params['interwikipage'] ) ) {
53 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
55 $source = ImportStreamSource
::newFromInterwiki(
56 $params['interwikisource'],
57 $params['interwikipage'],
58 $params['fullhistory'],
63 if ( !$wgUser->isAllowed( 'importupload' ) ) {
64 $this->dieUsageMsg( array( 'cantimport-upload' ) );
66 $source = ImportStreamSource
::newFromUpload( 'xml' );
68 if ( $source instanceof WikiErrorMsg
) {
69 $this->dieUsageMsg( array_merge(
70 array( $source->getMessageKey() ),
71 $source->getMessageArgs() ) );
72 } elseif ( WikiError
::isError( $source ) ) {
73 // This shouldn't happen
74 $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
77 $importer = new WikiImporter( $source );
78 if ( isset( $params['namespace'] ) ) {
79 $importer->setTargetNamespace( $params['namespace'] );
81 $reporter = new ApiImportReporter(
84 $params['interwikisource'],
88 $result = $importer->doImport();
89 if ( $result instanceof WikiXmlError
) {
90 $this->dieUsageMsg( array( 'import-xml-error',
93 $result->mByte
. $result->mContext
,
94 xml_error_string( $result->mXmlError
) ) );
95 } elseif ( WikiError
::isError( $result ) ) {
96 $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) ); // This shouldn't happen
99 $resultData = $reporter->getData();
100 $this->getResult()->setIndexedTagName( $resultData, 'page' );
101 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
104 public function mustBePosted() {
108 public function isWriteMode() {
112 public function getAllowedParams() {
113 global $wgImportSources;
118 'interwikisource' => array(
119 ApiBase
::PARAM_TYPE
=> $wgImportSources
121 'interwikipage' => null,
122 'fullhistory' => false,
123 'templates' => false,
124 'namespace' => array(
125 ApiBase
::PARAM_TYPE
=> 'namespace'
130 public function getParamDescription() {
132 'token' => 'Import token obtained through prop=info',
133 'summary' => 'Import summary',
134 'xml' => 'Uploaded XML file',
135 'interwikisource' => 'For interwiki imports: wiki to import from',
136 'interwikipage' => 'For interwiki imports: page to import',
137 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
138 'templates' => 'For interwiki imports: import all included templates as well',
139 'namespace' => 'For interwiki imports: import to this namespace',
143 public function getDescription() {
144 return 'Import a page from another wiki, or an XML file';
147 public function getPossibleErrors() {
148 return array_merge( parent
::getPossibleErrors(), array(
149 array( 'cantimport' ),
150 array( 'missingparam', 'interwikipage' ),
151 array( 'cantimport-upload' ),
152 array( 'import-unknownerror', 'source' ),
153 array( 'import-unknownerror', 'result' ),
157 public function needsToken() {
161 public function getTokenSalt() {
165 protected function getExamples() {
167 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
168 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
172 public function getVersion() {
173 return __CLASS__
. ': $Id$';
178 * Import reporter for the API
181 class ApiImportReporter
extends ImportReporter
{
182 private $mResultArr = array();
184 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
185 // Add a result entry
187 ApiQueryBase
::addTitleInfo( $r, $title );
188 $r['revisions'] = intval( $successCount );
189 $this->mResultArr
[] = $r;
191 // Piggyback on the parent to do the logging
192 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount );
196 return $this->mResultArr
;