<?php
/**
- * API for MediaWiki 1.8+
+ *
*
* Created on Feb 4, 2009
*
- * Copyright © 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
+ * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* @file
*/
-if ( !defined( 'MEDIAWIKI' ) ) {
- // Eclipse helper - will be ignored in production
- require_once( 'ApiBase.php' );
-}
-
/**
* API module that imports an XML file like Special:Import does
*
*/
class ApiImport extends ApiBase {
- public function __construct( $main, $action ) {
- parent::__construct( $main, $action );
- }
-
public function execute() {
- global $wgUser;
- if ( !$wgUser->isAllowed( 'import' ) ) {
- $this->dieUsageMsg( array( 'cantimport' ) );
- }
+ $this->useTransactionalTimeLimit();
+
+ $user = $this->getUser();
$params = $this->extractRequestParams();
+ $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
+
$isUpload = false;
if ( isset( $params['interwikisource'] ) ) {
+ if ( !$user->isAllowed( 'import' ) ) {
+ $this->dieWithError( 'apierror-cantimport' );
+ }
if ( !isset( $params['interwikipage'] ) ) {
- $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
+ $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
}
$source = ImportStreamSource::newFromInterwiki(
$params['interwikisource'],
);
} else {
$isUpload = true;
- if ( !$wgUser->isAllowed( 'importupload' ) ) {
- $this->dieUsageMsg( array( 'cantimport-upload' ) );
+ if ( !$user->isAllowed( 'importupload' ) ) {
+ $this->dieWithError( 'apierror-cantimport-upload' );
}
$source = ImportStreamSource::newFromUpload( 'xml' );
}
if ( !$source->isOK() ) {
- $this->dieUsageMsg( $source->getErrorsArray() );
+ $this->dieStatus( $source );
}
- $importer = new WikiImporter( $source->value );
+ // Check if user can add the log entry tags which were requested
+ if ( $params['tags'] ) {
+ $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
+ if ( !$ableToTag->isOK() ) {
+ $this->dieStatus( $ableToTag );
+ }
+ }
+
+ $importer = new WikiImporter( $source->value, $this->getConfig() );
if ( isset( $params['namespace'] ) ) {
$importer->setTargetNamespace( $params['namespace'] );
+ } elseif ( isset( $params['rootpage'] ) ) {
+ $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
+ if ( !$statusRootPage->isGood() ) {
+ $this->dieStatus( $statusRootPage );
+ }
}
$reporter = new ApiImportReporter(
$importer,
$params['interwikisource'],
$params['summary']
);
+ if ( $params['tags'] ) {
+ $reporter->setChangeTags( $params['tags'] );
+ }
try {
$importer->doImport();
- } catch ( MWException $e ) {
- $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
+ } catch ( Exception $e ) {
+ $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
}
$resultData = $reporter->getData();
- $this->getResult()->setIndexedTagName( $resultData, 'page' );
- $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
+ $result = $this->getResult();
+ ApiResult::setIndexedTagName( $resultData, 'page' );
+ $result->addValue( null, $this->getModuleName(), $resultData );
+ }
+
+ /**
+ * Returns a list of interwiki prefixes corresponding to each defined import
+ * source.
+ *
+ * @return array
+ * @since 1.27
+ */
+ public function getAllowedImportSources() {
+ $importSources = $this->getConfig()->get( 'ImportSources' );
+ Hooks::run( 'ImportSources', [ &$importSources ] );
+
+ $result = [];
+ foreach ( $importSources as $key => $value ) {
+ if ( is_int( $key ) ) {
+ $result[] = $value;
+ } else {
+ foreach ( $value as $subproject ) {
+ $result[] = "$key:$subproject";
+ }
+ }
+ }
+ return $result;
}
public function mustBePosted() {
}
public function getAllowedParams() {
- global $wgImportSources;
- return array(
- 'token' => null,
+ return [
'summary' => null,
- 'xml' => null,
- 'interwikisource' => array(
- ApiBase::PARAM_TYPE => $wgImportSources
- ),
+ 'xml' => [
+ ApiBase::PARAM_TYPE => 'upload',
+ ],
+ 'interwikisource' => [
+ ApiBase::PARAM_TYPE => $this->getAllowedImportSources(),
+ ],
'interwikipage' => null,
'fullhistory' => false,
'templates' => false,
- 'namespace' => array(
+ 'namespace' => [
ApiBase::PARAM_TYPE => 'namespace'
- )
- );
- }
-
- public function getParamDescription() {
- return array(
- 'token' => 'Import token obtained through prop=info',
- 'summary' => 'Import summary',
- 'xml' => 'Uploaded XML file',
- 'interwikisource' => 'For interwiki imports: wiki to import from',
- 'interwikipage' => 'For interwiki imports: page to import',
- 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
- 'templates' => 'For interwiki imports: import all included templates as well',
- 'namespace' => 'For interwiki imports: import to this namespace',
- );
- }
-
- public function getDescription() {
- return 'Import a page from another wiki, or an XML file';
- }
-
- public function getPossibleErrors() {
- return array_merge( parent::getPossibleErrors(), array(
- array( 'cantimport' ),
- array( 'missingparam', 'interwikipage' ),
- array( 'cantimport-upload' ),
- array( 'import-unknownerror', 'source' ),
- array( 'import-unknownerror', 'result' ),
- ) );
+ ],
+ 'rootpage' => null,
+ 'tags' => [
+ ApiBase::PARAM_TYPE => 'tags',
+ ApiBase::PARAM_ISMULTI => true,
+ ],
+ ];
}
public function needsToken() {
- return true;
+ return 'csrf';
}
- public function getTokenSalt() {
- return '';
+ protected function getExamplesMessages() {
+ return [
+ 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
+ 'namespace=100&fullhistory=&token=123ABC'
+ => 'apihelp-import-example-import',
+ ];
}
- protected function getExamples() {
- return array(
- 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
- ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC',
- );
- }
-
- public function getVersion() {
- return __CLASS__ . ': $Id$';
+ public function getHelpUrls() {
+ return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
}
}
* @ingroup API
*/
class ApiImportReporter extends ImportReporter {
- private $mResultArr = array();
-
- function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
+ private $mResultArr = [];
+
+ /**
+ * @param Title $title
+ * @param Title $origTitle
+ * @param int $revisionCount
+ * @param int $successCount
+ * @param array $pageInfo
+ * @return void
+ */
+ public function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
// Add a result entry
- $r = array();
- ApiQueryBase::addTitleInfo( $r, $title );
- $r['revisions'] = intval( $successCount );
+ $r = [];
+
+ if ( $title === null ) {
+ # Invalid or non-importable title
+ $r['title'] = $pageInfo['title'];
+ $r['invalid'] = true;
+ } else {
+ ApiQueryBase::addTitleInfo( $r, $title );
+ $r['revisions'] = intval( $successCount );
+ }
+
$this->mResultArr[] = $r;
// Piggyback on the parent to do the logging
parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
}
- function getData() {
+ public function getData() {
return $this->mResultArr;
}
}