]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiImport.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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 /**
28  * API module that imports an XML file like Special:Import does
29  *
30  * @ingroup API
31  */
32 class ApiImport extends ApiBase {
33
34         public function execute() {
35                 $this->useTransactionalTimeLimit();
36
37                 $user = $this->getUser();
38                 $params = $this->extractRequestParams();
39
40                 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
41
42                 $isUpload = false;
43                 if ( isset( $params['interwikisource'] ) ) {
44                         if ( !$user->isAllowed( 'import' ) ) {
45                                 $this->dieWithError( 'apierror-cantimport' );
46                         }
47                         if ( !isset( $params['interwikipage'] ) ) {
48                                 $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
49                         }
50                         $source = ImportStreamSource::newFromInterwiki(
51                                 $params['interwikisource'],
52                                 $params['interwikipage'],
53                                 $params['fullhistory'],
54                                 $params['templates']
55                         );
56                 } else {
57                         $isUpload = true;
58                         if ( !$user->isAllowed( 'importupload' ) ) {
59                                 $this->dieWithError( 'apierror-cantimport-upload' );
60                         }
61                         $source = ImportStreamSource::newFromUpload( 'xml' );
62                 }
63                 if ( !$source->isOK() ) {
64                         $this->dieStatus( $source );
65                 }
66
67                 // Check if user can add the log entry tags which were requested
68                 if ( $params['tags'] ) {
69                         $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
70                         if ( !$ableToTag->isOK() ) {
71                                 $this->dieStatus( $ableToTag );
72                         }
73                 }
74
75                 $importer = new WikiImporter( $source->value, $this->getConfig() );
76                 if ( isset( $params['namespace'] ) ) {
77                         $importer->setTargetNamespace( $params['namespace'] );
78                 } elseif ( isset( $params['rootpage'] ) ) {
79                         $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
80                         if ( !$statusRootPage->isGood() ) {
81                                 $this->dieStatus( $statusRootPage );
82                         }
83                 }
84                 $reporter = new ApiImportReporter(
85                         $importer,
86                         $isUpload,
87                         $params['interwikisource'],
88                         $params['summary']
89                 );
90                 if ( $params['tags'] ) {
91                         $reporter->setChangeTags( $params['tags'] );
92                 }
93
94                 try {
95                         $importer->doImport();
96                 } catch ( Exception $e ) {
97                         $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
98                 }
99
100                 $resultData = $reporter->getData();
101                 $result = $this->getResult();
102                 ApiResult::setIndexedTagName( $resultData, 'page' );
103                 $result->addValue( null, $this->getModuleName(), $resultData );
104         }
105
106         /**
107          * Returns a list of interwiki prefixes corresponding to each defined import
108          * source.
109          *
110          * @return array
111          * @since 1.27
112          */
113         public function getAllowedImportSources() {
114                 $importSources = $this->getConfig()->get( 'ImportSources' );
115                 Hooks::run( 'ImportSources', [ &$importSources ] );
116
117                 $result = [];
118                 foreach ( $importSources as $key => $value ) {
119                         if ( is_int( $key ) ) {
120                                 $result[] = $value;
121                         } else {
122                                 foreach ( $value as $subproject ) {
123                                         $result[] = "$key:$subproject";
124                                 }
125                         }
126                 }
127                 return $result;
128         }
129
130         public function mustBePosted() {
131                 return true;
132         }
133
134         public function isWriteMode() {
135                 return true;
136         }
137
138         public function getAllowedParams() {
139                 return [
140                         'summary' => null,
141                         'xml' => [
142                                 ApiBase::PARAM_TYPE => 'upload',
143                         ],
144                         'interwikisource' => [
145                                 ApiBase::PARAM_TYPE => $this->getAllowedImportSources(),
146                         ],
147                         'interwikipage' => null,
148                         'fullhistory' => false,
149                         'templates' => false,
150                         'namespace' => [
151                                 ApiBase::PARAM_TYPE => 'namespace'
152                         ],
153                         'rootpage' => null,
154                         'tags' => [
155                                 ApiBase::PARAM_TYPE => 'tags',
156                                 ApiBase::PARAM_ISMULTI => true,
157                         ],
158                 ];
159         }
160
161         public function needsToken() {
162                 return 'csrf';
163         }
164
165         protected function getExamplesMessages() {
166                 return [
167                         'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
168                                 'namespace=100&fullhistory=&token=123ABC'
169                                 => 'apihelp-import-example-import',
170                 ];
171         }
172
173         public function getHelpUrls() {
174                 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
175         }
176 }
177
178 /**
179  * Import reporter for the API
180  * @ingroup API
181  */
182 class ApiImportReporter extends ImportReporter {
183         private $mResultArr = [];
184
185         /**
186          * @param Title $title
187          * @param Title $origTitle
188          * @param int $revisionCount
189          * @param int $successCount
190          * @param array $pageInfo
191          * @return void
192          */
193         public function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
194                 // Add a result entry
195                 $r = [];
196
197                 if ( $title === null ) {
198                         # Invalid or non-importable title
199                         $r['title'] = $pageInfo['title'];
200                         $r['invalid'] = true;
201                 } else {
202                         ApiQueryBase::addTitleInfo( $r, $title );
203                         $r['revisions'] = intval( $successCount );
204                 }
205
206                 $this->mResultArr[] = $r;
207
208                 // Piggyback on the parent to do the logging
209                 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
210         }
211
212         public function getData() {
213                 return $this->mResultArr;
214         }
215 }