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 $this->getMain()->requestWriteMode();
45 if(!$wgUser->isAllowed('import'))
46 $this->dieUsageMsg(array('cantimport'));
47 $params = $this->extractRequestParams();
48 if(!isset($params['token']))
49 $this->dieUsageMsg(array('missingparam', 'token'));
50 if(!$wgUser->matchEditToken($params['token']))
51 $this->dieUsageMsg(array('sessionfailure'));
55 if(isset($params['interwikisource']))
57 if(!isset($params['interwikipage']))
58 $this->dieUsageMsg(array('missingparam', 'interwikipage'));
59 $source = ImportStreamSource
::newFromInterwiki(
60 $params['interwikisource'],
61 $params['interwikipage'],
62 $params['fullhistory']);
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 shouldn't happen
95 $this->dieUsageMsg(array('import-unknownerror', $result->getMessage()));
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 getAllowedParams() {
104 global $wgImportSources;
109 'interwikisource' => array(
110 ApiBase
:: PARAM_TYPE
=> $wgImportSources
112 'interwikipage' => null,
113 'fullhistory' => false,
114 'namespace' => array(
115 ApiBase
:: PARAM_TYPE
=> 'namespace'
120 public function getParamDescription() {
122 'token' => 'Import token obtained through prop=info',
123 'summary' => 'Import summary',
124 'xml' => 'Uploaded XML file',
125 'interwikisource' => 'For interwiki imports: wiki to import from',
126 'interwikipage' => 'For interwiki imports: page to import',
127 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
128 'namespace' => 'For interwiki imports: import to this namespace',
132 public function getDescription() {
134 'Import a page from another wiki, or an XML file'
138 protected function getExamples() {
140 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
141 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
145 public function getVersion() {
146 return __CLASS__
. ': $Id$';
151 * Import reporter for the API
154 class ApiImportReporter
extends ImportReporter
{
155 private $mResultArr = array();
157 function reportPage($title, $origTitle, $revisionCount, $successCount)
159 // Add a result entry
161 ApiQueryBase
::addTitleInfo($r, $title);
162 $r['revisions'] = $successCount;
163 $this->mResultArr
[] = $r;
165 // Piggyback on the parent to do the logging
166 parent
::reportPage($title, $origTitle, $revisionCount, $successCount);
171 return $this->mResultArr
;