Merge "Don't check namespace in SpecialWantedtemplates"
[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 /**
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->dieUsageMsg( 'cantimport' );
46 }
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieUsageMsg( array( '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->dieUsageMsg( 'cantimport-upload' );
60 }
61 $source = ImportStreamSource::newFromUpload( 'xml' );
62 }
63 if ( !$source->isOK() ) {
64 $this->dieStatus( $source );
65 }
66
67 $importer = new WikiImporter( $source->value, $this->getConfig() );
68 if ( isset( $params['namespace'] ) ) {
69 $importer->setTargetNamespace( $params['namespace'] );
70 } elseif ( isset( $params['rootpage'] ) ) {
71 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
72 if ( !$statusRootPage->isGood() ) {
73 $this->dieStatus( $statusRootPage );
74 }
75 }
76 $reporter = new ApiImportReporter(
77 $importer,
78 $isUpload,
79 $params['interwikisource'],
80 $params['summary']
81 );
82
83 try {
84 $importer->doImport();
85 } catch ( Exception $e ) {
86 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
87 }
88
89 $resultData = $reporter->getData();
90 $result = $this->getResult();
91 ApiResult::setIndexedTagName( $resultData, 'page' );
92 $result->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 return array(
105 'summary' => null,
106 'xml' => array(
107 ApiBase::PARAM_TYPE => 'upload',
108 ),
109 'interwikisource' => array(
110 ApiBase::PARAM_TYPE => $this->getConfig()->get( 'ImportSources' ),
111 ),
112 'interwikipage' => null,
113 'fullhistory' => false,
114 'templates' => false,
115 'namespace' => array(
116 ApiBase::PARAM_TYPE => 'namespace'
117 ),
118 'rootpage' => null,
119 );
120 }
121
122 public function needsToken() {
123 return 'csrf';
124 }
125
126 protected function getExamplesMessages() {
127 return array(
128 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
129 'namespace=100&fullhistory=&token=123ABC'
130 => 'apihelp-import-example-import',
131 );
132 }
133
134 public function getHelpUrls() {
135 return 'https://www.mediawiki.org/wiki/API:Import';
136 }
137 }
138
139 /**
140 * Import reporter for the API
141 * @ingroup API
142 */
143 class ApiImportReporter extends ImportReporter {
144 private $mResultArr = array();
145
146 /**
147 * @param Title $title
148 * @param Title $origTitle
149 * @param int $revisionCount
150 * @param int $successCount
151 * @param array $pageInfo
152 * @return void
153 */
154 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
155 // Add a result entry
156 $r = array();
157
158 if ( $title === null ) {
159 # Invalid or non-importable title
160 $r['title'] = $pageInfo['title'];
161 $r['invalid'] = true;
162 } else {
163 ApiQueryBase::addTitleInfo( $r, $title );
164 $r['revisions'] = intval( $successCount );
165 }
166
167 $this->mResultArr[] = $r;
168
169 // Piggyback on the parent to do the logging
170 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
171 }
172
173 function getData() {
174 return $this->mResultArr;
175 }
176 }