Make @deprecated documentation more consistent (see: http://svn.wikimedia.org/doc...
[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( array( '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( array( '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 $this->getResult()->setIndexedTagName( $resultData, 'page' );
92 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
93 }
94
95 public function mustBePosted() {
96 return true;
97 }
98
99 public function isWriteMode() {
100 return true;
101 }
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 'templates' => false,
115 'namespace' => array(
116 ApiBase::PARAM_TYPE => 'namespace'
117 )
118 );
119 }
120
121 public function getParamDescription() {
122 return array(
123 'token' => 'Import token obtained through prop=info',
124 'summary' => 'Import summary',
125 'xml' => 'Uploaded XML file',
126 'interwikisource' => 'For interwiki imports: wiki to import from',
127 'interwikipage' => 'For interwiki imports: page to import',
128 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
129 'templates' => 'For interwiki imports: import all included templates as well',
130 'namespace' => 'For interwiki imports: import to this namespace',
131 );
132 }
133
134 public function getDescription() {
135 return array(
136 'Import a page from another wiki, or an XML file.' ,
137 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
138 'sending a file for the "xml" parameter.'
139 );
140 }
141
142 public function getPossibleErrors() {
143 return array_merge( parent::getPossibleErrors(), array(
144 array( 'cantimport' ),
145 array( 'missingparam', 'interwikipage' ),
146 array( 'cantimport-upload' ),
147 array( 'import-unknownerror', 'source' ),
148 array( 'import-unknownerror', 'result' ),
149 ) );
150 }
151
152 public function needsToken() {
153 return true;
154 }
155
156 public function getTokenSalt() {
157 return '';
158 }
159
160 protected function getExamples() {
161 return array(
162 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
163 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC',
164 );
165 }
166
167 public function getVersion() {
168 return __CLASS__ . ': $Id$';
169 }
170 }
171
172 /**
173 * Import reporter for the API
174 * @ingroup API
175 */
176 class ApiImportReporter extends ImportReporter {
177 private $mResultArr = array();
178
179 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
180 // Add a result entry
181 $r = array();
182 ApiQueryBase::addTitleInfo( $r, $title );
183 $r['revisions'] = intval( $successCount );
184 $this->mResultArr[] = $r;
185
186 // Piggyback on the parent to do the logging
187 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
188 }
189
190 function getData() {
191 return $this->mResultArr;
192 }
193 }