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