3 * Implements Special:Import
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * http://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
{
34 private $interwiki = false;
36 private $rootpage = '';
37 private $frompage = '';
38 private $logcomment= false;
39 private $history = true;
40 private $includeTemplates = false;
41 private $pageLinkDepth;
46 public function __construct() {
47 parent
::__construct( 'Import', 'import' );
48 global $wgImportTargetNamespace;
49 $this->namespace = $wgImportTargetNamespace;
55 function execute( $par ) {
57 $this->outputHeader();
59 $user = $this->getUser();
60 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
61 throw new PermissionsError( 'import' );
64 # @todo Allow Title::getUserPermissionsErrors() to take an array
65 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
66 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
67 $errors = wfMergeErrorArrays(
68 $this->getTitle()->getUserPermissionsErrors(
69 'import', $user, true,
70 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
72 $this->getTitle()->getUserPermissionsErrors(
73 'importupload', $user, true,
74 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
79 throw new PermissionsError( 'import', $errors );
82 $this->checkReadOnly();
84 $request = $this->getRequest();
85 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
92 * Do the actual import
94 private function doImport() {
95 global $wgImportSources, $wgExportMaxLinkDepth;
98 $request = $this->getRequest();
99 $this->namespace = $request->getIntOrNull( 'namespace' );
100 $sourceName = $request->getVal( "source" );
102 $this->logcomment
= $request->getText( 'log-comment' );
103 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $request->getIntOrNull( 'pagelink-depth' );
104 $this->rootpage
= $request->getText( 'rootpage' );
106 $user = $this->getUser();
107 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
108 $source = Status
::newFatal( 'import-token-mismatch' );
109 } elseif ( $sourceName == 'upload' ) {
111 if( $user->isAllowed( 'importupload' ) ) {
112 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
114 throw new PermissionsError( 'importupload' );
116 } elseif ( $sourceName == "interwiki" ) {
117 if( !$user->isAllowed( 'import' ) ){
118 throw new PermissionsError( 'import' );
120 $this->interwiki
= $request->getVal( 'interwiki' );
121 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
122 $source = Status
::newFatal( "import-invalid-interwiki" );
124 $this->history
= $request->getCheck( 'interwikiHistory' );
125 $this->frompage
= $request->getText( "frompage" );
126 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
127 $source = ImportStreamSource
::newFromInterwiki(
131 $this->includeTemplates
,
132 $this->pageLinkDepth
);
135 $source = Status
::newFatal( "importunknownsource" );
138 $out = $this->getOutput();
139 if( !$source->isGood() ) {
140 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
142 $importer = new WikiImporter( $source->value
);
143 if( !is_null( $this->namespace ) ) {
144 $importer->setTargetNamespace( $this->namespace );
146 if( !is_null( $this->rootpage
) ) {
147 $statusRootPage = $importer->setTargetRootPage( $this->rootpage
);
148 if( !$statusRootPage->isGood() ) {
149 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'import-options-wrong', $statusRootPage->getWikiText(), count( $statusRootPage->getErrorsArray() ) ) );
154 $out->addWikiMsg( "importstart" );
156 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
157 $reporter->setContext( $this->getContext() );
162 $importer->doImport();
163 } catch ( MWException
$e ) {
166 $result = $reporter->close();
169 # No source or XML parse error
170 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
171 } elseif( !$result->isGood() ) {
173 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
176 $out->addWikiMsg( 'importsuccess' );
178 $out->addHTML( '<hr />' );
182 private function showForm() {
183 global $wgImportSources, $wgExportMaxLinkDepth;
185 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
186 $user = $this->getUser();
187 $out = $this->getOutput();
189 if( $user->isAllowed( 'importupload' ) ) {
191 Xml
::fieldset( $this->msg( 'import-upload' )->text() ).
192 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
193 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
194 $this->msg( 'importtext' )->parseAsBlock() .
195 Html
::hidden( 'action', 'submit' ) .
196 Html
::hidden( 'source', 'upload' ) .
197 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
200 <td class='mw-label'>" .
201 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
203 <td class='mw-input'>" .
204 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
208 <td class='mw-label'>" .
209 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
211 <td class='mw-input'>" .
212 Xml
::input( 'log-comment', 50, '',
213 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
217 <td class='mw-label'>" .
218 Xml
::label( $this->msg( 'import-interwiki-rootpage' )->text(), 'mw-interwiki-rootpage' ) .
220 <td class='mw-input'>" .
221 Xml
::input( 'rootpage', 50, $this->rootpage
,
222 array( 'id' => 'mw-interwiki-rootpage', 'type' => 'text' ) ) . ' ' .
227 <td class='mw-submit'>" .
228 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
231 Xml
::closeElement( 'table' ).
232 Html
::hidden( 'editToken', $user->getEditToken() ) .
233 Xml
::closeElement( 'form' ) .
234 Xml
::closeElement( 'fieldset' )
237 if( empty( $wgImportSources ) ) {
238 $out->addWikiMsg( 'importnosources' );
242 if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
243 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
245 if( $wgExportMaxLinkDepth > 0 ) {
247 <td class='mw-label'>" .
248 $this->msg( 'export-pagelinks' )->parse() .
250 <td class='mw-input'>" .
251 Xml
::input( 'pagelink-depth', 3, 0 ) .
257 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
258 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
259 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
260 Html
::hidden( 'action', 'submit' ) .
261 Html
::hidden( 'source', 'interwiki' ) .
262 Html
::hidden( 'editToken', $user->getEditToken() ) .
263 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
265 <td class='mw-label'>" .
266 Xml
::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
268 <td class='mw-input'>" .
269 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
271 foreach( $wgImportSources as $prefix ) {
272 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
273 $out->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
277 Xml
::closeElement( 'select' ) .
278 Xml
::input( 'frompage', 50, $this->frompage
) .
284 <td class='mw-input'>" .
285 Xml
::checkLabel( $this->msg( 'import-interwiki-history' )->text(), 'interwikiHistory', 'interwikiHistory', $this->history
) .
291 <td class='mw-input'>" .
292 Xml
::checkLabel( $this->msg( 'import-interwiki-templates' )->text(), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
297 <td class='mw-label'>" .
298 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
300 <td class='mw-input'>" .
301 Html
::namespaceSelector(
303 'selected' => $this->namespace,
306 'name' => 'namespace',
308 'class' => 'namespaceselector',
314 <td class='mw-label'>" .
315 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
317 <td class='mw-input'>" .
318 Xml
::input( 'log-comment', 50, '',
319 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
323 <td class='mw-label'>" .
324 Xml
::label( $this->msg( 'import-interwiki-rootpage' )->text(), 'mw-interwiki-rootpage' ) .
326 <td class='mw-input'>" .
327 Xml
::input( 'rootpage', 50, $this->rootpage
,
328 array( 'id' => 'mw-interwiki-rootpage', 'type' => 'text' ) ) . ' ' .
334 <td class='mw-submit'>" .
335 Xml
::submitButton( $this->msg( 'import-interwiki-submit' )->text(), Linker
::tooltipAndAccesskeyAttribs( 'import' ) ) .
338 Xml
::closeElement( 'table' ).
339 Xml
::closeElement( 'form' ) .
340 Xml
::closeElement( 'fieldset' )
348 * @ingroup SpecialPage
350 class ImportReporter
extends ContextSource
{
351 private $reason=false;
352 private $mOriginalLogCallback = null;
353 private $mOriginalPageOutCallback = null;
354 private $mLogItemCount = 0;
356 function __construct( $importer, $upload, $interwiki , $reason=false ) {
357 $this->mOriginalPageOutCallback
=
358 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
359 $this->mOriginalLogCallback
=
360 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
361 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
362 $this->mPageCount
= 0;
363 $this->mIsUpload
= $upload;
364 $this->mInterwiki
= $interwiki;
365 $this->reason
= $reason;
369 $this->getOutput()->addHTML( "<ul>\n" );
372 function reportNotice( $msg, array $params ) {
373 $this->getOutput()->addHTML( Html
::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
376 function reportLogItem( /* ... */ ) {
377 $this->mLogItemCount++
;
378 if ( is_callable( $this->mOriginalLogCallback
) ) {
379 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
384 * @param Title $title
385 * @param Title $origTitle
386 * @param int $revisionCount
387 * @param $successCount
391 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
392 $args = func_get_args();
393 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
395 if ( $title === null ) {
396 # Invalid or non-importable title; a notice is already displayed
402 if( $successCount > 0 ) {
403 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
404 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
408 $log = new LogPage( 'import' );
409 if( $this->mIsUpload
) {
410 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
411 $successCount )->inContentLanguage()->text();
412 if ( $this->reason
) {
413 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason
;
415 $log->addEntry( 'upload', $title, $detail );
417 $interwiki = '[[:' . $this->mInterwiki
. ':' .
418 $origTitle->getPrefixedText() . ']]';
419 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
420 $successCount )->params( $interwiki )->inContentLanguage()->text();
421 if ( $this->reason
) {
422 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason
;
424 $log->addEntry( 'interwiki', $title, $detail );
427 $comment = $detail; // quick
428 $dbw = wfGetDB( DB_MASTER
);
429 $latest = $title->getLatestRevID();
430 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleID(), $comment, true );
431 if (!is_null($nullRevision)) {
432 $nullRevision->insertOn( $dbw );
433 $page = WikiPage
::factory( $title );
435 $page->updateRevisionOn( $dbw, $nullRevision );
436 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
439 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
440 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
445 $out = $this->getOutput();
446 if ( $this->mLogItemCount
> 0 ) {
447 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
448 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
449 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
450 $out->addHTML( "</ul>\n" );
451 return Status
::newFatal( 'importnopages' );
453 $out->addHTML( "</ul>\n" );
455 return Status
::newGood( $this->mPageCount
);