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