]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiImport.php
MediaWiki 1.15.4-scripts
[autoinstallsdev/mediawiki.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                 if(!$wgUser->isAllowed('import'))
45                         $this->dieUsageMsg(array('cantimport'));
46                 $params = $this->extractRequestParams();
47                 if(!isset($params['token']))
48                         $this->dieUsageMsg(array('missingparam', 'token'));
49                 if(!$wgUser->matchEditToken($params['token']))
50                         $this->dieUsageMsg(array('sessionfailure'));
51
52                 $source = null;
53                 $isUpload = false;
54                 if(isset($params['interwikisource']))
55                 {
56                         if(!isset($params['interwikipage']))
57                                 $this->dieUsageMsg(array('missingparam', 'interwikipage'));
58                         $source = ImportStreamSource::newFromInterwiki(
59                                         $params['interwikisource'],
60                                         $params['interwikipage'],
61                                         $params['fullhistory'],
62                                         $params['templates']);
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 isWriteMode() {
104                 return true;
105         }
106
107         public function getAllowedParams() {
108                 global $wgImportSources;
109                 return array (
110                         'token' => null,
111                         'summary' => null,
112                         'xml' => null,
113                         'interwikisource' => array(
114                                 ApiBase :: PARAM_TYPE => $wgImportSources
115                         ),
116                         'interwikipage' => null,
117                         'fullhistory' => false,
118                         'templates' => false,
119                         'namespace' => array(
120                                 ApiBase :: PARAM_TYPE => 'namespace'
121                         )
122                 );
123         }
124
125         public function getParamDescription() {
126                 return array (
127                         'token' => 'Import token obtained through prop=info',
128                         'summary' => 'Import summary',
129                         'xml' => 'Uploaded XML file',
130                         'interwikisource' => 'For interwiki imports: wiki to import from',
131                         'interwikipage' => 'For interwiki imports: page to import',
132                         'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
133                         'templates' => 'For interwiki imports: import all included templates as well',
134                         'namespace' => 'For interwiki imports: import to this namespace',
135                 );
136         }
137
138         public function getDescription() {
139                 return array (
140                         'Import a page from another wiki, or an XML file'
141                 );
142         }
143
144         protected function getExamples() {
145                 return array(
146                         'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
147                         '  api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
148                 );
149         }
150
151         public function getVersion() {
152                 return __CLASS__ . ': $Id: ApiImport.php 48091 2009-03-06 13:49:44Z catrope $';
153         }
154 }
155
156 /**
157  * Import reporter for the API
158  * @ingroup API
159  */
160 class ApiImportReporter extends ImportReporter {
161         private $mResultArr = array();
162
163         function reportPage($title, $origTitle, $revisionCount, $successCount)
164         {
165                 // Add a result entry
166                 $r = array();
167                 ApiQueryBase::addTitleInfo($r, $title);
168                 $r['revisions'] = intval($successCount);
169                 $this->mResultArr[] = $r;
170
171                 // Piggyback on the parent to do the logging
172                 parent::reportPage($title, $origTitle, $revisionCount, $successCount);
173         }
174
175         function getData()
176         {
177                 return $this->mResultArr;
178         }
179 }