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 $history = true;
36 public function __construct() {
37 parent
::__construct( 'Import', 'import' );
38 global $wgImportTargetNamespace;
39 $this->namespace = $wgImportTargetNamespace;
45 function execute( $par ) {
49 $this->outputHeader();
53 $wgOut->readOnlyPage();
57 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
64 * Do the actual import
66 private function doImport() {
67 global $wgOut, $wgRequest, $wgUser, $wgImportSources;
69 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
70 $sourceName = $wgRequest->getVal( "source" );
72 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
73 $source = new WikiErrorMsg( 'import-token-mismatch' );
74 } elseif ( $sourceName == 'upload' ) {
76 if( $wgUser->isAllowed( 'importupload' ) ) {
77 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
79 return $wgOut->permissionRequired( 'importupload' );
81 } elseif ( $sourceName == "interwiki" ) {
82 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
83 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
84 $source = new WikiErrorMsg( "import-invalid-interwiki" );
86 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
87 $this->frompage
= $wgRequest->getText( "frompage" );
88 $source = ImportStreamSource
::newFromInterwiki(
94 $source = new WikiErrorMsg( "importunknownsource" );
97 if( WikiError
::isError( $source ) ) {
98 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
100 $wgOut->addWikiMsg( "importstart" );
102 $importer = new WikiImporter( $source );
103 if( !is_null( $this->namespace ) ) {
104 $importer->setTargetNamespace( $this->namespace );
106 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
);
109 $result = $importer->doImport();
110 $resultCount = $reporter->close();
112 if( WikiError
::isError( $result ) ) {
113 # No source or XML parse error
114 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
115 } elseif( WikiError
::isError( $resultCount ) ) {
117 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
120 $wgOut->addWikiMsg( 'importsuccess' );
122 $wgOut->addWikiText( '<hr />' );
126 private function showForm() {
127 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
128 $action = $wgTitle->getLocalUrl( 'action=submit' );
130 if( $wgUser->isAllowed( 'importupload' ) ) {
131 $wgOut->addWikiMsg( "importtext" );
133 Xml
::openElement( 'fieldset' ).
134 Xml
::element( 'legend', null, wfMsg( 'import-upload' ) ) .
135 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
136 Xml
::hidden( 'action', 'submit' ) .
137 Xml
::hidden( 'source', 'upload' ) .
138 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
139 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
140 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
141 Xml
::closeElement( 'form' ) .
142 Xml
::closeElement( 'fieldset' )
145 if( empty( $wgImportSources ) ) {
146 $wgOut->addWikiMsg( 'importnosources' );
150 if( !empty( $wgImportSources ) ) {
152 Xml
::openElement( 'fieldset' ) .
153 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
154 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
155 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
156 Xml
::hidden( 'action', 'submit' ) .
157 Xml
::hidden( 'source', 'interwiki' ) .
158 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
159 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
162 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
164 foreach( $wgImportSources as $prefix ) {
165 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
166 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
169 Xml
::closeElement( 'select' ) .
172 Xml
::input( 'frompage', 50, $this->frompage
) .
179 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
186 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
187 Xml
::namespaceSelector( $this->namespace, '' ) .
194 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
197 Xml
::closeElement( 'table' ).
198 Xml
::closeElement( 'form' ) .
199 Xml
::closeElement( 'fieldset' )
207 * @ingroup SpecialPage
209 class ImportReporter
{
210 function __construct( $importer, $upload, $interwiki ) {
211 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
212 $this->mPageCount
= 0;
213 $this->mIsUpload
= $upload;
214 $this->mInterwiki
= $interwiki;
219 $wgOut->addHTML( "<ul>\n" );
222 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
223 global $wgOut, $wgUser, $wgLang, $wgContLang;
225 $skin = $wgUser->getSkin();
229 $localCount = $wgLang->formatNum( $successCount );
230 $contentCount = $wgContLang->formatNum( $successCount );
232 if( $successCount > 0 ) {
233 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
234 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
238 $log = new LogPage( 'import' );
239 if( $this->mIsUpload
) {
240 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
242 $log->addEntry( 'upload', $title, $detail );
244 $interwiki = '[[:' . $this->mInterwiki
. ':' .
245 $origTitle->getPrefixedText() . ']]';
246 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
247 $contentCount, $interwiki );
248 $log->addEntry( 'interwiki', $title, $detail );
251 $comment = $detail; // quick
252 $dbw = wfGetDB( DB_MASTER
);
253 $latest = $title->getLatestRevID();
254 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
255 $nullRevision->insertOn( $dbw );
256 $article = new Article( $title );
258 $article->updateRevisionOn( $dbw, $nullRevision );
259 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest) );
261 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
267 if( $this->mPageCount
== 0 ) {
268 $wgOut->addHTML( "</ul>\n" );
269 return new WikiErrorMsg( "importnopages" );
271 $wgOut->addHTML( "</ul>\n" );
273 return $this->mPageCount
;