MediaWiki
REL1_20
|
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 if ( isset( $params['rootpage'] ) ) { 00072 $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] ); 00073 if( !$statusRootPage->isGood() ) { 00074 $this->dieUsageMsg( $statusRootPage->getErrorsArray() ); 00075 } 00076 } 00077 $reporter = new ApiImportReporter( 00078 $importer, 00079 $isUpload, 00080 $params['interwikisource'], 00081 $params['summary'] 00082 ); 00083 00084 try { 00085 $importer->doImport(); 00086 } catch ( MWException $e ) { 00087 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) ); 00088 } 00089 00090 $resultData = $reporter->getData(); 00091 $result = $this->getResult(); 00092 $result->setIndexedTagName( $resultData, 'page' ); 00093 $result->addValue( null, $this->getModuleName(), $resultData ); 00094 } 00095 00096 public function mustBePosted() { 00097 return true; 00098 } 00099 00100 public function isWriteMode() { 00101 return true; 00102 } 00103 00104 public function getAllowedParams() { 00105 global $wgImportSources; 00106 return array( 00107 'token' => array( 00108 ApiBase::PARAM_TYPE => 'string', 00109 ApiBase::PARAM_REQUIRED => true 00110 ), 00111 'summary' => null, 00112 'xml' => null, 00113 'interwikisource' => array( 00114 ApiBase::PARAM_TYPE => $wgImportSources 00115 ), 00116 'interwikipage' => null, 00117 'fullhistory' => false, 00118 'templates' => false, 00119 'namespace' => array( 00120 ApiBase::PARAM_TYPE => 'namespace' 00121 ), 00122 'rootpage' => null, 00123 ); 00124 } 00125 00126 public function getParamDescription() { 00127 return array( 00128 'token' => 'Import token obtained through prop=info', 00129 'summary' => 'Import summary', 00130 'xml' => 'Uploaded XML file', 00131 'interwikisource' => 'For interwiki imports: wiki to import from', 00132 'interwikipage' => 'For interwiki imports: page to import', 00133 'fullhistory' => 'For interwiki imports: import the full history, not just the current version', 00134 'templates' => 'For interwiki imports: import all included templates as well', 00135 'namespace' => 'For interwiki imports: import to this namespace', 00136 'rootpage' => 'Import as subpage of this page', 00137 ); 00138 } 00139 00140 public function getResultProperties() { 00141 return array( 00142 ApiBase::PROP_LIST => true, 00143 '' => array( 00144 'ns' => 'namespace', 00145 'title' => 'string', 00146 'revisions' => 'integer' 00147 ) 00148 ); 00149 } 00150 00151 public function getDescription() { 00152 return array( 00153 'Import a page from another wiki, or an XML file.' , 00154 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when', 00155 'sending a file for the "xml" parameter.' 00156 ); 00157 } 00158 00159 public function getPossibleErrors() { 00160 return array_merge( parent::getPossibleErrors(), array( 00161 array( 'cantimport' ), 00162 array( 'missingparam', 'interwikipage' ), 00163 array( 'cantimport-upload' ), 00164 array( 'import-unknownerror', 'source' ), 00165 array( 'import-unknownerror', 'result' ), 00166 array( 'import-rootpage-nosubpage', 'namespace' ), 00167 array( 'import-rootpage-invalid' ), 00168 ) ); 00169 } 00170 00171 public function needsToken() { 00172 return true; 00173 } 00174 00175 public function getTokenSalt() { 00176 return ''; 00177 } 00178 00179 public function getExamples() { 00180 return array( 00181 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&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 public function getVersion() { 00191 return __CLASS__ . ': $Id$'; 00192 } 00193 } 00194 00199 class ApiImportReporter extends ImportReporter { 00200 private $mResultArr = array(); 00201 00210 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) { 00211 // Add a result entry 00212 $r = array(); 00213 00214 if ( $title === null ) { 00215 # Invalid or non-importable title 00216 $r['title'] = $pageInfo['title']; 00217 $r['invalid'] = ''; 00218 } else { 00219 ApiQueryBase::addTitleInfo( $r, $title ); 00220 $r['revisions'] = intval( $successCount ); 00221 } 00222 00223 $this->mResultArr[] = $r; 00224 00225 // Piggyback on the parent to do the logging 00226 parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ); 00227 } 00228 00229 function getData() { 00230 return $this->mResultArr; 00231 } 00232 }