Add php code sniffer

Change-Id: I298b8b936a2b86deea75c302d88a7391cdb221c9
This commit is contained in:
Paladox 2015-10-28 15:52:04 +00:00
parent c3e6992bb3
commit 30490fba52
18 changed files with 113 additions and 48 deletions

View file

@ -108,8 +108,10 @@ $wgCaptchaTriggers['badlogin'] = true; // Special:Userlogin after failure
$wgCaptchaTriggersOnNamespace = array();
# Example:
# $wgCaptchaTriggersOnNamespace[NS_TALK]['create'] = false; //Allow creation of talk pages without captchas.
# $wgCaptchaTriggersOnNamespace[NS_PROJECT]['edit'] = true; //Show captcha whenever editing Project pages.
# // Allow creation of talk pages without captchas
# $wgCaptchaTriggersOnNamespace[NS_TALK]['create'] = false;
# // Show captcha whenever editing Project pages.
# $wgCaptchaTriggersOnNamespace[NS_PROJECT]['edit'] = true;
/**
* Indicate how to store per-session data required to match up the

View file

@ -12,7 +12,7 @@ class ApiFancyCaptchaReload extends ApiBase {
$captchaIndex = $captcha->getCaptchaIndex();
$result = $this->getResult();
$result->addValue( null, $this->getModuleName(), array ( 'index' => $captchaIndex ) );
$result->addValue( null, $this->getModuleName(), array( 'index' => $captchaIndex ) );
return true;
}

View file

@ -91,7 +91,7 @@ class FancyCaptcha extends SimpleCaptcha {
* @param OutputPage $out
*/
function getForm( OutputPage $out, $tabIndex = 1 ) {
global $wgOut, $wgEnableAPI;
global $wgEnableAPI;
// Uses addModuleStyles so it is loaded when JS is disabled.
$out->addModuleStyles( 'ext.confirmEdit.fancyCaptcha.styles' );
@ -145,7 +145,7 @@ class FancyCaptcha extends SimpleCaptcha {
); // tab in before the edit textarea
if ( $this->action == 'usercreate' ) {
// use raw element, because the message can contain links or some other html
$form .= HTML::rawelement( 'small',array(
$form .= HTML::rawelement( 'small', array(
'class' => 'mw-createacct-captcha-assisted'
), wfMessage( 'createacct-imgcaptcha-help' )->parse()
);
@ -229,7 +229,8 @@ class FancyCaptcha extends SimpleCaptcha {
$place = mt_rand( 0, count( $dirs ) - 1 ); // pick a random subdir
// In case all dirs are not filled, cycle through next digits...
for ( $j = 0; $j < count( $dirs ); $j++ ) {
$fancyCount = count( $dirs );
for ( $j = 0; $j < $fancyCount; $j++ ) {
$char = $dirs[( $place + $j ) % count( $dirs )];
$info = $this->pickImageDir( "$directory/$char", $levels - 1, $lockouts );
if ( $info ) {
@ -301,7 +302,8 @@ class FancyCaptcha extends SimpleCaptcha {
$backend = $this->getBackend();
$place = mt_rand( 0, count( $files ) - 1 ); // pick a random file
$misses = 0; // number of files in listing that don't actually exist
for ( $j = 0; $j < count( $files ); $j++ ) {
$fancyImageCount = count( $files );
for ( $j = 0; $j < $fancyImageCount; $j++ ) {
$entry = $files[( $place + $j ) % count( $files )];
if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
if ( $wgCaptchaDeleteOnSolve ) { // captcha will be deleted when solved

View file

@ -4,7 +4,8 @@ if ( function_exists( 'wfLoadExtension' ) ) {
// Keep i18n globals so mergeMessageFileList.php doesn't break
$wgMessagesDirs['FancyCaptcha'] = __DIR__ . '/i18n';
/* wfWarn(
'Deprecated PHP entry point used for FancyCaptcha extension. Please use wfLoadExtension instead, ' .
'Deprecated PHP entry point used for FancyCaptcha extension. ' .
'Please use wfLoadExtension instead, ' .
'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
); */
return;

View file

@ -25,7 +25,16 @@ class MathCaptcha extends SimpleCaptcha {
$index = $this->storeCaptcha( array( 'answer' => $answer ) );
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
$form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, array( 'tabindex' => $tabIndex, 'autocomplete' => 'off', 'required' ) ) . '</td></tr></table>';
$form .= '<td>' . Html::input(
'wpCaptchaWord',
false,
false,
array(
'tabindex' => $tabIndex,
'autocomplete' => 'off',
'required'
)
) . '</td></tr></table>';
$form .= Html::hidden( 'wpCaptchaId', $index );
return $form;
}
@ -45,7 +54,9 @@ class MathCaptcha extends SimpleCaptcha {
if ( class_exists( 'MathRenderer' ) ) {
$math = MathRenderer::getRenderer( $sum, array(), MW_MATH_PNG );
} else {
throw new Exception( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
throw new Exception(
'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.'
);
}
$html = $math->render();
return preg_replace( '/alt=".*?"/', '', $html );

View file

@ -4,7 +4,8 @@ if ( function_exists( 'wfLoadExtension' ) ) {
// Keep i18n globals so mergeMessageFileList.php doesn't break
$wgMessagesDirs['MathCaptcha'] = __DIR__ . '/i18n';
/* wfWarn(
'Deprecated PHP entry point used for MathCaptcha extension. Please use wfLoadExtension instead, ' .
'Deprecated PHP entry point used for MathCaptcha extension. ' .
'Please use wfLoadExtension instead, ' .
'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
); */
return;

View file

@ -31,7 +31,7 @@ class QuestyCaptcha extends SimpleCaptcha {
function getCaptcha() {
global $wgCaptchaQuestions;
//Backwards compatibility
// Backwards compatibility
if ( $wgCaptchaQuestions === array_values( $wgCaptchaQuestions ) ) {
return $wgCaptchaQuestions[ mt_rand( 0, count( $wgCaptchaQuestions ) - 1 ) ];
}
@ -44,7 +44,9 @@ class QuestyCaptcha extends SimpleCaptcha {
function getForm( OutputPage $out, $tabIndex = 1 ) {
$captcha = $this->getCaptcha();
if ( !$captcha ) {
die( "No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php." );
die(
"No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php."
);
}
$index = $this->storeCaptcha( $captcha );
return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
@ -68,7 +70,9 @@ class QuestyCaptcha extends SimpleCaptcha {
$text = wfMessage( $name )->text();
# Obtain a more tailored message, if possible, otherwise, fall back to
# the default for edits
return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'questycaptcha-edit' )->text() : $text;
return wfMessage(
$name, $text
)->isDisabled() ? wfMessage( 'questycaptcha-edit' )->text() : $text;
}
function showHelp() {

View file

@ -4,7 +4,8 @@ if ( function_exists( 'wfLoadExtension' ) ) {
// Keep i18n globals so mergeMessageFileList.php doesn't break
$wgMessagesDirs['QuestyCaptcha'] = __DIR__ . '/i18n';
/* wfWarn(
'Deprecated PHP entry point used for QuestyCaptcha extension. Please use wfLoadExtension instead, ' .
'Deprecated PHP entry point used for QuestyCaptcha extension. ' .
'Please use wfLoadExtension instead, ' .
'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
); */
return;

View file

@ -13,9 +13,13 @@ class ReCaptcha extends SimpleCaptcha {
global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
$useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
$js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => $tabIndex ) );
$js = 'var RecaptchaOptions = ' . Xml::encodeJsVar(
array( 'theme' => $wgReCaptchaTheme, 'tabindex' => $tabIndex )
);
return Html::inlineScript( $js ) . recaptcha_get_html( $wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps );
return Html::inlineScript(
$js
) . recaptcha_get_html( $wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps );
}
/**
@ -27,9 +31,14 @@ class ReCaptcha extends SimpleCaptcha {
function passCaptcha() {
global $wgReCaptchaPrivateKey, $wgRequest;
// API is hardwired to return wpCaptchaId and wpCaptchaWord, so use that if the standard two are empty
$challenge = $wgRequest->getVal( 'recaptcha_challenge_field', $wgRequest->getVal( 'wpCaptchaId' ) );
$response = $wgRequest->getVal( 'recaptcha_response_field', $wgRequest->getVal( 'wpCaptchaWord' ) );
// API is hardwired to return wpCaptchaId and wpCaptchaWord,
// so use that if the standard two are empty
$challenge = $wgRequest->getVal(
'recaptcha_challenge_field', $wgRequest->getVal( 'wpCaptchaId' )
);
$response = $wgRequest->getVal(
'recaptcha_response_field', $wgRequest->getVal( 'wpCaptchaWord' )
);
if ( $response === null ) {
// new captcha session

View file

@ -4,7 +4,8 @@ if ( function_exists( 'wfLoadExtension' ) ) {
// Keep i18n globals so mergeMessageFileList.php doesn't break
$wgMessagesDirs['ReCaptcha'] = __DIR__ . '/i18n';
/* wfWarn(
'Deprecated PHP entry point used for ReCaptcha extension. Please use wfLoadExtension instead, ' .
'Deprecated PHP entry point used for ReCaptcha extension. ' .
'Please use wfLoadExtension instead, ' .
'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
); */
return;

View file

@ -4,7 +4,8 @@ if ( function_exists( 'wfLoadExtension' ) ) {
// Keep i18n globals so mergeMessageFileList.php doesn't break
$wgMessagesDirs['ReCaptchaNoCaptcha'] = __DIR__ . '/i18n';
/* wfWarn(
'Deprecated PHP entry point used for ReCaptchaNoCaptcha extension. Please use wfLoadExtension instead, ' .
'Deprecated PHP entry point used for ReCaptchaNoCaptcha extension. ' .
'Please use wfLoadExtension instead, ' .
'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
); */
return;

View file

@ -153,8 +153,10 @@ class SimpleCaptcha {
$captcha = "<div class='captcha'>" .
$wgOut->parse( $this->getMessage( 'createaccount' ) ) .
// FIXME: Hardcoded tab index
// Usually, the CAPTCHA is added after the E-Mail address field, which actually has 6 as the tabIndex, but
// there may are wikis which allows to mention the "real name", which would have 7 as tabIndex, so increase
// Usually, the CAPTCHA is added after the E-Mail address field,
// which actually has 6 as the tabIndex, but
// there may are wikis which allows to mention the "real name",
// which would have 7 as tabIndex, so increase
// 6 by 2 and use it for the CAPTCHA -> 8 (the submit button has a tabIndex of 10)
$this->getForm( $wgOut, 8 ) .
"</div>\n";
@ -229,7 +231,9 @@ class SimpleCaptcha {
*/
function isBadLoginTriggered() {
global $wgMemc, $wgCaptchaTriggers, $wgCaptchaBadLoginAttempts;
return $wgCaptchaTriggers['badlogin'] && intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
return $wgCaptchaTriggers['badlogin'] && intval(
$wgMemc->get( $this->badLoginKey() )
) >= $wgCaptchaBadLoginAttempts;
}
/**
@ -303,7 +307,9 @@ class SimpleCaptcha {
* @return bool true if the captcha should run
*/
function shouldCheck( WikiPage $page, $content, $section, $context, $oldtext = null ) {
// @codingStandardsIgnoreStart
global $ceAllowConfirmedEmail;
// @codingStandardsIgnoreEnd
if ( !$context instanceof IContextSource ) {
$context = RequestContext::getMain();
@ -349,7 +355,7 @@ class SimpleCaptcha {
return true;
}
if ( $this->captchaTriggers( $title, 'create' ) && !$title->exists() ) {
if ( $this->captchaTriggers( $title, 'create' ) && !$title->exists() ) {
// Check if creating a page
$this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
$user->getName(),
@ -359,9 +365,12 @@ class SimpleCaptcha {
return true;
}
// The following checks are expensive and should be done only, if we can assume, that the edit will be saved
// The following checks are expensive and should be done only,
// if we can assume, that the edit will be saved
if ( !$request->wasPosted() ) {
wfDebug( "ConfirmEdit: request not posted, assuming that no content will be saved -> no CAPTCHA check" );
wfDebug(
"ConfirmEdit: request not posted, assuming that no content will be saved -> no CAPTCHA check"
);
return false;
}
@ -402,7 +411,9 @@ class SimpleCaptcha {
global $wgCaptchaRegexes;
if ( $newtext !== null && $wgCaptchaRegexes ) {
if ( !is_array( $wgCaptchaRegexes ) ) {
throw new UnexpectedValueException( '$wgCaptchaRegexes is required to be an array, ' . gettype( $wgCaptchaRegexes ) . ' given.' );
throw new UnexpectedValueException(
'$wgCaptchaRegexes is required to be an array, ' . gettype( $wgCaptchaRegexes ) . ' given.'
);
}
// Custom regex checks. Reuse $oldtext if set above.
$oldtext = isset( $oldtext ) ? $oldtext : $this->loadText( $title, $section );
@ -606,7 +617,8 @@ class SimpleCaptcha {
// otherwise it's an unknown page where this function is called from
$title = 'unknown';
}
// log this error, it could be a problem in another extension, edits should always have a WikiPage if
// log this error, it could be a problem in another extension,
// edits should always have a WikiPage if
// they go through EditFilterMergedContent.
wfDebug( __METHOD__ . ': Skipped ConfirmEdit check: No WikiPage for title ' . $title );
return true;
@ -660,7 +672,9 @@ class SimpleCaptcha {
if ( $this->needCreateAccountCaptcha() ) {
$this->trigger = "new account '" . $u->getName() . "'";
$success = $this->passCaptchaLimited();
LoggerFactory::getInstance( 'authmanager' )->info( 'Captcha submitted on account creation', array(
LoggerFactory::getInstance(
'authmanager'
)->info( 'Captcha submitted on account creation', array(
'event' => 'captcha.submit',
'type' => 'accountcreation',
'successful' => $success,
@ -834,7 +848,8 @@ class SimpleCaptcha {
function passCaptcha() {
global $wgRequest;
// Don't check the same CAPTCHA twice in one session, if the CAPTCHA was already checked - Bug T94276
// Don't check the same CAPTCHA twice in one session,
// if the CAPTCHA was already checked - Bug T94276
if ( isset( $this->captchaSolved ) ) {
return $this->captchaSolved;
}
@ -1005,7 +1020,9 @@ class SimpleCaptcha {
$this->addCaptchaAPI( $result );
$result['result'] = 'NeedCaptcha';
LoggerFactory::getInstance( 'authmanager' )->info( 'Captcha data added in account creation API', array(
LoggerFactory::getInstance(
'authmanager'
)->info( 'Captcha data added in account creation API', array(
'event' => 'captcha.display',
'type' => 'accountcreation',
) );

View file

@ -1,10 +1,12 @@
{
"require-dev": {
"jakub-onderka/php-parallel-lint": "0.9"
"jakub-onderka/php-parallel-lint": "0.9",
"mediawiki/mediawiki-codesniffer": "0.4.0"
},
"scripts": {
"test": [
"parallel-lint . --exclude vendor"
"parallel-lint . --exclude vendor",
"phpcs -p -s"
]
}
}

View file

@ -6,26 +6,26 @@ abstract class CaptchaStore {
* @param $index String
* @param $info String the captcha result
*/
public abstract function store( $index, $info );
abstract public function store( $index, $info );
/**
* Retrieve the answer for a given captcha
* @param $index String
* @return String
*/
public abstract function retrieve( $index );
abstract public function retrieve( $index );
/**
* Delete a result once the captcha has been used, so it cannot be reused
* @param $index
*/
public abstract function clear( $index );
abstract public function clear( $index );
/**
* Whether this type of CaptchaStore needs cookies
* @return Bool
*/
public abstract function cookiesNeeded();
abstract public function cookiesNeeded();
/**
* The singleton instance
@ -39,7 +39,7 @@ abstract class CaptchaStore {
* @throws Exception
* @return CaptchaStore
*/
public final static function get() {
final public static function get() {
if ( !self::$instance instanceof self ) {
global $wgCaptchaStorageClass;
if ( in_array( 'CaptchaStore', class_parents( $wgCaptchaStorageClass ) ) ) {
@ -54,7 +54,8 @@ abstract class CaptchaStore {
/**
* Protected constructor: no creating instances except through the factory method above
*/
protected function __construct() {}
protected function __construct() {
}
}
class CaptchaSessionStore extends CaptchaStore {

View file

@ -145,7 +145,7 @@ class ConfirmEditHooks {
* FIXME: This should be done in a better way, e.g. only load the libraray, if really needed.
*/
public static function onReCaptchaSetup() {
require_once( __DIR__ . '/../ReCaptcha/recaptchalib.php' );
require_once ( __DIR__ . '/../ReCaptcha/recaptchalib.php' );
}
/**
@ -154,7 +154,9 @@ class ConfirmEditHooks {
*/
public static function efReCaptcha() {
global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
// @codingStandardsIgnoreStart
global $recaptcha_public_key, $recaptcha_private_key;
// @codingStandardsIgnoreEnd
global $wgServerName;
// Backwards compatibility
@ -166,9 +168,10 @@ class ConfirmEditHooks {
}
if ( $wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '' ) {
die ( 'You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
die (
'You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
"use the reCAPTCHA plugin. You can sign up for a key <a href='" .
htmlentities( recaptcha_get_signup_url ( $wgServerName, "mediawiki" ) ) . "'>here</a>." );
htmlentities( recaptcha_get_signup_url( $wgServerName, "mediawiki" ) ) . "'>here</a>." );
}
}
}

View file

@ -9,7 +9,7 @@ class CaptchaSpecialPage extends UnlistedSpecialPage {
$instance = ConfirmEditHooks::getInstance();
switch( $par ) {
switch ( $par ) {
case "image":
if ( method_exists( $instance, 'showImage' ) ) {
return $instance->showImage();

View file

@ -24,10 +24,10 @@
if ( getenv( 'MW_INSTALL_PATH' ) ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$IP = dirname(__FILE__).'/../../..';
$IP = dirname( __FILE__ ).'/../../..';
}
require_once( "$IP/maintenance/Maintenance.php" );
require_once ( "$IP/maintenance/Maintenance.php" );
/**
* Maintenance script to change the password of a given user.
@ -127,4 +127,4 @@ class GenerateFancyCaptchas extends Maintenance {
}
$maintClass = "GenerateFancyCaptchas";
require_once( RUN_MAINTENANCE_IF_MAIN );
require_once ( RUN_MAINTENANCE_IF_MAIN );

9
phpcs.xml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<ruleset>
<rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/>
<file>.</file>
<arg name="extensions" value="php,php5,inc"/>
<arg name="encoding" value="utf8"/>
<exclude-pattern>ReCaptcha/recaptchalib.php</exclude-pattern>
<exclude-pattern>vendor</exclude-pattern>
</ruleset>