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 ) {
48 global $wgRequest, $wgUser, $wgOut;
51 $this->outputHeader();
55 $wgOut->readOnlyPage();
59 if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) )
60 return $wgOut->permissionRequired( 'import' );
62 # TODO: allow Title::getUserPermissionsErrors() to take an array
63 # FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
64 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
65 $errors = wfMergeErrorArrays(
66 $this->getTitle()->getUserPermissionsErrors(
67 'import', $wgUser, true,
68 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
70 $this->getTitle()->getUserPermissionsErrors(
71 'importupload', $wgUser, true,
72 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
77 $wgOut->showPermissionsErrorPage( $errors );
81 if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) {
88 * Do the actual import
90 private function doImport() {
91 global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth;
93 $this->namespace = $wgRequest->getIntOrNull( 'namespace' );
94 $sourceName = $wgRequest->getVal( "source" );
96 $this->logcomment = $wgRequest->getText( 'log-comment' );
97 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $wgRequest->getIntOrNull( 'pagelink-depth' );
99 if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) {
100 $source = new WikiErrorMsg( 'import-token-mismatch' );
101 } elseif ( $sourceName == 'upload' ) {
103 if( $wgUser->isAllowed( 'importupload' ) ) {
104 $source = ImportStreamSource::newFromUpload( "xmlimport" );
106 return $wgOut->permissionRequired( 'importupload' );
108 } elseif ( $sourceName == "interwiki" ) {
109 if( !$wgUser->isAllowed( 'import' ) ){
110 return $wgOut->permissionRequired( 'import' );
112 $this->interwiki = $wgRequest->getVal( 'interwiki' );
113 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
114 $source = new WikiErrorMsg( "import-invalid-interwiki" );
116 $this->history = $wgRequest->getCheck( 'interwikiHistory' );
117 $this->frompage = $wgRequest->getText( "frompage" );
118 $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' );
119 $source = ImportStreamSource::newFromInterwiki(
123 $this->includeTemplates,
124 $this->pageLinkDepth );
127 $source = new WikiErrorMsg( "importunknownsource" );
130 if( WikiError::isError( $source ) ) {
131 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $source->getMessage() ) );
133 $wgOut->addWikiMsg( "importstart" );
135 $importer = new WikiImporter( $source );
136 if( !is_null( $this->namespace ) ) {
137 $importer->setTargetNamespace( $this->namespace );
139 $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment);
142 $result = $importer->doImport();
143 $resultCount = $reporter->close();
145 if( WikiError::isError( $result ) ) {
146 # No source or XML parse error
147 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $result->getMessage() ) );
148 } elseif( WikiError::isError( $resultCount ) ) {
150 $wgOut->wrapWikiMsg( '<p class="error">$1</p>', array( 'importfailed', $resultCount->getMessage() ) );
153 $wgOut->addWikiMsg( 'importsuccess' );
155 $wgOut->addWikiText( '<hr />' );
159 private function showForm() {
160 global $wgUser, $wgOut, $wgRequest, $wgImportSources, $wgExportMaxLinkDepth;
162 $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) );
164 if( $wgUser->isAllowed( 'importupload' ) ) {
165 $wgOut->addWikiMsg( "importtext" );
167 Xml::fieldset( wfMsg( 'import-upload' ) ).
168 Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post',
169 'action' => $action, 'id' => 'mw-import-upload-form' ) ) .
170 Xml::hidden( 'action', 'submit' ) .
171 Xml::hidden( 'source', 'upload' ) .
172 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
175 <td class='mw-label'>" .
176 Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) .
178 <td class='mw-input'>" .
179 Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' .
183 <td class='mw-label'>" .
184 Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) .
186 <td class='mw-input'>" .
187 Xml::input( 'log-comment', 50, '',
188 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
193 <td class='mw-submit'>" .
194 Xml::submitButton( wfMsg( 'uploadbtn' ) ) .
197 Xml::closeElement( 'table' ).
198 Xml::hidden( 'editToken', $wgUser->editToken() ) .
199 Xml::closeElement( 'form' ) .
200 Xml::closeElement( 'fieldset' )
203 if( empty( $wgImportSources ) ) {
204 $wgOut->addWikiMsg( 'importnosources' );
208 if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
209 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
211 if( $wgExportMaxLinkDepth > 0 ) {
213 <td class='mw-label'>" .
214 wfMsgExt( 'export-pagelinks', 'parseinline' ) .
216 <td class='mw-input'>" .
217 Xml::input( 'pagelink-depth', 3, 0 ) .
223 Xml::fieldset( wfMsg( 'importinterwiki' ) ) .
224 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) .
225 wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) .
226 Xml::hidden( 'action', 'submit' ) .
227 Xml::hidden( 'source', 'interwiki' ) .
228 Xml::hidden( 'editToken', $wgUser->editToken() ) .
229 Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) .
231 <td class='mw-label'>" .
232 Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) .
234 <td class='mw-input'>" .
235 Xml::openElement( 'select', array( 'name' => 'interwiki' ) )
237 foreach( $wgImportSources as $prefix ) {
238 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
239 $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) );
243 Xml::closeElement( 'select' ) .
244 Xml::input( 'frompage', 50, $this->frompage ) .
250 <td class='mw-input'>" .
251 Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) .
257 <td class='mw-input'>" .
258 Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) .
263 <td class='mw-label'>" .
264 Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) .
266 <td class='mw-input'>" .
267 Xml::namespaceSelector( $this->namespace, '' ) .
271 <td class='mw-label'>" .
272 Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) .
274 <td class='mw-input'>" .
275 Xml::input( 'log-comment', 50, '',
276 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
282 <td class='mw-submit'>" .
283 Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) .
286 Xml::closeElement( 'table' ).
287 Xml::closeElement( 'form' ) .
288 Xml::closeElement( 'fieldset' )
296 * @ingroup SpecialPage
298 class ImportReporter {
299 private $reason=false;
301 function __construct( $importer, $upload, $interwiki , $reason=false ) {
302 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
303 $this->mPageCount = 0;
304 $this->mIsUpload = $upload;
305 $this->mInterwiki = $interwiki;
306 $this->reason = $reason;
311 $wgOut->addHTML( "<ul>\n" );
314 function reportPage( $title, $origTitle, $revisionCount, $successCount ) {
315 global $wgOut, $wgUser, $wgLang, $wgContLang;
317 $skin = $wgUser->getSkin();
321 $localCount = $wgLang->formatNum( $successCount );
322 $contentCount = $wgContLang->formatNum( $successCount );
324 if( $successCount > 0 ) {
325 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
326 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
330 $log = new LogPage( 'import' );
331 if( $this->mIsUpload ) {
332 $detail = wfMsgExt( 'import-logentry-upload-detail', array( 'content', 'parsemag' ),
334 if ( $this->reason ) {
335 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
337 $log->addEntry( 'upload', $title, $detail );
339 $interwiki = '[[:' . $this->mInterwiki . ':' .
340 $origTitle->getPrefixedText() . ']]';
341 $detail = wfMsgExt( 'import-logentry-interwiki-detail', array( 'content', 'parsemag' ),
342 $contentCount, $interwiki );
343 if ( $this->reason ) {
344 $detail .= wfMsgForContent( 'colon-separator' ) . $this->reason;
346 $log->addEntry( 'interwiki', $title, $detail );
349 $comment = $detail; // quick
350 $dbw = wfGetDB( DB_MASTER );
351 $latest = $title->getLatestRevID();
352 $nullRevision = Revision::newNullRevision( $dbw, $title->getArticleId(), $comment, true );
353 $nullRevision->insertOn( $dbw );
354 $article = new Article( $title );
356 $article->updateRevisionOn( $dbw, $nullRevision );
357 wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser) );
359 $wgOut->addHTML( "<li>" . $skin->linkKnown( $title ) . " " .
360 wfMsgHtml( 'import-nonewrevisions' ) . "</li>\n" );
366 if( $this->mPageCount == 0 ) {
367 $wgOut->addHTML( "</ul>\n" );
368 return new WikiErrorMsg( "importnopages" );
370 $wgOut->addHTML( "</ul>\n" );
372 return $this->mPageCount;