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;
37 public function __construct() {
38 parent
::__construct( 'Import', 'import' );
39 global $wgImportTargetNamespace;
40 $this->namespace = $wgImportTargetNamespace;
46 function execute( $par ) {
50 $this->outputHeader();
54 $wgOut->readOnlyPage();
58 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
65 * Do the actual import
67 private function doImport() {
68 global $wgOut, $wgRequest, $wgUser, $wgImportSources;
70 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
71 $sourceName = $wgRequest->getVal( "source" );
73 $this->logcomment
= $wgRequest->getText( 'log-comment' );
75 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
76 $source = new WikiErrorMsg( 'import-token-mismatch' );
77 } elseif ( $sourceName == 'upload' ) {
79 if( $wgUser->isAllowed( 'importupload' ) ) {
80 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
82 return $wgOut->permissionRequired( 'importupload' );
84 } elseif ( $sourceName == "interwiki" ) {
85 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
86 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
87 $source = new WikiErrorMsg( "import-invalid-interwiki" );
89 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
90 $this->frompage
= $wgRequest->getText( "frompage" );
91 $source = ImportStreamSource
::newFromInterwiki(
97 $source = new WikiErrorMsg( "importunknownsource" );
100 if( WikiError
::isError( $source ) ) {
101 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
103 $wgOut->addWikiMsg( "importstart" );
105 $importer = new WikiImporter( $source );
106 if( !is_null( $this->namespace ) ) {
107 $importer->setTargetNamespace( $this->namespace );
109 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
112 $result = $importer->doImport();
113 $resultCount = $reporter->close();
115 if( WikiError
::isError( $result ) ) {
116 # No source or XML parse error
117 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
118 } elseif( WikiError
::isError( $resultCount ) ) {
120 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
123 $wgOut->addWikiMsg( 'importsuccess' );
125 $wgOut->addWikiText( '<hr />' );
129 private function showForm() {
130 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
131 # FIXME: Quick hack to disable import for non privileged users /Raymond
132 # Regression from 43963
133 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
134 return $wgOut->permissionRequired( 'import' );
136 $action = $wgTitle->getLocalUrl( 'action=submit' );
138 if( $wgUser->isAllowed( 'importupload' ) ) {
139 $wgOut->addWikiMsg( "importtext" );
141 Xml
::openElement( 'fieldset' ).
142 Xml
::element( 'legend', null, wfMsg( 'import-upload' ) ) .
143 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
144 Xml
::hidden( 'action', 'submit' ) .
145 Xml
::hidden( 'source', 'upload' ) .
146 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
149 <td class='mw-label'>" .
150 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
152 <td class='mw-input'>" .
153 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
157 <td class='mw-label'>" .
158 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
160 <td class='mw-input'>" .
161 Xml
::input( 'log-comment', 50, '',
162 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
167 <td class='mw-input'>" .
168 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
171 Xml
::closeElement( 'table' ).
172 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
173 Xml
::closeElement( 'form' ) .
174 Xml
::closeElement( 'fieldset' )
177 if( empty( $wgImportSources ) ) {
178 $wgOut->addWikiMsg( 'importnosources' );
182 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
184 Xml
::openElement( 'fieldset' ) .
185 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
186 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
187 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
188 Xml
::hidden( 'action', 'submit' ) .
189 Xml
::hidden( 'source', 'interwiki' ) .
190 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
191 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
193 <td class='mw-label'>" .
194 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
196 <td class='mw-input'>" .
197 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
199 foreach( $wgImportSources as $prefix ) {
200 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
201 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
204 Xml
::closeElement( 'select' ) .
205 Xml
::input( 'frompage', 50, $this->frompage
) .
211 <td class='mw-input'>" .
212 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
217 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
219 <td class='mw-input'>" .
220 Xml
::namespaceSelector( $this->namespace, '' ) .
224 <td class='mw-label'>" .
225 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
227 <td class='mw-input'>" .
228 Xml
::input( 'log-comment', 50, '',
229 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
235 <td class='mw-input'>" .
236 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
239 Xml
::closeElement( 'table' ).
240 Xml
::closeElement( 'form' ) .
241 Xml
::closeElement( 'fieldset' )
249 * @ingroup SpecialPage
251 class ImportReporter
{
252 private $reason=false;
254 function __construct( $importer, $upload, $interwiki , $reason=false ) {
255 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
256 $this->mPageCount
= 0;
257 $this->mIsUpload
= $upload;
258 $this->mInterwiki
= $interwiki;
259 $this->reason
= $reason;
264 $wgOut->addHTML( "<ul>\n" );
267 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
268 global $wgOut, $wgUser, $wgLang, $wgContLang;
270 $skin = $wgUser->getSkin();
274 $localCount = $wgLang->formatNum( $successCount );
275 $contentCount = $wgContLang->formatNum( $successCount );
277 if( $successCount > 0 ) {
278 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
279 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
283 $log = new LogPage( 'import' );
284 if( $this->mIsUpload
) {
285 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
287 if ( $this->reason
) {
288 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
290 $log->addEntry( 'upload', $title, $detail );
292 $interwiki = '[[:' . $this->mInterwiki
. ':' .
293 $origTitle->getPrefixedText() . ']]';
294 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
295 $contentCount, $interwiki );
296 if ( $this->reason
) {
297 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
299 $log->addEntry( 'interwiki', $title, $detail );
302 $comment = $detail; // quick
303 $dbw = wfGetDB( DB_MASTER
);
304 $latest = $title->getLatestRevID();
305 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
306 $nullRevision->insertOn( $dbw );
307 $article = new Article( $title );
309 $article->updateRevisionOn( $dbw, $nullRevision );
310 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
312 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
318 if( $this->mPageCount
== 0 ) {
319 $wgOut->addHTML( "</ul>\n" );
320 return new WikiErrorMsg( "importnopages" );
322 $wgOut->addHTML( "</ul>\n" );
324 return $this->mPageCount
;