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