ApiImport.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API module that imports an XML file like Special:Import does
  24. *
  25. * @ingroup API
  26. */
  27. class ApiImport extends ApiBase {
  28. public function execute() {
  29. $this->useTransactionalTimeLimit();
  30. $user = $this->getUser();
  31. $params = $this->extractRequestParams();
  32. $this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
  33. $isUpload = false;
  34. if ( isset( $params['interwikisource'] ) ) {
  35. if ( !$this->getPermissionManager()->userHasRight( $user, 'import' ) ) {
  36. $this->dieWithError( 'apierror-cantimport' );
  37. }
  38. if ( !isset( $params['interwikipage'] ) ) {
  39. $this->dieWithError( [ 'apierror-missingparam', 'interwikipage' ] );
  40. }
  41. $source = ImportStreamSource::newFromInterwiki(
  42. $params['interwikisource'],
  43. $params['interwikipage'],
  44. $params['fullhistory'],
  45. $params['templates']
  46. );
  47. $usernamePrefix = $params['interwikisource'];
  48. } else {
  49. $isUpload = true;
  50. if ( !$this->getPermissionManager()->userHasRight( $user, 'importupload' ) ) {
  51. $this->dieWithError( 'apierror-cantimport-upload' );
  52. }
  53. $source = ImportStreamSource::newFromUpload( 'xml' );
  54. $usernamePrefix = (string)$params['interwikiprefix'];
  55. if ( $usernamePrefix === '' ) {
  56. $encParamName = $this->encodeParamName( 'interwikiprefix' );
  57. $this->dieWithError( [ 'apierror-missingparam', $encParamName ] );
  58. }
  59. }
  60. if ( !$source->isOK() ) {
  61. $this->dieStatus( $source );
  62. }
  63. // Check if user can add the log entry tags which were requested
  64. if ( $params['tags'] ) {
  65. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  66. if ( !$ableToTag->isOK() ) {
  67. $this->dieStatus( $ableToTag );
  68. }
  69. }
  70. $importer = new WikiImporter( $source->value, $this->getConfig() );
  71. if ( isset( $params['namespace'] ) ) {
  72. $importer->setTargetNamespace( $params['namespace'] );
  73. } elseif ( isset( $params['rootpage'] ) ) {
  74. $statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
  75. if ( !$statusRootPage->isGood() ) {
  76. $this->dieStatus( $statusRootPage );
  77. }
  78. }
  79. $importer->setUsernamePrefix( $usernamePrefix, $params['assignknownusers'] );
  80. $reporter = new ApiImportReporter(
  81. $importer,
  82. $isUpload,
  83. $params['interwikisource'],
  84. $params['summary']
  85. );
  86. if ( $params['tags'] ) {
  87. $reporter->setChangeTags( $params['tags'] );
  88. }
  89. try {
  90. $importer->doImport();
  91. } catch ( Exception $e ) {
  92. $this->dieWithException( $e, [ 'wrap' => 'apierror-import-unknownerror' ] );
  93. }
  94. $resultData = $reporter->getData();
  95. $result = $this->getResult();
  96. ApiResult::setIndexedTagName( $resultData, 'page' );
  97. $result->addValue( null, $this->getModuleName(), $resultData );
  98. }
  99. /**
  100. * Returns a list of interwiki prefixes corresponding to each defined import
  101. * source.
  102. *
  103. * @return array
  104. * @since 1.27
  105. */
  106. public function getAllowedImportSources() {
  107. $importSources = $this->getConfig()->get( 'ImportSources' );
  108. Hooks::run( 'ImportSources', [ &$importSources ] );
  109. $result = [];
  110. foreach ( $importSources as $key => $value ) {
  111. if ( is_int( $key ) ) {
  112. $result[] = $value;
  113. } else {
  114. foreach ( $value as $subproject ) {
  115. $result[] = "$key:$subproject";
  116. }
  117. }
  118. }
  119. return $result;
  120. }
  121. public function mustBePosted() {
  122. return true;
  123. }
  124. public function isWriteMode() {
  125. return true;
  126. }
  127. public function getAllowedParams() {
  128. return [
  129. 'summary' => null,
  130. 'xml' => [
  131. ApiBase::PARAM_TYPE => 'upload',
  132. ],
  133. 'interwikiprefix' => [
  134. ApiBase::PARAM_TYPE => 'string',
  135. ],
  136. 'interwikisource' => [
  137. ApiBase::PARAM_TYPE => $this->getAllowedImportSources(),
  138. ],
  139. 'interwikipage' => null,
  140. 'fullhistory' => false,
  141. 'templates' => false,
  142. 'namespace' => [
  143. ApiBase::PARAM_TYPE => 'namespace'
  144. ],
  145. 'assignknownusers' => false,
  146. 'rootpage' => null,
  147. 'tags' => [
  148. ApiBase::PARAM_TYPE => 'tags',
  149. ApiBase::PARAM_ISMULTI => true,
  150. ],
  151. ];
  152. }
  153. public function needsToken() {
  154. return 'csrf';
  155. }
  156. protected function getExamplesMessages() {
  157. return [
  158. 'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
  159. 'namespace=100&fullhistory=&token=123ABC'
  160. => 'apihelp-import-example-import',
  161. ];
  162. }
  163. public function getHelpUrls() {
  164. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Import';
  165. }
  166. }