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( 'import', $wgUser, true, array( 'ns-specialprotected' ) ),
75 $this->getTitle()->getUserPermissionsErrors( 'importupload', $wgUser, true, array( 'ns-specialprotected' ) )
78 $wgOut->showPermissionsErrorPage( $errors );
82 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
89 * Do the actual import
91 private function doImport() {
92 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
94 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
95 $sourceName = $wgRequest->getVal( "source" );
97 $this->logcomment
= $wgRequest->getText( 'log-comment' );
98 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
100 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
101 $source = Status
::newFatal( 'import-token-mismatch' );
102 } elseif ( $sourceName == 'upload' ) {
104 if( $wgUser->isAllowed( 'importupload' ) ) {
105 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
107 return $wgOut->permissionRequired( 'importupload' );
109 } elseif ( $sourceName == "interwiki" ) {
110 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
111 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
112 $source = Status
::newFatal( "import-invalid-interwiki" );
114 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
115 $this->frompage
= $wgRequest->getText( "frompage" );
116 $this->includeTemplates
= $wgRequest->getCheck( 'interwikiTemplates' );
117 $source = ImportStreamSource
::newFromInterwiki(
121 $this->includeTemplates
,
122 $this->pageLinkDepth
);
125 $source = Status
::newFatal( "importunknownsource" );
128 if( !$source->isGood() ) {
129 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
131 $wgOut->addWikiMsg( "importstart" );
133 $importer = new WikiImporter( $source->value
);
134 if( !is_null( $this->namespace ) ) {
135 $importer->setTargetNamespace( $this->namespace );
137 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
142 $importer->doImport();
143 } catch ( MWException
$e ) {
146 $result = $reporter->close();
149 # No source or XML parse error
150 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
151 } elseif( !$result->isGood() ) {
153 $wgOut->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
156 $wgOut->addWikiMsg( 'importsuccess' );
158 $wgOut->addHTML( '<hr />' );
162 private function showForm() {
163 global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth;
165 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
167 if( $wgUser->isAllowed( 'importupload' ) ) {
168 $wgOut->addWikiMsg( "importtext" );
170 Xml
::fieldset( wfMsg( 'import-upload' ) ).
171 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
172 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
173 Html
::hidden( 'action', 'submit' ) .
174 Html
::hidden( 'source', 'upload' ) .
175 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
178 <td class='mw-label'>" .
179 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
181 <td class='mw-input'>" .
182 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
186 <td class='mw-label'>" .
187 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
189 <td class='mw-input'>" .
190 Xml
::input( 'log-comment', 50, '',
191 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
196 <td class='mw-submit'>" .
197 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
200 Xml
::closeElement( 'table' ).
201 Html
::hidden( 'editToken', $wgUser->editToken() ) .
202 Xml
::closeElement( 'form' ) .
203 Xml
::closeElement( 'fieldset' )
206 if( empty( $wgImportSources ) ) {
207 $wgOut->addWikiMsg( 'importnosources' );
211 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
212 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
214 if( $wgExportMaxLinkDepth > 0 ) {
216 <td class='mw-label'>" .
217 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
219 <td class='mw-input'>" .
220 Xml
::input( 'pagelink-depth', 3, 0 ) .
226 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
227 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
228 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
229 Html
::hidden( 'action', 'submit' ) .
230 Html
::hidden( 'source', 'interwiki' ) .
231 Html
::hidden( 'editToken', $wgUser->editToken() ) .
232 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
234 <td class='mw-label'>" .
235 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
237 <td class='mw-input'>" .
238 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
240 foreach( $wgImportSources as $prefix ) {
241 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
242 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
246 Xml
::closeElement( 'select' ) .
247 Xml
::input( 'frompage', 50, $this->frompage
) .
253 <td class='mw-input'>" .
254 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
260 <td class='mw-input'>" .
261 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
266 <td class='mw-label'>" .
267 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
269 <td class='mw-input'>" .
270 Xml
::namespaceSelector( $this->namespace, '' ) .
274 <td class='mw-label'>" .
275 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
277 <td class='mw-input'>" .
278 Xml
::input( 'log-comment', 50, '',
279 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
285 <td class='mw-submit'>" .
286 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'import' ) ) .
289 Xml
::closeElement( 'table' ).
290 Xml
::closeElement( 'form' ) .
291 Xml
::closeElement( 'fieldset' )
299 * @ingroup SpecialPage
301 class ImportReporter
{
302 private $reason=false;
303 private $mOriginalLogCallback = null;
304 private $mOriginalPageOutCallback = null;
305 private $mLogItemCount = 0;
307 function __construct( $importer, $upload, $interwiki , $reason=false ) {
308 $this->mOriginalPageOutCallback
=
309 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
310 $this->mOriginalLogCallback
=
311 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
312 $this->mPageCount
= 0;
313 $this->mIsUpload
= $upload;
314 $this->mInterwiki
= $interwiki;
315 $this->reason
= $reason;
320 $wgOut->addHTML( "<ul>\n" );
323 function reportLogItem( /* ... */ ) {
324 $this->mLogItemCount++
;
325 if ( is_callable( $this->mOriginalLogCallback
) ) {
326 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
331 * @param Title $title
332 * @param Title $origTitle
333 * @param int $revisionCount
334 * @param $successCount
338 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
339 global $wgOut, $wgUser, $wgLang, $wgContLang;
341 $args = func_get_args();
342 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
344 $skin = $wgUser->getSkin();
348 $localCount = $wgLang->formatNum( $successCount );
349 $contentCount = $wgContLang->formatNum( $successCount );
351 if( $successCount > 0 ) {
352 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
353 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
357 $log = new LogPage( 'import' );
358 if( $this->mIsUpload
) {
359 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
361 if ( $this->reason
) {
362 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
364 $log->addEntry( 'upload', $title, $detail );
366 $interwiki = '[[:' . $this->mInterwiki
. ':' .
367 $origTitle->getPrefixedText() . ']]';
368 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
369 $contentCount, $interwiki );
370 if ( $this->reason
) {
371 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
373 $log->addEntry( 'interwiki', $title, $detail );
376 $comment = $detail; // quick
377 $dbw = wfGetDB( DB_MASTER
);
378 $latest = $title->getLatestRevID();
379 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
380 $nullRevision->insertOn( $dbw );
381 $article = new Article( $title );
383 $article->updateRevisionOn( $dbw, $nullRevision );
384 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
386 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
387 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
392 global $wgOut, $wgLang;
394 if ( $this->mLogItemCount
> 0 ) {
395 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
396 $wgLang->formatNum( $this->mLogItemCount
) );
397 $wgOut->addHTML( Xml
::tags( 'li', null, $msg ) );
398 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
399 $wgOut->addHTML( "</ul>\n" );
400 return Status
::newFatal( 'importnopages' );
402 $wgOut->addHTML( "</ul>\n" );
404 return Status
::newGood( $this->mPageCount
);