X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/api/ApiImport.php diff --git a/includes/api/ApiImport.php b/includes/api/ApiImport.php index 7b3fe8e4..b46f0b1e 100644 --- a/includes/api/ApiImport.php +++ b/includes/api/ApiImport.php @@ -1,10 +1,10 @@ .@home.nl + * Copyright © 2009 Roan Kattouw ".@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 @@ -24,11 +24,6 @@ * @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 * @@ -36,21 +31,21 @@ if ( !defined( 'MEDIAWIKI' ) ) { */ 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'], @@ -60,18 +55,31 @@ class ApiImport extends ApiBase { ); } 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, @@ -79,16 +87,44 @@ class ApiImport extends ApiBase { $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() { @@ -100,67 +136,42 @@ class ApiImport extends ApiBase { } 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'; } } @@ -169,20 +180,36 @@ class ApiImport extends ApiBase { * @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; } }