From f7f99a379395dcd95e70ae2dde8ab1136f9c9097 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Wed, 7 Sep 2011 08:18:51 +0000 Subject: [PATCH] Use local context instead of global variables --- includes/specials/SpecialImport.php | 116 ++++++++++++++-------------- 1 file changed, 60 insertions(+), 56 deletions(-) diff --git a/includes/specials/SpecialImport.php b/includes/specials/SpecialImport.php index d964b9361a..165e337b39 100644 --- a/includes/specials/SpecialImport.php +++ b/includes/specials/SpecialImport.php @@ -52,18 +52,16 @@ class SpecialImport extends SpecialPage { * Execute */ function execute( $par ) { - global $wgRequest, $wgUser, $wgOut; - $this->setHeaders(); $this->outputHeader(); - if ( wfReadOnly() ) { - $wgOut->readOnlyPage(); - return; + $user = $this->getUser(); + if ( !$user->isAllowedAny( 'import', 'importupload' ) ) { + throw new PermissionsError( 'import' ); } - if( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) { - return $wgOut->permissionRequired( 'import' ); + if ( wfReadOnly() ) { + throw new ReadOnlyError; } # @todo Allow Title::getUserPermissionsErrors() to take an array @@ -71,21 +69,22 @@ class SpecialImport extends SpecialPage { # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected' $errors = wfMergeErrorArrays( $this->getTitle()->getUserPermissionsErrors( - 'import', $wgUser, true, + 'import', $user, true, array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ) ), $this->getTitle()->getUserPermissionsErrors( - 'importupload', $wgUser, true, + 'importupload', $user, true, array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ) ) ); if( $errors ){ - $wgOut->showPermissionsErrorPage( $errors ); + $this->getOutput()->showPermissionsErrorPage( $errors ); return; } - if ( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit' ) { + $request = $this->getRequest(); + if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) { $this->doImport(); } $this->showForm(); @@ -95,34 +94,37 @@ class SpecialImport extends SpecialPage { * Do the actual import */ private function doImport() { - global $wgOut, $wgRequest, $wgUser, $wgImportSources, $wgExportMaxLinkDepth; + 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 = $wgExportMaxLinkDepth == 0 ? 0 : $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' ) ) ) { + $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' ); + return $this->getOutput()->permissionRequired( 'importupload' ); } } elseif ( $sourceName == "interwiki" ) { - if( !$wgUser->isAllowed( 'import' ) ){ - return $wgOut->permissionRequired( 'import' ); + if( !$user->isAllowed( 'import' ) ){ + return $this->getOutput()->permissionRequired( 'import' ); } - $this->interwiki = $wgRequest->getVal( 'interwiki' ); + $this->interwiki = $request->getVal( 'interwiki' ); if ( !in_array( $this->interwiki, $wgImportSources ) ) { $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, @@ -134,16 +136,18 @@ class SpecialImport extends SpecialPage { $source = Status::newFatal( "importunknownsource" ); } + $out = $this->getOutput(); if( !$source->isGood() ) { - $wgOut->wrapWikiMsg( "

\n$1\n

", array( 'importfailed', $source->getWikiText() ) ); + $out->wrapWikiMsg( "

\n$1\n

", array( 'importfailed', $source->getWikiText() ) ); } else { - $wgOut->addWikiMsg( "importstart" ); + $out->addWikiMsg( "importstart" ); $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(); @@ -156,26 +160,28 @@ class SpecialImport extends SpecialPage { if ( $exception ) { # No source or XML parse error - $wgOut->wrapWikiMsg( "

\n$1\n

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

\n$1\n

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

\n$1\n

", array( 'importfailed', $result->getWikiText() ) ); + $out->wrapWikiMsg( "

\n$1\n

", array( 'importfailed', $result->getWikiText() ) ); } else { # Success! - $wgOut->addWikiMsg( 'importsuccess' ); + $out->addWikiMsg( 'importsuccess' ); } - $wgOut->addHTML( '
' ); + $out->addHTML( '
' ); } } private function showForm() { - global $wgUser, $wgOut, $wgImportSources, $wgExportMaxLinkDepth; + global $wgImportSources, $wgExportMaxLinkDepth; $action = $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ); + $user = $this->getUser(); + $out = $this->getOutput(); - if( $wgUser->isAllowed( 'importupload' ) ) { - $wgOut->addWikiMsg( "importtext" ); - $wgOut->addHTML( + if( $user->isAllowed( 'importupload' ) ) { + $out->addWikiMsg( "importtext" ); + $out->addHTML( Xml::fieldset( wfMsg( 'import-upload' ) ). Xml::openElement( 'form', array( 'enctype' => 'multipart/form-data', 'method' => 'post', 'action' => $action, 'id' => 'mw-import-upload-form' ) ) . @@ -207,17 +213,17 @@ class SpecialImport extends SpecialPage { " " . Xml::closeElement( 'table' ). - Html::hidden( 'editToken', $wgUser->editToken() ) . + Html::hidden( 'editToken', $user->editToken() ) . Xml::closeElement( 'form' ) . Xml::closeElement( 'fieldset' ) ); } else { if( empty( $wgImportSources ) ) { - $wgOut->addWikiMsg( 'importnosources' ); + $out->addWikiMsg( 'importnosources' ); } } - if( $wgUser->isAllowed( 'import' ) && !empty( $wgImportSources ) ) { + if( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) { # Show input field for import depth only if $wgExportMaxLinkDepth > 0 $importDepth = ''; if( $wgExportMaxLinkDepth > 0 ) { @@ -231,13 +237,13 @@ class SpecialImport extends SpecialPage { "; } - $wgOut->addHTML( + $out->addHTML( Xml::fieldset( wfMsg( 'importinterwiki' ) ) . Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'mw-import-interwiki-form' ) ) . wfMsgExt( 'import-interwiki-text', array( 'parse' ) ) . Html::hidden( 'action', 'submit' ) . Html::hidden( 'source', 'interwiki' ) . - Html::hidden( 'editToken', $wgUser->editToken() ) . + Html::hidden( 'editToken', $user->editToken() ) . Xml::openElement( 'table', array( 'id' => 'mw-import-table' ) ) . " " . @@ -248,10 +254,10 @@ class SpecialImport extends SpecialPage { ); 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 ) . " @@ -307,7 +313,7 @@ class SpecialImport extends SpecialPage { * Reporting callback * @ingroup SpecialPage */ -class ImportReporter { +class ImportReporter extends ContextSource { private $reason=false; private $mOriginalLogCallback = null; private $mOriginalPageOutCallback = null; @@ -325,8 +331,7 @@ class ImportReporter { } function open() { - global $wgOut; - $wgOut->addHTML( "\n" ); return Status::newFatal( 'importnopages' ); } - $wgOut->addHTML( "\n" ); + $out->addHTML( "\n" ); return Status::newGood( $this->mPageCount ); } -- 2.20.1