00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( 'ApiBase.php' );
00029 }
00030
00036 class ApiImport extends ApiBase {
00037
00038 public function __construct( $main, $action ) {
00039 parent :: __construct( $main, $action );
00040 }
00041
00042 public function execute() {
00043 global $wgUser;
00044 if ( !$wgUser->isAllowed( 'import' ) )
00045 $this->dieUsageMsg( array( 'cantimport' ) );
00046 $params = $this->extractRequestParams();
00047
00048 $source = null;
00049 $isUpload = false;
00050 if ( isset( $params['interwikisource'] ) )
00051 {
00052 if ( !isset( $params['interwikipage'] ) )
00053 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
00054 $source = ImportStreamSource::newFromInterwiki(
00055 $params['interwikisource'],
00056 $params['interwikipage'],
00057 $params['fullhistory'],
00058 $params['templates'] );
00059 }
00060 else
00061 {
00062 $isUpload = true;
00063 if ( !$wgUser->isAllowed( 'importupload' ) )
00064 $this->dieUsageMsg( array( 'cantimport-upload' ) );
00065 $source = ImportStreamSource::newFromUpload( 'xml' );
00066 }
00067 if ( $source instanceof WikiErrorMsg )
00068 $this->dieUsageMsg( array_merge(
00069 array( $source->getMessageKey() ),
00070 $source->getMessageArgs() ) );
00071 else if ( WikiError::isError( $source ) )
00072
00073 $this->dieUsageMsg( array( 'import-unknownerror', $source->getMessage() ) );
00074
00075 $importer = new WikiImporter( $source );
00076 if ( isset( $params['namespace'] ) )
00077 $importer->setTargetNamespace( $params['namespace'] );
00078 $reporter = new ApiImportReporter( $importer, $isUpload,
00079 $params['interwikisource'],
00080 $params['summary'] );
00081
00082 $result = $importer->doImport();
00083 if ( $result instanceof WikiXmlError )
00084 $this->dieUsageMsg( array( 'import-xml-error',
00085 $result->mLine,
00086 $result->mColumn,
00087 $result->mByte . $result->mContext,
00088 xml_error_string( $result->mXmlError ) ) );
00089 else if ( WikiError::isError( $result ) )
00090 $this->dieUsageMsg( array( 'import-unknownerror', $result->getMessage() ) );
00091
00092 $resultData = $reporter->getData();
00093 $this->getResult()->setIndexedTagName( $resultData, 'page' );
00094 $this->getResult()->addValue( null, $this->getModuleName(), $resultData );
00095 }
00096
00097 public function mustBePosted() {
00098 return true;
00099 }
00100
00101 public function isWriteMode() {
00102 return true;
00103 }
00104
00105 public function getAllowedParams() {
00106 global $wgImportSources;
00107 return array (
00108 'token' => null,
00109 'summary' => null,
00110 'xml' => null,
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 );
00121 }
00122
00123 public function getParamDescription() {
00124 return array (
00125 'token' => 'Import token obtained through prop=info',
00126 'summary' => 'Import summary',
00127 'xml' => 'Uploaded XML file',
00128 'interwikisource' => 'For interwiki imports: wiki to import from',
00129 'interwikipage' => 'For interwiki imports: page to import',
00130 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
00131 'templates' => 'For interwiki imports: import all included templates as well',
00132 'namespace' => 'For interwiki imports: import to this namespace',
00133 );
00134 }
00135
00136 public function getDescription() {
00137 return array (
00138 'Import a page from another wiki, or an XML file'
00139 );
00140 }
00141
00142 public function getPossibleErrors() {
00143 return array_merge( parent::getPossibleErrors(), array(
00144 array( 'cantimport' ),
00145 array( 'missingparam', 'interwikipage' ),
00146 array( 'cantimport-upload' ),
00147 array( 'import-unknownerror', 'source' ),
00148 array( 'import-unknownerror', 'result' ),
00149 ) );
00150 }
00151
00152 public function needsToken() {
00153 return true;
00154 }
00155
00156 public function getTokenSalt() {
00157 return '';
00158 }
00159
00160 protected function getExamples() {
00161 return array(
00162 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
00163 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
00164 );
00165 }
00166
00167 public function getVersion() {
00168 return __CLASS__ . ': $Id: ApiImport.php 74217 2010-10-03 15:53:07Z reedy $';
00169 }
00170 }
00171
00176 class ApiImportReporter extends ImportReporter {
00177 private $mResultArr = array();
00178
00179 function reportPage( $title, $origTitle, $revisionCount, $successCount )
00180 {
00181
00182 $r = array();
00183 ApiQueryBase::addTitleInfo( $r, $title );
00184 $r['revisions'] = intval( $successCount );
00185 $this->mResultArr[] = $r;
00186
00187
00188 parent::reportPage( $title, $origTitle, $revisionCount, $successCount );
00189 }
00190
00191 function getData()
00192 {
00193 return $this->mResultArr;
00194 }
00195 }