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' );
63 # @todo Allow Title::getUserPermissionsErrors() to take an array
64 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
65 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
66 $errors = wfMergeErrorArrays(
67 $this->getTitle()->getUserPermissionsErrors(
68 'import', $user, true,
69 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
71 $this->getTitle()->getUserPermissionsErrors(
72 'importupload', $user, true,
73 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
78 throw new PermissionsError( 'import', $errors );
82 throw new ReadOnlyError
;
85 $request = $this->getRequest();
86 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
93 * Do the actual import
95 private function doImport() {
96 global $wgImportSources, $wgExportMaxLinkDepth;
99 $request = $this->getRequest();
100 $this->namespace = $request->getIntOrNull( 'namespace' );
101 $sourceName = $request->getVal( "source" );
103 $this->logcomment
= $request->getText( 'log-comment' );
104 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $request->getIntOrNull( 'pagelink-depth' );
106 $user = $this->getUser();
107 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
108 $source = Status
::newFatal( 'import-token-mismatch' );
109 } elseif ( $sourceName == 'upload' ) {
111 if( $user->isAllowed( 'importupload' ) ) {
112 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
114 throw new PermissionsError( 'importupload' );
116 } elseif ( $sourceName == "interwiki" ) {
117 if( !$user->isAllowed( 'import' ) ){
118 throw new PermissionsError( 'import' );
120 $this->interwiki
= $request->getVal( 'interwiki' );
121 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
122 $source = Status
::newFatal( "import-invalid-interwiki" );
124 $this->history
= $request->getCheck( 'interwikiHistory' );
125 $this->frompage
= $request->getText( "frompage" );
126 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
127 $source = ImportStreamSource
::newFromInterwiki(
131 $this->includeTemplates
,
132 $this->pageLinkDepth
);
135 $source = Status
::newFatal( "importunknownsource" );
138 $out = $this->getOutput();
139 if( !$source->isGood() ) {
140 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
142 $out->addWikiMsg( "importstart" );
144 $importer = new WikiImporter( $source->value
);
145 if( !is_null( $this->namespace ) ) {
146 $importer->setTargetNamespace( $this->namespace );
148 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
149 $reporter->setContext( $this->getContext() );
154 $importer->doImport();
155 } catch ( MWException
$e ) {
158 $result = $reporter->close();
161 # No source or XML parse error
162 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
163 } elseif( !$result->isGood() ) {
165 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
168 $out->addWikiMsg( 'importsuccess' );
170 $out->addHTML( '<hr />' );
174 private function showForm() {
175 global $wgImportSources, $wgExportMaxLinkDepth;
177 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
178 $user = $this->getUser();
179 $out = $this->getOutput();
181 if( $user->isAllowed( 'importupload' ) ) {
182 $out->addWikiMsg( "importtext" );
184 Xml
::fieldset( wfMsg( 'import-upload' ) ).
185 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
186 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
187 Html
::hidden( 'action', 'submit' ) .
188 Html
::hidden( 'source', 'upload' ) .
189 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
192 <td class='mw-label'>" .
193 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
195 <td class='mw-input'>" .
196 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
200 <td class='mw-label'>" .
201 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
203 <td class='mw-input'>" .
204 Xml
::input( 'log-comment', 50, '',
205 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
210 <td class='mw-submit'>" .
211 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
214 Xml
::closeElement( 'table' ).
215 Html
::hidden( 'editToken', $user->editToken() ) .
216 Xml
::closeElement( 'form' ) .
217 Xml
::closeElement( 'fieldset' )
220 if( empty( $wgImportSources ) ) {
221 $out->addWikiMsg( 'importnosources' );
225 if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
226 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
228 if( $wgExportMaxLinkDepth > 0 ) {
230 <td class='mw-label'>" .
231 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
233 <td class='mw-input'>" .
234 Xml
::input( 'pagelink-depth', 3, 0 ) .
240 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
241 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
242 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
243 Html
::hidden( 'action', 'submit' ) .
244 Html
::hidden( 'source', 'interwiki' ) .
245 Html
::hidden( 'editToken', $user->editToken() ) .
246 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
248 <td class='mw-label'>" .
249 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
251 <td class='mw-input'>" .
252 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
254 foreach( $wgImportSources as $prefix ) {
255 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
256 $out->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
260 Xml
::closeElement( 'select' ) .
261 Xml
::input( 'frompage', 50, $this->frompage
) .
267 <td class='mw-input'>" .
268 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
274 <td class='mw-input'>" .
275 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
280 <td class='mw-label'>" .
281 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
283 <td class='mw-input'>" .
284 Xml
::namespaceSelector( $this->namespace, '' ) .
288 <td class='mw-label'>" .
289 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
291 <td class='mw-input'>" .
292 Xml
::input( 'log-comment', 50, '',
293 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
299 <td class='mw-submit'>" .
300 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), Linker
::tooltipAndAccesskeyAttribs( 'import' ) ) .
303 Xml
::closeElement( 'table' ).
304 Xml
::closeElement( 'form' ) .
305 Xml
::closeElement( 'fieldset' )
313 * @ingroup SpecialPage
315 class ImportReporter
extends ContextSource
{
316 private $reason=false;
317 private $mOriginalLogCallback = null;
318 private $mOriginalPageOutCallback = null;
319 private $mLogItemCount = 0;
321 function __construct( $importer, $upload, $interwiki , $reason=false ) {
322 $this->mOriginalPageOutCallback
=
323 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
324 $this->mOriginalLogCallback
=
325 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
326 $this->mPageCount
= 0;
327 $this->mIsUpload
= $upload;
328 $this->mInterwiki
= $interwiki;
329 $this->reason
= $reason;
333 $this->getOutput()->addHTML( "<ul>\n" );
336 function reportLogItem( /* ... */ ) {
337 $this->mLogItemCount++
;
338 if ( is_callable( $this->mOriginalLogCallback
) ) {
339 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
344 * @param Title $title
345 * @param Title $origTitle
346 * @param int $revisionCount
347 * @param $successCount
351 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
354 $args = func_get_args();
355 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
359 $localCount = $this->getLang()->formatNum( $successCount );
360 $contentCount = $wgContLang->formatNum( $successCount );
362 if( $successCount > 0 ) {
363 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
364 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
368 $log = new LogPage( 'import' );
369 if( $this->mIsUpload
) {
370 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
372 if ( $this->reason
) {
373 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
375 $log->addEntry( 'upload', $title, $detail );
377 $interwiki = '[[:' . $this->mInterwiki
. ':' .
378 $origTitle->getPrefixedText() . ']]';
379 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
380 $contentCount, $interwiki );
381 if ( $this->reason
) {
382 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
384 $log->addEntry( 'interwiki', $title, $detail );
387 $comment = $detail; // quick
388 $dbw = wfGetDB( DB_MASTER
);
389 $latest = $title->getLatestRevID();
390 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
391 if (!is_null($nullRevision)) {
392 $nullRevision->insertOn( $dbw );
393 $page = WikiPage
::factory( $title );
395 $page->updateRevisionOn( $dbw, $nullRevision );
396 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
399 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
400 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
405 $out = $this->getOutput();
406 if ( $this->mLogItemCount
> 0 ) {
407 $msg = wfMsgExt( 'imported-log-entries', 'parseinline',
408 $this->getLang()->formatNum( $this->mLogItemCount
) );
409 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
410 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
411 $out->addHTML( "</ul>\n" );
412 return Status
::newFatal( 'importnopages' );
414 $out->addHTML( "</ul>\n" );
416 return Status
::newGood( $this->mPageCount
);