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 $action = $wgTitle->getLocalUrl( 'action=submit' );
133 if( $wgUser->isAllowed( 'importupload' ) ) {
134 $wgOut->addWikiMsg( "importtext" );
136 Xml
::openElement( 'fieldset' ).
137 Xml
::element( 'legend', null, wfMsg( 'import-upload' ) ) .
138 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
139 Xml
::hidden( 'action', 'submit' ) .
140 Xml
::hidden( 'source', 'upload' ) .
141 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
145 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
150 Xml
::label( wfMsg('import-upload-comment'), 'log-comment' ) .
151 Xml
::input( 'log-comment', 50, '', array( 'id' => 'log-comment', 'type' => 'text' ) ) . ' ' .
156 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
159 Xml
::closeElement( 'table' ).
160 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
161 Xml
::closeElement( 'form' ) .
162 Xml
::closeElement( 'fieldset' )
165 if( empty( $wgImportSources ) ) {
166 $wgOut->addWikiMsg( 'importnosources' );
170 if( !empty( $wgImportSources ) ) {
172 Xml
::openElement( 'fieldset' ) .
173 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
174 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
175 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
176 Xml
::hidden( 'action', 'submit' ) .
177 Xml
::hidden( 'source', 'interwiki' ) .
178 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
179 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
182 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
184 foreach( $wgImportSources as $prefix ) {
185 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
186 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
189 Xml
::closeElement( 'select' ) .
192 Xml
::input( 'frompage', 50, $this->frompage
) .
199 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
206 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
207 Xml
::namespaceSelector( $this->namespace, '' ) .
212 Xml
::label( wfMsg( 'import-comment' ), 'comment' ) .
215 Xml
::input( 'log-comment', 50, '', array( 'type' => 'text' ) ) . ' ' .
222 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
225 Xml
::closeElement( 'table' ).
226 Xml
::closeElement( 'form' ) .
227 Xml
::closeElement( 'fieldset' )
235 * @ingroup SpecialPage
237 class ImportReporter
{
238 private $reason=false;
240 function __construct( $importer, $upload, $interwiki , $reason=false ) {
241 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
242 $this->mPageCount
= 0;
243 $this->mIsUpload
= $upload;
244 $this->mInterwiki
= $interwiki;
245 $this->reason
= $reason;
250 $wgOut->addHTML( "<ul>\n" );
253 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
254 global $wgOut, $wgUser, $wgLang, $wgContLang;
256 $skin = $wgUser->getSkin();
260 $localCount = $wgLang->formatNum( $successCount );
261 $contentCount = $wgContLang->formatNum( $successCount );
263 if( $successCount > 0 ) {
264 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
265 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
269 $log = new LogPage( 'import' );
270 if( $this->mIsUpload
) {
271 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
274 $detail .= ' (' . $this->reason
.')';
276 $log->addEntry( 'upload', $title, $detail );
278 $interwiki = '[[:' . $this->mInterwiki
. ':' .
279 $origTitle->getPrefixedText() . ']]';
280 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
281 $contentCount, $interwiki );
283 $detail .= ' (' . $this->reason
.')';
285 $log->addEntry( 'interwiki', $title, $detail );
288 $comment = $detail; // quick
289 $dbw = wfGetDB( DB_MASTER
);
290 $latest = $title->getLatestRevID();
291 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
292 $nullRevision->insertOn( $dbw );
293 $article = new Article( $title );
295 $article->updateRevisionOn( $dbw, $nullRevision );
296 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
298 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
304 if( $this->mPageCount
== 0 ) {
305 $wgOut->addHTML( "</ul>\n" );
306 return new WikiErrorMsg( "importnopages" );
308 $wgOut->addHTML( "</ul>\n" );
310 return $this->mPageCount
;