]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiImport.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.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                 $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->isOK() ) {
69                         $this->dieUsageMsg( $source->getErrorsArray() );
70                 }
71
72                 $importer = new WikiImporter( $source->value );
73                 if ( isset( $params['namespace'] ) ) {
74                         $importer->setTargetNamespace( $params['namespace'] );
75                 }
76                 $reporter = new ApiImportReporter(
77                         $importer,
78                         $isUpload,
79                         $params['interwikisource'],
80                         $params['summary']
81                 );
82
83                 try {
84                         $importer->doImport();
85                 } catch ( MWException $e ) {
86                         $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
87                 }
88
89                 $resultData = $reporter->getData();
90                 $this->getResult()->setIndexedTagName( $resultData, 'page' );
91                 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
92         }
93
94         public function mustBePosted() {
95                 return true;
96         }
97
98         public function isWriteMode() {
99                 return true;
100         }
101
102         public function getAllowedParams() {
103                 global $wgImportSources;
104                 return array(
105                         'token' => null,
106                         'summary' => null,
107                         'xml' => null,
108                         'interwikisource' => array(
109                                 ApiBase::PARAM_TYPE => $wgImportSources
110                         ),
111                         'interwikipage' => null,
112                         'fullhistory' => false,
113                         'templates' => false,
114                         'namespace' => array(
115                                 ApiBase::PARAM_TYPE => 'namespace'
116                         )
117                 );
118         }
119
120         public function getParamDescription() {
121                 return array(
122                         'token' => 'Import token obtained through prop=info',
123                         'summary' => 'Import summary',
124                         'xml' => 'Uploaded XML file',
125                         'interwikisource' => 'For interwiki imports: wiki to import from',
126                         'interwikipage' => 'For interwiki imports: page to import',
127                         'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
128                         'templates' => 'For interwiki imports: import all included templates as well',
129                         'namespace' => 'For interwiki imports: import to this namespace',
130                 );
131         }
132
133         public function getDescription() {
134                 return 'Import a page from another wiki, or an XML file';
135         }
136
137         public function getPossibleErrors() {
138                 return array_merge( parent::getPossibleErrors(), array(
139                         array( 'cantimport' ),
140                         array( 'missingparam', 'interwikipage' ),
141                         array( 'cantimport-upload' ),
142                         array( 'import-unknownerror', 'source' ),
143                         array( 'import-unknownerror', 'result' ),
144                 ) );
145         }
146
147         public function needsToken() {
148                 return true;
149         }
150
151         public function getTokenSalt() {
152                 return '';
153         }
154
155         protected function getExamples() {
156                 return array(
157                         'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
158                         '  api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC',
159                 );
160         }
161
162         public function getVersion() {
163                 return __CLASS__ . ': $Id$';
164         }
165 }
166
167 /**
168  * Import reporter for the API
169  * @ingroup API
170  */
171 class ApiImportReporter extends ImportReporter {
172         private $mResultArr = array();
173
174         function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
175                 // Add a result entry
176                 $r = array();
177                 ApiQueryBase::addTitleInfo( $r, $title );
178                 $r['revisions'] = intval( $successCount );
179                 $this->mResultArr[] = $r;
180
181                 // Piggyback on the parent to do the logging
182                 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
183         }
184
185         function getData() {
186                 return $this->mResultArr;
187         }
188 }