Call Linker methods statically
[lhc/web/wiklou.git] / includes / api / ApiImport.php
1 <?php
2 /**
3 *
4 *
5 * Created on Feb 4, 2009
6 *
7 * Copyright © 2009 Roan Kattouw <Firstname>.<Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
30 }
31
32 /**
33 * API module that imports an XML file like Special:Import does
34 *
35 * @ingroup API
36 */
37 class ApiImport extends ApiBase {
38
39 public function __construct( $main, $action ) {
40 parent::__construct( $main, $action );
41 }
42
43 public function execute() {
44 global $wgUser;
45
46 $params = $this->extractRequestParams();
47
48 $isUpload = false;
49 if ( isset( $params['interwikisource'] ) ) {
50 if ( !$wgUser->isAllowed( 'import' ) ) {
51 $this->dieUsageMsg( 'cantimport' );
52 }
53 if ( !isset( $params['interwikipage'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
55 }
56 $source = ImportStreamSource::newFromInterwiki(
57 $params['interwikisource'],
58 $params['interwikipage'],
59 $params['fullhistory'],
60 $params['templates']
61 );
62 } else {
63 $isUpload = true;
64 if ( !$wgUser->isAllowed( 'importupload' ) ) {
65 $this->dieUsageMsg( 'cantimport-upload' );
66 }
67 $source = ImportStreamSource::newFromUpload( 'xml' );
68 }
69 if ( !$source->isOK() ) {
70 $this->dieUsageMsg( $source->getErrorsArray() );
71 }
72
73 $importer = new WikiImporter( $source->value );
74 if ( isset( $params['namespace'] ) ) {
75 $importer->setTargetNamespace( $params['namespace'] );
76 }
77 $reporter = new ApiImportReporter(
78 $importer,
79 $isUpload,
80 $params['interwikisource'],
81 $params['summary']
82 );
83
84 try {
85 $importer->doImport();
86 } catch ( MWException $e ) {
87 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
88 }
89
90 $resultData = $reporter->getData();
91 $result = $this->getResult();
92 $result->setIndexedTagName( $resultData, 'page' );
93 $result->addValue( null, $this->getModuleName(), $resultData );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
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 '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.'
140 );
141 }
142
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' ),
150 ) );
151 }
152
153 public function needsToken() {
154 return true;
155 }
156
157 public function getTokenSalt() {
158 return '';
159 }
160
161 public function getExamples() {
162 return array(
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',
165 );
166 }
167
168 public function getHelpUrls() {
169 return 'http://www.mediawiki.org/wiki/API:Import';
170 }
171
172 public function getVersion() {
173 return __CLASS__ . ': $Id$';
174 }
175 }
176
177 /**
178 * Import reporter for the API
179 * @ingroup API
180 */
181 class ApiImportReporter extends ImportReporter {
182 private $mResultArr = array();
183
184 /**
185 * @param $title Title
186 * @param $origTitle Title
187 * @param $revisionCount int
188 * @param $successCount int
189 * @param $pageInfo
190 * @return void
191 */
192 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
193 // Add a result entry
194 $r = array();
195 ApiQueryBase::addTitleInfo( $r, $title );
196 $r['revisions'] = intval( $successCount );
197 $this->mResultArr[] = $r;
198
199 // Piggyback on the parent to do the logging
200 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
201 }
202
203 function getData() {
204 return $this->mResultArr;
205 }
206 }