X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fspecials%2FSpecialImport.php;h=2ea030eda578202b15eb467b24d508b93a30ac94;hb=150c56d82cef1a4d241a5be93287d29daa76b91c;hp=6d9b2c872399eff92f951634637c0746ed6d8004;hpb=5b2926495100869f18741fbcae35f653dc893c09;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/specials/SpecialImport.php b/includes/specials/SpecialImport.php index 6d9b2c8723..2ea030eda5 100644 --- a/includes/specials/SpecialImport.php +++ b/includes/specials/SpecialImport.php @@ -1,7 +1,8 @@ + * Implements Special:Import + * + * Copyright © 2003,2005 Brion Vibber * http://www.mediawiki.org/ * * This program is free software; you can redistribute it and/or modify @@ -23,15 +24,21 @@ * @ingroup SpecialPage */ +/** + * MediaWiki page data importer + * + * @ingroup SpecialPage + */ class SpecialImport extends SpecialPage { - + private $interwiki = false; private $namespace; private $frompage = ''; private $logcomment= false; private $history = true; private $includeTemplates = false; - + private $pageLinkDepth; + /** * Constructor */ @@ -40,57 +47,81 @@ class SpecialImport extends SpecialPage { global $wgImportTargetNamespace; $this->namespace = $wgImportTargetNamespace; } - + /** * Execute */ function execute( $par ) { - global $wgRequest; - $this->setHeaders(); $this->outputHeader(); - - if ( wfReadOnly() ) { - global $wgOut; - $wgOut->readOnlyPage(); - return; + + $user = $this->getUser(); + if ( !$user->isAllowedAny( 'import', 'importupload' ) ) { + throw new PermissionsError( 'import' ); } - - if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) { + + # @todo Allow Title::getUserPermissionsErrors() to take an array + # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what + # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected' + $errors = wfMergeErrorArrays( + $this->getTitle()->getUserPermissionsErrors( + 'import', $user, true, + array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ) + ), + $this->getTitle()->getUserPermissionsErrors( + 'importupload', $user, true, + array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ) + ) + ); + + if ( $errors ) { + throw new PermissionsError( 'import', $errors ); + } + + $this->checkReadOnly(); + + $request = $this->getRequest(); + if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) { $this->doImport(); } $this->showForm(); } - + /** * Do the actual import */ private function doImport() { - global $wgOut, $wgRequest, $wgUser, $wgImportSources; + global $wgImportSources, $wgExportMaxLinkDepth; + $isUpload = false; - $this->namespace = $wgRequest->getIntOrNull( 'namespace' ); - $sourceName = $wgRequest->getVal( "source" ); + $request = $this->getRequest(); + $this->namespace = $request->getIntOrNull( 'namespace' ); + $sourceName = $request->getVal( "source" ); - $this->logcomment = $wgRequest->getText( 'log-comment' ); - $this->pageLinkDepth = $wgRequest->getIntOrNull( 'pagelink-depth' ); + $this->logcomment = $request->getText( 'log-comment' ); + $this->pageLinkDepth = $wgExportMaxLinkDepth == 0 ? 0 : $request->getIntOrNull( 'pagelink-depth' ); - if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'editToken' ) ) ) { - $source = new WikiErrorMsg( 'import-token-mismatch' ); + $user = $this->getUser(); + if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) { + $source = Status::newFatal( 'import-token-mismatch' ); } elseif ( $sourceName == 'upload' ) { $isUpload = true; - if( $wgUser->isAllowed( 'importupload' ) ) { + if( $user->isAllowed( 'importupload' ) ) { $source = ImportStreamSource::newFromUpload( "xmlimport" ); } else { - return $wgOut->permissionRequired( 'importupload' ); + throw new PermissionsError( 'importupload' ); } } elseif ( $sourceName == "interwiki" ) { - $this->interwiki = $wgRequest->getVal( 'interwiki' ); + if( !$user->isAllowed( 'import' ) ){ + throw new PermissionsError( 'import' ); + } + $this->interwiki = $request->getVal( 'interwiki' ); if ( !in_array( $this->interwiki, $wgImportSources ) ) { - $source = new WikiErrorMsg( "import-invalid-interwiki" ); + $source = Status::newFatal( "import-invalid-interwiki" ); } else { - $this->history = $wgRequest->getCheck( 'interwikiHistory' ); - $this->frompage = $wgRequest->getText( "frompage" ); - $this->includeTemplates = $wgRequest->getCheck( 'interwikiTemplates' ); + $this->history = $request->getCheck( 'interwikiHistory' ); + $this->frompage = $request->getText( "frompage" ); + $this->includeTemplates = $request->getCheck( 'interwikiTemplates' ); $source = ImportStreamSource::newFromInterwiki( $this->interwiki, $this->frompage, @@ -99,60 +130,65 @@ class SpecialImport extends SpecialPage { $this->pageLinkDepth ); } } else { - $source = new WikiErrorMsg( "importunknownsource" ); + $source = Status::newFatal( "importunknownsource" ); } - if( WikiError::isError( $source ) ) { - $wgOut->wrapWikiMsg( '

