MediaWiki  REL1_24
ApiImport.php
Go to the documentation of this file.
00001 <?php
00032 class ApiImport extends ApiBase {
00033 
00034     public function execute() {
00035         $user = $this->getUser();
00036         $params = $this->extractRequestParams();
00037 
00038         $isUpload = false;
00039         if ( isset( $params['interwikisource'] ) ) {
00040             if ( !$user->isAllowed( 'import' ) ) {
00041                 $this->dieUsageMsg( 'cantimport' );
00042             }
00043             if ( !isset( $params['interwikipage'] ) ) {
00044                 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
00045             }
00046             $source = ImportStreamSource::newFromInterwiki(
00047                 $params['interwikisource'],
00048                 $params['interwikipage'],
00049                 $params['fullhistory'],
00050                 $params['templates']
00051             );
00052         } else {
00053             $isUpload = true;
00054             if ( !$user->isAllowed( 'importupload' ) ) {
00055                 $this->dieUsageMsg( 'cantimport-upload' );
00056             }
00057             $source = ImportStreamSource::newFromUpload( 'xml' );
00058         }
00059         if ( !$source->isOK() ) {
00060             $this->dieStatus( $source );
00061         }
00062 
00063         $importer = new WikiImporter( $source->value );
00064         if ( isset( $params['namespace'] ) ) {
00065             $importer->setTargetNamespace( $params['namespace'] );
00066         }
00067         if ( isset( $params['rootpage'] ) ) {
00068             $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
00069             if ( !$statusRootPage->isGood() ) {
00070                 $this->dieStatus( $statusRootPage );
00071             }
00072         }
00073         $reporter = new ApiImportReporter(
00074             $importer,
00075             $isUpload,
00076             $params['interwikisource'],
00077             $params['summary']
00078         );
00079 
00080         try {
00081             $importer->doImport();
00082         } catch ( MWException $e ) {
00083             $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
00084         }
00085 
00086         $resultData = $reporter->getData();
00087         $result = $this->getResult();
00088         $result->setIndexedTagName( $resultData, 'page' );
00089         $result->addValue( null, $this->getModuleName(), $resultData );
00090     }
00091 
00092     public function mustBePosted() {
00093         return true;
00094     }
00095 
00096     public function isWriteMode() {
00097         return true;
00098     }
00099 
00100     public function getAllowedParams() {
00101         return array(
00102             'summary' => null,
00103             'xml' => array(
00104                 ApiBase::PARAM_TYPE => 'upload',
00105             ),
00106             'interwikisource' => array(
00107                 ApiBase::PARAM_TYPE => $this->getConfig()->get( 'ImportSources' ),
00108             ),
00109             'interwikipage' => null,
00110             'fullhistory' => false,
00111             'templates' => false,
00112             'namespace' => array(
00113                 ApiBase::PARAM_TYPE => 'namespace'
00114             ),
00115             'rootpage' => null,
00116         );
00117     }
00118 
00119     public function getParamDescription() {
00120         return array(
00121             'summary' => 'Import summary',
00122             'xml' => 'Uploaded XML file',
00123             'interwikisource' => 'For interwiki imports: wiki to import from',
00124             'interwikipage' => 'For interwiki imports: page to import',
00125             'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
00126             'templates' => 'For interwiki imports: import all included templates as well',
00127             'namespace' => 'For interwiki imports: import to this namespace',
00128             'rootpage' => 'Import as subpage of this page',
00129         );
00130     }
00131 
00132     public function getDescription() {
00133         return array(
00134             'Import a page from another wiki, or an XML file.',
00135             'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
00136             'sending a file for the "xml" parameter.'
00137         );
00138     }
00139 
00140     public function needsToken() {
00141         return 'csrf';
00142     }
00143 
00144     public function getExamples() {
00145         return array(
00146             'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
00147                 'namespace=100&fullhistory=&token=123ABC'
00148                 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
00149         );
00150     }
00151 
00152     public function getHelpUrls() {
00153         return 'https://www.mediawiki.org/wiki/API:Import';
00154     }
00155 }
00156 
00161 class ApiImportReporter extends ImportReporter {
00162     private $mResultArr = array();
00163 
00172     function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
00173         // Add a result entry
00174         $r = array();
00175 
00176         if ( $title === null ) {
00177             # Invalid or non-importable title
00178             $r['title'] = $pageInfo['title'];
00179             $r['invalid'] = '';
00180         } else {
00181             ApiQueryBase::addTitleInfo( $r, $title );
00182             $r['revisions'] = intval( $successCount );
00183         }
00184 
00185         $this->mResultArr[] = $r;
00186 
00187         // Piggyback on the parent to do the logging
00188         parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
00189     }
00190 
00191     function getData() {
00192         return $this->mResultArr;
00193     }
00194 }