MediaWiki  REL1_23
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         global $wgImportSources;
00102 
00103         return array(
00104             'token' => array(
00105                 ApiBase::PARAM_TYPE => 'string',
00106                 ApiBase::PARAM_REQUIRED => true
00107             ),
00108             'summary' => null,
00109             'xml' => array(
00110                 ApiBase::PARAM_TYPE => 'upload',
00111             ),
00112             'interwikisource' => array(
00113                 ApiBase::PARAM_TYPE => $wgImportSources
00114             ),
00115             'interwikipage' => null,
00116             'fullhistory' => false,
00117             'templates' => false,
00118             'namespace' => array(
00119                 ApiBase::PARAM_TYPE => 'namespace'
00120             ),
00121             'rootpage' => null,
00122         );
00123     }
00124 
00125     public function getParamDescription() {
00126         return array(
00127             'token' => 'Import token obtained through prop=info',
00128             'summary' => 'Import summary',
00129             'xml' => 'Uploaded XML file',
00130             'interwikisource' => 'For interwiki imports: wiki to import from',
00131             'interwikipage' => 'For interwiki imports: page to import',
00132             'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
00133             'templates' => 'For interwiki imports: import all included templates as well',
00134             'namespace' => 'For interwiki imports: import to this namespace',
00135             'rootpage' => 'Import as subpage of this page',
00136         );
00137     }
00138 
00139     public function getResultProperties() {
00140         return array(
00141             ApiBase::PROP_LIST => true,
00142             '' => array(
00143                 'ns' => 'namespace',
00144                 'title' => 'string',
00145                 'revisions' => 'integer'
00146             )
00147         );
00148     }
00149 
00150     public function getDescription() {
00151         return array(
00152             'Import a page from another wiki, or an XML file.',
00153             'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
00154             'sending a file for the "xml" parameter.'
00155         );
00156     }
00157 
00158     public function getPossibleErrors() {
00159         return array_merge( parent::getPossibleErrors(), array(
00160             array( 'cantimport' ),
00161             array( 'missingparam', 'interwikipage' ),
00162             array( 'cantimport-upload' ),
00163             array( 'import-unknownerror', 'source' ),
00164             array( 'import-unknownerror', 'result' ),
00165             array( 'import-rootpage-nosubpage', 'namespace' ),
00166             array( 'import-rootpage-invalid' ),
00167         ) );
00168     }
00169 
00170     public function needsToken() {
00171         return true;
00172     }
00173 
00174     public function getTokenSalt() {
00175         return '';
00176     }
00177 
00178     public function getExamples() {
00179         return array(
00180             'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
00181                 'namespace=100&fullhistory=&token=123ABC'
00182                 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
00183         );
00184     }
00185 
00186     public function getHelpUrls() {
00187         return 'https://www.mediawiki.org/wiki/API:Import';
00188     }
00189 }
00190 
00195 class ApiImportReporter extends ImportReporter {
00196     private $mResultArr = array();
00197 
00206     function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
00207         // Add a result entry
00208         $r = array();
00209 
00210         if ( $title === null ) {
00211             # Invalid or non-importable title
00212             $r['title'] = $pageInfo['title'];
00213             $r['invalid'] = '';
00214         } else {
00215             ApiQueryBase::addTitleInfo( $r, $title );
00216             $r['revisions'] = intval( $successCount );
00217         }
00218 
00219         $this->mResultArr[] = $r;
00220 
00221         // Piggyback on the parent to do the logging
00222         parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
00223     }
00224 
00225     function getData() {
00226         return $this->mResultArr;
00227     }
00228 }