$1

', array( 'importfailed', $source->getMessage() ) ); + $out = $this->getOutput(); + if( !$source->isGood() ) { + $out->wrapWikiMsg( "

\n$1\n

", array( 'importfailed', $source->getWikiText() ) ); } else { - $wgOut->addWikiMsg( "importstart" ); + $out->addWikiMsg( "importstart" ); - $importer = new WikiImporter( $source ); + $importer = new WikiImporter( $source->value ); if( !is_null( $this->namespace ) ) { $importer->setTargetNamespace( $this->namespace ); } $reporter = new ImportReporter( $importer, $isUpload, $this->interwiki , $this->logcomment); + $reporter->setContext( $this->getContext() ); + $exception = false; $reporter->open(); - $result = $importer->doImport(); - $resultCount = $reporter->close(); + try { + $importer->doImport(); + } catch ( MWException $e ) { + $exception = $e; + } + $result = $reporter->close(); - if( WikiError::isError( $result ) ) { + if ( $exception ) { # No source or XML parse error - $wgOut->wrapWikiMsg( '

$1

', array( 'importfailed', $result->getMessage() ) ); - } elseif( WikiError::isError( $resultCount ) ) { + $out->wrapWikiMsg( "

\n$1\n

", array( 'importfailed', $exception->getMessage() ) ); + } elseif( !$result->isGood() ) { # Zero revisions - $wgOut->wrapWikiMsg( '

$1

', array( 'importfailed', $resultCount->getMessage() ) ); + $out->wrapWikiMsg( "

\n$1\n

