SpecialImport.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. /**
  3. * Implements Special:Import
  4. *
  5. * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup SpecialPage
  25. */
  26. use MediaWiki\MediaWikiServices;
  27. use MediaWiki\Permissions\PermissionManager;
  28. /**
  29. * MediaWiki page data importer
  30. *
  31. * @ingroup SpecialPage
  32. */
  33. class SpecialImport extends SpecialPage {
  34. private $sourceName = false;
  35. private $interwiki = false;
  36. private $subproject;
  37. private $fullInterwikiPrefix;
  38. private $mapping = 'default';
  39. private $namespace;
  40. private $rootpage = '';
  41. private $frompage = '';
  42. private $logcomment = false;
  43. private $history = true;
  44. private $includeTemplates = false;
  45. private $pageLinkDepth;
  46. private $importSources;
  47. private $assignKnownUsers;
  48. private $usernamePrefix;
  49. public function __construct() {
  50. parent::__construct( 'Import', 'import' );
  51. }
  52. public function doesWrites() {
  53. return true;
  54. }
  55. /**
  56. * Execute
  57. * @param string|null $par
  58. * @throws PermissionsError
  59. * @throws ReadOnlyError
  60. */
  61. function execute( $par ) {
  62. $this->useTransactionalTimeLimit();
  63. $this->setHeaders();
  64. $this->outputHeader();
  65. $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
  66. $this->getOutput()->addModules( 'mediawiki.special.import' );
  67. $this->importSources = $this->getConfig()->get( 'ImportSources' );
  68. Hooks::run( 'ImportSources', [ &$this->importSources ] );
  69. $user = $this->getUser();
  70. if ( !MediaWikiServices::getInstance()
  71. ->getPermissionManager()
  72. ->userHasAnyRight( $user, 'import', 'importupload' )
  73. ) {
  74. throw new PermissionsError( 'import' );
  75. }
  76. # @todo Allow Title::getUserPermissionsErrors() to take an array
  77. # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very weird expectation of what
  78. # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
  79. $errors = wfMergeErrorArrays(
  80. $this->getPageTitle()->getUserPermissionsErrors(
  81. 'import', $user, PermissionManager::RIGOR_FULL,
  82. [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
  83. ),
  84. $this->getPageTitle()->getUserPermissionsErrors(
  85. 'importupload', $user, PermissionManager::RIGOR_FULL,
  86. [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
  87. )
  88. );
  89. if ( $errors ) {
  90. throw new PermissionsError( 'import', $errors );
  91. }
  92. $this->checkReadOnly();
  93. $request = $this->getRequest();
  94. if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
  95. $this->doImport();
  96. }
  97. $this->showForm();
  98. }
  99. /**
  100. * Do the actual import
  101. */
  102. private function doImport() {
  103. $isUpload = false;
  104. $request = $this->getRequest();
  105. $this->sourceName = $request->getVal( "source" );
  106. $this->assignKnownUsers = $request->getCheck( 'assignKnownUsers' );
  107. $this->logcomment = $request->getText( 'log-comment' );
  108. $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
  109. ? 0
  110. : $request->getIntOrNull( 'pagelink-depth' );
  111. $this->mapping = $request->getVal( 'mapping' );
  112. if ( $this->mapping === 'namespace' ) {
  113. $this->namespace = $request->getIntOrNull( 'namespace' );
  114. } elseif ( $this->mapping === 'subpage' ) {
  115. $this->rootpage = $request->getText( 'rootpage' );
  116. } else {
  117. $this->mapping = 'default';
  118. }
  119. $user = $this->getUser();
  120. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  121. if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
  122. $source = Status::newFatal( 'import-token-mismatch' );
  123. } elseif ( $this->sourceName === 'upload' ) {
  124. $isUpload = true;
  125. $this->usernamePrefix = $this->fullInterwikiPrefix = $request->getVal( 'usernamePrefix' );
  126. if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
  127. $source = ImportStreamSource::newFromUpload( "xmlimport" );
  128. } else {
  129. throw new PermissionsError( 'importupload' );
  130. }
  131. } elseif ( $this->sourceName === 'interwiki' ) {
  132. if ( !$permissionManager->userHasRight( $user, 'import' ) ) {
  133. throw new PermissionsError( 'import' );
  134. }
  135. $this->interwiki = $this->fullInterwikiPrefix = $request->getVal( 'interwiki' );
  136. // does this interwiki have subprojects?
  137. $hasSubprojects = array_key_exists( $this->interwiki, $this->importSources );
  138. if ( !$hasSubprojects && !in_array( $this->interwiki, $this->importSources ) ) {
  139. $source = Status::newFatal( "import-invalid-interwiki" );
  140. } else {
  141. if ( $hasSubprojects ) {
  142. $this->subproject = $request->getVal( 'subproject' );
  143. $this->fullInterwikiPrefix .= ':' . $request->getVal( 'subproject' );
  144. }
  145. if ( $hasSubprojects &&
  146. !in_array( $this->subproject, $this->importSources[$this->interwiki] )
  147. ) {
  148. $source = Status::newFatal( "import-invalid-interwiki" );
  149. } else {
  150. $this->history = $request->getCheck( 'interwikiHistory' );
  151. $this->frompage = $request->getText( "frompage" );
  152. $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
  153. $source = ImportStreamSource::newFromInterwiki(
  154. $this->fullInterwikiPrefix,
  155. $this->frompage,
  156. $this->history,
  157. $this->includeTemplates,
  158. $this->pageLinkDepth );
  159. }
  160. }
  161. } else {
  162. $source = Status::newFatal( "importunknownsource" );
  163. }
  164. if ( (string)$this->fullInterwikiPrefix === '' ) {
  165. $source->fatal( 'importnoprefix' );
  166. }
  167. $out = $this->getOutput();
  168. if ( !$source->isGood() ) {
  169. $out->wrapWikiTextAsInterface( 'error',
  170. $this->msg( 'importfailed', $source->getWikiText( false, false, $this->getLanguage() ) )
  171. ->plain()
  172. );
  173. } else {
  174. $importer = new WikiImporter( $source->value, $this->getConfig() );
  175. if ( !is_null( $this->namespace ) ) {
  176. $importer->setTargetNamespace( $this->namespace );
  177. } elseif ( !is_null( $this->rootpage ) ) {
  178. $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
  179. if ( !$statusRootPage->isGood() ) {
  180. $out->wrapWikiMsg(
  181. "<div class=\"error\">\n$1\n</div>",
  182. [
  183. 'import-options-wrong',
  184. $statusRootPage->getWikiText( false, false, $this->getLanguage() ),
  185. count( $statusRootPage->getErrorsArray() )
  186. ]
  187. );
  188. return;
  189. }
  190. }
  191. $importer->setUsernamePrefix( $this->fullInterwikiPrefix, $this->assignKnownUsers );
  192. $out->addWikiMsg( "importstart" );
  193. $reporter = new ImportReporter(
  194. $importer,
  195. $isUpload,
  196. $this->fullInterwikiPrefix,
  197. $this->logcomment
  198. );
  199. $reporter->setContext( $this->getContext() );
  200. $exception = false;
  201. $reporter->open();
  202. try {
  203. $importer->doImport();
  204. } catch ( Exception $e ) {
  205. $exception = $e;
  206. }
  207. $result = $reporter->close();
  208. if ( $exception ) {
  209. # No source or XML parse error
  210. $out->wrapWikiMsg(
  211. "<div class=\"error\">\n$1\n</div>",
  212. [ 'importfailed', $exception->getMessage() ]
  213. );
  214. } elseif ( !$result->isGood() ) {
  215. # Zero revisions
  216. $out->wrapWikiMsg(
  217. "<div class=\"error\">\n$1\n</div>",
  218. [ 'importfailed', $result->getWikiText( false, false, $this->getLanguage() ) ]
  219. );
  220. } else {
  221. # Success!
  222. $out->addWikiMsg( 'importsuccess' );
  223. }
  224. $out->addHTML( '<hr />' );
  225. }
  226. }
  227. private function getMappingFormPart( $sourceName ) {
  228. $isSameSourceAsBefore = ( $this->sourceName === $sourceName );
  229. $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
  230. return "<tr>
  231. <td>
  232. </td>
  233. <td class='mw-input'>" .
  234. Xml::radioLabel(
  235. $this->msg( 'import-mapping-default' )->text(),
  236. 'mapping',
  237. 'default',
  238. // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
  239. "mw-import-mapping-$sourceName-default",
  240. ( $isSameSourceAsBefore ?
  241. ( $this->mapping === 'default' ) :
  242. is_null( $defaultNamespace ) )
  243. ) .
  244. "</td>
  245. </tr>
  246. <tr>
  247. <td>
  248. </td>
  249. <td class='mw-input'>" .
  250. Xml::radioLabel(
  251. $this->msg( 'import-mapping-namespace' )->text(),
  252. 'mapping',
  253. 'namespace',
  254. // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
  255. "mw-import-mapping-$sourceName-namespace",
  256. ( $isSameSourceAsBefore ?
  257. ( $this->mapping === 'namespace' ) :
  258. !is_null( $defaultNamespace ) )
  259. ) . ' ' .
  260. Html::namespaceSelector(
  261. [
  262. 'selected' => ( $isSameSourceAsBefore ?
  263. $this->namespace :
  264. ( $defaultNamespace || '' ) ),
  265. 'in-user-lang' => true,
  266. ], [
  267. 'name' => "namespace",
  268. // mw-import-namespace-interwiki, mw-import-namespace-upload
  269. 'id' => "mw-import-namespace-$sourceName",
  270. 'class' => 'namespaceselector',
  271. ]
  272. ) .
  273. "</td>
  274. </tr>
  275. <tr>
  276. <td>
  277. </td>
  278. <td class='mw-input'>" .
  279. Xml::radioLabel(
  280. $this->msg( 'import-mapping-subpage' )->text(),
  281. 'mapping',
  282. 'subpage',
  283. // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
  284. "mw-import-mapping-$sourceName-subpage",
  285. ( $isSameSourceAsBefore ? ( $this->mapping === 'subpage' ) : '' )
  286. ) . ' ' .
  287. Xml::input( 'rootpage', 50,
  288. ( $isSameSourceAsBefore ? $this->rootpage : '' ),
  289. [
  290. // Should be "mw-import-rootpage-...", but we keep this inaccurate
  291. // ID for legacy reasons
  292. // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
  293. 'id' => "mw-interwiki-rootpage-$sourceName",
  294. 'type' => 'text'
  295. ]
  296. ) . ' ' .
  297. "</td>
  298. </tr>";
  299. }
  300. private function showForm() {
  301. $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
  302. $user = $this->getUser();
  303. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  304. $out = $this->getOutput();
  305. $this->addHelpLink( 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
  306. if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
  307. $mappingSelection = $this->getMappingFormPart( 'upload' );
  308. $out->addHTML(
  309. Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
  310. Xml::openElement(
  311. 'form',
  312. [
  313. 'enctype' => 'multipart/form-data',
  314. 'method' => 'post',
  315. 'action' => $action,
  316. 'id' => 'mw-import-upload-form'
  317. ]
  318. ) .
  319. $this->msg( 'importtext' )->parseAsBlock() .
  320. Html::hidden( 'action', 'submit' ) .
  321. Html::hidden( 'source', 'upload' ) .
  322. Xml::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
  323. "<tr>
  324. <td class='mw-label'>" .
  325. Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
  326. "</td>
  327. <td class='mw-input'>" .
  328. Html::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
  329. "</td>
  330. </tr>
  331. <tr>
  332. <td class='mw-label'>" .
  333. Xml::label( $this->msg( 'import-upload-username-prefix' )->text(),
  334. 'mw-import-usernamePrefix' ) .
  335. "</td>
  336. <td class='mw-input'>" .
  337. Xml::input( 'usernamePrefix', 50,
  338. $this->usernamePrefix,
  339. [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
  340. "</td>
  341. </tr>
  342. <tr>
  343. <td></td>
  344. <td class='mw-input'>" .
  345. Xml::checkLabel(
  346. $this->msg( 'import-assign-known-users' )->text(),
  347. 'assignKnownUsers',
  348. 'assignKnownUsers',
  349. $this->assignKnownUsers
  350. ) .
  351. "</td>
  352. </tr>
  353. <tr>
  354. <td class='mw-label'>" .
  355. Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
  356. "</td>
  357. <td class='mw-input'>" .
  358. Xml::input( 'log-comment', 50,
  359. ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
  360. [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
  361. "</td>
  362. </tr>
  363. $mappingSelection
  364. <tr>
  365. <td></td>
  366. <td class='mw-submit'>" .
  367. Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
  368. "</td>
  369. </tr>" .
  370. Xml::closeElement( 'table' ) .
  371. Html::hidden( 'editToken', $user->getEditToken() ) .
  372. Xml::closeElement( 'form' ) .
  373. Xml::closeElement( 'fieldset' )
  374. );
  375. } elseif ( empty( $this->importSources ) ) {
  376. $out->addWikiMsg( 'importnosources' );
  377. }
  378. if ( $permissionManager->userHasRight( $user, 'import' ) && !empty( $this->importSources ) ) {
  379. # Show input field for import depth only if $wgExportMaxLinkDepth > 0
  380. $importDepth = '';
  381. if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
  382. $importDepth = "<tr>
  383. <td class='mw-label'>" .
  384. $this->msg( 'export-pagelinks' )->parse() .
  385. "</td>
  386. <td class='mw-input'>" .
  387. Xml::input( 'pagelink-depth', 3, 0 ) .
  388. "</td>
  389. </tr>";
  390. }
  391. $mappingSelection = $this->getMappingFormPart( 'interwiki' );
  392. $out->addHTML(
  393. Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
  394. Xml::openElement(
  395. 'form',
  396. [
  397. 'method' => 'post',
  398. 'action' => $action,
  399. 'id' => 'mw-import-interwiki-form'
  400. ]
  401. ) .
  402. $this->msg( 'import-interwiki-text' )->parseAsBlock() .
  403. Html::hidden( 'action', 'submit' ) .
  404. Html::hidden( 'source', 'interwiki' ) .
  405. Html::hidden( 'editToken', $user->getEditToken() ) .
  406. Xml::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
  407. "<tr>
  408. <td class='mw-label'>" .
  409. Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
  410. "</td>
  411. <td class='mw-input'>" .
  412. Xml::openElement(
  413. 'select',
  414. [ 'name' => 'interwiki', 'id' => 'interwiki' ]
  415. )
  416. );
  417. $needSubprojectField = false;
  418. foreach ( $this->importSources as $key => $value ) {
  419. if ( is_int( $key ) ) {
  420. $key = $value;
  421. } elseif ( $value !== $key ) {
  422. $needSubprojectField = true;
  423. }
  424. $attribs = [
  425. 'value' => $key,
  426. ];
  427. if ( is_array( $value ) ) {
  428. $attribs['data-subprojects'] = implode( ' ', $value );
  429. }
  430. if ( $this->interwiki === $key ) {
  431. $attribs['selected'] = 'selected';
  432. }
  433. $out->addHTML( Html::element( 'option', $attribs, $key ) );
  434. }
  435. $out->addHTML(
  436. Xml::closeElement( 'select' )
  437. );
  438. if ( $needSubprojectField ) {
  439. $out->addHTML(
  440. Xml::openElement(
  441. 'select',
  442. [ 'name' => 'subproject', 'id' => 'subproject' ]
  443. )
  444. );
  445. $subprojectsToAdd = [];
  446. foreach ( $this->importSources as $key => $value ) {
  447. if ( is_array( $value ) ) {
  448. $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
  449. }
  450. }
  451. $subprojectsToAdd = array_unique( $subprojectsToAdd );
  452. sort( $subprojectsToAdd );
  453. foreach ( $subprojectsToAdd as $subproject ) {
  454. $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
  455. }
  456. $out->addHTML(
  457. Xml::closeElement( 'select' )
  458. );
  459. }
  460. $out->addHTML(
  461. "</td>
  462. </tr>
  463. <tr>
  464. <td class='mw-label'>" .
  465. Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
  466. "</td>
  467. <td class='mw-input'>" .
  468. Xml::input( 'frompage', 50, $this->frompage, [ 'id' => 'frompage' ] ) .
  469. "</td>
  470. </tr>
  471. <tr>
  472. <td>
  473. </td>
  474. <td class='mw-input'>" .
  475. Xml::checkLabel(
  476. $this->msg( 'import-interwiki-history' )->text(),
  477. 'interwikiHistory',
  478. 'interwikiHistory',
  479. $this->history
  480. ) .
  481. "</td>
  482. </tr>
  483. <tr>
  484. <td>
  485. </td>
  486. <td class='mw-input'>" .
  487. Xml::checkLabel(
  488. $this->msg( 'import-interwiki-templates' )->text(),
  489. 'interwikiTemplates',
  490. 'interwikiTemplates',
  491. $this->includeTemplates
  492. ) .
  493. "</td>
  494. </tr>
  495. <tr>
  496. <td></td>
  497. <td class='mw-input'>" .
  498. Xml::checkLabel(
  499. $this->msg( 'import-assign-known-users' )->text(),
  500. 'assignKnownUsers',
  501. 'interwikiAssignKnownUsers',
  502. $this->assignKnownUsers
  503. ) .
  504. "</td>
  505. </tr>
  506. $importDepth
  507. <tr>
  508. <td class='mw-label'>" .
  509. Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
  510. "</td>
  511. <td class='mw-input'>" .
  512. Xml::input( 'log-comment', 50,
  513. ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
  514. [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
  515. "</td>
  516. </tr>
  517. $mappingSelection
  518. <tr>
  519. <td>
  520. </td>
  521. <td class='mw-submit'>" .
  522. Xml::submitButton(
  523. $this->msg( 'import-interwiki-submit' )->text(),
  524. Linker::tooltipAndAccesskeyAttribs( 'import' )
  525. ) .
  526. "</td>
  527. </tr>" .
  528. Xml::closeElement( 'table' ) .
  529. Xml::closeElement( 'form' ) .
  530. Xml::closeElement( 'fieldset' )
  531. );
  532. }
  533. }
  534. protected function getGroupName() {
  535. return 'pagetools';
  536. }
  537. }