Add site name and language variables

In T43172 it was told that adding the site name could increase the risk of
attracting more spam, but I don't see how this variable could cause that.

Bug: T240948
Bug: T97933
Change-Id: I1d2aeabaf008ac06798b8d7e4af7d61ae1702776
This commit is contained in:
Daimona Eaytoy 2019-12-17 16:56:36 +01:00
parent 661a77f0eb
commit d9ae71f578
6 changed files with 61 additions and 1 deletions

View file

@ -408,6 +408,8 @@
"abusefilter-edit-builder-vars-file-width": "Width of the file in pixels",
"abusefilter-edit-builder-vars-file-height": "Height of the file in pixels",
"abusefilter-edit-builder-vars-file-bits-per-channel": "Bits per color channel of the file",
"abusefilter-edit-builder-vars-wiki-name": "Database name of the wiki",
"abusefilter-edit-builder-vars-wiki-language": "Language code of the wiki",
"abusefilter-filter-log": "Recent filter changes",
"abusefilter-history": "Change history for Abuse Filter #$1",
"abusefilter-history-foruser": "Changes by $1",

View file

@ -442,6 +442,8 @@
"abusefilter-edit-builder-vars-file-width": "This variable contains the width of the file in pixels",
"abusefilter-edit-builder-vars-file-height": "This variable contains the height of the file in pixels",
"abusefilter-edit-builder-vars-file-bits-per-channel": "This variable contains the number of bits per color channel of the file",
"abusefilter-edit-builder-vars-wiki-name": "Abuse filter syntax option in a dropdown from the group {{msg-mw|abusefilter-edit-builder-group-vars}}.",
"abusefilter-edit-builder-vars-wiki-language": "Abuse filter syntax option in a dropdown from the group {{msg-mw|abusefilter-edit-builder-group-vars}}.",
"abusefilter-filter-log": "Used as page title.",
"abusefilter-history": "Used as page title.\n\n\"Change history\" is the \"history of changes\"\n\nParameters:\n* $1 - filter ID\n\nIf the filter ID is not specified, {{msg-mw|Abusefilter-filter-log}} will be used.",
"abusefilter-history-foruser": "Parameters:\n* $1 - a link to the changing user's page\n* $2 - (Optional) the plain text username",

View file

@ -474,6 +474,12 @@ class AFComputedVariable {
$rev = Revision::loadFromTimestamp( $dbr, $title, $timestamp );
$result = AbuseFilter::revisionToString( $rev, $wgUser );
break;
case 'get-wiki-name':
$result = WikiMap::getCurrentWikiDbDomain()->getId();
break;
case 'get-wiki-language':
$result = MediaWikiServices::getInstance()->getContentLanguage()->getCode();
break;
default:
if ( Hooks::run( 'AbuseFilter-computeVariable',
[ $this->mMethod, $vars, $parameters, &$result ] ) ) {

View file

@ -211,6 +211,8 @@ class AbuseFilter {
'file_width' => 'file-width',
'file_height' => 'file-height',
'file_bits_per_channel' => 'file-bits-per-channel',
'wiki_name' => 'wiki-name',
'wiki_language' => 'wiki-language',
],
];

View file

@ -42,7 +42,11 @@ class VariableGenerator {
* @return $this For chaining
*/
public function addGenericVars( RCDatabaseLogEntry $entry = null ) : self {
// For now, we don't have variables to add; other extensions could.
// These are lazy-loaded just to reduce the amount of preset variables, but they
// shouldn't be expensive.
$this->vars->setLazyLoadVar( 'wiki_name', 'get-wiki-name', [] );
$this->vars->setLazyLoadVar( 'wiki_language', 'get-wiki-language', [] );
Hooks::run( 'AbuseFilter-generateGenericVars', [ $this->vars, $entry ] );
return $this;
}

View file

@ -478,4 +478,48 @@ class AbuseFilterDBTest extends MediaWikiTestCase {
[ 'my_tag', null ],
];
}
/**
* Test for the wiki_name variable.
*
* @covers AbuseFilter::generateGenericVars
* @covers AFComputedVariable::compute
*/
public function testWikiNameVar() {
$name = 'foo';
$prefix = 'bar';
$this->setMwGlobals( [
'wgDBname' => $name,
'wgDBprefix' => $prefix
] );
$vars = new AbuseFilterVariableHolder();
$vars->setLazyLoadVar( 'wiki_name', 'get-wiki-name', [] );
$this->assertSame(
"$name-$prefix",
$vars->getVar( 'wiki_name' )->toNative()
);
}
/**
* Test for the wiki_language variable.
*
* @covers AbuseFilter::generateGenericVars
* @covers AFComputedVariable::compute
*/
public function testWikiLanguageVar() {
$fakeCode = 'foobar';
$fakeLang = $this->getMockBuilder( Language::class )
->setMethods( [ 'getCode' ] )
->getMock();
$fakeLang->method( 'getCode' )->willReturn( $fakeCode );
$this->setService( 'ContentLanguage', $fakeLang );
$vars = new AbuseFilterVariableHolder();
$vars->setLazyLoadVar( 'wiki_language', 'get-wiki-language', [] );
$this->assertSame(
$fakeCode,
$vars->getVar( 'wiki_language' )->toNative()
);
}
}