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 = new WikiErrorMsg( '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 = new WikiErrorMsg( "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 = new WikiErrorMsg( "importunknownsource" );
111 if( WikiError
::isError( $source ) ) {
112 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getMessage() ) );
114 $wgOut->addWikiMsg( "importstart" );
116 $importer = new WikiImporter( $source );
117 if( !is_null( $this->namespace ) ) {
118 $importer->setTargetNamespace( $this->namespace );
120 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
123 $result = $importer->doImport();
124 $resultCount = $reporter->close();
126 if( WikiError
::isError( $result ) ) {
127 # No source or XML parse error
128 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getMessage() ) );
129 } elseif( WikiError
::isError( $resultCount ) ) {
131 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $resultCount->getMessage() ) );
134 $wgOut->addWikiMsg( 'importsuccess' );
136 $wgOut->addWikiText( '<hr />' );
140 private function showForm() {
141 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
142 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
143 return $wgOut->permissionRequired( 'import' );
145 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
147 if( $wgUser->isAllowed( 'importupload' ) ) {
148 $wgOut->addWikiMsg( "importtext" );
150 Xml
::fieldset( wfMsg( 'import-upload' ) ).
151 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
152 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
153 Xml
::hidden( 'action', 'submit' ) .
154 Xml
::hidden( 'source', 'upload' ) .
155 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
158 <td class='mw-label'>" .
159 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
161 <td class='mw-input'>" .
162 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
166 <td class='mw-label'>" .
167 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
169 <td class='mw-input'>" .
170 Xml
::input( 'log-comment', 50, '',
171 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
176 <td class='mw-submit'>" .
177 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
180 Xml
::closeElement( 'table' ).
181 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
182 Xml
::closeElement( 'form' ) .
183 Xml
::closeElement( 'fieldset' )
186 if( empty( $wgImportSources ) ) {
187 $wgOut->addWikiMsg( 'importnosources' );
191 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
192 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
194 if( $wgExportMaxLinkDepth > 0 ) {
196 <td class='mw-label'>" .
197 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
199 <td class='mw-input'>" .
200 Xml
::input( 'pagelink-depth', 3, 0 ) .
206 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
207 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
208 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
209 Xml
::hidden( 'action', 'submit' ) .
210 Xml
::hidden( 'source', 'interwiki' ) .
211 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
212 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
214 <td class='mw-label'>" .
215 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
217 <td class='mw-input'>" .
218 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
220 foreach( $wgImportSources as $prefix ) {
221 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
222 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
226 Xml
::closeElement( 'select' ) .
227 Xml
::input( 'frompage', 50, $this->frompage
) .
233 <td class='mw-input'>" .
234 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
240 <td class='mw-input'>" .
241 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
246 <td class='mw-label'>" .
247 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
249 <td class='mw-input'>" .
250 Xml
::namespaceSelector( $this->namespace, '' ) .
254 <td class='mw-label'>" .
255 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
257 <td class='mw-input'>" .
258 Xml
::input( 'log-comment', 50, '',
259 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
265 <td class='mw-submit'>" .
266 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
269 Xml
::closeElement( 'table' ).
270 Xml
::closeElement( 'form' ) .
271 Xml
::closeElement( 'fieldset' )
279 * @ingroup SpecialPage
281 class ImportReporter
{
282 private $reason=false;
283 private $mOriginalLogCallback = null;
284 private $mOriginalPageOutCallback = null;
285 private $mLogItemCount = 0;
287 function __construct( $importer, $upload, $interwiki , $reason=false ) {
288 $this->mOriginalPageOutCallback
=
289 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
290 $this->mOriginalLogCallback
=
291 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
292 $this->mPageCount
= 0;
293 $this->mIsUpload
= $upload;
294 $this->mInterwiki
= $interwiki;
295 $this->reason
= $reason;
300 $wgOut->addHTML( "<ul>\n" );
303 function reportLogItem( /* ... */ ) {
304 $this->mLogItemCount++
;
305 if ( is_callable( $this->mOriginalLogCallback
) ) {
306 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
310 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
311 global $wgOut, $wgUser, $wgLang, $wgContLang;
313 $args = func_get_args();
314 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
316 $skin = $wgUser->getSkin();
320 $localCount = $wgLang->formatNum( $successCount );
321 $contentCount = $wgContLang->formatNum( $successCount );
323 if( $successCount > 0 ) {
324 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
325 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
329 $log = new LogPage( 'import' );
330 if( $this->mIsUpload
) {
331 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
333 if ( $this->reason
) {
334 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
336 $log->addEntry( 'upload', $title, $detail );
338 $interwiki = '[[:' . $this->mInterwiki
. ':' .
339 $origTitle->getPrefixedText() . ']]';
340 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
341 $contentCount, $interwiki );
342 if ( $this->reason
) {
343 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
345 $log->addEntry( 'interwiki', $title, $detail );
348 $comment = $detail; // quick
349 $dbw = wfGetDB( DB_MASTER
);
350 $latest = $title->getLatestRevID();
351 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
352 $nullRevision->insertOn( $dbw );
353 $article = new Article( $title );
355 $article->updateRevisionOn( $dbw, $nullRevision );
356 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
358 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
359 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
364 global $wgOut, $wgLang;
366 if ( $this->mLogItemCount
> 0 ) {
367 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
368 $wgLang->formatNum( $this->mLogItemCount
) );
369 $wgOut->addHTML( Xml
::tags( 'li', null, $msg ) );
370 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
371 $wgOut->addHTML( "</ul>\n" );
372 return new WikiErrorMsg( "importnopages" );
374 $wgOut->addHTML( "</ul>\n" );
376 return $this->mPageCount
;