Merge "Consistency tweaks: xml -> XML, Href -> href"
[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 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
307 $importSources = $this->getConfig()->get( 'ImportSources' );
308
309 if ( $user->isAllowed( 'importupload' ) ) {
310 $mappingSelection = $this->getMappingFormPart( 'upload' );
311 $out->addHTML(
312 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
313 Xml::openElement(
314 'form',
315 array(
316 'enctype' => 'multipart/form-data',
317 'method' => 'post',
318 'action' => $action,
319 'id' => 'mw-import-upload-form'
320 )
321 ) .
322 $this->msg( 'importtext' )->parseAsBlock() .
323 Html::hidden( 'action', 'submit' ) .
324 Html::hidden( 'source', 'upload' ) .
325 Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
326 "<tr>
327 <td class='mw-label'>" .
328 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
329 "</td>
330 <td class='mw-input'>" .
331 Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
332 "</td>
333 </tr>
334 <tr>
335 <td class='mw-label'>" .
336 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
337 "</td>
338 <td class='mw-input'>" .
339 Xml::input( 'log-comment', 50,
340 ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
341 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
342 "</td>
343 </tr>
344 $mappingSelection
345 <tr>
346 <td></td>
347 <td class='mw-submit'>" .
348 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
349 "</td>
350 </tr>" .
351 Xml::closeElement( 'table' ) .
352 Html::hidden( 'editToken', $user->getEditToken() ) .
353 Xml::closeElement( 'form' ) .
354 Xml::closeElement( 'fieldset' )
355 );
356 } else {
357 if ( empty( $importSources ) ) {
358 $out->addWikiMsg( 'importnosources' );
359 }
360 }
361
362 if ( $user->isAllowed( 'import' ) && !empty( $importSources ) ) {
363 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
364 $importDepth = '';
365 if ( $this->getConfig()->get( 'ExportMaxLinkDepth' ) > 0 ) {
366 $importDepth = "<tr>
367 <td class='mw-label'>" .
368 $this->msg( 'export-pagelinks' )->parse() .
369 "</td>
370 <td class='mw-input'>" .
371 Xml::input( 'pagelink-depth', 3, 0 ) .
372 "</td>
373 </tr>";
374 }
375 $mappingSelection = $this->getMappingFormPart( 'interwiki' );
376
377 $out->addHTML(
378 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
379 Xml::openElement(
380 'form',
381 array(
382 'method' => 'post',
383 'action' => $action,
384 'id' => 'mw-import-interwiki-form'
385 )
386 ) .
387 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
388 Html::hidden( 'action', 'submit' ) .
389 Html::hidden( 'source', 'interwiki' ) .
390 Html::hidden( 'editToken', $user->getEditToken() ) .
391 Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
392 "<tr>
393 <td class='mw-label'>" .
394 Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
395 "</td>
396 <td class='mw-input'>" .
397 Xml::openElement(
398 'select',
399 array( 'name' => 'interwiki', 'id' => 'interwiki' )
400 )
401 );
402
403 $needSubprojectField = false;
404 foreach ( $importSources as $key => $value ) {
405 if ( is_int( $key ) ) {
406 $key = $value;
407 } elseif ( $value !== $key ) {
408 $needSubprojectField = true;
409 }
410
411 $attribs = array(
412 'value' => $key,
413 );
414 if ( is_array( $value ) ) {
415 $attribs['data-subprojects'] = implode( ' ', $value );
416 }
417 if ( $this->interwiki === $key ) {
418 $attribs['selected'] = 'selected';
419 }
420 $out->addHTML( Html::element( 'option', $attribs, $key ) );
421 }
422
423 $out->addHTML(
424 Xml::closeElement( 'select' )
425 );
426
427 if ( $needSubprojectField ) {
428 $out->addHTML(
429 Xml::openElement(
430 'select',
431 array( 'name' => 'subproject', 'id' => 'subproject' )
432 )
433 );
434
435 $subprojectsToAdd = array();
436 foreach ( $importSources as $key => $value ) {
437 if ( is_array( $value ) ) {
438 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
439 }
440 }
441 $subprojectsToAdd = array_unique( $subprojectsToAdd );
442 sort( $subprojectsToAdd );
443 foreach ( $subprojectsToAdd as $subproject ) {
444 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
445 }
446
447 $out->addHTML(
448 Xml::closeElement( 'select' )
449 );
450 }
451
452 $out->addHTML(
453 "</td>
454 </tr>
455 <tr>
456 <td class='mw-label'>" .
457 Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
458 "</td>
459 <td class='mw-input'>" .
460 Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
461 "</td>
462 </tr>
463 <tr>
464 <td>
465 </td>
466 <td class='mw-input'>" .
467 Xml::checkLabel(
468 $this->msg( 'import-interwiki-history' )->text(),
469 'interwikiHistory',
470 'interwikiHistory',
471 $this->history
472 ) .
473 "</td>
474 </tr>
475 <tr>
476 <td>
477 </td>
478 <td class='mw-input'>" .
479 Xml::checkLabel(
480 $this->msg( 'import-interwiki-templates' )->text(),
481 'interwikiTemplates',
482 'interwikiTemplates',
483 $this->includeTemplates
484 ) .
485 "</td>
486 </tr>
487 $importDepth
488 <tr>
489 <td class='mw-label'>" .
490 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
491 "</td>
492 <td class='mw-input'>" .
493 Xml::input( 'log-comment', 50,
494 ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
495 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
496 "</td>
497 </tr>
498 $mappingSelection
499 <tr>
500 <td>
501 </td>
502 <td class='mw-submit'>" .
503 Xml::submitButton(
504 $this->msg( 'import-interwiki-submit' )->text(),
505 Linker::tooltipAndAccesskeyAttribs( 'import' )
506 ) .
507 "</td>
508 </tr>" .
509 Xml::closeElement( 'table' ) .
510 Xml::closeElement( 'form' ) .
511 Xml::closeElement( 'fieldset' )
512 );
513 }
514 }
515
516 protected function getGroupName() {
517 return 'pagetools';
518 }
519 }
520
521 /**
522 * Reporting callback
523 * @ingroup SpecialPage
524 */
525 class ImportReporter extends ContextSource {
526 private $reason = false;
527 private $mOriginalLogCallback = null;
528 private $mOriginalPageOutCallback = null;
529 private $mLogItemCount = 0;
530
531 /**
532 * @param WikiImporter $importer
533 * @param bool $upload
534 * @param string $interwiki
535 * @param string|bool $reason
536 */
537 function __construct( $importer, $upload, $interwiki, $reason = false ) {
538 $this->mOriginalPageOutCallback =
539 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
540 $this->mOriginalLogCallback =
541 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
542 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
543 $this->mPageCount = 0;
544 $this->mIsUpload = $upload;
545 $this->mInterwiki = $interwiki;
546 $this->reason = $reason;
547 }
548
549 function open() {
550 $this->getOutput()->addHTML( "<ul>\n" );
551 }
552
553 function reportNotice( $msg, array $params ) {
554 $this->getOutput()->addHTML(
555 Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
556 );
557 }
558
559 function reportLogItem( /* ... */ ) {
560 $this->mLogItemCount++;
561 if ( is_callable( $this->mOriginalLogCallback ) ) {
562 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
563 }
564 }
565
566 /**
567 * @param Title $title
568 * @param ForeignTitle $foreignTitle
569 * @param int $revisionCount
570 * @param int $successCount
571 * @param array $pageInfo
572 * @return void
573 */
574 function reportPage( $title, $foreignTitle, $revisionCount,
575 $successCount, $pageInfo ) {
576 $args = func_get_args();
577 call_user_func_array( $this->mOriginalPageOutCallback, $args );
578
579 if ( $title === null ) {
580 # Invalid or non-importable title; a notice is already displayed
581 return;
582 }
583
584 $this->mPageCount++;
585
586 if ( $successCount > 0 ) {
587 $this->getOutput()->addHTML(
588 "<li>" . Linker::linkKnown( $title ) . " " .
589 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
590 "</li>\n"
591 );
592
593 if ( $this->mIsUpload ) {
594 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
595 $successCount )->inContentLanguage()->text();
596 if ( $this->reason ) {
597 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
598 . $this->reason;
599 }
600 $action = 'upload';
601 } else {
602 $interwiki = '[[:' . $this->mInterwiki . ':' .
603 $foreignTitle->getFullText() . ']]';
604 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
605 $successCount )->params( $interwiki )->inContentLanguage()->text();
606 if ( $this->reason ) {
607 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
608 . $this->reason;
609 }
610 $action = 'interwiki';
611 }
612
613 $logEntry = new ManualLogEntry( 'import', $action );
614 $logEntry->setTarget( $title );
615 $logEntry->setComment( $detail );
616 $logEntry->setPerformer( $this->getUser() );
617 $logid = $logEntry->insert();
618 $logEntry->publish( $logid );
619
620 $comment = $detail; // quick
621 $dbw = wfGetDB( DB_MASTER );
622 $latest = $title->getLatestRevID();
623 $nullRevision = Revision::newNullRevision(
624 $dbw,
625 $title->getArticleID(),
626 $comment,
627 true,
628 $this->getUser()
629 );
630
631 if ( !is_null( $nullRevision ) ) {
632 $nullRevision->insertOn( $dbw );
633 $page = WikiPage::factory( $title );
634 # Update page record
635 $page->updateRevisionOn( $dbw, $nullRevision );
636 Hooks::run(
637 'NewRevisionFromEditComplete',
638 array( $page, $nullRevision, $latest, $this->getUser() )
639 );
640 }
641 } else {
642 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
643 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
644 }
645 }
646
647 function close() {
648 $out = $this->getOutput();
649 if ( $this->mLogItemCount > 0 ) {
650 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
651 $out->addHTML( Xml::tags( 'li', null, $msg ) );
652 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
653 $out->addHTML( "</ul>\n" );
654
655 return Status::newFatal( 'importnopages' );
656 }
657 $out->addHTML( "</ul>\n" );
658
659 return Status::newGood( $this->mPageCount );
660 }
661 }