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 if(!isset($params['token']))
00048 $this->dieUsageMsg(array('missingparam', 'token'));
00049 if(!$wgUser->matchEditToken($params['token']))
00050 $this->dieUsageMsg(array('sessionfailure'));
00051
00052 $source = null;
00053 $isUpload = false;
00054 if(isset($params['interwikisource']))
00055 {
00056 if(!isset($params['interwikipage']))
00057 $this->dieUsageMsg(array('missingparam', 'interwikipage'));
00058 $source = ImportStreamSource::newFromInterwiki(
00059 $params['interwikisource'],
00060 $params['interwikipage'],
00061 $params['fullhistory'],
00062 $params['templates']);
00063 }
00064 else
00065 {
00066 $isUpload = true;
00067 if(!$wgUser->isAllowed('importupload'))
00068 $this->dieUsageMsg(array('cantimport-upload'));
00069 $source = ImportStreamSource::newFromUpload('xml');
00070 }
00071 if($source instanceof WikiErrorMsg)
00072 $this->dieUsageMsg(array_merge(
00073 array($source->getMessageKey()),
00074 $source->getMessageArgs()));
00075 else if(WikiError::isError($source))
00076
00077 $this->dieUsageMsg(array('import-unknownerror', $source->getMessage()));
00078
00079 $importer = new WikiImporter($source);
00080 if(isset($params['namespace']))
00081 $importer->setTargetNamespace($params['namespace']);
00082 $reporter = new ApiImportReporter($importer, $isUpload,
00083 $params['interwikisource'],
00084 $params['summary']);
00085
00086 $result = $importer->doImport();
00087 if($result instanceof WikiXmlError)
00088 $this->dieUsageMsg(array('import-xml-error',
00089 $result->mLine,
00090 $result->mColumn,
00091 $result->mByte . $result->mContext,
00092 xml_error_string($result->mXmlError)));
00093 else if(WikiError::isError($result))
00094
00095 $this->dieUsageMsg(array('import-unknownerror', $result->getMessage()));
00096 $resultData = $reporter->getData();
00097 $this->getResult()->setIndexedTagName($resultData, 'page');
00098 $this->getResult()->addValue(null, $this->getModuleName(), $resultData);
00099 }
00100
00101 public function mustBePosted() { return true; }
00102
00103 public function isWriteMode() {
00104 return true;
00105 }
00106
00107 public function getAllowedParams() {
00108 global $wgImportSources;
00109 return array (
00110 'token' => null,
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 );
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 );
00136 }
00137
00138 public function getDescription() {
00139 return array (
00140 'Import a page from another wiki, or an XML file'
00141 );
00142 }
00143
00144 protected function getExamples() {
00145 return array(
00146 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
00147 ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
00148 );
00149 }
00150
00151 public function getVersion() {
00152 return __CLASS__ . ': $Id: ApiImport.php 48091 2009-03-06 13:49:44Z catrope $';
00153 }
00154 }
00155
00160 class ApiImportReporter extends ImportReporter {
00161 private $mResultArr = array();
00162
00163 function reportPage($title, $origTitle, $revisionCount, $successCount)
00164 {
00165
00166 $r = array();
00167 ApiQueryBase::addTitleInfo($r, $title);
00168 $r['revisions'] = intval($successCount);
00169 $this->mResultArr[] = $r;
00170
00171
00172 parent::reportPage($title, $origTitle, $revisionCount, $successCount);
00173 }
00174
00175 function getData()
00176 {
00177 return $this->mResultArr;
00178 }
00179 }