ApiImport.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * Created on Feb 4, 2009
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiBase.php');
  27. }
  28. /**
  29. * API module that imports an XML file like Special:Import does
  30. *
  31. * @ingroup API
  32. */
  33. class ApiImport extends ApiBase {
  34. public function __construct($main, $action) {
  35. parent :: __construct($main, $action);
  36. }
  37. public function execute() {
  38. global $wgUser;
  39. if(!$wgUser->isAllowed('import'))
  40. $this->dieUsageMsg(array('cantimport'));
  41. $params = $this->extractRequestParams();
  42. if(!isset($params['token']))
  43. $this->dieUsageMsg(array('missingparam', 'token'));
  44. if(!$wgUser->matchEditToken($params['token']))
  45. $this->dieUsageMsg(array('sessionfailure'));
  46. $source = null;
  47. $isUpload = false;
  48. if(isset($params['interwikisource']))
  49. {
  50. if(!isset($params['interwikipage']))
  51. $this->dieUsageMsg(array('missingparam', 'interwikipage'));
  52. $source = ImportStreamSource::newFromInterwiki(
  53. $params['interwikisource'],
  54. $params['interwikipage'],
  55. $params['fullhistory'],
  56. $params['templates']);
  57. }
  58. else
  59. {
  60. $isUpload = true;
  61. if(!$wgUser->isAllowed('importupload'))
  62. $this->dieUsageMsg(array('cantimport-upload'));
  63. $source = ImportStreamSource::newFromUpload('xml');
  64. }
  65. if($source instanceof WikiErrorMsg)
  66. $this->dieUsageMsg(array_merge(
  67. array($source->getMessageKey()),
  68. $source->getMessageArgs()));
  69. else if(WikiError::isError($source))
  70. // This shouldn't happen
  71. $this->dieUsageMsg(array('import-unknownerror', $source->getMessage()));
  72. $importer = new WikiImporter($source);
  73. if(isset($params['namespace']))
  74. $importer->setTargetNamespace($params['namespace']);
  75. $reporter = new ApiImportReporter($importer, $isUpload,
  76. $params['interwikisource'],
  77. $params['summary']);
  78. $result = $importer->doImport();
  79. if($result instanceof WikiXmlError)
  80. $this->dieUsageMsg(array('import-xml-error',
  81. $result->mLine,
  82. $result->mColumn,
  83. $result->mByte . $result->mContext,
  84. xml_error_string($result->mXmlError)));
  85. else if(WikiError::isError($result))
  86. // This shouldn't happen
  87. $this->dieUsageMsg(array('import-unknownerror', $result->getMessage()));
  88. $resultData = $reporter->getData();
  89. $this->getResult()->setIndexedTagName($resultData, 'page');
  90. $this->getResult()->addValue(null, $this->getModuleName(), $resultData);
  91. }
  92. public function mustBePosted() { return true; }
  93. public function isWriteMode() {
  94. return true;
  95. }
  96. public function getAllowedParams() {
  97. global $wgImportSources;
  98. return array (
  99. 'token' => null,
  100. 'summary' => null,
  101. 'xml' => null,
  102. 'interwikisource' => array(
  103. ApiBase :: PARAM_TYPE => $wgImportSources
  104. ),
  105. 'interwikipage' => null,
  106. 'fullhistory' => false,
  107. 'templates' => false,
  108. 'namespace' => array(
  109. ApiBase :: PARAM_TYPE => 'namespace'
  110. )
  111. );
  112. }
  113. public function getParamDescription() {
  114. return array (
  115. 'token' => 'Import token obtained through prop=info',
  116. 'summary' => 'Import summary',
  117. 'xml' => 'Uploaded XML file',
  118. 'interwikisource' => 'For interwiki imports: wiki to import from',
  119. 'interwikipage' => 'For interwiki imports: page to import',
  120. 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
  121. 'templates' => 'For interwiki imports: import all included templates as well',
  122. 'namespace' => 'For interwiki imports: import to this namespace',
  123. );
  124. }
  125. public function getDescription() {
  126. return array (
  127. 'Import a page from another wiki, or an XML file'
  128. );
  129. }
  130. protected function getExamples() {
  131. return array(
  132. 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
  133. ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
  134. );
  135. }
  136. public function getVersion() {
  137. return __CLASS__ . ': $Id: ApiImport.php 48091 2009-03-06 13:49:44Z catrope $';
  138. }
  139. }
  140. /**
  141. * Import reporter for the API
  142. * @ingroup API
  143. */
  144. class ApiImportReporter extends ImportReporter {
  145. private $mResultArr = array();
  146. function reportPage($title, $origTitle, $revisionCount, $successCount)
  147. {
  148. // Add a result entry
  149. $r = array();
  150. ApiQueryBase::addTitleInfo($r, $title);
  151. $r['revisions'] = intval($successCount);
  152. $this->mResultArr[] = $r;
  153. // Piggyback on the parent to do the logging
  154. parent::reportPage($title, $origTitle, $revisionCount, $successCount);
  155. }
  156. function getData()
  157. {
  158. return $this->mResultArr;
  159. }
  160. }