3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
26 class SpecialImport
extends SpecialPage
{
28 private $interwiki = false;
30 private $frompage = '';
31 private $logcomment= false;
32 private $history = true;
33 private $includeTemplates = false;
38 public function __construct() {
39 parent
::__construct( 'Import', 'import' );
40 global $wgImportTargetNamespace;
41 $this->namespace = $wgImportTargetNamespace;
47 function execute( $par ) {
51 $this->outputHeader();
55 $wgOut->readOnlyPage();
59 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
66 * Do the actual import
68 private function doImport() {
69 global $wgOut, $wgRequest, $wgUser, $wgImportSources;
71 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
72 $sourceName = $wgRequest->getVal( "source" );
74 $this->logcomment
= $wgRequest->getText( 'log-comment' );
75 $this->pageLinkDepth
= $wgRequest->getIntOrNull( 'pagelink-depth' );
77 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
78 $source = new WikiErrorMsg( 'import-token-mismatch' );
79 } elseif ( $sourceName == 'upload' ) {
81 if( $wgUser->isAllowed( 'importupload' ) ) {
82 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
84 return $wgOut->permissionRequired( 'importupload' );
86 } elseif ( $sourceName == "interwiki" ) {
87 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
88 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
89 $source = new WikiErrorMsg( "import-invalid-interwiki" );
91 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
92 $this->frompage
= $wgRequest->getText( "frompage" );
93 $this->includeTemplates
= $wgRequest->getCheck( 'interwikiTemplates' );
94 $source = ImportStreamSource
::newFromInterwiki(
98 $this->includeTemplates
,
99 $this->pageLinkDepth
);
102 $source = new WikiErrorMsg( "importunknownsource" );
105 if( WikiError
::isError( $source ) ) {
106 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
108 $wgOut->addWikiMsg( "importstart" );
110 $importer = new WikiImporter( $source );
111 if( !is_null( $this->namespace ) ) {
112 $importer->setTargetNamespace( $this->namespace );
114 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
117 $result = $importer->doImport();
118 $resultCount = $reporter->close();
120 if( WikiError
::isError( $result ) ) {
121 # No source or XML parse error
122 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
123 } elseif( WikiError
::isError( $resultCount ) ) {
125 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
128 $wgOut->addWikiMsg( 'importsuccess' );
130 $wgOut->addWikiText( '<hr />' );
134 private function showForm() {
135 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
136 # FIXME: Quick hack to disable import for non privileged users /Raymond
137 # Regression from 43963
138 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
139 return $wgOut->permissionRequired( 'import' );
141 $action = $wgTitle->getLocalUrl( 'action=submit' );
143 if( $wgUser->isAllowed( 'importupload' ) ) {
144 $wgOut->addWikiMsg( "importtext" );
146 Xml
::openElement( 'fieldset' ).
147 Xml
::element( 'legend', null, wfMsg( 'import-upload' ) ) .
148 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
149 Xml
::hidden( 'action', 'submit' ) .
150 Xml
::hidden( 'source', 'upload' ) .
151 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
154 <td class='mw-label'>" .
155 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
157 <td class='mw-input'>" .
158 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
162 <td class='mw-label'>" .
163 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
165 <td class='mw-input'>" .
166 Xml
::input( 'log-comment', 50, '',
167 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
172 <td class='mw-input'>" .
173 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
176 Xml
::closeElement( 'table' ).
177 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
178 Xml
::closeElement( 'form' ) .
179 Xml
::closeElement( 'fieldset' )
182 if( empty( $wgImportSources ) ) {
183 $wgOut->addWikiMsg( 'importnosources' );
187 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
189 Xml
::openElement( 'fieldset' ) .
190 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
191 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
192 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
193 Xml
::hidden( 'action', 'submit' ) .
194 Xml
::hidden( 'source', 'interwiki' ) .
195 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
196 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
198 <td class='mw-label'>" .
199 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
201 <td class='mw-input'>" .
202 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
204 foreach( $wgImportSources as $prefix ) {
205 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
206 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
209 Xml
::closeElement( 'select' ) .
210 Xml
::input( 'frompage', 50, $this->frompage
) .
216 <td class='mw-input'>" .
217 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
223 <td class='mw-input'>" .
224 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
228 <td>".wfMsgExt( 'export-pagelinks', 'parseinline' )."</td>
229 <td class=\"mw-input\">" . Xml
::input( 'pagelink-depth', 20, 0 ) . "<br/>
233 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
235 <td class='mw-input'>" .
236 Xml
::namespaceSelector( $this->namespace, '' ) .
240 <td class='mw-label'>" .
241 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
243 <td class='mw-input'>" .
244 Xml
::input( 'log-comment', 50, '',
245 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
251 <td class='mw-input'>" .
252 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
255 Xml
::closeElement( 'table' ).
256 Xml
::closeElement( 'form' ) .
257 Xml
::closeElement( 'fieldset' )
265 * @ingroup SpecialPage
267 class ImportReporter
{
268 private $reason=false;
270 function __construct( $importer, $upload, $interwiki , $reason=false ) {
271 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
272 $this->mPageCount
= 0;
273 $this->mIsUpload
= $upload;
274 $this->mInterwiki
= $interwiki;
275 $this->reason
= $reason;
280 $wgOut->addHTML( "<ul>\n" );
283 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
284 global $wgOut, $wgUser, $wgLang, $wgContLang;
286 $skin = $wgUser->getSkin();
290 $localCount = $wgLang->formatNum( $successCount );
291 $contentCount = $wgContLang->formatNum( $successCount );
293 if( $successCount > 0 ) {
294 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
295 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
299 $log = new LogPage( 'import' );
300 if( $this->mIsUpload
) {
301 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
303 if ( $this->reason
) {
304 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
306 $log->addEntry( 'upload', $title, $detail );
308 $interwiki = '[[:' . $this->mInterwiki
. ':' .
309 $origTitle->getPrefixedText() . ']]';
310 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
311 $contentCount, $interwiki );
312 if ( $this->reason
) {
313 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
315 $log->addEntry( 'interwiki', $title, $detail );
318 $comment = $detail; // quick
319 $dbw = wfGetDB( DB_MASTER
);
320 $latest = $title->getLatestRevID();
321 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
322 $nullRevision->insertOn( $dbw );
323 $article = new Article( $title );
325 $article->updateRevisionOn( $dbw, $nullRevision );
326 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
328 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
334 if( $this->mPageCount
== 0 ) {
335 $wgOut->addHTML( "</ul>\n" );
336 return new WikiErrorMsg( "importnopages" );
338 $wgOut->addHTML( "</ul>\n" );
340 return $this->mPageCount
;