Merge "Declare dynamic properties"
[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 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
139 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
140 $source = Status::newFatal( 'import-token-mismatch' );
141 } elseif ( $this->sourceName === 'upload' ) {
142 $isUpload = true;
143 $this->usernamePrefix = $this->fullInterwikiPrefix = $request->getVal( 'usernamePrefix' );
144 if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
145 $source = ImportStreamSource::newFromUpload( "xmlimport" );
146 } else {
147 throw new PermissionsError( 'importupload' );
148 }
149 } elseif ( $this->sourceName === 'interwiki' ) {
150 if ( !$permissionManager->userHasRight( $user, 'import' ) ) {
151 throw new PermissionsError( 'import' );
152 }
153 $this->interwiki = $this->fullInterwikiPrefix = $request->getVal( 'interwiki' );
154 // does this interwiki have subprojects?
155 $hasSubprojects = array_key_exists( $this->interwiki, $this->importSources );
156 if ( !$hasSubprojects && !in_array( $this->interwiki, $this->importSources ) ) {
157 $source = Status::newFatal( "import-invalid-interwiki" );
158 } else {
159 if ( $hasSubprojects ) {
160 $this->subproject = $request->getVal( 'subproject' );
161 $this->fullInterwikiPrefix .= ':' . $request->getVal( 'subproject' );
162 }
163 if ( $hasSubprojects &&
164 !in_array( $this->subproject, $this->importSources[$this->interwiki] )
165 ) {
166 $source = Status::newFatal( "import-invalid-interwiki" );
167 } else {
168 $this->history = $request->getCheck( 'interwikiHistory' );
169 $this->frompage = $request->getText( "frompage" );
170 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
171 $source = ImportStreamSource::newFromInterwiki(
172 $this->fullInterwikiPrefix,
173 $this->frompage,
174 $this->history,
175 $this->includeTemplates,
176 $this->pageLinkDepth );
177 }
178 }
179 } else {
180 $source = Status::newFatal( "importunknownsource" );
181 }
182
183 if ( (string)$this->fullInterwikiPrefix === '' ) {
184 $source->fatal( 'importnoprefix' );
185 }
186
187 $out = $this->getOutput();
188 if ( !$source->isGood() ) {
189 $out->wrapWikiTextAsInterface( 'error',
190 $this->msg( 'importfailed', $source->getWikiText() )->plain()
191 );
192 } else {
193 $importer = new WikiImporter( $source->value, $this->getConfig() );
194 if ( !is_null( $this->namespace ) ) {
195 $importer->setTargetNamespace( $this->namespace );
196 } elseif ( !is_null( $this->rootpage ) ) {
197 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
198 if ( !$statusRootPage->isGood() ) {
199 $out->wrapWikiMsg(
200 "<div class=\"error\">\n$1\n</div>",
201 [
202 'import-options-wrong',
203 $statusRootPage->getWikiText(),
204 count( $statusRootPage->getErrorsArray() )
205 ]
206 );
207
208 return;
209 }
210 }
211 $importer->setUsernamePrefix( $this->fullInterwikiPrefix, $this->assignKnownUsers );
212
213 $out->addWikiMsg( "importstart" );
214
215 $reporter = new ImportReporter(
216 $importer,
217 $isUpload,
218 $this->fullInterwikiPrefix,
219 $this->logcomment
220 );
221 $reporter->setContext( $this->getContext() );
222 $exception = false;
223
224 $reporter->open();
225 try {
226 $importer->doImport();
227 } catch ( Exception $e ) {
228 $exception = $e;
229 }
230 $result = $reporter->close();
231
232 if ( $exception ) {
233 # No source or XML parse error
234 $out->wrapWikiMsg(
235 "<div class=\"error\">\n$1\n</div>",
236 [ 'importfailed', $exception->getMessage() ]
237 );
238 } elseif ( !$result->isGood() ) {
239 # Zero revisions
240 $out->wrapWikiMsg(
241 "<div class=\"error\">\n$1\n</div>",
242 [ 'importfailed', $result->getWikiText() ]
243 );
244 } else {
245 # Success!
246 $out->addWikiMsg( 'importsuccess' );
247 }
248 $out->addHTML( '<hr />' );
249 }
250 }
251
252 private function getMappingFormPart( $sourceName ) {
253 $isSameSourceAsBefore = ( $this->sourceName === $sourceName );
254 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
255 return "<tr>
256 <td>
257 </td>
258 <td class='mw-input'>" .
259 Xml::radioLabel(
260 $this->msg( 'import-mapping-default' )->text(),
261 'mapping',
262 'default',
263 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
264 "mw-import-mapping-$sourceName-default",
265 ( $isSameSourceAsBefore ?
266 ( $this->mapping === 'default' ) :
267 is_null( $defaultNamespace ) )
268 ) .
269 "</td>
270 </tr>
271 <tr>
272 <td>
273 </td>
274 <td class='mw-input'>" .
275 Xml::radioLabel(
276 $this->msg( 'import-mapping-namespace' )->text(),
277 'mapping',
278 'namespace',
279 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
280 "mw-import-mapping-$sourceName-namespace",
281 ( $isSameSourceAsBefore ?
282 ( $this->mapping === 'namespace' ) :
283 !is_null( $defaultNamespace ) )
284 ) . ' ' .
285 Html::namespaceSelector(
286 [
287 'selected' => ( $isSameSourceAsBefore ?
288 $this->namespace :
289 ( $defaultNamespace || '' ) ),
290 'in-user-lang' => true,
291 ], [
292 'name' => "namespace",
293 // mw-import-namespace-interwiki, mw-import-namespace-upload
294 'id' => "mw-import-namespace-$sourceName",
295 'class' => 'namespaceselector',
296 ]
297 ) .
298 "</td>
299 </tr>
300 <tr>
301 <td>
302 </td>
303 <td class='mw-input'>" .
304 Xml::radioLabel(
305 $this->msg( 'import-mapping-subpage' )->text(),
306 'mapping',
307 'subpage',
308 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
309 "mw-import-mapping-$sourceName-subpage",
310 ( $isSameSourceAsBefore ? ( $this->mapping === 'subpage' ) : '' )
311 ) . ' ' .
312 Xml::input( 'rootpage', 50,
313 ( $isSameSourceAsBefore ? $this->rootpage : '' ),
314 [
315 // Should be "mw-import-rootpage-...", but we keep this inaccurate
316 // ID for legacy reasons
317 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
318 'id' => "mw-interwiki-rootpage-$sourceName",
319 'type' => 'text'
320 ]
321 ) . ' ' .
322 "</td>
323 </tr>";
324 }
325
326 private function showForm() {
327 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
328 $user = $this->getUser();
329 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
330 $out = $this->getOutput();
331 $this->addHelpLink( 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
332
333 if ( $permissionManager->userHasRight( $user, 'importupload' ) ) {
334 $mappingSelection = $this->getMappingFormPart( 'upload' );
335 $out->addHTML(
336 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
337 Xml::openElement(
338 'form',
339 [
340 'enctype' => 'multipart/form-data',
341 'method' => 'post',
342 'action' => $action,
343 'id' => 'mw-import-upload-form'
344 ]
345 ) .
346 $this->msg( 'importtext' )->parseAsBlock() .
347 Html::hidden( 'action', 'submit' ) .
348 Html::hidden( 'source', 'upload' ) .
349 Xml::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
350 "<tr>
351 <td class='mw-label'>" .
352 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
353 "</td>
354 <td class='mw-input'>" .
355 Html::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
356 "</td>
357 </tr>
358 <tr>
359 <td class='mw-label'>" .
360 Xml::label( $this->msg( 'import-upload-username-prefix' )->text(),
361 'mw-import-usernamePrefix' ) .
362 "</td>
363 <td class='mw-input'>" .
364 Xml::input( 'usernamePrefix', 50,
365 $this->usernamePrefix,
366 [ 'id' => 'usernamePrefix', 'type' => 'text' ] ) . ' ' .
367 "</td>
368 </tr>
369 <tr>
370 <td></td>
371 <td class='mw-input'>" .
372 Xml::checkLabel(
373 $this->msg( 'import-assign-known-users' )->text(),
374 'assignKnownUsers',
375 'assignKnownUsers',
376 $this->assignKnownUsers
377 ) .
378 "</td>
379 </tr>
380 <tr>
381 <td class='mw-label'>" .
382 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
383 "</td>
384 <td class='mw-input'>" .
385 Xml::input( 'log-comment', 50,
386 ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
387 [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
388 "</td>
389 </tr>
390 $mappingSelection
391 <tr>
392 <td></td>
393 <td class='mw-submit'>" .
394 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
395 "</td>
396 </tr>" .
397 Xml::closeElement( 'table' ) .
398 Html::hidden( 'editToken', $user->getEditToken() ) .
399 Xml::closeElement( 'form' ) .
400 Xml::closeElement( 'fieldset' )
401 );
402 } elseif ( empty( $this->importSources ) ) {
403 $out->addWikiMsg( 'importnosources' );
404 }
405
406 if ( $permissionManager->userHasRight( $user, 'import' ) && !empty( $this->importSources ) ) {
407 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
408 $importDepth = '';
409 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
410 $importDepth = "<tr>
411 <td class='mw-label'>" .
412 $this->msg( 'export-pagelinks' )->parse() .
413 "</td>
414 <td class='mw-input'>" .
415 Xml::input( 'pagelink-depth', 3, 0 ) .
416 "</td>
417 </tr>";
418 }
419 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
420
421 $out->addHTML(
422 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
423 Xml::openElement(
424 'form',
425 [
426 'method' => 'post',
427 'action' => $action,
428 'id' => 'mw-import-interwiki-form'
429 ]
430 ) .
431 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
432 Html::hidden( 'action', 'submit' ) .
433 Html::hidden( 'source', 'interwiki' ) .
434 Html::hidden( 'editToken', $user->getEditToken() ) .
435 Xml::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
436 "<tr>
437 <td class='mw-label'>" .
438 Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
439 "</td>
440 <td class='mw-input'>" .
441 Xml::openElement(
442 'select',
443 [ 'name' => 'interwiki', 'id' => 'interwiki' ]
444 )
445 );
446
447 $needSubprojectField = false;
448 foreach ( $this->importSources as $key => $value ) {
449 if ( is_int( $key ) ) {
450 $key = $value;
451 } elseif ( $value !== $key ) {
452 $needSubprojectField = true;
453 }
454
455 $attribs = [
456 'value' => $key,
457 ];
458 if ( is_array( $value ) ) {
459 $attribs['data-subprojects'] = implode( ' ', $value );
460 }
461 if ( $this->interwiki === $key ) {
462 $attribs['selected'] = 'selected';
463 }
464 $out->addHTML( Html::element( 'option', $attribs, $key ) );
465 }
466
467 $out->addHTML(
468 Xml::closeElement( 'select' )
469 );
470
471 if ( $needSubprojectField ) {
472 $out->addHTML(
473 Xml::openElement(
474 'select',
475 [ 'name' => 'subproject', 'id' => 'subproject' ]
476 )
477 );
478
479 $subprojectsToAdd = [];
480 foreach ( $this->importSources as $key => $value ) {
481 if ( is_array( $value ) ) {
482 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
483 }
484 }
485 $subprojectsToAdd = array_unique( $subprojectsToAdd );
486 sort( $subprojectsToAdd );
487 foreach ( $subprojectsToAdd as $subproject ) {
488 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
489 }
490
491 $out->addHTML(
492 Xml::closeElement( 'select' )
493 );
494 }
495
496 $out->addHTML(
497 "</td>
498 </tr>
499 <tr>
500 <td class='mw-label'>" .
501 Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
502 "</td>
503 <td class='mw-input'>" .
504 Xml::input( 'frompage', 50, $this->frompage, [ 'id' => 'frompage' ] ) .
505 "</td>
506 </tr>
507 <tr>
508 <td>
509 </td>
510 <td class='mw-input'>" .
511 Xml::checkLabel(
512 $this->msg( 'import-interwiki-history' )->text(),
513 'interwikiHistory',
514 'interwikiHistory',
515 $this->history
516 ) .
517 "</td>
518 </tr>
519 <tr>
520 <td>
521 </td>
522 <td class='mw-input'>" .
523 Xml::checkLabel(
524 $this->msg( 'import-interwiki-templates' )->text(),
525 'interwikiTemplates',
526 'interwikiTemplates',
527 $this->includeTemplates
528 ) .
529 "</td>
530 </tr>
531 <tr>
532 <td></td>
533 <td class='mw-input'>" .
534 Xml::checkLabel(
535 $this->msg( 'import-assign-known-users' )->text(),
536 'assignKnownUsers',
537 'interwikiAssignKnownUsers',
538 $this->assignKnownUsers
539 ) .
540 "</td>
541 </tr>
542 $importDepth
543 <tr>
544 <td class='mw-label'>" .
545 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
546 "</td>
547 <td class='mw-input'>" .
548 Xml::input( 'log-comment', 50,
549 ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
550 [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
551 "</td>
552 </tr>
553 $mappingSelection
554 <tr>
555 <td>
556 </td>
557 <td class='mw-submit'>" .
558 Xml::submitButton(
559 $this->msg( 'import-interwiki-submit' )->text(),
560 Linker::tooltipAndAccesskeyAttribs( 'import' )
561 ) .
562 "</td>
563 </tr>" .
564 Xml::closeElement( 'table' ) .
565 Xml::closeElement( 'form' ) .
566 Xml::closeElement( 'fieldset' )
567 );
568 }
569 }
570
571 protected function getGroupName() {
572 return 'pagetools';
573 }
574 }