]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/api/ApiImport.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / api / ApiImport.php
index 032b684c30bd1f30255336d82722145a7ab24c7d..b46f0b1e5105d46cc2833336c2b7f5bfc0e430f7 100644 (file)
@@ -1,11 +1,10 @@
 <?php
-
-/*
- * Created on Feb 4, 2009
+/**
  *
- * API for MediaWiki 1.8+
  *
- * Copyright (C) 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
+ * Created on Feb 4, 2009
+ *
+ * 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
  *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @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
  *
@@ -35,63 +31,100 @@ 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();
 
-               $source = null;
+               $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
+
                $isUpload = false;
-               if ( isset( $params['interwikisource'] ) )
-               {
-                       if ( !isset( $params['interwikipage'] ) )
-                               $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
+               if ( isset( $params['interwikisource'] ) ) {
+                       if ( !$user->isAllowed( 'import' ) ) {
+                               $this->dieWithError( 'apierror-cantimport' );
+                       }
+                       if ( !isset( $params['interwikipage'] ) ) {
+                               $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
+                       }
                        $source = ImportStreamSource::newFromInterwiki(
-                                       $params['interwikisource'],
-                                       $params['interwikipage'],
-                                       $params['fullhistory'],
-                                       $params['templates'] );
-               }
-               else
-               {
+                               $params['interwikisource'],
+                               $params['interwikipage'],
+                               $params['fullhistory'],
+                               $params['templates']
+                       );
+               } 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 instanceof WikiErrorMsg )
-                       $this->dieUsageMsg( array_merge(
-                               array( $source->getMessageKey() ),
-                               $source->getMessageArgs() ) );
-               else if ( WikiError::isError( $source ) )
-                       // This shouldn't happen
-                       $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
-
-               $importer = new WikiImporter( $source );
-               if ( isset( $params['namespace'] ) )
+               if ( !$source->isOK() ) {
+                       $this->dieStatus( $source );
+               }
+
+               // 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'] );
-               $reporter = new ApiImportReporter( $importer, $isUpload,
-                                       $params['interwikisource'],
-                                       $params['summary'] );
-
-               $result = $importer->doImport();
-               if ( $result instanceof WikiXmlError )
-                       $this->dieUsageMsg( array( 'import-xml-error',
-                               $result->mLine,
-                               $result->mColumn,
-                               $result->mByte . $result->mContext,
-                               xml_error_string( $result->mXmlError ) ) );
-               else if ( WikiError::isError( $result ) )
-                       $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) ); // This shouldn't happen
+               } elseif ( isset( $params['rootpage'] ) ) {
+                       $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
+                       if ( !$statusRootPage->isGood() ) {
+                               $this->dieStatus( $statusRootPage );
+                       }
+               }
+               $reporter = new ApiImportReporter(
+                       $importer,
+                       $isUpload,
+                       $params['interwikisource'],
+                       $params['summary']
+               );
+               if ( $params['tags'] ) {
+                       $reporter->setChangeTags( $params['tags'] );
+               }
+
+               try {
+                       $importer->doImport();
+               } 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() {
@@ -103,65 +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(
-                               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',
-               );
+                       'namespace' => [
+                               ApiBase::PARAM_TYPE => 'namespace'
+                       ],
+                       'rootpage' => null,
+                       'tags' => [
+                               ApiBase::PARAM_TYPE => 'tags',
+                               ApiBase::PARAM_ISMULTI => true,
+                       ],
+               ];
        }
 
-       public function getDescription() {
-               return array (
-                       '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' ),
-               ) );
-       }
-       
-       public function getTokenSalt() {
-               return '';
+       public function needsToken() {
+               return 'csrf';
        }
 
-       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',
-               );
+       protected function getExamplesMessages() {
+               return [
+                       'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
+                               'namespace=100&fullhistory=&token=123ABC'
+                               => 'apihelp-import-example-import',
+               ];
        }
 
-       public function getVersion() {
-               return __CLASS__ . ': $Id: ApiImport.php 62599 2010-02-16 21:59:16Z reedy $';
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
        }
 }
 
@@ -170,22 +180,36 @@ class ApiImport extends ApiBase {
  * @ingroup API
  */
 class ApiImportReporter extends ImportReporter {
-       private $mResultArr = array();
-
-       function reportPage( $title, $origTitle, $revisionCount, $successCount )
-       {
+       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 );
+               parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
        }
 
-       function getData()
-       {
+       public function getData() {
                return $this->mResultArr;
        }
-}
\ No newline at end of file
+}