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 );
96 * Returns a list of interwiki prefixes corresponding to each defined import
102 public function getAllowedImportSources() {
103 $importSources = $this->getConfig()->get( 'ImportSources' );
104 Hooks
::run( 'ImportSources', array( &$importSources ) );
107 foreach ( $importSources as $key => $value ) {
108 if ( is_int( $key ) ) {
111 foreach ( $value as $subproject ) {
112 $result[] = "$key:$subproject";
119 public function mustBePosted() {
123 public function isWriteMode() {
127 public function getAllowedParams() {
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'
146 public function needsToken() {
150 protected function getExamplesMessages() {
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
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
178 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
179 // Add a result entry
182 if ( $title === null ) {
183 # Invalid or non-importable title
184 $r['title'] = $pageInfo['title'];
185 $r['invalid'] = true;
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 );
198 return $this->mResultArr
;