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