", array( 'importfailed', $result->getWikiText() ) ); } else { # Success! - $wgOut->addWikiMsg( 'importsuccess' ); + $out->addWikiMsg( 'importsuccess' ); } - $wgOut->addWikiText( '
' ); + $out->addHTML( '
' ); } } private function showForm() { - global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources; - # FIXME: Quick hack to disable import for non privileged users /Raymond - # Regression from 43963 - if( !$wgUser->isAllowed( 'import' ) && !$wgUser->isAllowed( 'importupload' ) ) - return $wgOut->permissionRequired( 'import' ); - - $action = $wgTitle->getLocalUrl( 'action=submit' ); - - if( $wgUser->isAllowed( 'importupload' ) ) { - $wgOut->addWikiMsg( "importtext" ); - $wgOut->addHTML( - Xml::openElement( 'fieldset' ). - Xml::element( 'legend', null, wfMsg( 'import-upload' ) ) . - Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action ) ) . - Xml::hidden( 'action', 'submit' ) . - Xml::hidden( 'source', 'upload' ) . + global $wgImportSources, $wgExportMaxLinkDepth; + + $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ); + $user = $this->getUser(); + $out = $this->getOutput(); + + if( $user->isAllowed( 'importupload' ) ) { + $out->addWikiMsg( "importtext" ); + $out->addHTML( + Xml::fieldset( $this->msg( 'import-upload' )->text() ). + Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', + 'action' => $action, 'id' => 'mw-import-upload-form' ) ) . + Html::hidden( 'action', 'submit' ) . + Html::hidden( 'source', 'upload' ) . Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) . " " . - Xml::label( wfMsg( 'import-upload-filename' ), 'xmlimport' ) . + Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) . " " . Xml::input( 'xmlimport', 50, '', array( 'type' => 'file' ) ) . ' ' . @@ -160,7 +196,7 @@ class SpecialImport extends SpecialPage { " . - Xml::label( wfMsg( 'import-comment' ), 'mw-import-comment' ) . + Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) . " " . Xml::input( 'log-comment', 50, '', @@ -169,43 +205,56 @@ class SpecialImport extends SpecialPage { - " . - Xml::submitButton( wfMsg( 'uploadbtn' ) ) . + " . + Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) . " " . Xml::closeElement( 'table' ). - Xml::hidden( 'editToken', $wgUser->editToken() ) . + Html::hidden( 'editToken', $user->getEditToken() ) . Xml::closeElement( 'form' ) . Xml::closeElement( 'fieldset' ) ); } else { if( empty( $wgImportSources ) ) { - $wgOut->addWikiMsg( 'importnosources' ); + $out->addWikiMsg( 'importnosources' ); } } - if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) { - $wgOut->addHTML( - Xml::openElement( 'fieldset' ) . - Xml::element( 'legend', null, wfMsg( 'importinterwiki' ) ) . - Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action ) ) . - wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) . - Xml::hidden( 'action', 'submit' ) . - Xml::hidden( 'source', 'interwiki' ) . - Xml::hidden( 'editToken', $wgUser->editToken() ) . + if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) { + # Show input field for import depth only if $wgExportMaxLinkDepth > 0 + $importDepth = ''; + if( $wgExportMaxLinkDepth > 0 ) { + $importDepth = " + " . + $this->msg( 'export-pagelinks' )->parse() . + " + " . + Xml::input( 'pagelink-depth', 3, 0 ) . + " + "; + } + + $out->addHTML( + Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) . + Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) . + $this->msg( 'import-interwiki-text' )->parseAsBlock() . + Html::hidden( 'action', 'submit' ) . + Html::hidden( 'source', 'interwiki' ) . + Html::hidden( 'editToken', $user->getEditToken() ) . Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) . " " . - Xml::label( wfMsg( 'import-interwiki-source' ), 'interwiki' ) . + Xml::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) . " " . Xml::openElement( 'select', array( 'name' => 'interwiki' ) ) ); foreach( $wgImportSources as $prefix ) { $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : ''; - $wgOut->addHTML( Xml::option( $prefix, $prefix, $selected ) ); + $out->addHTML( Xml::option( $prefix, $prefix, $selected ) ); } - $wgOut->addHTML( + + $out->addHTML( Xml::closeElement( 'select' ) . Xml::input( 'frompage', 50, $this->frompage ) . " @@ -214,31 +263,37 @@ class SpecialImport extends SpecialPage { " . - Xml::checkLabel( wfMsg( 'import-interwiki-history' ), 'interwikiHistory', 'interwikiHistory', $this->history ) . + Xml::checkLabel( $this->msg( 'import-interwiki-history' )->text(), 'interwikiHistory', 'interwikiHistory', $this->history ) . " " . - Xml::checkLabel( wfMsg( 'import-interwiki-templates' ), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) . + Xml::checkLabel( $this->msg( 'import-interwiki-templates' )->text(), 'interwikiTemplates', 'interwikiTemplates', $this->includeTemplates ) . " + $importDepth - ".wfMsgExt( 'export-pagelinks', 'parseinline' )." - " . Xml::input( 'pagelink-depth', 20, 0 ) . "
- - - " . - Xml::label( wfMsg( 'import-interwiki-namespace' ), 'namespace' ) . + " . + Xml::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) . " " . - Xml::namespaceSelector( $this->namespace, '' ) . + Html::namespaceSelector( + array( + 'selected' => $this->namespace, + 'all' => '', + ), array( + 'name' => 'namespace', + 'id' => 'namespace', + 'class' => 'namespaceselector', + ) + ) . " " . - Xml::label( wfMsg( 'import-comment' ), 'mw-interwiki-comment' ) . + Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) . " " . Xml::input( 'log-comment', 50, '', @@ -248,8 +303,8 @@ class SpecialImport extends SpecialPage { - " . - Xml::submitButton( wfMsg( 'import-interwiki-submit' ), array( 'accesskey' => 's' ) ) . + " . + Xml::submitButton( $this->msg( 'import-interwiki-submit' )->text(), Linker::tooltipAndAccesskeyAttribs( 'import' ) ) . " " . Xml::closeElement( 'table' ). @@ -264,11 +319,18 @@ class SpecialImport extends SpecialPage { * Reporting callback * @ingroup SpecialPage */ -class ImportReporter { - private $reason=false; +class ImportReporter extends ContextSource { + private $reason=false; + private $mOriginalLogCallback = null; + private $mOriginalPageOutCallback = null; + private $mLogItemCount = 0; function __construct( $importer, $upload, $interwiki , $reason=false ) { - $importer->setPageOutCallback( array( $this, 'reportPage' ) ); + $this->mOriginalPageOutCallback = + $importer->setPageOutCallback( array( $this, 'reportPage' ) ); + $this->mOriginalLogCallback = + $importer->setLogItemCallback( array( $this, 'reportLogItem' ) ); + $importer->setNoticeCallback( array( $this, 'reportNotice' ) ); $this->mPageCount = 0; $this->mIsUpload = $upload; $this->mInterwiki = $interwiki; @@ -276,41 +338,60 @@ class ImportReporter { } function open() { - global $wgOut; - $wgOut->addHTML( "\n" ); + return Status::newFatal( 'importnopages' ); } - $wgOut->addHTML( "\n" ); + $out->addHTML( "\n" ); - return $this->mPageCount; + return Status::newGood( $this->mPageCount ); } }