Merge "objectcache: Add some newlines to WANObjectCache docs"
[mediawiki.git] / includes / api / ApiImport.php
bloba6aae4bb1c3dac6aa9311af96dd89993fb20ab28
1 <?php
2 /**
5 * Created on Feb 4, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * API module that imports an XML file like Special:Import does
30 * @ingroup API
32 class ApiImport extends ApiBase {
34 public function execute() {
35 $this->useTransactionalTimeLimit();
37 $user = $this->getUser();
38 $params = $this->extractRequestParams();
40 $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
42 $isUpload = false;
43 if ( isset( $params['interwikisource'] ) ) {
44 if ( !$user->isAllowed( 'import' ) ) {
45 $this->dieUsageMsg( 'cantimport' );
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
50 $source = ImportStreamSource::newFromInterwiki(
51 $params['interwikisource'],
52 $params['interwikipage'],
53 $params['fullhistory'],
54 $params['templates']
56 } else {
57 $isUpload = true;
58 if ( !$user->isAllowed( 'importupload' ) ) {
59 $this->dieUsageMsg( 'cantimport-upload' );
61 $source = ImportStreamSource::newFromUpload( 'xml' );
63 if ( !$source->isOK() ) {
64 $this->dieStatus( $source );
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 );
76 $reporter = new ApiImportReporter(
77 $importer,
78 $isUpload,
79 $params['interwikisource'],
80 $params['summary']
83 try {
84 $importer->doImport();
85 } catch ( Exception $e ) {
86 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
89 $resultData = $reporter->getData();
90 $result = $this->getResult();
91 ApiResult::setIndexedTagName( $resultData, 'page' );
92 $result->addValue( null, $this->getModuleName(), $resultData );
95 /**
96 * Returns a list of interwiki prefixes corresponding to each defined import
97 * source.
99 * @return array
100 * @since 1.26
102 public function getAllowedImportSources() {
103 $importSources = $this->getConfig()->get( 'ImportSources' );
104 Hooks::run( 'ImportSources', array( &$importSources ) );
106 $result = array();
107 foreach ( $importSources as $key => $value ) {
108 if ( is_int( $key ) ) {
109 $result[] = $value;
110 } else {
111 foreach ( $value as $subproject ) {
112 $result[] = "$key:$subproject";
116 return $result;
119 public function mustBePosted() {
120 return true;
123 public function isWriteMode() {
124 return true;
127 public function getAllowedParams() {
128 return array(
129 'summary' => null,
130 'xml' => array(
131 ApiBase::PARAM_TYPE => 'upload',
133 'interwikisource' => array(
134 ApiBase::PARAM_TYPE => $this->getAllowedImportSources(),
136 'interwikipage' => null,
137 'fullhistory' => false,
138 'templates' => false,
139 'namespace' => array(
140 ApiBase::PARAM_TYPE => 'namespace'
142 'rootpage' => null,
146 public function needsToken() {
147 return 'csrf';
150 protected function getExamplesMessages() {
151 return array(
152 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
153 'namespace=100&fullhistory=&token=123ABC'
154 => 'apihelp-import-example-import',
158 public function getHelpUrls() {
159 return 'https://www.mediawiki.org/wiki/API:Import';
164 * Import reporter for the API
165 * @ingroup API
167 class ApiImportReporter extends ImportReporter {
168 private $mResultArr = array();
171 * @param Title $title
172 * @param Title $origTitle
173 * @param int $revisionCount
174 * @param int $successCount
175 * @param array $pageInfo
176 * @return void
178 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
179 // Add a result entry
180 $r = array();
182 if ( $title === null ) {
183 # Invalid or non-importable title
184 $r['title'] = $pageInfo['title'];
185 $r['invalid'] = true;
186 } else {
187 ApiQueryBase::addTitleInfo( $r, $title );
188 $r['revisions'] = intval( $successCount );
191 $this->mResultArr[] = $r;
193 // Piggyback on the parent to do the logging
194 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
197 function getData() {
198 return $this->mResultArr;