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