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' );
76 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
77 $source = new WikiErrorMsg( 'import-token-mismatch' );
78 } elseif ( $sourceName == 'upload' ) {
80 if( $wgUser->isAllowed( 'importupload' ) ) {
81 $source = ImportStreamSource
::newFromUpload( "xmlimport" );
83 return $wgOut->permissionRequired( 'importupload' );
85 } elseif ( $sourceName == "interwiki" ) {
86 $this->interwiki
= $wgRequest->getVal( 'interwiki' );
87 if ( !in_array( $this->interwiki
, $wgImportSources ) ) {
88 $source = new WikiErrorMsg( "import-invalid-interwiki" );
90 $this->history
= $wgRequest->getCheck( 'interwikiHistory' );
91 $this->frompage
= $wgRequest->getText( "frompage" );
92 $this->includeTemplates
= $wgRequest->getCheck( 'interwikiTemplates' );
93 $source = ImportStreamSource
::newFromInterwiki(
97 $this->includeTemplates
);
100 $source = new WikiErrorMsg( "importunknownsource" );
103 if( WikiError
::isError( $source ) ) {
104 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
106 $wgOut->addWikiMsg( "importstart" );
108 $importer = new WikiImporter( $source );
109 if( !is_null( $this->namespace ) ) {
110 $importer->setTargetNamespace( $this->namespace );
112 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki
, $this->logcomment
);
115 $result = $importer->doImport();
116 $resultCount = $reporter->close();
118 if( WikiError
::isError( $result ) ) {
119 # No source or XML parse error
120 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
121 } elseif( WikiError
::isError( $resultCount ) ) {
123 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
126 $wgOut->addWikiMsg( 'importsuccess' );
128 $wgOut->addWikiText( '<hr />' );
132 private function showForm() {
133 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
134 # FIXME: Quick hack to disable import for non privileged users /Raymond
135 # Regression from 43963
136 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
137 return $wgOut->permissionRequired( 'import' );
139 $action = $wgTitle->getLocalUrl( 'action=submit' );
141 if( $wgUser->isAllowed( 'importupload' ) ) {
142 $wgOut->addWikiMsg( "importtext" );
144 Xml
::openElement( 'fieldset' ).
145 Xml
::element( 'legend', null, wfMsg( 'import-upload' ) ) .
146 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) .
147 Xml
::hidden( 'action', 'submit' ) .
148 Xml
::hidden( 'source', 'upload' ) .
149 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
152 <td class='mw-label'>" .
153 Xml
::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
155 <td class='mw-input'>" .
156 Xml
::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
160 <td class='mw-label'>" .
161 Xml
::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
163 <td class='mw-input'>" .
164 Xml
::input( 'log-comment', 50, '',
165 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
170 <td class='mw-input'>" .
171 Xml
::submitButton( wfMsg( 'uploadbtn' ) ) .
174 Xml
::closeElement( 'table' ).
175 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
176 Xml
::closeElement( 'form' ) .
177 Xml
::closeElement( 'fieldset' )
180 if( empty( $wgImportSources ) ) {
181 $wgOut->addWikiMsg( 'importnosources' );
185 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
187 Xml
::openElement( 'fieldset' ) .
188 Xml
::element( 'legend', null, wfMsg( 'importinterwiki' ) ) .
189 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) .
190 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
191 Xml
::hidden( 'action', 'submit' ) .
192 Xml
::hidden( 'source', 'interwiki' ) .
193 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
194 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
196 <td class='mw-label'>" .
197 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
199 <td class='mw-input'>" .
200 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
202 foreach( $wgImportSources as $prefix ) {
203 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
204 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
207 Xml
::closeElement( 'select' ) .
208 Xml
::input( 'frompage', 50, $this->frompage
) .
214 <td class='mw-input'>" .
215 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
221 <td class='mw-input'>" .
222 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
226 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
228 <td class='mw-input'>" .
229 Xml
::namespaceSelector( $this->namespace, '' ) .
233 <td class='mw-label'>" .
234 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
236 <td class='mw-input'>" .
237 Xml
::input( 'log-comment', 50, '',
238 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
244 <td class='mw-input'>" .
245 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
248 Xml
::closeElement( 'table' ).
249 Xml
::closeElement( 'form' ) .
250 Xml
::closeElement( 'fieldset' )
258 * @ingroup SpecialPage
260 class ImportReporter
{
261 private $reason=false;
263 function __construct( $importer, $upload, $interwiki , $reason=false ) {
264 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
265 $this->mPageCount
= 0;
266 $this->mIsUpload
= $upload;
267 $this->mInterwiki
= $interwiki;
268 $this->reason
= $reason;
273 $wgOut->addHTML( "<ul>\n" );
276 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
277 global $wgOut, $wgUser, $wgLang, $wgContLang;
279 $skin = $wgUser->getSkin();
283 $localCount = $wgLang->formatNum( $successCount );
284 $contentCount = $wgContLang->formatNum( $successCount );
286 if( $successCount > 0 ) {
287 $wgOut->addHTML( "<li>" . $skin->makeKnownLinkObj( $title ) . " " .
288 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
292 $log = new LogPage( 'import' );
293 if( $this->mIsUpload
) {
294 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
296 if ( $this->reason
) {
297 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
299 $log->addEntry( 'upload', $title, $detail );
301 $interwiki = '[[:' . $this->mInterwiki
. ':' .
302 $origTitle->getPrefixedText() . ']]';
303 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
304 $contentCount, $interwiki );
305 if ( $this->reason
) {
306 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
308 $log->addEntry( 'interwiki', $title, $detail );
311 $comment = $detail; // quick
312 $dbw = wfGetDB( DB_MASTER
);
313 $latest = $title->getLatestRevID();
314 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
315 $nullRevision->insertOn( $dbw );
316 $article = new Article( $title );
318 $article->updateRevisionOn( $dbw, $nullRevision );
319 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
321 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
327 if( $this->mPageCount
== 0 ) {
328 $wgOut->addHTML( "</ul>\n" );
329 return new WikiErrorMsg( "importnopages" );
331 $wgOut->addHTML( "</ul>\n" );
333 return $this->mPageCount
;