Implement extension registration from an extension.json file
[mediawiki.git] / includes / specials / SpecialImport.php
blob9e9f8c083e2fc43a45a9b23329f399dc439118a0
1 <?php
2 /**
3 * Implements Special:Import
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup SpecialPage
27 /**
28 * MediaWiki page data importer
30 * @ingroup SpecialPage
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;
45 /**
46 * Constructor
48 public function __construct() {
49 parent::__construct( 'Import', 'import' );
52 /**
53 * Execute
54 * @param string|null $par
55 * @throws PermissionsError
56 * @throws ReadOnlyError
58 function execute( $par ) {
59 $this->setHeaders();
60 $this->outputHeader();
62 $this->namespace = $this->getConfig()->get( 'ImportTargetNamespace' );
64 $this->getOutput()->addModules( 'mediawiki.special.import' );
66 $user = $this->getUser();
67 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
68 throw new PermissionsError( 'import' );
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' )
79 $this->getPageTitle()->getUserPermissionsErrors(
80 'importupload', $user, true,
81 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
85 if ( $errors ) {
86 throw new PermissionsError( 'import', $errors );
89 $this->checkReadOnly();
91 $request = $this->getRequest();
92 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
93 $this->doImport();
95 $this->showForm();
98 /**
99 * Do the actual import
101 private function doImport() {
102 $isUpload = false;
103 $request = $this->getRequest();
104 $this->namespace = $request->getIntOrNull( 'namespace' );
105 $this->sourceName = $request->getVal( "source" );
107 $this->logcomment = $request->getText( 'log-comment' );
108 $this->pageLinkDepth = $this->getConfig()->get( 'ExportMaxLinkDepth' ) == 0
110 : $request->getIntOrNull( 'pagelink-depth' );
111 $this->rootpage = $request->getText( 'rootpage' );
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' );
123 } elseif ( $this->sourceName == "interwiki" ) {
124 if ( !$user->isAllowed( 'import' ) ) {
125 throw new PermissionsError( 'import' );
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' );
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 );
152 } else {
153 $source = Status::newFatal( "importunknownsource" );
156 $out = $this->getOutput();
157 if ( !$source->isGood() ) {
158 $out->wrapWikiMsg(
159 "<p class=\"error\">\n$1\n</p>",
160 array( 'importfailed', $source->getWikiText() )
162 } else {
163 $importer = new WikiImporter( $source->value, $this->getConfig() );
164 if ( !is_null( $this->namespace ) ) {
165 $importer->setTargetNamespace( $this->namespace );
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() )
179 return;
183 $out->addWikiMsg( "importstart" );
185 $reporter = new ImportReporter(
186 $importer,
187 $isUpload,
188 $this->fullInterwikiPrefix,
189 $this->logcomment
191 $reporter->setContext( $this->getContext() );
192 $exception = false;
194 $reporter->open();
195 try {
196 $importer->doImport();
197 } catch ( MWException $e ) {
198 $exception = $e;
200 $result = $reporter->close();
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() )
208 } elseif ( !$result->isGood() ) {
209 # Zero revisions
210 $out->wrapWikiMsg(
211 "<p class=\"error\">\n$1\n</p>",
212 array( 'importfailed', $result->getWikiText() )
214 } else {
215 # Success!
216 $out->addWikiMsg( 'importsuccess' );
218 $out->addHTML( '<hr />' );
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' );
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'
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'
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' )
285 } else {
286 if ( empty( $importSources ) ) {
287 $out->addWikiMsg( 'importnosources' );
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>";
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'
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' )
331 $needSubprojectField = false;
332 foreach ( $importSources as $key => $value ) {
333 if ( is_int( $key ) ) {
334 $key = $value;
335 } elseif ( $value !== $key ) {
336 $needSubprojectField = true;
339 $attribs = array(
340 'value' => $key,
342 if ( is_array( $value ) ) {
343 $attribs['data-subprojects'] = implode( ' ', $value );
345 if ( $this->interwiki === $key ) {
346 $attribs['selected'] = 'selected';
348 $out->addHTML( Html::element( 'option', $attribs, $key ) );
351 $out->addHTML(
352 Xml::closeElement( 'select' )
355 if ( $needSubprojectField ) {
356 $out->addHTML(
357 Xml::openElement(
358 'select',
359 array( 'name' => 'subproject', 'id' => 'subproject' )
363 $subprojectsToAdd = array();
364 foreach ( $importSources as $key => $value ) {
365 if ( is_array( $value ) ) {
366 $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
369 $subprojectsToAdd = array_unique( $subprojectsToAdd );
370 sort( $subprojectsToAdd );
371 foreach ( $subprojectsToAdd as $subproject ) {
372 $out->addHTML( Xml::option( $subproject, $subproject, $this->subproject === $subproject ) );
375 $out->addHTML(
376 Xml::closeElement( 'select' )
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
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
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',
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'
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' )
463 "</td>
464 </tr>" .
465 Xml::closeElement( 'table' ) .
466 Xml::closeElement( 'form' ) .
467 Xml::closeElement( 'fieldset' )
472 protected function getGroupName() {
473 return 'pagetools';
478 * Reporting callback
479 * @ingroup SpecialPage
481 class ImportReporter extends ContextSource {
482 private $reason = false;
483 private $mOriginalLogCallback = null;
484 private $mOriginalPageOutCallback = null;
485 private $mLogItemCount = 0;
488 * @param WikiImporter $importer
489 * @param bool $upload
490 * @param string $interwiki
491 * @param string|bool $reason
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;
505 function open() {
506 $this->getOutput()->addHTML( "<ul>\n" );
509 function reportNotice( $msg, array $params ) {
510 $this->getOutput()->addHTML(
511 Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
515 function reportLogItem( /* ... */ ) {
516 $this->mLogItemCount++;
517 if ( is_callable( $this->mOriginalLogCallback ) ) {
518 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
523 * @param Title $title
524 * @param ForeignTitle $foreignTitle
525 * @param int $revisionCount
526 * @param int $successCount
527 * @param array $pageInfo
528 * @return void
530 function reportPage( $title, $foreignTitle, $revisionCount,
531 $successCount, $pageInfo ) {
532 $args = func_get_args();
533 call_user_func_array( $this->mOriginalPageOutCallback, $args );
535 if ( $title === null ) {
536 # Invalid or non-importable title; a notice is already displayed
537 return;
540 $this->mPageCount++;
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"
549 $log = new LogPage( 'import' );
550 if ( $this->mIsUpload ) {
551 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
552 $successCount )->inContentLanguage()->text();
553 if ( $this->reason ) {
554 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
555 . $this->reason;
557 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
558 } else {
559 $interwiki = '[[:' . $this->mInterwiki . ':' .
560 $foreignTitle->getFullText() . ']]';
561 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
562 $successCount )->params( $interwiki )->inContentLanguage()->text();
563 if ( $this->reason ) {
564 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
565 . $this->reason;
567 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
570 $comment = $detail; // quick
571 $dbw = wfGetDB( DB_MASTER );
572 $latest = $title->getLatestRevID();
573 $nullRevision = Revision::newNullRevision(
574 $dbw,
575 $title->getArticleID(),
576 $comment,
577 true,
578 $this->getUser()
581 if ( !is_null( $nullRevision ) ) {
582 $nullRevision->insertOn( $dbw );
583 $page = WikiPage::factory( $title );
584 # Update page record
585 $page->updateRevisionOn( $dbw, $nullRevision );
586 Hooks::run(
587 'NewRevisionFromEditComplete',
588 array( $page, $nullRevision, $latest, $this->getUser() )
591 } else {
592 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
593 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
597 function close() {
598 $out = $this->getOutput();
599 if ( $this->mLogItemCount > 0 ) {
600 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
601 $out->addHTML( Xml::tags( 'li', null, $msg ) );
602 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
603 $out->addHTML( "</ul>\n" );
605 return Status::newFatal( 'importnopages' );
607 $out->addHTML( "</ul>\n" );
609 return Status::newGood( $this->mPageCount );