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, $wgExportMaxLinkDepth;
71 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
72 $sourceName = $wgRequest->getVal( "source" );
74 $this->logcomment
= $wgRequest->getText( 'log-comment' );
75 $this->pageLinkDepth
= $wgExportMaxLinkDepth == 0 ?
0 : $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, $wgImportSources, $wgExportMaxLinkDepth;
136 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
137 return $wgOut->permissionRequired( 'import' );
139 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
141 if( $wgUser->isAllowed( 'importupload' ) ) {
142 $wgOut->addWikiMsg( "importtext" );
144 Xml
::fieldset( wfMsg( 'import-upload' ) ).
145 Xml
::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
146 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
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-submit'>" .
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 ) ) {
186 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
188 if( $wgExportMaxLinkDepth > 0 ) {
190 <td class='mw-label'>" .
191 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
193 <td class='mw-input'>" .
194 Xml
::input( 'pagelink-depth', 3, 0 ) .
200 Xml
::fieldset( wfMsg( 'importinterwiki' ) ) .
201 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
202 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
203 Xml
::hidden( 'action', 'submit' ) .
204 Xml
::hidden( 'source', 'interwiki' ) .
205 Xml
::hidden( 'editToken', $wgUser->editToken() ) .
206 Xml
::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
208 <td class='mw-label'>" .
209 Xml
::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
211 <td class='mw-input'>" .
212 Xml
::openElement( 'select', array( 'name' => 'interwiki' ) )
214 foreach( $wgImportSources as $prefix ) {
215 $selected = ( $this->interwiki
=== $prefix ) ?
' selected="selected"' : '';
216 $wgOut->addHTML( Xml
::option( $prefix, $prefix, $selected ) );
220 Xml
::closeElement( 'select' ) .
221 Xml
::input( 'frompage', 50, $this->frompage
) .
227 <td class='mw-input'>" .
228 Xml
::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history
) .
234 <td class='mw-input'>" .
235 Xml
::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates
) .
240 <td class='mw-label'>" .
241 Xml
::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
243 <td class='mw-input'>" .
244 Xml
::namespaceSelector( $this->namespace, '' ) .
248 <td class='mw-label'>" .
249 Xml
::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
251 <td class='mw-input'>" .
252 Xml
::input( 'log-comment', 50, '',
253 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
259 <td class='mw-submit'>" .
260 Xml
::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
263 Xml
::closeElement( 'table' ).
264 Xml
::closeElement( 'form' ) .
265 Xml
::closeElement( 'fieldset' )
273 * @ingroup SpecialPage
275 class ImportReporter
{
276 private $reason=false;
278 function __construct( $importer, $upload, $interwiki , $reason=false ) {
279 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
280 $this->mPageCount
= 0;
281 $this->mIsUpload
= $upload;
282 $this->mInterwiki
= $interwiki;
283 $this->reason
= $reason;
288 $wgOut->addHTML( "<ul>\n" );
291 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
292 global $wgOut, $wgUser, $wgLang, $wgContLang;
294 $skin = $wgUser->getSkin();
298 $localCount = $wgLang->formatNum( $successCount );
299 $contentCount = $wgContLang->formatNum( $successCount );
301 if( $successCount > 0 ) {
302 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
303 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
307 $log = new LogPage( 'import' );
308 if( $this->mIsUpload
) {
309 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
311 if ( $this->reason
) {
312 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
314 $log->addEntry( 'upload', $title, $detail );
316 $interwiki = '[[:' . $this->mInterwiki
. ':' .
317 $origTitle->getPrefixedText() . ']]';
318 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
319 $contentCount, $interwiki );
320 if ( $this->reason
) {
321 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason
;
323 $log->addEntry( 'interwiki', $title, $detail );
326 $comment = $detail; // quick
327 $dbw = wfGetDB( DB_MASTER
);
328 $latest = $title->getLatestRevID();
329 $nullRevision = Revision
::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
330 $nullRevision->insertOn( $dbw );
331 $article = new Article( $title );
333 $article->updateRevisionOn( $dbw, $nullRevision );
334 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
336 $wgOut->addHTML( '<li>' . wfMsgHtml( 'import-nonewrevisions' ) . '</li>' );
342 if( $this->mPageCount
== 0 ) {
343 $wgOut->addHTML( "</ul>\n" );
344 return new WikiErrorMsg( "importnopages" );
346 $wgOut->addHTML( "</ul>\n" );
348 return $this->mPageCount
;