]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiImport.php
MediaWiki 1.16.1
[autoinstallsdev/mediawiki.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 (C) 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                 $params = $this->extractRequestParams();
47
48                 $source = null;
49                 $isUpload = false;
50                 if ( isset( $params['interwikisource'] ) )
51                 {
52                         if ( !isset( $params['interwikipage'] ) )
53                                 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
54                         $source = ImportStreamSource::newFromInterwiki(
55                                         $params['interwikisource'],
56                                         $params['interwikipage'],
57                                         $params['fullhistory'],
58                                         $params['templates'] );
59                 }
60                 else
61                 {
62                         $isUpload = true;
63                         if ( !$wgUser->isAllowed( 'importupload' ) )
64                                 $this->dieUsageMsg( array( 'cantimport-upload' ) );
65                         $source = ImportStreamSource::newFromUpload( 'xml' );
66                 }
67                 if ( $source instanceof WikiErrorMsg )
68                         $this->dieUsageMsg( array_merge(
69                                 array( $source->getMessageKey() ),
70                                 $source->getMessageArgs() ) );
71                 else if ( WikiError::isError( $source ) )
72                         // This shouldn't happen
73                         $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
74
75                 $importer = new WikiImporter( $source );
76                 if ( isset( $params['namespace'] ) )
77                         $importer->setTargetNamespace( $params['namespace'] );
78                 $reporter = new ApiImportReporter( $importer, $isUpload,
79                                         $params['interwikisource'],
80                                         $params['summary'] );
81
82                 $result = $importer->doImport();
83                 if ( $result instanceof WikiXmlError )
84                         $this->dieUsageMsg( array( 'import-xml-error',
85                                 $result->mLine,
86                                 $result->mColumn,
87                                 $result->mByte . $result->mContext,
88                                 xml_error_string( $result->mXmlError ) ) );
89                 else if ( WikiError::isError( $result ) )
90                         $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) ); // This shouldn't happen
91
92                 $resultData = $reporter->getData();
93                 $this->getResult()->setIndexedTagName( $resultData, 'page' );
94                 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
95         }
96
97         public function mustBePosted() {
98                 return true;
99         }
100
101         public function isWriteMode() {
102                 return true;
103         }
104
105         public function getAllowedParams() {
106                 global $wgImportSources;
107                 return array (
108                         'token' => null,
109                         'summary' => null,
110                         'xml' => null,
111                         'interwikisource' => array(
112                                 ApiBase :: PARAM_TYPE => $wgImportSources
113                         ),
114                         'interwikipage' => null,
115                         'fullhistory' => false,
116                         'templates' => false,
117                         'namespace' => array(
118                                 ApiBase :: PARAM_TYPE => 'namespace'
119                         )
120                 );
121         }
122
123         public function getParamDescription() {
124                 return array (
125                         'token' => 'Import token obtained through prop=info',
126                         'summary' => 'Import summary',
127                         'xml' => 'Uploaded XML file',
128                         'interwikisource' => 'For interwiki imports: wiki to import from',
129                         'interwikipage' => 'For interwiki imports: page to import',
130                         'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
131                         'templates' => 'For interwiki imports: import all included templates as well',
132                         'namespace' => 'For interwiki imports: import to this namespace',
133                 );
134         }
135
136         public function getDescription() {
137                 return array (
138                         'Import a page from another wiki, or an XML file'
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: ApiImport.php 74217 2010-10-03 15:53:07Z reedy $';
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 )
180         {
181                 // Add a result entry
182                 $r = array();
183                 ApiQueryBase::addTitleInfo( $r, $title );
184                 $r['revisions'] = intval( $successCount );
185                 $this->mResultArr[] = $r;
186
187                 // Piggyback on the parent to do the logging
188                 parent::reportPage( $title, $origTitle, $revisionCount, $successCount );
189         }
190
191         function getData()
192         {
193                 return $this->mResultArr;
194         }
195 }