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