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