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