* API: (bug 17007) Add action=import
[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 }
64 else
65 {
66 $isUpload = true;
67 if(!$wgUser->isAllowed('importupload'))
68 $this->dieUsageMsg(array('cantimport-upload'));
69 $source = ImportStreamSource::newFromUpload('xml');
70 }
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()));
78
79 $importer = new WikiImporter($source);
80 if(isset($params['namespace']))
81 $importer->setTargetNamespace($params['namespace']);
82 $reporter = new ApiImportReporter($importer, $isUpload,
83 $params['interwikisource'],
84 $params['summary']);
85
86 $result = $importer->doImport();
87 if($result instanceof WikiXmlError)
88 $this->dieUsageMsg(array('import-xml-error',
89 $result->mLine,
90 $result->mColumn,
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);
99 }
100
101 public function mustBePosted() { return true; }
102
103 public function getAllowedParams() {
104 global $wgImportSources;
105 return array (
106 'token' => null,
107 'summary' => null,
108 'xml' => null,
109 'interwikisource' => array(
110 ApiBase :: PARAM_TYPE => $wgImportSources
111 ),
112 'interwikipage' => null,
113 'fullhistory' => false,
114 'namespace' => array(
115 ApiBase :: PARAM_TYPE => 'namespace'
116 )
117 );
118 }
119
120 public function getParamDescription() {
121 return array (
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',
129 );
130 }
131
132 public function getDescription() {
133 return array (
134 'Import a page from another wiki, or an XML file'
135 );
136 }
137
138 protected function getExamples() {
139 return array(
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',
142 );
143 }
144
145 public function getVersion() {
146 return __CLASS__ . ': $Id$';
147 }
148 }
149
150 /**
151 * Import reporter for the API
152 * @ingroup API
153 */
154 class ApiImportReporter extends ImportReporter {
155 private $mResultArr = array();
156
157 function reportPage($title, $origTitle, $revisionCount, $successCount)
158 {
159 // Add a result entry
160 $r = array();
161 ApiQueryBase::addTitleInfo($r, $title);
162 $r['revisions'] = $successCount;
163 $this->mResultArr[] = $r;
164
165 // Piggyback on the parent to do the logging
166 parent::reportPage($title, $origTitle, $revisionCount, $successCount);
167 }
168
169 function getData()
170 {
171 return $this->mResultArr;
172 }
173 }