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