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
24 * @ingroup SpecialPage
28 * MediaWiki page data importer
30 * @ingroup SpecialPage
32 class SpecialImport
extends SpecialPage
{
33 private $interwiki = false;
35 private $rootpage = '';
36 private $frompage = '';
37 private $logcomment = false;
38 private $history = true;
39 private $includeTemplates = false;
40 private $pageLinkDepth;
45 public function __construct() {
46 parent
::__construct( 'Import', 'import' );
47 global $wgImportTargetNamespace;
48 $this->namespace = $wgImportTargetNamespace;
54 function execute( $par ) {
56 $this->outputHeader();
58 $user = $this->getUser();
59 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
60 throw new PermissionsError( 'import' );
63 # @todo Allow Title::getUserPermissionsErrors() to take an array
64 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
65 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
66 $errors = wfMergeErrorArrays(
67 $this->getPageTitle()->getUserPermissionsErrors(
68 'import', $user, true,
69 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
71 $this->getPageTitle()->getUserPermissionsErrors(
72 'importupload', $user, true,
73 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
78 throw new PermissionsError( 'import', $errors );
81 $this->checkReadOnly();
83 $request = $this->getRequest();
84 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
91 * Do the actual import
93 private function doImport() {
94 global $wgImportSources, $wgExportMaxLinkDepth;
97 $request = $this->getRequest();
98 $this->namespace = $request->getIntOrNull( 'namespace' );
99 $sourceName = $request->getVal( "source" );
101 $this->logcomment
= $request->getText( 'log-comment' );
102 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $request->getIntOrNull( 'pagelink-depth' );
103 $this->rootpage
= $request->getText( 'rootpage' );
105 $user = $this->getUser();
106 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
107 $source = Status
::newFatal( 'import-token-mismatch' );
108 } elseif ( $sourceName == 'upload' ) {
110 if ( $user->isAllowed( 'importupload' ) ) {
111 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
113 throw new PermissionsError( 'importupload' );
115 } elseif ( $sourceName == "interwiki" ) {
116 if ( !$user->isAllowed( 'import' ) ) {
117 throw new PermissionsError( 'import' );
119 $this->interwiki
= $request->getVal( 'interwiki' );
120 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
121 $source = Status
::newFatal( "import-invalid-interwiki" );
123 $this->history
= $request->getCheck( 'interwikiHistory' );
124 $this->frompage
= $request->getText( "frompage" );
125 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
126 $source = ImportStreamSource
::newFromInterwiki(
130 $this->includeTemplates
,
131 $this->pageLinkDepth
);
134 $source = Status
::newFatal( "importunknownsource" );
137 $out = $this->getOutput();
138 if ( !$source->isGood() ) {
140 "<p class=\"error\">\n$1\n</p>",
141 array( 'importfailed', $source->getWikiText() )
144 $importer = new WikiImporter( $source->value
);
145 if ( !is_null( $this->namespace ) ) {
146 $importer->setTargetNamespace( $this->namespace );
148 if ( !is_null( $this->rootpage
) ) {
149 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
150 if ( !$statusRootPage->isGood() ) {
152 "<p class=\"error\">\n$1\n</p>",
154 'import-options-wrong',
155 $statusRootPage->getWikiText(),
156 count( $statusRootPage->getErrorsArray() )
164 $out->addWikiMsg( "importstart" );
166 $reporter = new ImportReporter(
172 $reporter->setContext( $this->getContext() );
177 $importer->doImport();
178 } catch ( MWException
$e ) {
181 $result = $reporter->close();
184 # No source or XML parse error
186 "<p class=\"error\">\n$1\n</p>",
187 array( 'importfailed', $exception->getMessage() )
189 } elseif ( !$result->isGood() ) {
192 "<p class=\"error\">\n$1\n</p>",
193 array( 'importfailed', $result->getWikiText() )
197 $out->addWikiMsg( 'importsuccess' );
199 $out->addHTML( '<hr />' );
203 private function showForm() {
204 global $wgImportSources, $wgExportMaxLinkDepth;
206 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
207 $user = $this->getUser();
208 $out = $this->getOutput();
210 if ( $user->isAllowed( 'importupload' ) ) {
212 Xml
::fieldset( $this->msg( 'import-upload' )->text() ) .
216 'enctype' => 'multipart/form-data',
219 'id' => 'mw-import-upload-form'
222 $this->msg( 'importtext' )->parseAsBlock() .
223 Html
::hidden( 'action', 'submit' ) .
224 Html
::hidden( 'source', 'upload' ) .
225 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
227 <td class='mw-label'>" .
228 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
230 <td class='mw-input'>" .
231 Html
::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
235 <td class='mw-label'>" .
236 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
238 <td class='mw-input'>" .
239 Xml
::input( 'log-comment', 50, '',
240 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
244 <td class='mw-label'>" .
245 Xml
::label( $this->msg( 'import-interwiki-rootpage' )->text(), 'mw-interwiki-rootpage-upload' ) .
247 <td class='mw-input'>" .
248 Xml
::input( 'rootpage', 50, $this->rootpage
,
249 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
254 <td class='mw-submit'>" .
255 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
258 Xml
::closeElement( 'table' ) .
259 Html
::hidden( 'editToken', $user->getEditToken() ) .
260 Xml
::closeElement( 'form' ) .
261 Xml
::closeElement( 'fieldset' )
264 if ( empty( $wgImportSources ) ) {
265 $out->addWikiMsg( 'importnosources' );
269 if ( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
270 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
272 if ( $wgExportMaxLinkDepth > 0 ) {
274 <td class='mw-label'>" .
275 $this->msg( 'export-pagelinks' )->parse() .
277 <td class='mw-input'>" .
278 Xml
::input( 'pagelink-depth', 3, 0 ) .
284 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
290 'id' => 'mw-import-interwiki-form'
293 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
294 Html
::hidden( 'action', 'submit' ) .
295 Html
::hidden( 'source', 'interwiki' ) .
296 Html
::hidden( 'editToken', $user->getEditToken() ) .
297 Xml
::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
299 <td class='mw-label'>" .
300 Xml
::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
302 <td class='mw-input'>" .
305 array( 'name' => 'interwiki', 'id' => 'interwiki' )
309 foreach ( $wgImportSources as $prefix ) {
310 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
311 $out->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
315 Xml
::closeElement( 'select' ) .
316 Xml
::input( 'frompage', 50, $this->frompage
, array( 'id' => 'frompage' ) ) .
322 <td class='mw-input'>" .
324 $this->msg( 'import-interwiki-history' )->text(),
334 <td class='mw-input'>" .
336 $this->msg( 'import-interwiki-templates' )->text(),
337 'interwikiTemplates',
338 'interwikiTemplates',
339 $this->includeTemplates
345 <td class='mw-label'>" .
346 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
348 <td class='mw-input'>" .
349 Html
::namespaceSelector(
351 'selected' => $this->namespace,
354 'name' => 'namespace',
356 'class' => 'namespaceselector',
362 <td class='mw-label'>" .
363 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
365 <td class='mw-input'>" .
366 Xml
::input( 'log-comment', 50, '',
367 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
371 <td class='mw-label'>" .
373 $this->msg( 'import-interwiki-rootpage' )->text(),
374 'mw-interwiki-rootpage-interwiki'
377 <td class='mw-input'>" .
378 Xml
::input( 'rootpage', 50, $this->rootpage
,
379 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
385 <td class='mw-submit'>" .
387 $this->msg( 'import-interwiki-submit' )->text(),
388 Linker
::tooltipAndAccesskeyAttribs( 'import' )
392 Xml
::closeElement( 'table' ) .
393 Xml
::closeElement( 'form' ) .
394 Xml
::closeElement( 'fieldset' )
399 protected function getGroupName() {
406 * @ingroup SpecialPage
408 class ImportReporter
extends ContextSource
{
409 private $reason = false;
410 private $mOriginalLogCallback = null;
411 private $mOriginalPageOutCallback = null;
412 private $mLogItemCount = 0;
415 * @param WikiImporter $importer
416 * @param bool $upload
417 * @param string $interwiki
418 * @param string|bool $reason
420 function __construct( $importer, $upload, $interwiki, $reason = false ) {
421 $this->mOriginalPageOutCallback
=
422 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
423 $this->mOriginalLogCallback
=
424 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
425 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
426 $this->mPageCount
= 0;
427 $this->mIsUpload
= $upload;
428 $this->mInterwiki
= $interwiki;
429 $this->reason
= $reason;
433 $this->getOutput()->addHTML( "<ul>\n" );
436 function reportNotice( $msg, array $params ) {
437 $this->getOutput()->addHTML( Html
::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
440 function reportLogItem( /* ... */ ) {
441 $this->mLogItemCount++
;
442 if ( is_callable( $this->mOriginalLogCallback
) ) {
443 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
448 * @param Title $title
449 * @param Title $origTitle
450 * @param int $revisionCount
451 * @param int $successCount
452 * @param array $pageInfo
455 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
456 $args = func_get_args();
457 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
459 if ( $title === null ) {
460 # Invalid or non-importable title; a notice is already displayed
466 if ( $successCount > 0 ) {
467 $this->getOutput()->addHTML(
468 "<li>" . Linker
::linkKnown( $title ) . " " .
469 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
473 $log = new LogPage( 'import' );
474 if ( $this->mIsUpload
) {
475 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
476 $successCount )->inContentLanguage()->text();
477 if ( $this->reason
) {
478 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason
;
480 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
482 $interwiki = '[[:' . $this->mInterwiki
. ':' .
483 $origTitle->getPrefixedText() . ']]';
484 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
485 $successCount )->params( $interwiki )->inContentLanguage()->text();
486 if ( $this->reason
) {
487 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason
;
489 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
492 $comment = $detail; // quick
493 $dbw = wfGetDB( DB_MASTER
);
494 $latest = $title->getLatestRevID();
495 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleID(), $comment, true );
496 if ( !is_null( $nullRevision ) ) {
497 $nullRevision->insertOn( $dbw );
498 $page = WikiPage
::factory( $title );
500 $page->updateRevisionOn( $dbw, $nullRevision );
501 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
504 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
505 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
510 $out = $this->getOutput();
511 if ( $this->mLogItemCount
> 0 ) {
512 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
513 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
514 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
515 $out->addHTML( "</ul>\n" );
517 return Status
::newFatal( 'importnopages' );
519 $out->addHTML( "</ul>\n" );
521 return Status
::newGood( $this->mPageCount
);