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 $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 ) {
55 global $wgRequest, $wgUser, $wgOut;
58 $this->outputHeader();
62 $wgOut->readOnlyPage();
66 if( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
67 return $wgOut->permissionRequired( 'import' );
70 # TODO: allow Title::getUserPermissionsErrors() to take an array
71 # FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
72 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
73 $errors = wfMergeErrorArrays(
74 $this->getTitle()->getUserPermissionsErrors(
75 'import', $wgUser, true,
76 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
78 $this->getTitle()->getUserPermissionsErrors(
79 'importupload', $wgUser, true,
80 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
85 $wgOut->showPermissionsErrorPage( $errors );
89 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
96 * Do the actual import
98 private function doImport() {
99 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
101 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
102 $sourceName = $wgRequest->getVal( "source" );
104 $this->logcomment
= $wgRequest->getText( 'log-comment' );
105 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
107 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
108 $source = Status
::newFatal( 'import-token-mismatch' );
109 } elseif ( $sourceName == 'upload' ) {
111 if( $wgUser->isAllowed( 'importupload' ) ) {
112 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
114 return $wgOut->permissionRequired( 'importupload' );
116 } elseif ( $sourceName == "interwiki" ) {
117 if( !$wgUser->isAllowed( 'import' ) ){
118 return $wgOut->permissionRequired( 'import' );
120 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
121 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
122 $source = Status
::newFatal( "import-invalid-interwiki" );
124 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
125 $this->frompage
= $wgRequest->getText( "frompage" );
126 $this->includeTemplates
= $wgRequest->getCheck( 'interwikiTemplates' );
127 $source = ImportStreamSource
::newFromInterwiki(
131 $this->includeTemplates
,
132 $this->pageLinkDepth
);
135 $source = Status
::newFatal( "importunknownsource" );
138 if( !$source->isGood() ) {
139 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
141 $wgOut->addWikiMsg( "importstart" );
143 $importer = new WikiImporter( $source->value
);
144 if( !is_null( $this->namespace ) ) {
145 $importer->setTargetNamespace( $this->namespace );
147 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
152 $importer->doImport();
153 } catch ( MWException
$e ) {
156 $result = $reporter->close();
159 # No source or XML parse error
160 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
161 } elseif( !$result->isGood() ) {
163 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
166 $wgOut->addWikiMsg( 'importsuccess' );
168 $wgOut->addHTML( '<hr />' );
172 private function showForm() {
173 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
175 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
177 if( $wgUser->isAllowed( 'importupload' ) ) {
178 $wgOut->addWikiMsg( "importtext" );
180 Xml
::fieldset( wfMsg( 'import-upload' ) ).
181 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
182 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
183 Html
::hidden( 'action', 'submit' ) .
184 Html
::hidden( 'source', 'upload' ) .
185 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
188 <td class='mw-label'>" .
189 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
191 <td class='mw-input'>" .
192 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
196 <td class='mw-label'>" .
197 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
199 <td class='mw-input'>" .
200 Xml
::input( 'log-comment', 50, '',
201 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
206 <td class='mw-submit'>" .
207 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
210 Xml
::closeElement( 'table' ).
211 Html
::hidden( 'editToken', $wgUser->editToken() ) .
212 Xml
::closeElement( 'form' ) .
213 Xml
::closeElement( 'fieldset' )
216 if( empty( $wgImportSources ) ) {
217 $wgOut->addWikiMsg( 'importnosources' );
221 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
222 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
224 if( $wgExportMaxLinkDepth > 0 ) {
226 <td class='mw-label'>" .
227 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
229 <td class='mw-input'>" .
230 Xml
::input( 'pagelink-depth', 3, 0 ) .
236 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
237 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
238 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
239 Html
::hidden( 'action', 'submit' ) .
240 Html
::hidden( 'source', 'interwiki' ) .
241 Html
::hidden( 'editToken', $wgUser->editToken() ) .
242 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
244 <td class='mw-label'>" .
245 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
247 <td class='mw-input'>" .
248 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
250 foreach( $wgImportSources as $prefix ) {
251 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
252 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
256 Xml
::closeElement( 'select' ) .
257 Xml
::input( 'frompage', 50, $this->frompage
) .
263 <td class='mw-input'>" .
264 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
270 <td class='mw-input'>" .
271 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
276 <td class='mw-label'>" .
277 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
279 <td class='mw-input'>" .
280 Xml
::namespaceSelector( $this->namespace, '' ) .
284 <td class='mw-label'>" .
285 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
287 <td class='mw-input'>" .
288 Xml
::input( 'log-comment', 50, '',
289 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
295 <td class='mw-submit'>" .
296 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
299 Xml
::closeElement( 'table' ).
300 Xml
::closeElement( 'form' ) .
301 Xml
::closeElement( 'fieldset' )
309 * @ingroup SpecialPage
311 class ImportReporter
{
312 private $reason=false;
313 private $mOriginalLogCallback = null;
314 private $mOriginalPageOutCallback = null;
315 private $mLogItemCount = 0;
317 function __construct( $importer, $upload, $interwiki , $reason=false ) {
318 $this->mOriginalPageOutCallback
=
319 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
320 $this->mOriginalLogCallback
=
321 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
322 $this->mPageCount
= 0;
323 $this->mIsUpload
= $upload;
324 $this->mInterwiki
= $interwiki;
325 $this->reason
= $reason;
330 $wgOut->addHTML( "<ul>\n" );
333 function reportLogItem( /* ... */ ) {
334 $this->mLogItemCount++
;
335 if ( is_callable( $this->mOriginalLogCallback
) ) {
336 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
341 * @param Title $title
342 * @param Title $origTitle
343 * @param int $revisionCount
344 * @param $successCount
348 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
349 global $wgOut, $wgUser, $wgLang, $wgContLang;
351 $args = func_get_args();
352 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
354 $skin = $wgUser->getSkin();
358 $localCount = $wgLang->formatNum( $successCount );
359 $contentCount = $wgContLang->formatNum( $successCount );
361 if( $successCount > 0 ) {
362 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
363 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
367 $log = new LogPage( 'import' );
368 if( $this->mIsUpload
) {
369 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
371 if ( $this->reason
) {
372 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
374 $log->addEntry( 'upload', $title, $detail );
376 $interwiki = '[[:' . $this->mInterwiki
. ':' .
377 $origTitle->getPrefixedText() . ']]';
378 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
379 $contentCount, $interwiki );
380 if ( $this->reason
) {
381 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
383 $log->addEntry( 'interwiki', $title, $detail );
386 $comment = $detail; // quick
387 $dbw = wfGetDB( DB_MASTER
);
388 $latest = $title->getLatestRevID();
389 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
390 $nullRevision->insertOn( $dbw );
391 $article = new Article( $title );
393 $article->updateRevisionOn( $dbw, $nullRevision );
394 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
396 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
397 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
402 global $wgOut, $wgLang;
404 if ( $this->mLogItemCount
> 0 ) {
405 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
406 $wgLang->formatNum( $this->mLogItemCount
) );
407 $wgOut->addHTML( Xml
::tags( 'li', null, $msg ) );
408 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
409 $wgOut->addHTML( "</ul>\n" );
410 return Status
::newFatal( 'importnopages' );
412 $wgOut->addHTML( "</ul>\n" );
414 return Status
::newGood( $this->mPageCount
);