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 $user = $this->getUser();
36 $params = $this->extractRequestParams();
39 if ( isset( $params['interwikisource'] ) ) {
40 if ( !$user->isAllowed( 'import' ) ) {
41 $this->dieUsageMsg( 'cantimport' );
43 if ( !isset( $params['interwikipage'] ) ) {
44 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
46 $source = ImportStreamSource
::newFromInterwiki(
47 $params['interwikisource'],
48 $params['interwikipage'],
49 $params['fullhistory'],
54 if ( !$user->isAllowed( 'importupload' ) ) {
55 $this->dieUsageMsg( 'cantimport-upload' );
57 $source = ImportStreamSource
::newFromUpload( 'xml' );
59 if ( !$source->isOK() ) {
60 $this->dieStatus( $source );
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 );
72 $reporter = new ApiImportReporter(
75 $params['interwikisource'],
80 $importer->doImport();
81 } catch ( Exception
$e ) {
82 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
85 $resultData = $reporter->getData();
86 $result = $this->getResult();
87 ApiResult
::setIndexedTagName( $resultData, 'page' );
88 $result->addValue( null, $this->getModuleName(), $resultData );
91 public function mustBePosted() {
95 public function isWriteMode() {
99 public function getAllowedParams() {
103 ApiBase
::PARAM_TYPE
=> 'upload',
105 'interwikisource' => array(
106 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'ImportSources' ),
108 'interwikipage' => null,
109 'fullhistory' => false,
110 'templates' => false,
111 'namespace' => array(
112 ApiBase
::PARAM_TYPE
=> 'namespace'
118 public function needsToken() {
122 protected function getExamplesMessages() {
124 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
125 'namespace=100&fullhistory=&token=123ABC'
126 => 'apihelp-import-example-import',
130 public function getHelpUrls() {
131 return 'https://www.mediawiki.org/wiki/API:Import';
136 * Import reporter for the API
139 class ApiImportReporter
extends ImportReporter
{
140 private $mResultArr = array();
143 * @param Title $title
144 * @param Title $origTitle
145 * @param int $revisionCount
146 * @param int $successCount
147 * @param array $pageInfo
150 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
151 // Add a result entry
154 if ( $title === null ) {
155 # Invalid or non-importable title
156 $r['title'] = $pageInfo['title'];
157 $r['invalid'] = true;
159 ApiQueryBase
::addTitleInfo( $r, $title );
160 $r['revisions'] = intval( $successCount );
163 $this->mResultArr
[] = $r;
165 // Piggyback on the parent to do the logging
166 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
170 return $this->mResultArr
;