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