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 );
81 $this->checkReadOnly();
83 $request = $this->getRequest();
84 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
91 * Do the actual import
93 private function doImport() {
94 global $wgImportSources, $wgExportMaxLinkDepth;
97 $request = $this->getRequest();
98 $this->namespace = $request->getIntOrNull( 'namespace' );
99 $sourceName = $request->getVal( "source" );
101 $this->logcomment
= $request->getText( 'log-comment' );
102 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $request->getIntOrNull( 'pagelink-depth' );
104 $user = $this->getUser();
105 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
106 $source = Status
::newFatal( 'import-token-mismatch' );
107 } elseif ( $sourceName == 'upload' ) {
109 if( $user->isAllowed( 'importupload' ) ) {
110 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
112 throw new PermissionsError( 'importupload' );
114 } elseif ( $sourceName == "interwiki" ) {
115 if( !$user->isAllowed( 'import' ) ){
116 throw new PermissionsError( 'import' );
118 $this->interwiki
= $request->getVal( 'interwiki' );
119 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
120 $source = Status
::newFatal( "import-invalid-interwiki" );
122 $this->history
= $request->getCheck( 'interwikiHistory' );
123 $this->frompage
= $request->getText( "frompage" );
124 $this->includeTemplates
= $request->getCheck( 'interwikiTemplates' );
125 $source = ImportStreamSource
::newFromInterwiki(
129 $this->includeTemplates
,
130 $this->pageLinkDepth
);
133 $source = Status
::newFatal( "importunknownsource" );
136 $out = $this->getOutput();
137 if( !$source->isGood() ) {
138 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $source->getWikiText() ) );
140 $out->addWikiMsg( "importstart" );
142 $importer = new WikiImporter( $source->value
);
143 if( !is_null( $this->namespace ) ) {
144 $importer->setTargetNamespace( $this->namespace );
146 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
147 $reporter->setContext( $this->getContext() );
152 $importer->doImport();
153 } catch ( MWException
$e ) {
156 $result = $reporter->close();
159 # No source or XML parse error
160 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $exception->getMessage() ) );
161 } elseif( !$result->isGood() ) {
163 $out->wrapWikiMsg( "<p class=\"error\">\n$1\n</p>", array( 'importfailed', $result->getWikiText() ) );
166 $out->addWikiMsg( 'importsuccess' );
168 $out->addHTML( '<hr />' );
172 private function showForm() {
173 global $wgImportSources, $wgExportMaxLinkDepth;
175 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
176 $user = $this->getUser();
177 $out = $this->getOutput();
179 if( $user->isAllowed( 'importupload' ) ) {
180 $out->addWikiMsg( "importtext" );
182 Xml
::fieldset( $this->msg( 'import-upload' )->text() ).
183 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
184 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
185 Html
::hidden( 'action', 'submit' ) .
186 Html
::hidden( 'source', 'upload' ) .
187 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
190 <td class='mw-label'>" .
191 Xml
::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
193 <td class='mw-input'>" .
194 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
198 <td class='mw-label'>" .
199 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
201 <td class='mw-input'>" .
202 Xml
::input( 'log-comment', 50, '',
203 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
208 <td class='mw-submit'>" .
209 Xml
::submitButton( $this->msg( 'uploadbtn' )->text() ) .
212 Xml
::closeElement( 'table' ).
213 Html
::hidden( 'editToken', $user->getEditToken() ) .
214 Xml
::closeElement( 'form' ) .
215 Xml
::closeElement( 'fieldset' )
218 if( empty( $wgImportSources ) ) {
219 $out->addWikiMsg( 'importnosources' );
223 if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
224 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
226 if( $wgExportMaxLinkDepth > 0 ) {
228 <td class='mw-label'>" .
229 $this->msg( 'export-pagelinks' )->parse() .
231 <td class='mw-input'>" .
232 Xml
::input( 'pagelink-depth', 3, 0 ) .
238 Xml
::fieldset( $this->msg( 'importinterwiki' )->text() ) .
239 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
240 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
241 Html
::hidden( 'action', 'submit' ) .
242 Html
::hidden( 'source', 'interwiki' ) .
243 Html
::hidden( 'editToken', $user->getEditToken() ) .
244 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
246 <td class='mw-label'>" .
247 Xml
::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
249 <td class='mw-input'>" .
250 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
252 foreach( $wgImportSources as $prefix ) {
253 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
254 $out->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
258 Xml
::closeElement( 'select' ) .
259 Xml
::input( 'frompage', 50, $this->frompage
) .
265 <td class='mw-input'>" .
266 Xml
::checkLabel( $this->msg( 'import-interwiki-history' )->text(), 'interwikiHistory', 'interwikiHistory', $this->history
) .
272 <td class='mw-input'>" .
273 Xml
::checkLabel( $this->msg( 'import-interwiki-templates' )->text(), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
278 <td class='mw-label'>" .
279 Xml
::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
281 <td class='mw-input'>" .
282 Html
::namespaceSelector(
284 'selected' => $this->namespace,
287 'name' => 'namespace',
289 'class' => 'namespaceselector',
295 <td class='mw-label'>" .
296 Xml
::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
298 <td class='mw-input'>" .
299 Xml
::input( 'log-comment', 50, '',
300 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
306 <td class='mw-submit'>" .
307 Xml
::submitButton( $this->msg( 'import-interwiki-submit' )->text(), Linker
::tooltipAndAccesskeyAttribs( 'import' ) ) .
310 Xml
::closeElement( 'table' ).
311 Xml
::closeElement( 'form' ) .
312 Xml
::closeElement( 'fieldset' )
320 * @ingroup SpecialPage
322 class ImportReporter
extends ContextSource
{
323 private $reason=false;
324 private $mOriginalLogCallback = null;
325 private $mOriginalPageOutCallback = null;
326 private $mLogItemCount = 0;
328 function __construct( $importer, $upload, $interwiki , $reason=false ) {
329 $this->mOriginalPageOutCallback
=
330 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
331 $this->mOriginalLogCallback
=
332 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
333 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
334 $this->mPageCount
= 0;
335 $this->mIsUpload
= $upload;
336 $this->mInterwiki
= $interwiki;
337 $this->reason
= $reason;
341 $this->getOutput()->addHTML( "<ul>\n" );
344 function reportNotice( $msg, array $params ) {
345 $this->getOutput()->addHTML( Html
::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
348 function reportLogItem( /* ... */ ) {
349 $this->mLogItemCount++
;
350 if ( is_callable( $this->mOriginalLogCallback
) ) {
351 call_user_func_array( $this->mOriginalLogCallback
, func_get_args() );
356 * @param Title $title
357 * @param Title $origTitle
358 * @param int $revisionCount
359 * @param $successCount
363 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
364 $args = func_get_args();
365 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
367 if ( $title === null ) {
368 # Invalid or non-importable title; a notice is already displayed
374 if( $successCount > 0 ) {
375 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
376 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
380 $log = new LogPage( 'import' );
381 if( $this->mIsUpload
) {
382 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
383 $successCount )->inContentLanguage()->text();
384 if ( $this->reason
) {
385 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason
;
387 $log->addEntry( 'upload', $title, $detail );
389 $interwiki = '[[:' . $this->mInterwiki
. ':' .
390 $origTitle->getPrefixedText() . ']]';
391 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
392 $successCount )->params( $interwiki )->inContentLanguage()->text();
393 if ( $this->reason
) {
394 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->reason
;
396 $log->addEntry( 'interwiki', $title, $detail );
399 $comment = $detail; // quick
400 $dbw = wfGetDB( DB_MASTER
);
401 $latest = $title->getLatestRevID();
402 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleID(), $comment, true );
403 if (!is_null($nullRevision)) {
404 $nullRevision->insertOn( $dbw );
405 $page = WikiPage
::factory( $title );
407 $page->updateRevisionOn( $dbw, $nullRevision );
408 wfRunHooks( 'NewRevisionFromEditComplete', array( $page, $nullRevision, $latest, $this->getUser() ) );
411 $this->getOutput()->addHTML( "<li>" . Linker
::linkKnown( $title ) . " " .
412 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
417 $out = $this->getOutput();
418 if ( $this->mLogItemCount
> 0 ) {
419 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
420 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
421 } elseif( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
422 $out->addHTML( "</ul>\n" );
423 return Status
::newFatal( 'importnopages' );
425 $out->addHTML( "</ul>\n" );
427 return Status
::newGood( $this->mPageCount
);