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 ) {
56 $this->outputHeader();
58 $user = $this->getUser();
59 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
60 throw new PermissionsError( 'import' );
64 throw new ReadOnlyError
;
67 # @todo Allow Title::getUserPermissionsErrors() to take an array
68 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
69 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
70 $errors = wfMergeErrorArrays(
71 $this->getTitle()->getUserPermissionsErrors(
72 'import', $user, true,
73 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
75 $this->getTitle()->getUserPermissionsErrors(
76 'importupload', $user, true,
77 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
82 $this->getOutput()->showPermissionsErrorPage( $errors );
86 $request = $this->getRequest();
87 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
94 * Do the actual import
96 private function doImport() {
97 global $wgImportSources, $wgExportMaxLinkDepth;
100 $request = $this->getRequest();
101 $this->namespace = $request->getIntOrNull( 'namespace' );
102 $sourceName = $request->getVal( "source" );
104 $this->logcomment
= $request->getText( 'log-comment' );
105 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $request->getIntOrNull( 'pagelink-depth' );
107 $user = $this->getUser();
108 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
109 $source = Status
::newFatal( 'import-token-mismatch' );
110 } elseif ( $sourceName == 'upload' ) {
112 if( $user->isAllowed( 'importupload' ) ) {
113 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
115 throw new PermissionsError( 'importupload' );
117 } elseif ( $sourceName == "interwiki" ) {
118 if( !$user->isAllowed( 'import' ) ){
119 throw new PermissionsError( 'import' );
121 $this->interwiki
= $request->getVal( 'interwiki' );
122 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
123 $source = Status
::newFatal( "import-invalid-interwiki" );
125 $this->history
= $request->getCheck( 'interwikiHistory' );
126 $this->frompage
= $request->getText( "frompage" );
127 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
128 $source = ImportStreamSource
::newFromInterwiki(
132 $this->includeTemplates
,
133 $this->pageLinkDepth
);
136 $source = Status
::newFatal( "importunknownsource" );
139 $out = $this->getOutput();
140 if( !$source->isGood() ) {
141 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
143 $out->addWikiMsg( "importstart" );
145 $importer = new WikiImporter( $source->value
);
146 if( !is_null( $this->namespace ) ) {
147 $importer->setTargetNamespace( $this->namespace );
149 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
150 $reporter->setContext( $this->getContext() );
155 $importer->doImport();
156 } catch ( MWException
$e ) {
159 $result = $reporter->close();
162 # No source or XML parse error
163 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
164 } elseif( !$result->isGood() ) {
166 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
169 $out->addWikiMsg( 'importsuccess' );
171 $out->addHTML( '<hr />' );
175 private function showForm() {
176 global $wgImportSources, $wgExportMaxLinkDepth;
178 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
179 $user = $this->getUser();
180 $out = $this->getOutput();
182 if( $user->isAllowed( 'importupload' ) ) {
183 $out->addWikiMsg( "importtext" );
185 Xml
::fieldset( wfMsg( 'import-upload' ) ).
186 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
187 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
188 Html
::hidden( 'action', 'submit' ) .
189 Html
::hidden( 'source', 'upload' ) .
190 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
193 <td class='mw-label'>" .
194 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
196 <td class='mw-input'>" .
197 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
201 <td class='mw-label'>" .
202 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
204 <td class='mw-input'>" .
205 Xml
::input( 'log-comment', 50, '',
206 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
211 <td class='mw-submit'>" .
212 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
215 Xml
::closeElement( 'table' ).
216 Html
::hidden( 'editToken', $user->editToken() ) .
217 Xml
::closeElement( 'form' ) .
218 Xml
::closeElement( 'fieldset' )
221 if( empty( $wgImportSources ) ) {
222 $out->addWikiMsg( 'importnosources' );
226 if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
227 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
229 if( $wgExportMaxLinkDepth > 0 ) {
231 <td class='mw-label'>" .
232 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
234 <td class='mw-input'>" .
235 Xml
::input( 'pagelink-depth', 3, 0 ) .
241 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
242 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
243 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
244 Html
::hidden( 'action', 'submit' ) .
245 Html
::hidden( 'source', 'interwiki' ) .
246 Html
::hidden( 'editToken', $user->editToken() ) .
247 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
249 <td class='mw-label'>" .
250 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
252 <td class='mw-input'>" .
253 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
255 foreach( $wgImportSources as $prefix ) {
256 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
257 $out->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
261 Xml
::closeElement( 'select' ) .
262 Xml
::input( 'frompage', 50, $this->frompage
) .
268 <td class='mw-input'>" .
269 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
275 <td class='mw-input'>" .
276 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
281 <td class='mw-label'>" .
282 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
284 <td class='mw-input'>" .
285 Xml
::namespaceSelector( $this->namespace, '' ) .
289 <td class='mw-label'>" .
290 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
292 <td class='mw-input'>" .
293 Xml
::input( 'log-comment', 50, '',
294 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
300 <td class='mw-submit'>" .
301 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), Linker
::tooltipAndAccesskeyAttribs( 'import' ) ) .
304 Xml
::closeElement( 'table' ).
305 Xml
::closeElement( 'form' ) .
306 Xml
::closeElement( 'fieldset' )
314 * @ingroup SpecialPage
316 class ImportReporter
extends ContextSource
{
317 private $reason=false;
318 private $mOriginalLogCallback = null;
319 private $mOriginalPageOutCallback = null;
320 private $mLogItemCount = 0;
322 function __construct( $importer, $upload, $interwiki , $reason=false ) {
323 $this->mOriginalPageOutCallback
=
324 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
325 $this->mOriginalLogCallback
=
326 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
327 $this->mPageCount
= 0;
328 $this->mIsUpload
= $upload;
329 $this->mInterwiki
= $interwiki;
330 $this->reason
= $reason;
334 $this->getOutput()->addHTML( "<ul>\n" );
337 function reportLogItem( /* ... */ ) {
338 $this->mLogItemCount++
;
339 if ( is_callable( $this->mOriginalLogCallback
) ) {
340 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
345 * @param Title $title
346 * @param Title $origTitle
347 * @param int $revisionCount
348 * @param $successCount
352 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
355 $args = func_get_args();
356 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
360 $localCount = $this->getLang()->formatNum( $successCount );
361 $contentCount = $wgContLang->formatNum( $successCount );
363 if( $successCount > 0 ) {
364 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
365 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
369 $log = new LogPage( 'import' );
370 if( $this->mIsUpload
) {
371 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
373 if ( $this->reason
) {
374 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
376 $log->addEntry( 'upload', $title, $detail );
378 $interwiki = '[[:' . $this->mInterwiki
. ':' .
379 $origTitle->getPrefixedText() . ']]';
380 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
381 $contentCount, $interwiki );
382 if ( $this->reason
) {
383 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
385 $log->addEntry( 'interwiki', $title, $detail );
388 $comment = $detail; // quick
389 $dbw = wfGetDB( DB_MASTER
);
390 $latest = $title->getLatestRevID();
391 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
392 if (!is_null($nullRevision)) {
393 $nullRevision->insertOn( $dbw );
394 $article = new Article( $title );
396 $article->updateRevisionOn( $dbw, $nullRevision );
397 wfRunHooks( 'NewRevisionFromEditComplete', array( $article, $nullRevision, $latest, $this->getUser() ) );
400 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
401 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
406 $out = $this->getOutput();
407 if ( $this->mLogItemCount
> 0 ) {
408 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
409 $this->getLang()->formatNum( $this->mLogItemCount
) );
410 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
411 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
412 $out->addHTML( "</ul>\n" );
413 return Status
::newFatal( 'importnopages' );
415 $out->addHTML( "</ul>\n" );
417 return Status
::newGood( $this->mPageCount
);