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'] );
67 if ( isset( $params['rootpage'] ) ) {
68 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
69 if ( !$statusRootPage->isGood() ) {
70 $this->dieStatus( $statusRootPage );
73 $reporter = new ApiImportReporter(
76 $params['interwikisource'],
81 $importer->doImport();
82 } catch ( Exception
$e ) {
83 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
86 $resultData = $reporter->getData();
87 $result = $this->getResult();
88 $result->setIndexedTagName( $resultData, 'page' );
89 $result->addValue( null, $this->getModuleName(), $resultData );
92 public function mustBePosted() {
96 public function isWriteMode() {
100 public function getAllowedParams() {
104 ApiBase
::PARAM_TYPE
=> 'upload',
106 'interwikisource' => array(
107 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'ImportSources' ),
109 'interwikipage' => null,
110 'fullhistory' => false,
111 'templates' => false,
112 'namespace' => array(
113 ApiBase
::PARAM_TYPE
=> 'namespace'
119 public function needsToken() {
123 protected function getExamplesMessages() {
125 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
126 'namespace=100&fullhistory=&token=123ABC'
127 => 'apihelp-import-example-import',
131 public function getHelpUrls() {
132 return 'https://www.mediawiki.org/wiki/API:Import';
137 * Import reporter for the API
140 class ApiImportReporter
extends ImportReporter
{
141 private $mResultArr = array();
144 * @param Title $title
145 * @param Title $origTitle
146 * @param int $revisionCount
147 * @param int $successCount
148 * @param array $pageInfo
151 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
152 // Add a result entry
155 if ( $title === null ) {
156 # Invalid or non-importable title
157 $r['title'] = $pageInfo['title'];
160 ApiQueryBase
::addTitleInfo( $r, $title );
161 $r['revisions'] = intval( $successCount );
164 $this->mResultArr
[] = $r;
166 // Piggyback on the parent to do the logging
167 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
171 return $this->mResultArr
;