Merge "Remove --max-slave-lag options and remnants from maintenance scripts"
[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
46 /**
47 * Constructor
48 */
49 public function __construct() {
50 parent::__construct( 'Import', 'import' );
51 }
52
53 /**
54 * Execute
55 * @param string|null $par
56 * @throws PermissionsError
57 * @throws ReadOnlyError
58 */
59 function execute( $par ) {
60 $this->setHeaders();
61 $this->outputHeader();
62
63 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
64
65 $this->getOutput()->addModules( 'mediawiki.special.import' );
66
67 $user = $this->getUser();
68 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
69 throw new PermissionsError( 'import' );
70 }
71
72 # @todo Allow Title::getUserPermissionsErrors() to take an array
73 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
74 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
75 $errors = wfMergeErrorArrays(
76 $this->getPageTitle()->getUserPermissionsErrors(
77 'import', $user, true,
78 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
79 ),
80 $this->getPageTitle()->getUserPermissionsErrors(
81 'importupload', $user, true,
82 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
83 )
84 );
85
86 if ( $errors ) {
87 throw new PermissionsError( 'import', $errors );
88 }
89
90 $this->checkReadOnly();
91
92 $request = $this->getRequest();
93 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
94 $this->doImport();
95 }
96 $this->showForm();
97 }
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
107 $this->logcomment = $request->getText( 'log-comment' );
108 $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
109 ? 0
110 : $request->getIntOrNull( 'pagelink-depth' );
111
112 $this->mapping = $request->getVal( 'mapping' );
113 if ( $this->mapping === 'namespace' ) {
114 $this->namespace = $request->getIntOrNull( 'namespace' );
115 } elseif ( $this->mapping === 'subpage' ) {
116 $this->rootpage = $request->getText( 'rootpage' );
117 } else {
118 $this->mapping = 'default';
119 }
120
121 $user = $this->getUser();
122 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
123 $source = Status::newFatal( 'import-token-mismatch' );
124 } elseif ( $this->sourceName === 'upload' ) {
125 $isUpload = true;
126 if ( $user->isAllowed( 'importupload' ) ) {
127 $source = ImportStreamSource::newFromUpload( "xmlimport" );
128 } else {
129 throw new PermissionsError( 'importupload' );
130 }
131 } elseif ( $this->sourceName === 'interwiki' ) {
132 if ( !$user->isAllowed( 'import' ) ) {
133 throw new PermissionsError( 'import' );
134 }
135 $this->interwiki = $this->fullInterwikiPrefix = $request->getVal( 'interwiki' );
136 // does this interwiki have subprojects?
137 $importSources = $this->getConfig()->get( 'ImportSources' );
138 $hasSubprojects = array_key_exists( $this->interwiki, $importSources );
139 if ( !$hasSubprojects && !in_array( $this->interwiki, $importSources ) ) {
140 $source = Status::newFatal( "import-invalid-interwiki" );
141 } else {
142 if ( $hasSubprojects ) {
143 $this->subproject = $request->getVal( 'subproject' );
144 $this->fullInterwikiPrefix .= ':' . $request->getVal( 'subproject' );
145 }
146 if ( $hasSubprojects && !in_array( $this->subproject, $importSources[$this->interwiki] ) ) {
147 $source = Status::newFatal( "import-invalid-interwiki" );
148 } else {
149 $this->history = $request->getCheck( 'interwikiHistory' );
150 $this->frompage = $request->getText( "frompage" );
151 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
152 $source = ImportStreamSource::newFromInterwiki(
153 $this->fullInterwikiPrefix,
154 $this->frompage,
155 $this->history,
156 $this->includeTemplates,
157 $this->pageLinkDepth );
158 }
159 }
160 } else {
161 $source = Status::newFatal( "importunknownsource" );
162 }
163
164 $out = $this->getOutput();
165 if ( !$source->isGood() ) {
166 $out->wrapWikiMsg(
167 "<p class=\"error\">\n$1\n</p>",
168 array( 'importfailed', $source->getWikiText() )
169 );
170 } else {
171 $importer = new WikiImporter( $source->value, $this->getConfig() );
172 if ( !is_null( $this->namespace ) ) {
173 $importer->setTargetNamespace( $this->namespace );
174 } elseif ( !is_null( $this->rootpage ) ) {
175 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
176 if ( !$statusRootPage->isGood() ) {
177 $out->wrapWikiMsg(
178 "<p class=\"error\">\n$1\n</p>",
179 array(
180 'import-options-wrong',
181 $statusRootPage->getWikiText(),
182 count( $statusRootPage->getErrorsArray() )
183 )
184 );
185
186 return;
187 }
188 }
189
190 $out->addWikiMsg( "importstart" );
191
192 $reporter = new ImportReporter(
193 $importer,
194 $isUpload,
195 $this->fullInterwikiPrefix,
196 $this->logcomment
197 );
198 $reporter->setContext( $this->getContext() );
199 $exception = false;
200
201 $reporter->open();
202 try {
203 $importer->doImport();
204 } catch ( Exception $e ) {
205 $exception = $e;
206 }
207 $result = $reporter->close();
208
209 if ( $exception ) {
210 # No source or XML parse error
211 $out->wrapWikiMsg(
212 "<p class=\"error\">\n$1\n</p>",
213 array( 'importfailed', $exception->getMessage() )
214 );
215 } elseif ( !$result->isGood() ) {
216 # Zero revisions
217 $out->wrapWikiMsg(
218 "<p class=\"error\">\n$1\n</p>",
219 array( 'importfailed', $result->getWikiText() )
220 );
221 } else {
222 # Success!
223 $out->addWikiMsg( 'importsuccess' );
224 }
225 $out->addHTML( '<hr />' );
226 }
227 }
228
229 private function getMappingFormPart( $sourceName ) {
230 $isSameSourceAsBefore = ( $this->sourceName === $sourceName );
231 $defaultNamespace = $this->getConfig()->get( 'ImportTargetNamespace' );
232 return "<tr>
233 <td>
234 </td>
235 <td class='mw-input'>" .
236 Xml::radioLabel(
237 $this->msg( 'import-mapping-default' )->text(),
238 'mapping',
239 'default',
240 // mw-import-mapping-interwiki-default, mw-import-mapping-upload-default
241 "mw-import-mapping-$sourceName-default",
242 ( $isSameSourceAsBefore ?
243 ( $this->mapping === 'default' ) :
244 is_null( $defaultNamespace ) )
245 ) .
246 "</td>
247 </tr>
248 <tr>
249 <td>
250 </td>
251 <td class='mw-input'>" .
252 Xml::radioLabel(
253 $this->msg( 'import-mapping-namespace' )->text(),
254 'mapping',
255 'namespace',
256 // mw-import-mapping-interwiki-namespace, mw-import-mapping-upload-namespace
257 "mw-import-mapping-$sourceName-namespace",
258 ( $isSameSourceAsBefore ?
259 ( $this->mapping === 'namespace' ) :
260 !is_null( $defaultNamespace ) )
261 ) . ' ' .
262 Html::namespaceSelector(
263 array(
264 'selected' => ( $isSameSourceAsBefore ?
265 $this->namespace :
266 ( $defaultNamespace || '' ) ),
267 ), array(
268 'name' => "namespace",
269 // mw-import-namespace-interwiki, mw-import-namespace-upload
270 'id' => "mw-import-namespace-$sourceName",
271 'class' => 'namespaceselector',
272 )
273 ) .
274 "</td>
275 </tr>
276 <tr>
277 <td>
278 </td>
279 <td class='mw-input'>" .
280 Xml::radioLabel(
281 $this->msg( 'import-mapping-subpage' )->text(),
282 'mapping',
283 'subpage',
284 // mw-import-mapping-interwiki-subpage, mw-import-mapping-upload-subpage
285 "mw-import-mapping-$sourceName-subpage",
286 ( $isSameSourceAsBefore ? ( $this->mapping === 'subpage' ) : '' )
287 ) . ' ' .
288 Xml::input( 'rootpage', 50,
289 ( $isSameSourceAsBefore ? $this->rootpage : '' ),
290 array(
291 // Should be "mw-import-rootpage-...", but we keep this inaccurate
292 // ID for legacy reasons
293 // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
294 'id' => "mw-interwiki-rootpage-$sourceName",
295 'type' => 'text'
296 )
297 ) . ' ' .
298 "</td>
299 </tr>";
300 }
301
302 private function showForm() {
303 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
304 $user = $this->getUser();
305 $out = $this->getOutput();
306 $importSources = $this->getConfig()->get( 'ImportSources' );
307
308 if ( $user->isAllowed( 'importupload' ) ) {
309 $mappingSelection = $this->getMappingFormPart( 'upload' );
310 $out->addHTML(
311 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
312 Xml::openElement(
313 'form',
314 array(
315 'enctype' => 'multipart/form-data',
316 'method' => 'post',
317 'action' => $action,
318 'id' => 'mw-import-upload-form'
319 )
320 ) .
321 $this->msg( 'importtext' )->parseAsBlock() .
322 Html::hidden( 'action', 'submit' ) .
323 Html::hidden( 'source', 'upload' ) .
324 Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
325 "<tr>
326 <td class='mw-label'>" .
327 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
328 "</td>
329 <td class='mw-input'>" .
330 Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
331 "</td>
332 </tr>
333 <tr>
334 <td class='mw-label'>" .
335 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
336 "</td>
337 <td class='mw-input'>" .
338 Xml::input( 'log-comment', 50,
339 ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
340 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
341 "</td>
342 </tr>
343 $mappingSelection
344 <tr>
345 <td></td>
346 <td class='mw-submit'>" .
347 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
348 "</td>
349 </tr>" .
350 Xml::closeElement( 'table' ) .
351 Html::hidden( 'editToken', $user->getEditToken() ) .
352 Xml::closeElement( 'form' ) .
353 Xml::closeElement( 'fieldset' )
354 );
355 } else {
356 if ( empty( $importSources ) ) {
357 $out->addWikiMsg( 'importnosources' );
358 }
359 }
360
361 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
362 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
363 $importDepth = '';
364 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
365 $importDepth = "<tr>
366 <td class='mw-label'>" .
367 $this->msg( 'export-pagelinks' )->parse() .
368 "</td>
369 <td class='mw-input'>" .
370 Xml::input( 'pagelink-depth', 3, 0 ) .
371 "</td>
372 </tr>";
373 }
374 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
375
376 $out->addHTML(
377 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
378 Xml::openElement(
379 'form',
380 array(
381 'method' => 'post',
382 'action' => $action,
383 'id' => 'mw-import-interwiki-form'
384 )
385 ) .
386 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
387 Html::hidden( 'action', 'submit' ) .
388 Html::hidden( 'source', 'interwiki' ) .
389 Html::hidden( 'editToken', $user->getEditToken() ) .
390 Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
391 "<tr>
392 <td class='mw-label'>" .
393 Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
394 "</td>
395 <td class='mw-input'>" .
396 Xml::openElement(
397 'select',
398 array( 'name' => 'interwiki', 'id' => 'interwiki' )
399 )
400 );
401
402 $needSubprojectField = false;
403 foreach ( $importSources as $key => $value ) {
404 if ( is_int( $key ) ) {
405 $key = $value;
406 } elseif ( $value !== $key ) {
407 $needSubprojectField = true;
408 }
409
410 $attribs = array(
411 'value' => $key,
412 );
413 if ( is_array( $value ) ) {
414 $attribs['data-subprojects'] = implode( ' ', $value );
415 }
416 if ( $this->interwiki === $key ) {
417 $attribs['selected'] = 'selected';
418 }
419 $out->addHTML( Html::element( 'option', $attribs, $key ) );
420 }
421
422 $out->addHTML(
423 Xml::closeElement( 'select' )
424 );
425
426 if ( $needSubprojectField ) {
427 $out->addHTML(
428 Xml::openElement(
429 'select',
430 array( 'name' => 'subproject', 'id' => 'subproject' )
431 )
432 );
433
434 $subprojectsToAdd = array();
435 foreach ( $importSources as $key => $value ) {
436 if ( is_array( $value ) ) {
437 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
438 }
439 }
440 $subprojectsToAdd = array_unique( $subprojectsToAdd );
441 sort( $subprojectsToAdd );
442 foreach ( $subprojectsToAdd as $subproject ) {
443 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
444 }
445
446 $out->addHTML(
447 Xml::closeElement( 'select' )
448 );
449 }
450
451 $out->addHTML(
452 "</td>
453 </tr>
454 <tr>
455 <td class='mw-label'>" .
456 Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
457 "</td>
458 <td class='mw-input'>" .
459 Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
460 "</td>
461 </tr>
462 <tr>
463 <td>
464 </td>
465 <td class='mw-input'>" .
466 Xml::checkLabel(
467 $this->msg( 'import-interwiki-history' )->text(),
468 'interwikiHistory',
469 'interwikiHistory',
470 $this->history
471 ) .
472 "</td>
473 </tr>
474 <tr>
475 <td>
476 </td>
477 <td class='mw-input'>" .
478 Xml::checkLabel(
479 $this->msg( 'import-interwiki-templates' )->text(),
480 'interwikiTemplates',
481 'interwikiTemplates',
482 $this->includeTemplates
483 ) .
484 "</td>
485 </tr>
486 $importDepth
487 <tr>
488 <td class='mw-label'>" .
489 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
490 "</td>
491 <td class='mw-input'>" .
492 Xml::input( 'log-comment', 50,
493 ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
494 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
495 "</td>
496 </tr>
497 $mappingSelection
498 <tr>
499 <td>
500 </td>
501 <td class='mw-submit'>" .
502 Xml::submitButton(
503 $this->msg( 'import-interwiki-submit' )->text(),
504 Linker::tooltipAndAccesskeyAttribs( 'import' )
505 ) .
506 "</td>
507 </tr>" .
508 Xml::closeElement( 'table' ) .
509 Xml::closeElement( 'form' ) .
510 Xml::closeElement( 'fieldset' )
511 );
512 }
513 }
514
515 protected function getGroupName() {
516 return 'pagetools';
517 }
518 }
519
520 /**
521 * Reporting callback
522 * @ingroup SpecialPage
523 */
524 class ImportReporter extends ContextSource {
525 private $reason = false;
526 private $mOriginalLogCallback = null;
527 private $mOriginalPageOutCallback = null;
528 private $mLogItemCount = 0;
529
530 /**
531 * @param WikiImporter $importer
532 * @param bool $upload
533 * @param string $interwiki
534 * @param string|bool $reason
535 */
536 function __construct( $importer, $upload, $interwiki, $reason = false ) {
537 $this->mOriginalPageOutCallback =
538 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
539 $this->mOriginalLogCallback =
540 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
541 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
542 $this->mPageCount = 0;
543 $this->mIsUpload = $upload;
544 $this->mInterwiki = $interwiki;
545 $this->reason = $reason;
546 }
547
548 function open() {
549 $this->getOutput()->addHTML( "<ul>\n" );
550 }
551
552 function reportNotice( $msg, array $params ) {
553 $this->getOutput()->addHTML(
554 Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
555 );
556 }
557
558 function reportLogItem( /* ... */ ) {
559 $this->mLogItemCount++;
560 if ( is_callable( $this->mOriginalLogCallback ) ) {
561 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
562 }
563 }
564
565 /**
566 * @param Title $title
567 * @param ForeignTitle $foreignTitle
568 * @param int $revisionCount
569 * @param int $successCount
570 * @param array $pageInfo
571 * @return void
572 */
573 function reportPage( $title, $foreignTitle, $revisionCount,
574 $successCount, $pageInfo ) {
575 $args = func_get_args();
576 call_user_func_array( $this->mOriginalPageOutCallback, $args );
577
578 if ( $title === null ) {
579 # Invalid or non-importable title; a notice is already displayed
580 return;
581 }
582
583 $this->mPageCount++;
584
585 if ( $successCount > 0 ) {
586 $this->getOutput()->addHTML(
587 "<li>" . Linker::linkKnown( $title ) . " " .
588 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
589 "</li>\n"
590 );
591
592 if ( $this->mIsUpload ) {
593 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
594 $successCount )->inContentLanguage()->text();
595 if ( $this->reason ) {
596 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
597 . $this->reason;
598 }
599 $action = 'upload';
600 } else {
601 $interwiki = '[[:' . $this->mInterwiki . ':' .
602 $foreignTitle->getFullText() . ']]';
603 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
604 $successCount )->params( $interwiki )->inContentLanguage()->text();
605 if ( $this->reason ) {
606 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
607 . $this->reason;
608 }
609 $action = 'interwiki';
610 }
611
612 $logEntry = new ManualLogEntry( 'import', $action );
613 $logEntry->setTarget( $title );
614 $logEntry->setComment( $detail );
615 $logEntry->setPerformer( $this->getUser() );
616 $logid = $logEntry->insert();
617 $logEntry->publish( $logid );
618
619 $comment = $detail; // quick
620 $dbw = wfGetDB( DB_MASTER );
621 $latest = $title->getLatestRevID();
622 $nullRevision = Revision::newNullRevision(
623 $dbw,
624 $title->getArticleID(),
625 $comment,
626 true,
627 $this->getUser()
628 );
629
630 if ( !is_null( $nullRevision ) ) {
631 $nullRevision->insertOn( $dbw );
632 $page = WikiPage::factory( $title );
633 # Update page record
634 $page->updateRevisionOn( $dbw, $nullRevision );
635 Hooks::run(
636 'NewRevisionFromEditComplete',
637 array( $page, $nullRevision, $latest, $this->getUser() )
638 );
639 }
640 } else {
641 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
642 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
643 }
644 }
645
646 function close() {
647 $out = $this->getOutput();
648 if ( $this->mLogItemCount > 0 ) {
649 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
650 $out->addHTML( Xml::tags( 'li', null, $msg ) );
651 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
652 $out->addHTML( "</ul>\n" );
653
654 return Status::newFatal( 'importnopages' );
655 }
656 $out->addHTML( "</ul>\n" );
657
658 return Status::newGood( $this->mPageCount );
659 }
660 }