Florianschmidtwelzow has uploaded a new change for review.
https://gerrit.wikimedia.org/r/179838
Change subject: Getting rid of some globals
......................................................................
Getting rid of some globals
wgUser/wgOut/wgRequest shouldn't be used, if there is a context available.
Change-Id: Ife4fca8ecf03780046634f5871192f6380291c10
---
M Captcha.php
M ConfirmEditHooks.php
M QuestyCaptcha.class.php
3 files changed, 75 insertions(+), 60 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ConfirmEdit
refs/changes/38/179838/1
diff --git a/Captcha.php b/Captcha.php
index 851936c..6cf78a0 100644
--- a/Captcha.php
+++ b/Captcha.php
@@ -91,15 +91,19 @@
* @return bool true to keep running callbacks
*/
function injectEmailUser( &$form ) {
- global $wgCaptchaTriggers, $wgOut, $wgUser;
+ global $wgCaptchaTriggers;
+
+ $user = $form->getUser();
+ $out = $form->getOutput();
+
if ( $wgCaptchaTriggers['sendemail'] ) {
- if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+ if ( $user->isAllowed( 'skipcaptcha' ) ) {
wfDebug( "ConfirmEdit: user group allows
skipping captcha on email sending\n" );
return true;
}
$form->addFooterText(
"<div class='captcha'>" .
- $wgOut->parse( $this->getMessage( 'sendemail' )
) .
+ $out->parse( $this->getMessage( 'sendemail' ) )
.
$this->getForm() .
"</div>\n" );
}
@@ -113,15 +117,19 @@
* @return bool true to keep running callbacks
*/
function injectUserCreate( &$template ) {
- global $wgCaptchaTriggers, $wgOut, $wgUser;
+ global $wgCaptchaTriggers;
+
+ $user = $template->getSkin()->getUser();
+ $out = $template->getSkin()->getOutput();
+
if ( $wgCaptchaTriggers['createaccount'] ) {
- if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+ if ( $user->isAllowed( 'skipcaptcha' ) ) {
wfDebug( "ConfirmEdit: user group allows
skipping captcha on account creation\n" );
return true;
}
$template->set( 'header',
"<div class='captcha'>" .
- $wgOut->parse( $this->getMessage(
'createaccount' ) ) .
+ $out->parse( $this->getMessage( 'createaccount'
) ) .
$this->getForm() .
"</div>\n" );
}
@@ -137,10 +145,11 @@
*/
function injectUserLogin( &$template ) {
if ( $this->isBadLoginTriggered() ) {
- global $wgOut;
+ $out = $template->getOutput();
+
$template->set( 'header',
"<div class='captcha'>" .
- $wgOut->parse( $this->getMessage( 'badlogin' )
) .
+ $out->parse( $this->getMessage( 'badlogin' ) ) .
$this->getForm() .
"</div>\n" );
}
@@ -158,6 +167,7 @@
*/
function triggerUserLogin( $user, $password, $retval ) {
global $wgCaptchaTriggers, $wgCaptchaBadLoginExpiration,
$wgMemc;
+
if ( $retval == LoginForm::WRONG_PASS &&
$wgCaptchaTriggers['badlogin'] ) {
$key = $this->badLoginKey();
$count = $wgMemc->get( $key );
@@ -249,9 +259,13 @@
* @param $isContent bool If true, $content is a Content object
* @return bool true if the captcha should run
*/
- function shouldCheck( WikiPage $page, $content, $section, $isContent =
false ) {
+ function shouldCheck( WikiPage $page, $content, $section,
$context,$isContent = false ) {
+ global $wgEmailAuthentication, $ceAllowConfirmedEmail,
$wgCaptchaRegexes;
+
$title = $page->getTitle();
$this->trigger = '';
+ $user = $context->getUser();
+
if ( $isContent ) {
if ( $content->getModel() == CONTENT_MODEL_WIKITEXT ) {
$newtext = $content->getNativeData();
@@ -262,27 +276,23 @@
$newtext = $content;
}
- global $wgUser;
- if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+ if ( $user->isAllowed( 'skipcaptcha' ) ) {
wfDebug( "ConfirmEdit: user group allows skipping
captcha\n" );
return false;
}
if ( $this->isIPWhitelisted() )
return false;
-
- global $wgEmailAuthentication, $ceAllowConfirmedEmail;
if ( $wgEmailAuthentication && $ceAllowConfirmedEmail &&
- $wgUser->isEmailConfirmed() ) {
+ $user->isEmailConfirmed() ) {
wfDebug( "ConfirmEdit: user has confirmed mail,
skipping captcha\n" );
return false;
}
if ( $this->captchaTriggers( $title, 'edit' ) ) {
// Check on all edits
- global $wgUser;
$this->trigger = sprintf( "edit trigger by '%s' at
[[%s]]",
- $wgUser->getName(),
+ $user->getName(),
$title->getPrefixedText() );
$this->action = 'edit';
wfDebug( "ConfirmEdit: checking all edits...\n" );
@@ -291,9 +301,8 @@
if ( $this->captchaTriggers( $title, 'create' ) &&
!$title->exists() ) {
// Check if creating a page
- global $wgUser;
$this->trigger = sprintf( "Create trigger by '%s' at
[[%s]]",
- $wgUser->getName(),
+ $user->getName(),
$title->getPrefixedText() );
$this->action = 'create';
wfDebug( "ConfirmEdit: checking on page creation...\n"
);
@@ -315,8 +324,8 @@
} else {
// Get link changes in the slowest way known to
man
$oldtext = $this->loadText( $title, $section );
- $oldLinks = $this->findLinks( $title, $oldtext
);
- $newLinks = $this->findLinks( $title, $newtext
);
+ $oldLinks = $this->findLinks( $title, $oldtext,
$user );
+ $newLinks = $this->findLinks( $title, $newtext,
$user );
}
$unknownLinks = array_filter( $newLinks, array( &$this,
'filterLink' ) );
@@ -324,10 +333,9 @@
$numLinks = count( $addedLinks );
if ( $numLinks > 0 ) {
- global $wgUser;
$this->trigger = sprintf( "%dx url trigger by
'%s' at [[%s]]: %s",
$numLinks,
- $wgUser->getName(),
+ $user->getName(),
$title->getPrefixedText(),
implode( ", ", $addedLinks ) );
$this->action = 'addurl';
@@ -335,7 +343,6 @@
}
}
- global $wgCaptchaRegexes;
if ( $newtext !== null && $wgCaptchaRegexes ) {
// Custom regex checks. Reuse $oldtext if set above.
$oldtext = isset( $oldtext ) ? $oldtext :
$this->loadText( $title, $section );
@@ -350,11 +357,10 @@
$numHits = count( $addedMatches );
if ( $numHits > 0 ) {
- global $wgUser;
$this->trigger = sprintf( "%dx
%s at [[%s]]: %s",
$numHits,
$regex,
- $wgUser->getName(),
+ $user->getName(),
$title->getPrefixedText(),
implode( ", ",
$addedMatches ) );
$this->action = 'edit';
@@ -494,18 +500,20 @@
* @param WikiPage $page
* @param $newtext string
* @param $section
+ * @param ContextSource $context
* @param $isContent bool
* @return bool false if the CAPTCHA is rejected, true otherwise
*/
- private function doConfirmEdit( WikiPage $page, $newtext, $section,
$isContent = false ) {
- global $wgRequest;
- if ( $wgRequest->getVal( 'captchaid' ) ) {
- $wgRequest->setVal( 'wpCaptchaId', $wgRequest->getVal(
'captchaid' ) );
+ private function doConfirmEdit( WikiPage $page, $newtext, $section,
$context, $isContent = false ) {
+ $request = $context->getRequest();
+
+ if ( $request->getVal( 'captchaid' ) ) {
+ $request->setVal( 'wpCaptchaId', $request->getVal(
'captchaid' ) );
}
- if ( $wgRequest->getVal( 'captchaword' ) ) {
- $wgRequest->setVal( 'wpCaptchaWord',
$wgRequest->getVal( 'captchaword' ) );
+ if ( $request->getVal( 'captchaword' ) ) {
+ $request->setVal( 'wpCaptchaWord', $request->getVal(
'captchaword' ) );
}
- if ( $this->shouldCheck( $page, $newtext, $section, $isContent
) ) {
+ if ( $this->shouldCheck( $page, $newtext, $section, $context,
$isContent ) ) {
return $this->passCaptcha();
} else {
wfDebug( "ConfirmEdit: no need to show captcha.\n" );
@@ -531,7 +539,7 @@
return true;
}
$page = $context->getWikiPage();
- if ( !$this->doConfirmEdit( $page, $content, false, true ) ) {
+ if ( !$this->doConfirmEdit( $page, $content, false, $context,
true ) ) {
if ( $legacyMode ) {
$status->fatal( 'hookaborted' );
}
@@ -546,7 +554,7 @@
function confirmEditAPI( $editPage, $newText, &$resultArr ) {
$page = $editPage->getArticle()->getPage();
- if ( !$this->doConfirmEdit( $page, $newText, false, false ) ) {
+ if ( !$this->doConfirmEdit( $page, $newText, false,
$editPage->getArticle()->getContext(), false ) ) {
$this->addCaptchaAPI( $resultArr );
return false;
}
@@ -586,10 +594,14 @@
*
* @return bool true to show captcha, false to skip captcha
*/
- function needCreateAccountCaptcha() {
- global $wgCaptchaTriggers, $wgUser;
+ function needCreateAccountCaptcha( $user = null ) {
+ global $wgCaptchaTriggers;
+ if ( is_null( $user ) ) {
+ global $wgUser;
+ $user = $wgUser;
+ }
if ( $wgCaptchaTriggers['createaccount'] ) {
- if ( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+ if ( $user->isAllowed( 'skipcaptcha' ) ) {
wfDebug( "ConfirmEdit: user group allows
skipping captcha on account creation\n" );
return false;
}
@@ -700,9 +712,10 @@
* @return bool if passed, false if failed or new session
*/
function passCaptcha() {
- $info = $this->retrieveCaptcha();
+ global $wgRequest;
+
+ $info = $this->retrieveCaptcha( $wgRequest );
if ( $info ) {
- global $wgRequest;
if ( $this->keyMatch( $wgRequest->getVal(
'wpCaptchaWord' ), $info ) ) {
$this->log( "passed" );
$this->clearCaptcha( $info );
@@ -748,11 +761,11 @@
/**
* Fetch this session's captcha info.
+ * @param WebRequest
* @return mixed array of info, or false if missing
*/
- function retrieveCaptcha() {
- global $wgRequest;
- $index = $wgRequest->getVal( 'wpCaptchaId' );
+ function retrieveCaptcha( $request ) {
+ $index = $request->getVal( 'wpCaptchaId' );
return CaptchaStore::get()->retrieve( $index );
}
@@ -790,13 +803,14 @@
* Extract a list of all recognized HTTP links in the text.
* @param $title Title
* @param $text string
+ * @param $user User
* @return array of strings
*/
- function findLinks( $title, $text ) {
- global $wgParser, $wgUser;
+ function findLinks( $title, $text, User $user ) {
+ global $wgParser;
$options = new ParserOptions();
- $text = $wgParser->preSaveTransform( $text, $title, $wgUser,
$options );
+ $text = $wgParser->preSaveTransform( $text, $title, $user,
$options );
$out = $wgParser->parse( $text, $title, $options );
return array_keys( $out->getExternalLinks() );
@@ -804,13 +818,13 @@
/**
* Show a page explaining what this wacky thing is.
+ * @param OutputPage
*/
- function showHelp() {
- global $wgOut;
- $wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text()
);
- $wgOut->addWikiMsg( 'captchahelp-text' );
+ function showHelp( OutputPage $out ) {
+ $out->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
+ $out->addWikiMsg( 'captchahelp-text' );
if ( CaptchaStore::get()->cookiesNeeded() ) {
- $wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
+ $out->addWikiMsg( 'captchahelp-cookies-needed' );
}
}
@@ -823,12 +837,12 @@
* @return hook return value
*/
function addNewAccountApiForm( $apiModule, $loginForm ) {
- global $wgRequest;
+ $request = $loginForm->getContext()->getRequest();
$main = $apiModule->getMain();
$id = $main->getVal( 'captchaid' );
if ( $id ) {
- $wgRequest->setVal( 'wpCaptchaId', $id );
+ $request->setVal( 'wpCaptchaId', $id );
// Suppress "unrecognized parameter" warning:
$main->getVal( 'wpCaptchaId' );
@@ -836,7 +850,7 @@
$word = $main->getVal( 'captchaword' );
if ( $word ) {
- $wgRequest->setVal( 'wpCaptchaWord', $word );
+ $request->setVal( 'wpCaptchaWord', $word );
// Suppress "unrecognized parameter" warning:
$main->getVal( 'wpCaptchaWord' );
@@ -854,7 +868,9 @@
* @return bool: Hook return value
*/
function addNewAccountApiResult( $apiModule, $loginPage, &$result ) {
- if ( $result['result'] !== 'Success' &&
$this->needCreateAccountCaptcha() ) {
+ $user = $loginPage->getContext()->getUser();
+
+ if ( $result['result'] !== 'Success' &&
$this->needCreateAccountCaptcha( $user ) ) {
// If we failed a captcha, override the generic
'Warning' result string
if ( $result['result'] === 'Warning' && isset(
$result['warnings'] ) ) {
diff --git a/ConfirmEditHooks.php b/ConfirmEditHooks.php
index 20f4207..d731a67 100644
--- a/ConfirmEditHooks.php
+++ b/ConfirmEditHooks.php
@@ -95,7 +95,7 @@
}
case "help":
default:
- return $instance->showHelp();
+ return $instance->showHelp(
$this->getContext()->getOutput() );
}
}
}
diff --git a/QuestyCaptcha.class.php b/QuestyCaptcha.class.php
index a7ab37b..00a232b 100644
--- a/QuestyCaptcha.class.php
+++ b/QuestyCaptcha.class.php
@@ -63,12 +63,11 @@
return wfMessage( $name, $text )->isDisabled() ? wfMessage(
'questycaptcha-edit' )->text() : $text;
}
- function showHelp() {
- global $wgOut;
- $wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text()
);
- $wgOut->addWikiMsg( 'questycaptchahelp-text' );
+ function showHelp( OutputPage $out ) {
+ $out->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
+ $out->addWikiMsg( 'questycaptchahelp-text' );
if ( CaptchaStore::get()->cookiesNeeded() ) {
- $wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
+ $out->addWikiMsg( 'captchahelp-cookies-needed' );
}
}
}
--
To view, visit https://gerrit.wikimedia.org/r/179838
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ife4fca8ecf03780046634f5871192f6380291c10
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ConfirmEdit
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits