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