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
28 * API module that imports an XML file like Special:Import does
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' );
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'],
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(
79 $params['interwikisource'],
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 public function mustBePosted() {
99 public function isWriteMode() {
103 public function getAllowedParams() {
107 ApiBase
::PARAM_TYPE
=> 'upload',
109 'interwikisource' => array(
110 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'ImportSources' ),
112 'interwikipage' => null,
113 'fullhistory' => false,
114 'templates' => false,
115 'namespace' => array(
116 ApiBase
::PARAM_TYPE
=> 'namespace'
122 public function needsToken() {
126 protected function getExamplesMessages() {
128 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
129 'namespace=100&fullhistory=&token=123ABC'
130 => 'apihelp-import-example-import',
134 public function getHelpUrls() {
135 return 'https://www.mediawiki.org/wiki/API:Import';
140 * Import reporter for the API
143 class ApiImportReporter
extends ImportReporter
{
144 private $mResultArr = array();
147 * @param Title $title
148 * @param Title $origTitle
149 * @param int $revisionCount
150 * @param int $successCount
151 * @param array $pageInfo
154 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
155 // Add a result entry
158 if ( $title === null ) {
159 # Invalid or non-importable title
160 $r['title'] = $pageInfo['title'];
161 $r['invalid'] = true;
163 ApiQueryBase
::addTitleInfo( $r, $title );
164 $r['revisions'] = intval( $successCount );
167 $this->mResultArr
[] = $r;
169 // Piggyback on the parent to do the logging
170 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
174 return $this->mResultArr
;