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
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() {
46 $params = $this->extractRequestParams();
49 if ( isset( $params['interwikisource'] ) ) {
50 if ( !$wgUser->isAllowed( 'import' ) ) {
51 $this->dieUsageMsg( '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'],
64 if ( !$wgUser->isAllowed( 'importupload' ) ) {
65 $this->dieUsageMsg( '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(
80 $params['interwikisource'],
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() {
100 public function isWriteMode() {
104 public function getAllowedParams() {
105 global $wgImportSources;
110 'interwikisource' => array(
111 ApiBase
::PARAM_TYPE
=> $wgImportSources
113 'interwikipage' => null,
114 'fullhistory' => false,
115 'templates' => false,
116 'namespace' => array(
117 ApiBase
::PARAM_TYPE
=> 'namespace'
122 public function getParamDescription() {
124 'token' => 'Import token obtained through prop=info',
125 'summary' => 'Import summary',
126 'xml' => 'Uploaded XML file',
127 'interwikisource' => 'For interwiki imports: wiki to import from',
128 'interwikipage' => 'For interwiki imports: page to import',
129 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
130 'templates' => 'For interwiki imports: import all included templates as well',
131 'namespace' => 'For interwiki imports: import to this namespace',
135 public function getDescription() {
137 'Import a page from another wiki, or an XML file.' ,
138 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
139 'sending a file for the "xml" parameter.'
143 public function getPossibleErrors() {
144 return array_merge( parent
::getPossibleErrors(), array(
145 array( 'cantimport' ),
146 array( 'missingparam', 'interwikipage' ),
147 array( 'cantimport-upload' ),
148 array( 'import-unknownerror', 'source' ),
149 array( 'import-unknownerror', 'result' ),
153 public function needsToken() {
157 public function getTokenSalt() {
161 protected function getExamples() {
163 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
164 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC',
168 public function getVersion() {
169 return __CLASS__
. ': $Id$';
174 * Import reporter for the API
177 class ApiImportReporter
extends ImportReporter
{
178 private $mResultArr = array();
181 * @param $title Title
182 * @param $origTitle Title
183 * @param $revisionCount int
184 * @param $successCount int
188 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
189 // Add a result entry
191 ApiQueryBase
::addTitleInfo( $r, $title );
192 $r['revisions'] = intval( $successCount );
193 $this->mResultArr
[] = $r;
195 // Piggyback on the parent to do the logging
196 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
200 return $this->mResultArr
;