API: fix copyright symbol, coding style cleanup, more braces
[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 © 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 }
47 $params = $this->extractRequestParams();
48
49 $source = null;
50 $isUpload = false;
51 if ( isset( $params['interwikisource'] ) ) {
52 if ( !isset( $params['interwikipage'] ) ) {
53 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
54 }
55 $source = ImportStreamSource::newFromInterwiki(
56 $params['interwikisource'],
57 $params['interwikipage'],
58 $params['fullhistory'],
59 $params['templates']
60 );
61 } else {
62 $isUpload = true;
63 if ( !$wgUser->isAllowed( 'importupload' ) ) {
64 $this->dieUsageMsg( array( 'cantimport-upload' ) );
65 }
66 $source = ImportStreamSource::newFromUpload( 'xml' );
67 }
68 if ( $source instanceof WikiErrorMsg ) {
69 $this->dieUsageMsg( array_merge(
70 array( $source->getMessageKey() ),
71 $source->getMessageArgs() ) );
72 } elseif ( WikiError::isError( $source ) ) {
73 // This shouldn't happen
74 $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
75 }
76
77 $importer = new WikiImporter( $source );
78 if ( isset( $params['namespace'] ) ) {
79 $importer->setTargetNamespace( $params['namespace'] );
80 }
81 $reporter = new ApiImportReporter(
82 $importer,
83 $isUpload,
84 $params['interwikisource'],
85 $params['summary']
86 );
87
88 $result = $importer->doImport();
89 if ( $result instanceof WikiXmlError ) {
90 $this->dieUsageMsg( array( 'import-xml-error',
91 $result->mLine,
92 $result->mColumn,
93 $result->mByte . $result->mContext,
94 xml_error_string( $result->mXmlError ) ) );
95 } elseif ( WikiError::isError( $result ) ) {
96 $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) ); // This shouldn't happen
97 }
98
99 $resultData = $reporter->getData();
100 $this->getResult()->setIndexedTagName( $resultData, 'page' );
101 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
102 }
103
104 public function mustBePosted() {
105 return true;
106 }
107
108 public function isWriteMode() {
109 return true;
110 }
111
112 public function getAllowedParams() {
113 global $wgImportSources;
114 return array(
115 'token' => null,
116 'summary' => null,
117 'xml' => null,
118 'interwikisource' => array(
119 ApiBase::PARAM_TYPE => $wgImportSources
120 ),
121 'interwikipage' => null,
122 'fullhistory' => false,
123 'templates' => false,
124 'namespace' => array(
125 ApiBase::PARAM_TYPE => 'namespace'
126 )
127 );
128 }
129
130 public function getParamDescription() {
131 return array(
132 'token' => 'Import token obtained through prop=info',
133 'summary' => 'Import summary',
134 'xml' => 'Uploaded XML file',
135 'interwikisource' => 'For interwiki imports: wiki to import from',
136 'interwikipage' => 'For interwiki imports: page to import',
137 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
138 'templates' => 'For interwiki imports: import all included templates as well',
139 'namespace' => 'For interwiki imports: import to this namespace',
140 );
141 }
142
143 public function getDescription() {
144 return array(
145 'Import a page from another wiki, or an XML file'
146 );
147 }
148
149 public function getPossibleErrors() {
150 return array_merge( parent::getPossibleErrors(), array(
151 array( 'cantimport' ),
152 array( 'missingparam', 'interwikipage' ),
153 array( 'cantimport-upload' ),
154 array( 'import-unknownerror', 'source' ),
155 array( 'import-unknownerror', 'result' ),
156 ) );
157 }
158
159 public function getTokenSalt() {
160 return '';
161 }
162
163 protected function getExamples() {
164 return array(
165 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
166 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
167 );
168 }
169
170 public function getVersion() {
171 return __CLASS__ . ': $Id$';
172 }
173 }
174
175 /**
176 * Import reporter for the API
177 * @ingroup API
178 */
179 class ApiImportReporter extends ImportReporter {
180 private $mResultArr = array();
181
182 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
183 // Add a result entry
184 $r = array();
185 ApiQueryBase::addTitleInfo( $r, $title );
186 $r['revisions'] = intval( $successCount );
187 $this->mResultArr[] = $r;
188
189 // Piggyback on the parent to do the logging
190 parent::reportPage( $title, $origTitle, $revisionCount, $successCount );
191 }
192
193 function getData() {
194 return $this->mResultArr;
195 }
196 }