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;
44 public function __construct() {
45 parent
::__construct( 'Import', 'import' );
46 global $wgImportTargetNamespace;
47 $this->namespace = $wgImportTargetNamespace;
53 function execute( $par ) {
57 $this->outputHeader();
61 $wgOut->readOnlyPage();
65 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
72 * Do the actual import
74 private function doImport() {
75 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
77 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
78 $sourceName = $wgRequest->getVal( "source" );
80 $this->logcomment
= $wgRequest->getText( 'log-comment' );
81 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
83 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
84 $source = Status
::newFatal( 'import-token-mismatch' );
85 } elseif ( $sourceName == 'upload' ) {
87 if( $wgUser->isAllowed( 'importupload' ) ) {
88 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
90 return $wgOut->permissionRequired( 'importupload' );
92 } elseif ( $sourceName == "interwiki" ) {
93 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
94 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
95 $source = Status
::newFatal( "import-invalid-interwiki" );
97 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
98 $this->frompage
= $wgRequest->getText( "frompage" );
99 $this->includeTemplates
= $wgRequest->getCheck( 'interwikiTemplates' );
100 $source = ImportStreamSource
::newFromInterwiki(
104 $this->includeTemplates
,
105 $this->pageLinkDepth
);
108 $source = Status
::newFatal( "importunknownsource" );
111 if( !$source->isGood() ) {
112 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
114 $wgOut->addWikiMsg( "importstart" );
116 $importer = new WikiImporter( $source->value
);
117 if( !is_null( $this->namespace ) ) {
118 $importer->setTargetNamespace( $this->namespace );
120 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
125 $importer->doImport();
126 } catch ( MWException
$e ) {
129 $result = $reporter->close();
132 # No source or XML parse error
133 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
134 } elseif( !$result->isGood() ) {
136 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
139 $wgOut->addWikiMsg( 'importsuccess' );
141 $wgOut->addWikiText( '<hr />' );
145 private function showForm() {
146 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
147 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
148 return $wgOut->permissionRequired( 'import' );
150 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
152 if( $wgUser->isAllowed( 'importupload' ) ) {
153 $wgOut->addWikiMsg( "importtext" );
155 Xml
::fieldset( wfMsg( 'import-upload' ) ).
156 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
157 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
158 Html
::hidden( 'action', 'submit' ) .
159 Html
::hidden( 'source', 'upload' ) .
160 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
163 <td class='mw-label'>" .
164 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
166 <td class='mw-input'>" .
167 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
171 <td class='mw-label'>" .
172 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
174 <td class='mw-input'>" .
175 Xml
::input( 'log-comment', 50, '',
176 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
181 <td class='mw-submit'>" .
182 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
185 Xml
::closeElement( 'table' ).
186 Html
::hidden( 'editToken', $wgUser->editToken() ) .
187 Xml
::closeElement( 'form' ) .
188 Xml
::closeElement( 'fieldset' )
191 if( empty( $wgImportSources ) ) {
192 $wgOut->addWikiMsg( 'importnosources' );
196 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
197 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
199 if( $wgExportMaxLinkDepth > 0 ) {
201 <td class='mw-label'>" .
202 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
204 <td class='mw-input'>" .
205 Xml
::input( 'pagelink-depth', 3, 0 ) .
211 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
212 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
213 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
214 Html
::hidden( 'action', 'submit' ) .
215 Html
::hidden( 'source', 'interwiki' ) .
216 Html
::hidden( 'editToken', $wgUser->editToken() ) .
217 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
219 <td class='mw-label'>" .
220 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
222 <td class='mw-input'>" .
223 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
225 foreach( $wgImportSources as $prefix ) {
226 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
227 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
231 Xml
::closeElement( 'select' ) .
232 Xml
::input( 'frompage', 50, $this->frompage
) .
238 <td class='mw-input'>" .
239 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
245 <td class='mw-input'>" .
246 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
251 <td class='mw-label'>" .
252 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
254 <td class='mw-input'>" .
255 Xml
::namespaceSelector( $this->namespace, '' ) .
259 <td class='mw-label'>" .
260 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
262 <td class='mw-input'>" .
263 Xml
::input( 'log-comment', 50, '',
264 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
270 <td class='mw-submit'>" .
271 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
274 Xml
::closeElement( 'table' ).
275 Xml
::closeElement( 'form' ) .
276 Xml
::closeElement( 'fieldset' )
284 * @ingroup SpecialPage
286 class ImportReporter
{
287 private $reason=false;
288 private $mOriginalLogCallback = null;
289 private $mOriginalPageOutCallback = null;
290 private $mLogItemCount = 0;
292 function __construct( $importer, $upload, $interwiki , $reason=false ) {
293 $this->mOriginalPageOutCallback
=
294 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
295 $this->mOriginalLogCallback
=
296 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
297 $this->mPageCount
= 0;
298 $this->mIsUpload
= $upload;
299 $this->mInterwiki
= $interwiki;
300 $this->reason
= $reason;
305 $wgOut->addHTML( "<ul>\n" );
308 function reportLogItem( /* ... */ ) {
309 $this->mLogItemCount++
;
310 if ( is_callable( $this->mOriginalLogCallback
) ) {
311 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
315 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
316 global $wgOut, $wgUser, $wgLang, $wgContLang;
318 $args = func_get_args();
319 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
321 $skin = $wgUser->getSkin();
325 $localCount = $wgLang->formatNum( $successCount );
326 $contentCount = $wgContLang->formatNum( $successCount );
328 if( $successCount > 0 ) {
329 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
330 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
334 $log = new LogPage( 'import' );
335 if( $this->mIsUpload
) {
336 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
338 if ( $this->reason
) {
339 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
341 $log->addEntry( 'upload', $title, $detail );
343 $interwiki = '[[:' . $this->mInterwiki
. ':' .
344 $origTitle->getPrefixedText() . ']]';
345 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
346 $contentCount, $interwiki );
347 if ( $this->reason
) {
348 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
350 $log->addEntry( 'interwiki', $title, $detail );
353 $comment = $detail; // quick
354 $dbw = wfGetDB( DB_MASTER
);
355 $latest = $title->getLatestRevID();
356 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
357 $nullRevision->insertOn( $dbw );
358 $article = new Article( $title );
360 $article->updateRevisionOn( $dbw, $nullRevision );
361 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
363 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
364 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
369 global $wgOut, $wgLang;
371 if ( $this->mLogItemCount
> 0 ) {
372 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
373 $wgLang->formatNum( $this->mLogItemCount
) );
374 $wgOut->addHTML( Xml
::tags( 'li', null, $msg ) );
375 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
376 $wgOut->addHTML( "</ul>\n" );
377 return Status
::newFatal( 'importnopages' );
379 $wgOut->addHTML( "</ul>\n" );
381 return Status
::newGood( $this->mPageCount
);