Add phpcs and make pass

Change-Id: If5a2ec9700148eb842d6580945e51e1942a2e0ed
This commit is contained in:
Umherirrender 2017-06-06 18:18:36 +02:00
parent 968c6e8cd2
commit 55d068708a
8 changed files with 92 additions and 71 deletions

View file

@ -54,10 +54,10 @@ class TitleBlacklistHooks {
$blacklisted = TitleBlacklist::singleton()->userCannot( $title, $user, $action ); $blacklisted = TitleBlacklist::singleton()->userCannot( $title, $user, $action );
if ( $blacklisted instanceof TitleBlacklistEntry ) { if ( $blacklisted instanceof TitleBlacklistEntry ) {
$errmsg = $blacklisted->getErrorMessage( 'edit' ); $errmsg = $blacklisted->getErrorMessage( 'edit' );
$params = array( $params = [
$blacklisted->getRaw(), $blacklisted->getRaw(),
$title->getFullText() $title->getFullText()
); ];
ApiResult::setIndexedTagName( $params, 'param' ); ApiResult::setIndexedTagName( $params, 'param' );
$result = ApiMessage::create( $result = ApiMessage::create(
wfMessage( wfMessage(
@ -66,18 +66,18 @@ class TitleBlacklistHooks {
$title->getFullText() $title->getFullText()
), ),
'titleblacklist-forbidden', 'titleblacklist-forbidden',
array( [
'message' => array( 'message' => [
'key' => $errmsg, 'key' => $errmsg,
'params' => $params, 'params' => $params,
), ],
'line' => $blacklisted->getRaw(), 'line' => $blacklisted->getRaw(),
// As $errmsg usually represents a non-default message here, and ApiBase // As $errmsg usually represents a non-default message here, and ApiBase
// uses ->inLanguage( 'en' )->useDatabase( false ) for all messages, it will // uses ->inLanguage( 'en' )->useDatabase( false ) for all messages, it will
// never result in useful 'info' text in the API. Try this, extra data seems // never result in useful 'info' text in the API. Try this, extra data seems
// to override the default. // to override the default.
'info' => 'TitleBlacklist prevents this title from being created', 'info' => 'TitleBlacklist prevents this title from being created',
) ]
); );
return false; return false;
} }
@ -265,12 +265,12 @@ class TitleBlacklistHooks {
$errlines = '* <code>' . $errlines = '* <code>' .
implode( "</code>\n* <code>", array_map( 'wfEscapeWikiText', $ok ) ) . implode( "</code>\n* <code>", array_map( 'wfEscapeWikiText', $ok ) ) .
'</code>'; '</code>';
$error = Html::openElement( 'div', array( 'class' => 'errorbox' ) ) . $error = Html::openElement( 'div', [ 'class' => 'errorbox' ] ) .
$errmsg . $errmsg .
"\n" . "\n" .
$errlines . $errlines .
Html::closeElement( 'div' ) . "\n" . Html::closeElement( 'div' ) . "\n" .
Html::element( 'br', array( 'clear' => 'all' ) ) . "\n"; Html::element( 'br', [ 'clear' => 'all' ] ) . "\n";
// $error will be displayed by the edit class // $error will be displayed by the edit class
} }
@ -283,8 +283,8 @@ class TitleBlacklistHooks {
* @param Article $article * @param Article $article
*/ */
public static function clearBlacklist( &$article, &$user, public static function clearBlacklist( &$article, &$user,
$content, $summary, $isminor, $iswatch, $section ) $content, $summary, $isminor, $iswatch, $section
{ ) {
$title = $article->getTitle(); $title = $article->getTitle();
if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) { if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) {
TitleBlacklist::singleton()->invalidate(); TitleBlacklist::singleton()->invalidate();
@ -311,10 +311,10 @@ class TitleBlacklistHooks {
*/ */
public static function onAPIGetAllowedParams( ApiBase &$module, array &$params ) { public static function onAPIGetAllowedParams( ApiBase &$module, array &$params ) {
if ( $module instanceof ApiCreateAccount ) { if ( $module instanceof ApiCreateAccount ) {
$params['ignoretitleblacklist'] = array( $params['ignoretitleblacklist'] = [
ApiBase::PARAM_TYPE => 'boolean', ApiBase::PARAM_TYPE => 'boolean',
ApiBase::PARAM_DFLT => false ApiBase::PARAM_DFLT => false
); ];
} }
return true; return true;
@ -356,9 +356,9 @@ class TitleBlacklistHooks {
$logEntry = new ManualLogEntry( 'titleblacklist', 'hit-username' ); $logEntry = new ManualLogEntry( 'titleblacklist', 'hit-username' );
$logEntry->setPerformer( $user ); $logEntry->setPerformer( $user );
$logEntry->setTarget( $title ); $logEntry->setTarget( $title );
$logEntry->setParameters( array( $logEntry->setParameters( [
'4::entry' => $entry, '4::entry' => $entry,
) ); ] );
$logid = $logEntry->insert(); $logid = $logEntry->insert();
$logEntry->publish( $logid ); $logEntry->publish( $logid );
} }
@ -372,7 +372,7 @@ class TitleBlacklistHooks {
* @return bool * @return bool
*/ */
public static function scribuntoExternalLibraries( $engine, array &$extraLibraries ) { public static function scribuntoExternalLibraries( $engine, array &$extraLibraries ) {
if( $engine == 'lua' ) { if ( $engine == 'lua' ) {
$extraLibraries['mw.ext.TitleBlacklist'] = 'Scribunto_LuaTitleBlacklistLibrary'; $extraLibraries['mw.ext.TitleBlacklist'] = 'Scribunto_LuaTitleBlacklistLibrary';
} }
return true; return true;

View file

@ -2,12 +2,12 @@
class Scribunto_LuaTitleBlacklistLibrary extends Scribunto_LuaLibraryBase { class Scribunto_LuaTitleBlacklistLibrary extends Scribunto_LuaLibraryBase {
public function register() { public function register() {
$lib = array( $lib = [
'test' => array( $this, 'test' ), 'test' => [ $this, 'test' ],
); ];
return $this->getEngine()->registerInterface( return $this->getEngine()->registerInterface(
__DIR__ . '/mw.ext.TitleBlacklist.lua', $lib, array() __DIR__ . '/mw.ext.TitleBlacklist.lua', $lib, []
); );
} }
@ -20,16 +20,16 @@ class Scribunto_LuaTitleBlacklistLibrary extends Scribunto_LuaLibraryBase {
} }
$entry = TitleBlacklist::singleton()->isBlacklisted( $title, $action ); $entry = TitleBlacklist::singleton()->isBlacklisted( $title, $action );
if ( $entry ) { if ( $entry ) {
return array( array( return [ [
'params' => $entry->getParams(), 'params' => $entry->getParams(),
'regex' => $entry->getRegex(), 'regex' => $entry->getRegex(),
'raw' => $entry->getRaw(), 'raw' => $entry->getRaw(),
'version' => $entry->getFormatVersion(), 'version' => $entry->getFormatVersion(),
'message' => $entry->getErrorMessage( $action ), 'message' => $entry->getErrorMessage( $action ),
'custommessage' => $entry->getCustomMessage() 'custommessage' => $entry->getCustomMessage()
) ); ] ];
} }
return array( null ); return [ null ];
} }
} }

View file

@ -7,7 +7,6 @@
* @file * @file
*/ */
//@{
/** /**
* @ingroup Extensions * @ingroup Extensions
*/ */
@ -70,9 +69,9 @@ class TitleBlacklist {
} }
$sources = $wgTitleBlacklistSources; $sources = $wgTitleBlacklistSources;
$sources['local'] = array( 'type' => 'message' ); $sources['local'] = [ 'type' => 'message' ];
$this->mBlacklist = array(); $this->mBlacklist = [];
foreach( $sources as $sourceName => $source ) { foreach ( $sources as $sourceName => $source ) {
$this->mBlacklist = array_merge( $this->mBlacklist = array_merge(
$this->mBlacklist, $this->mBlacklist,
$this->parseBlacklist( $this->getBlacklistText( $source ), $sourceName ) $this->parseBlacklist( $this->getBlacklistText( $source ), $sourceName )
@ -156,7 +155,7 @@ class TitleBlacklist {
*/ */
public static function parseBlacklist( $list, $sourceName ) { public static function parseBlacklist( $list, $sourceName ) {
$lines = preg_split( "/\r?\n/", $list ); $lines = preg_split( "/\r?\n/", $list );
$result = array(); $result = [];
foreach ( $lines as $line ) { foreach ( $lines as $line ) {
$line = TitleBlacklistEntry :: newFromString( $line, $sourceName ); $line = TitleBlacklistEntry :: newFromString( $line, $sourceName );
if ( $line ) { if ( $line ) {
@ -312,7 +311,7 @@ class TitleBlacklist {
* @return Array of bad entries; empty array means blacklist is valid * @return Array of bad entries; empty array means blacklist is valid
*/ */
public function validate( $blacklist ) { public function validate( $blacklist ) {
$badEntries = array(); $badEntries = [];
foreach ( $blacklist as $e ) { foreach ( $blacklist as $e ) {
wfSuppressWarnings(); wfSuppressWarnings();
$regex = $e->getRegex(); $regex = $e->getRegex();
@ -337,7 +336,6 @@ class TitleBlacklist {
} }
} }
/** /**
* Represents a title blacklist entry * Represents a title blacklist entry
*/ */
@ -370,15 +368,15 @@ class TitleBlacklistEntry {
private function filtersNewAccounts() { private function filtersNewAccounts() {
global $wgTitleBlacklistUsernameSources; global $wgTitleBlacklistUsernameSources;
if( $wgTitleBlacklistUsernameSources === '*' ) { if ( $wgTitleBlacklistUsernameSources === '*' ) {
return true; return true;
} }
if( !$wgTitleBlacklistUsernameSources ) { if ( !$wgTitleBlacklistUsernameSources ) {
return false; return false;
} }
if( !is_array( $wgTitleBlacklistUsernameSources ) ) { if ( !is_array( $wgTitleBlacklistUsernameSources ) ) {
throw new Exception( throw new Exception(
'$wgTitleBlacklistUsernameSources must be "*", false or an array' ); '$wgTitleBlacklistUsernameSources must be "*", false or an array' );
} }
@ -462,7 +460,7 @@ class TitleBlacklistEntry {
*/ */
public static function newFromString( $line, $source ) { public static function newFromString( $line, $source ) {
$raw = $line; // Keep line for raw data $raw = $line; // Keep line for raw data
$options = array(); $options = [];
// Strip comments // Strip comments
$line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line ); $line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
$line = trim( $line ); $line = trim( $line );
@ -507,7 +505,7 @@ class TitleBlacklistEntry {
preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER ); preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER );
foreach ( $magicwords as $mword ) { foreach ( $magicwords as $mword ) {
global $wgParser; // Functions we're calling don't need, nevertheless let's use it global $wgParser; // Functions we're calling don't need, nevertheless let's use it
switch( strtolower( $mword[1] ) ) { switch ( strtolower( $mword[1] ) ) {
case 'ns': case 'ns':
$cpf_result = CoreParserFunctions::ns( $wgParser, $mword[2] ); $cpf_result = CoreParserFunctions::ns( $wgParser, $mword[2] );
if ( is_string( $cpf_result ) ) { if ( is_string( $cpf_result ) ) {
@ -523,7 +521,7 @@ class TitleBlacklistEntry {
} }
} }
// Return result // Return result
if( $regex ) { if ( $regex ) {
return new TitleBlacklistEntry( $regex, $options, $raw, $source ); return new TitleBlacklistEntry( $regex, $options, $raw, $source );
} else { } else {
return null; return null;
@ -561,14 +559,18 @@ class TitleBlacklistEntry {
/** /**
* @return string The format version * @return string The format version
*/ */
public function getFormatVersion() { return $this->mFormatVersion; } public function getFormatVersion() {
return $this->mFormatVersion;
}
/** /**
* Set the format version * Set the format version
* *
* @param $v string New version to set * @param $v string New version to set
*/ */
public function setFormatVersion( $v ) { $this->mFormatVersion = $v; } public function setFormatVersion( $v ) {
$this->mFormatVersion = $v;
}
/** /**
* Return the error message name for the blacklist entry. * Return the error message name for the blacklist entry.
@ -585,5 +587,3 @@ class TitleBlacklistEntry {
return $message ? $message : "titleblacklist-forbidden-{$operation}"; return $message ? $message : "titleblacklist-forbidden-{$operation}";
} }
} }
//@}

View file

@ -47,10 +47,10 @@ class ApiQueryTitleBlacklist extends ApiBase {
if ( !$title ) { if ( !$title ) {
if ( is_callable( [ $this, 'dieWithError' ] ) ) { if ( is_callable( [ $this, 'dieWithError' ] ) ) {
$this->dieWithError( $this->dieWithError(
array( 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ) [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ]
); );
} else { } else {
$this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
} }
} }
@ -59,10 +59,10 @@ class ApiQueryTitleBlacklist extends ApiBase {
); );
if ( $blacklisted instanceof TitleBlacklistEntry ) { if ( $blacklisted instanceof TitleBlacklistEntry ) {
// this title is blacklisted. // this title is blacklisted.
$result = array( $result = [
htmlspecialchars( $blacklisted->getRaw() ), htmlspecialchars( $blacklisted->getRaw() ),
htmlspecialchars( $params['title'] ), htmlspecialchars( $params['title'] ),
); ];
$res = $this->getResult(); $res = $this->getResult();
$res->addValue( 'titleblacklist', 'result', 'blacklisted' ); $res->addValue( 'titleblacklist', 'result', 'blacklisted' );
@ -78,33 +78,33 @@ class ApiQueryTitleBlacklist extends ApiBase {
} }
public function getAllowedParams() { public function getAllowedParams() {
return array( return [
'title' => array( 'title' => [
ApiBase::PARAM_REQUIRED => true, ApiBase::PARAM_REQUIRED => true,
), ],
'action' => array( 'action' => [
ApiBase::PARAM_DFLT => 'edit', ApiBase::PARAM_DFLT => 'edit',
ApiBase::PARAM_ISMULTI => false, ApiBase::PARAM_ISMULTI => false,
ApiBase::PARAM_TYPE => array( ApiBase::PARAM_TYPE => [
// createtalk and createpage are useless as they're treated exactly like create // createtalk and createpage are useless as they're treated exactly like create
'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account' 'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account'
), ],
), ],
'nooverride' => array( 'nooverride' => [
ApiBase::PARAM_DFLT => false, ApiBase::PARAM_DFLT => false,
) ]
); ];
} }
/** /**
* @see ApiBase::getExamplesMessages() * @see ApiBase::getExamplesMessages()
*/ */
protected function getExamplesMessages() { protected function getExamplesMessages() {
return array( return [
'action=titleblacklist&tbtitle=Foo' 'action=titleblacklist&tbtitle=Foo'
=> 'apihelp-titleblacklist-example-1', => 'apihelp-titleblacklist-example-1',
'action=titleblacklist&tbtitle=Bar&tbaction=edit' 'action=titleblacklist&tbtitle=Bar&tbaction=edit'
=> 'apihelp-titleblacklist-example-2', => 'apihelp-titleblacklist-example-2',
); ];
} }
} }

View file

@ -1,11 +1,14 @@
{ {
"require-dev": { "require-dev": {
"jakub-onderka/php-parallel-lint": "0.9.2", "jakub-onderka/php-parallel-lint": "0.9.2",
"jakub-onderka/php-console-highlighter": "0.3.2" "jakub-onderka/php-console-highlighter": "0.3.2",
"mediawiki/mediawiki-codesniffer": "0.7.2"
}, },
"scripts": { "scripts": {
"fix": "phpcbf",
"test": [ "test": [
"parallel-lint . --exclude node_modules --exclude vendor" "parallel-lint . --exclude node_modules --exclude vendor",
"phpcs -p -s"
] ]
} }
} }

18
phpcs.xml Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
<exclude name="PSR2.Classes.PropertyDeclaration.ScopeMissing"/><!-- T166381 -->
<exclude name="Squiz.WhiteSpace.ScopeKeywordSpacing.Incorrect"/><!-- T166381 -->
</rule>
<rule ref="MediaWiki.NamingConventions.ValidGlobalName">
<properties>
<property name="ignoreList" type="array" value="$messageMemc" />
</properties>
</rule>
<file>.</file>
<arg name="extensions" value="php,php5,inc"/>
<arg name="encoding" value="UTF-8"/>
<exclude-pattern>vendor</exclude-pattern>
<exclude-pattern>node_modules</exclude-pattern>
</ruleset>

View file

@ -24,12 +24,12 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase {
$this->doLogin(); $this->doLogin();
TitleBlacklist::destroySingleton(); TitleBlacklist::destroySingleton();
$this->setMwGlobals( 'wgTitleBlacklistSources', array( $this->setMwGlobals( 'wgTitleBlacklistSources', [
array( [
'type' => 'file', 'type' => 'file',
'src' => __DIR__ . '/testSource', 'src' => __DIR__ . '/testSource',
), ],
) ); ] );
} }
function tearDown() { function tearDown() {
@ -41,13 +41,13 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase {
* Verify we allow a title which is not blacklisted * Verify we allow a title which is not blacklisted
*/ */
function testCheckingUnlistedTitle() { function testCheckingUnlistedTitle() {
$unlisted = $this->doApiRequest( array( $unlisted = $this->doApiRequest( [
'action' => 'titleblacklist', 'action' => 'titleblacklist',
// evil_acc is blacklisted as <newaccountonly> // evil_acc is blacklisted as <newaccountonly>
'tbtitle' => 'evil_acc', 'tbtitle' => 'evil_acc',
'tbaction' => 'create', 'tbaction' => 'create',
'tbnooverride' => true, 'tbnooverride' => true,
) ); ] );
$this->assertEquals( $this->assertEquals(
'ok', 'ok',
@ -66,11 +66,11 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase {
$this->stashMwGlobals( 'wgGroupPermissions' ); $this->stashMwGlobals( 'wgGroupPermissions' );
$wgGroupPermissions['*']['tboverride'] = true; $wgGroupPermissions['*']['tboverride'] = true;
$unlisted = $this->doApiRequest( array( $unlisted = $this->doApiRequest( [
'action' => 'titleblacklist', 'action' => 'titleblacklist',
'tbtitle' => 'bar', 'tbtitle' => 'bar',
'tbaction' => 'create', 'tbaction' => 'create',
) ); ] );
$this->assertEquals( $this->assertEquals(
'ok', 'ok',
@ -83,12 +83,12 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase {
* Verify a blacklisted title gives out an error. * Verify a blacklisted title gives out an error.
*/ */
function testCheckingBlackListedTitle() { function testCheckingBlackListedTitle() {
$listed = $this->doApiRequest( array( $listed = $this->doApiRequest( [
'action' => 'titleblacklist', 'action' => 'titleblacklist',
'tbtitle' => 'bar', 'tbtitle' => 'bar',
'tbaction' => 'create', 'tbaction' => 'create',
'tbnooverride' => true, 'tbnooverride' => true,
) ); ] );
$this->assertEquals( $this->assertEquals(
'blacklisted', 'blacklisted',
@ -119,16 +119,16 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase {
* Tests integration with the AntiSpoof extension * Tests integration with the AntiSpoof extension
*/ */
function testAntiSpoofIntegration() { function testAntiSpoofIntegration() {
if ( !class_exists( 'AntiSpoof') ) { if ( !class_exists( 'AntiSpoof' ) ) {
$this->markTestSkipped( "This test requires the AntiSpoof extension" ); $this->markTestSkipped( "This test requires the AntiSpoof extension" );
} }
$listed = $this->doApiRequest( array( $listed = $this->doApiRequest( [
'action' => 'titleblacklist', 'action' => 'titleblacklist',
'tbtitle' => 'AVVVV', 'tbtitle' => 'AVVVV',
'tbaction' => 'create', 'tbaction' => 'create',
'tbnooverride' => true, 'tbnooverride' => true,
) ); ] );
$this->assertEquals( $this->assertEquals(
'blacklisted', 'blacklisted',

View file

@ -12,7 +12,7 @@ class TitleBlacklistAuthenticationRequestTest extends AuthenticationRequestTestC
parent::setUp(); parent::setUp();
} }
protected function getInstance( array $args = [ ] ) { protected function getInstance( array $args = [] ) {
return new TitleBlacklistAuthenticationRequest(); return new TitleBlacklistAuthenticationRequest();
} }