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();
61 $wgOut->readOnlyPage();
65 if( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
66 return $wgOut->permissionRequired( 'import' );
69 # @todo Allow Title::getUserPermissionsErrors() to take an array
70 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
71 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
72 $errors = wfMergeErrorArrays(
73 $this->getTitle()->getUserPermissionsErrors(
74 'import', $wgUser, true,
75 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
77 $this->getTitle()->getUserPermissionsErrors(
78 'importupload', $wgUser, true,
79 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
84 $wgOut->showPermissionsErrorPage( $errors );
88 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
95 * Do the actual import
97 private function doImport() {
98 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
100 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
101 $sourceName = $wgRequest->getVal( "source" );
103 $this->logcomment
= $wgRequest->getText( 'log-comment' );
104 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
106 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
107 $source = Status
::newFatal( 'import-token-mismatch' );
108 } elseif ( $sourceName == 'upload' ) {
110 if( $wgUser->isAllowed( 'importupload' ) ) {
111 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
113 return $wgOut->permissionRequired( 'importupload' );
115 } elseif ( $sourceName == "interwiki" ) {
116 if( !$wgUser->isAllowed( 'import' ) ){
117 return $wgOut->permissionRequired( 'import' );
119 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
120 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
121 $source = Status
::newFatal( "import-invalid-interwiki" );
123 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
124 $this->frompage
= $wgRequest->getText( "frompage" );
125 $this->includeTemplates
= $wgRequest->getCheck( 'interwikiTemplates' );
126 $source = ImportStreamSource
::newFromInterwiki(
130 $this->includeTemplates
,
131 $this->pageLinkDepth
);
134 $source = Status
::newFatal( "importunknownsource" );
137 if( !$source->isGood() ) {
138 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
140 $wgOut->addWikiMsg( "importstart" );
142 $importer = new WikiImporter( $source->value
);
143 if( !is_null( $this->namespace ) ) {
144 $importer->setTargetNamespace( $this->namespace );
146 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
151 $importer->doImport();
152 } catch ( MWException
$e ) {
155 $result = $reporter->close();
158 # No source or XML parse error
159 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
160 } elseif( !$result->isGood() ) {
162 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
165 $wgOut->addWikiMsg( 'importsuccess' );
167 $wgOut->addHTML( '<hr />' );
171 private function showForm() {
172 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
174 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
176 if( $wgUser->isAllowed( 'importupload' ) ) {
177 $wgOut->addWikiMsg( "importtext" );
179 Xml
::fieldset( wfMsg( 'import-upload' ) ).
180 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
181 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
182 Html
::hidden( 'action', 'submit' ) .
183 Html
::hidden( 'source', 'upload' ) .
184 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
187 <td class='mw-label'>" .
188 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
190 <td class='mw-input'>" .
191 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
195 <td class='mw-label'>" .
196 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
198 <td class='mw-input'>" .
199 Xml
::input( 'log-comment', 50, '',
200 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
205 <td class='mw-submit'>" .
206 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
209 Xml
::closeElement( 'table' ).
210 Html
::hidden( 'editToken', $wgUser->editToken() ) .
211 Xml
::closeElement( 'form' ) .
212 Xml
::closeElement( 'fieldset' )
215 if( empty( $wgImportSources ) ) {
216 $wgOut->addWikiMsg( 'importnosources' );
220 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
221 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
223 if( $wgExportMaxLinkDepth > 0 ) {
225 <td class='mw-label'>" .
226 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
228 <td class='mw-input'>" .
229 Xml
::input( 'pagelink-depth', 3, 0 ) .
235 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
236 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
237 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
238 Html
::hidden( 'action', 'submit' ) .
239 Html
::hidden( 'source', 'interwiki' ) .
240 Html
::hidden( 'editToken', $wgUser->editToken() ) .
241 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
243 <td class='mw-label'>" .
244 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
246 <td class='mw-input'>" .
247 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
249 foreach( $wgImportSources as $prefix ) {
250 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
251 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
255 Xml
::closeElement( 'select' ) .
256 Xml
::input( 'frompage', 50, $this->frompage
) .
262 <td class='mw-input'>" .
263 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
269 <td class='mw-input'>" .
270 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
275 <td class='mw-label'>" .
276 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
278 <td class='mw-input'>" .
279 Xml
::namespaceSelector( $this->namespace, '' ) .
283 <td class='mw-label'>" .
284 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
286 <td class='mw-input'>" .
287 Xml
::input( 'log-comment', 50, '',
288 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
294 <td class='mw-submit'>" .
295 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
298 Xml
::closeElement( 'table' ).
299 Xml
::closeElement( 'form' ) .
300 Xml
::closeElement( 'fieldset' )
308 * @ingroup SpecialPage
310 class ImportReporter
{
311 private $reason=false;
312 private $mOriginalLogCallback = null;
313 private $mOriginalPageOutCallback = null;
314 private $mLogItemCount = 0;
316 function __construct( $importer, $upload, $interwiki , $reason=false ) {
317 $this->mOriginalPageOutCallback
=
318 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
319 $this->mOriginalLogCallback
=
320 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
321 $this->mPageCount
= 0;
322 $this->mIsUpload
= $upload;
323 $this->mInterwiki
= $interwiki;
324 $this->reason
= $reason;
329 $wgOut->addHTML( "<ul>\n" );
332 function reportLogItem( /* ... */ ) {
333 $this->mLogItemCount++
;
334 if ( is_callable( $this->mOriginalLogCallback
) ) {
335 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
340 * @param Title $title
341 * @param Title $origTitle
342 * @param int $revisionCount
343 * @param $successCount
347 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
348 global $wgOut, $wgUser, $wgLang, $wgContLang;
350 $args = func_get_args();
351 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
353 $skin = $wgUser->getSkin();
357 $localCount = $wgLang->formatNum( $successCount );
358 $contentCount = $wgContLang->formatNum( $successCount );
360 if( $successCount > 0 ) {
361 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
362 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
366 $log = new LogPage( 'import' );
367 if( $this->mIsUpload
) {
368 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
370 if ( $this->reason
) {
371 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
373 $log->addEntry( 'upload', $title, $detail );
375 $interwiki = '[[:' . $this->mInterwiki
. ':' .
376 $origTitle->getPrefixedText() . ']]';
377 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
378 $contentCount, $interwiki );
379 if ( $this->reason
) {
380 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
382 $log->addEntry( 'interwiki', $title, $detail );
385 $comment = $detail; // quick
386 $dbw = wfGetDB( DB_MASTER
);
387 $latest = $title->getLatestRevID();
388 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
389 $nullRevision->insertOn( $dbw );
390 $article = new Article( $title );
392 $article->updateRevisionOn( $dbw, $nullRevision );
393 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
395 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
396 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
401 global $wgOut, $wgLang;
403 if ( $this->mLogItemCount
> 0 ) {
404 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
405 $wgLang->formatNum( $this->mLogItemCount
) );
406 $wgOut->addHTML( Xml
::tags( 'li', null, $msg ) );
407 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
408 $wgOut->addHTML( "</ul>\n" );
409 return Status
::newFatal( 'importnopages' );
411 $wgOut->addHTML( "</ul>\n" );
413 return Status
::newGood( $this->mPageCount
);