API: Some cleanup
[lhc/web/wiklou.git] / includes / api / ApiImport.php
1 <?php
2
3 /*
4 * Created on Feb 4, 2009
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiBase.php');
29 }
30
31 /**
32 * API module that imports an XML file like Special:Import does
33 *
34 * @ingroup API
35 */
36 class ApiImport extends ApiBase {
37
38 public function __construct($main, $action) {
39 parent :: __construct($main, $action);
40 }
41
42 public function execute() {
43 global $wgUser;
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'));
52
53 $source = null;
54 $isUpload = false;
55 if(isset($params['interwikisource']))
56 {
57 if(!isset($params['interwikipage']))
58 $this->dieUsageMsg(array('missingparam', 'interwikipage'));
59 $source = ImportStreamSource::newFromInterwiki(
60 $params['interwikisource'],
61 $params['interwikipage'],
62 $params['fullhistory'],
63 $params['templates']);
64 }
65 else
66 {
67 $isUpload = true;
68 if(!$wgUser->isAllowed('importupload'))
69 $this->dieUsageMsg(array('cantimport-upload'));
70 $source = ImportStreamSource::newFromUpload('xml');
71 }
72 if($source instanceof WikiErrorMsg)
73 $this->dieUsageMsg(array_merge(
74 array($source->getMessageKey()),
75 $source->getMessageArgs()));
76 else if(WikiError::isError($source))
77 // This shouldn't happen
78 $this->dieUsageMsg(array('import-unknownerror', $source->getMessage()));
79
80 $importer = new WikiImporter($source);
81 if(isset($params['namespace']))
82 $importer->setTargetNamespace($params['namespace']);
83 $reporter = new ApiImportReporter($importer, $isUpload,
84 $params['interwikisource'],
85 $params['summary']);
86
87 $result = $importer->doImport();
88 if($result instanceof WikiXmlError)
89 $this->dieUsageMsg(array('import-xml-error',
90 $result->mLine,
91 $result->mColumn,
92 $result->mByte . $result->mContext,
93 xml_error_string($result->mXmlError)));
94 else if(WikiError::isError($result))
95 // This shouldn't happen
96 $this->dieUsageMsg(array('import-unknownerror', $result->getMessage()));
97 $resultData = $reporter->getData();
98 $this->getResult()->setIndexedTagName($resultData, 'page');
99 $this->getResult()->addValue(null, $this->getModuleName(), $resultData);
100 }
101
102 public function mustBePosted() { return true; }
103
104 public function getAllowedParams() {
105 global $wgImportSources;
106 return array (
107 'token' => null,
108 'summary' => null,
109 'xml' => null,
110 'interwikisource' => array(
111 ApiBase :: PARAM_TYPE => $wgImportSources
112 ),
113 'interwikipage' => null,
114 'fullhistory' => false,
115 'templates' => false,
116 'namespace' => array(
117 ApiBase :: PARAM_TYPE => 'namespace'
118 )
119 );
120 }
121
122 public function getParamDescription() {
123 return array (
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',
132 );
133 }
134
135 public function getDescription() {
136 return array (
137 'Import a page from another wiki, or an XML file'
138 );
139 }
140
141 protected function getExamples() {
142 return array(
143 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
144 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
145 );
146 }
147
148 public function getVersion() {
149 return __CLASS__ . ': $Id$';
150 }
151 }
152
153 /**
154 * Import reporter for the API
155 * @ingroup API
156 */
157 class ApiImportReporter extends ImportReporter {
158 private $mResultArr = array();
159
160 function reportPage($title, $origTitle, $revisionCount, $successCount)
161 {
162 // Add a result entry
163 $r = array();
164 ApiQueryBase::addTitleInfo($r, $title);
165 $r['revisions'] = intval($successCount);
166 $this->mResultArr[] = $r;
167
168 // Piggyback on the parent to do the logging
169 parent::reportPage($title, $origTitle, $revisionCount, $successCount);
170 }
171
172 function getData()
173 {
174 return $this->mResultArr;
175 }
176 }