User:松/Drafts/Extension:AbuseFilter/Rules format: Difference between revisions

From TestWiki
Content added Content deleted
No edit summary
No edit summary
Line 1: Line 1:
{{DISPLAYTITLE:*}}
The rules are formatted much as conditionals in a C/Java/Perl-like language.
The rules are formatted much as conditionals in a C/Java/Perl-like language.


Line 14: Line 15:
'''Action''' variable can be <code>'edit'</code>, <code>'move'</code>, <code>'createaccount'</code>, <code>'autocreateaccount'</code>, <code>'delete'</code> or <code>'upload'</code>.
'''Action''' variable can be <code>'edit'</code>, <code>'move'</code>, <code>'createaccount'</code>, <code>'autocreateaccount'</code>, <code>'delete'</code> or <code>'upload'</code>.


You can define more variables for ease of understanding with the assign symbol <code>:=</code> in a line (closed by <code>;</code>) within a condition. Example (from [[w:en:Special:AbuseFilter/79]]):
You can define more variables for ease of understanding with the assign symbol <code>:=</code> in a line (closed by <code>;</code>) within a condition. Example (from w:en:Special:AbuseFilter/79):
<syntaxhighlight lang="c">
(
line1:="(\{\{(r|R)eflist|\{\{(r|R)efs|<references\s?/>|</references\s?>)";
rcount(line1, removed_lines)
) > (
rcount(line1, added_lines)
)
</syntaxhighlight>


Lists:
Lists:
<syntaxhighlight lang="c">
a_list := [ 5, 6, 7];
</syntaxhighlight>


=== <ref name="bug27987">Be aware of bug 27987</ref> ===


====All variables====
====All variables====
{| class="wikitable sortable"
{| class="wikitable sortable"
|+ Variables available
|+ Variables available
! Description !! Name !! Data type !! Values
! Description !! Name !! Data type
|-
|-
| {{int:abusefilter-edit-builder-vars-user-editcount}} || <code>user_editcount</code> || string || Empty for unregistered users.
| {{int:abusefilter-edit-builder-vars-user-editcount}} || <code>user_editcount</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-user-name}} || <code>user_name</code> || string ||
| {{int:abusefilter-edit-builder-vars-user-name}} || <code>user_name</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-user-emailconfirm}} || <code>user_emailconfirm</code> || string || YYYYMMDDHHMMSS
| {{int:abusefilter-edit-builder-vars-user-emailconfirm}} || <code>user_emailconfirm</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-user-age}} || <code>user_age</code> || || in seconds; 0 for IP
| {{int:abusefilter-edit-builder-vars-user-age}} || <code>user_age</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-user-groups}} || <code>user_groups</code> || ||
| {{int:abusefilter-edit-builder-vars-user-groups}} || <code>user_groups</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-user-rights}} || <code>user_rights</code> || ||
| {{int:abusefilter-edit-builder-vars-user-rights}} || <code>user_rights</code> ||
|-
|-
| [[Manual:Page_table#page_id|{{int:abusefilter-edit-builder-vars-article-id}}]] (found in the page's HTML source - search for wgArticleId) || <code>article_articleid</code> || integer || In theory this is 0 for new pages, but this is unreliable. Instead, use "old_size==0" to identify new page creation.
| {{int:abusefilter-edit-builder-vars-article-ns}} || <code>article_namespace</code> || integer
|-
|-
| {{int:abusefilter-edit-builder-vars-article-ns}} || <code>article_namespace</code> || integer || refers to [[Manual:Namespace#Built-in_namespaces|namespace index]]
| {{int:abusefilter-edit-builder-vars-article-text}} || <code>article_text</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-article-text}} || <code>article_text</code> || string ||
| {{int:abusefilter-edit-builder-vars-article-prefixedtext}} || <code>article_prefixedtext</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-article-prefixedtext}} || <code>article_prefixedtext</code> || string ||
| {{int:abusefilter-edit-builder-vars-restrictions-edit}} || <code>article_restrictions_edit</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-restrictions-edit}} || <code>article_restrictions_edit</code> || ||
| {{int:abusefilter-edit-builder-vars-restrictions-move}} || <code>article_restrictions_move</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-restrictions-move}} || <code>article_restrictions_move</code> || ||
| {{int:abusefilter-edit-builder-vars-recent-contributors}} || <code>article_recent_contributors</code> ||
|-
|-
| Action || <code>action</code> || string
| {{int:abusefilter-edit-builder-vars-recent-contributors}} || <code>article_recent_contributors</code> || || Empty if the user is the only contributor to the page
|-
|-
| Action || <code>action</code> || string || edit, move, createaccount, autocreateaccount, delete, upload
| {{int:abusefilter-edit-builder-vars-summary}} || <code>summary</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-summary}} || <code>summary</code> || string ||
| {{int:abusefilter-edit-builder-vars-minor-edit}} || <code>minor_edit</code> || string
|-
|-
| {{int:abusefilter-edit-builder-vars-minor-edit}} || <code>minor_edit</code> || string ||
| {{int:abusefilter-edit-builder-vars-old-text}} || <code>old_wikitext</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-old-text}} || <code>old_wikitext</code> || ||
| {{int:abusefilter-edit-builder-vars-new-text}} || <code>new_wikitext</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-new-text}} || <code>new_wikitext</code> || ||
| {{int:abusefilter-edit-builder-vars-diff}} || <code>edit_diff</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-diff}} || <code>edit_diff</code> || ||
| {{int:abusefilter-edit-builder-vars-newsize}} || <code>new_size</code> || integer
|-
|-
| {{int:abusefilter-edit-builder-vars-newsize}} || <code>new_size</code> || integer ||
| {{int:abusefilter-edit-builder-vars-oldsize}} || <code>old_size</code> || integer
|-
|-
| {{int:abusefilter-edit-builder-vars-oldsize}} || <code>old_size</code> || integer ||
| {{int:abusefilter-edit-builder-vars-delta}} || <code>edit_delta</code> || <!-- test this with === when edit_delta < 0 -->
|-
|-
| {{int:abusefilter-edit-builder-vars-delta}} || <code>edit_delta</code> || <!-- test this with === when edit_delta < 0 --> ||
| {{int:abusefilter-edit-builder-vars-addedlines}} || <code>added_lines</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-addedlines}} || <code>added_lines</code> || ||
| {{int:abusefilter-edit-builder-vars-removedlines}} || <code>removed_lines</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-removedlines}} || <code>removed_lines</code> || ||
| {{int:abusefilter-edit-builder-vars-all-links}} || <code>all_links</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-all-links}} || <code>all_links</code> || ||
| {{int:abusefilter-edit-builder-vars-old-links}} || <code>old_links</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-old-links}} || <code>old_links</code> || ||
| {{int:abusefilter-edit-builder-vars-added-links}} || <code>added_links</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-added-links}} || <code>added_links</code> || ||
| {{int:abusefilter-edit-builder-vars-removed-links}} || <code>removed_links</code> ||
|-
|-
| {{int:abusefilter-edit-builder-vars-removed-links}} || <code>removed_links</code> || ||
| Parsed HTML source of the new revision || <code>new_html</code> ||
|-
|-
| Parsed HTML source of the new revision || <code>new_html</code> || ||
| New page text, stripped of any markup || <code>new_text</code> ||
|-
|-
| New page text, stripped of any markup || <code>new_text</code> || ||
| ''Disabled'' || <code>old_html</code> ||
|-
|-
| ''Disabled'' || <code>old_html</code> || ||
| ''Disabled'' || <code>old_text</code> ||
|-
|-
| ''Disabled'' || <code>old_text</code> || ||
| Whether or not the change was made through a tor exit node || <code>tor_exit_node</code> ||
|-
|-
| Whether or not the change was made through a tor exit node || <code>tor_exit_node</code> || || 0, 1
| Unix timestamp of change || <code>timestamp</code> || string
|-
| Unix timestamp of change || <code>timestamp</code> || string || int(timestamp) gives you a number with which you can calculate the date, time, day of week, etc.
|}
|}


Line 109: Line 98:


===Page/Article namespace===
===Page/Article namespace===
''See also [[Manual:Namespace]]''
''See also Manual:Namespace''
{| class="wikitable" style="float: right; margin:1em 0 1em 1em; text-align: center; font-size: 90%;"
{| class="wikitable" style="float: right; margin:1em 0 1em 1em; text-align: center; font-size: 90%;"
! colspan="4" style="background-color: #F0F8FF; font-size:110%;" | [[:en:Wikipedia:Namespace|English Wikipedia namespaces]]
![[:en:Wikipedia:Namespace|English Wikipedia namespaces]]
|-
|-
!Talk namespaces
! colspan="2" style="background-color: #F0F8FF;" | Basic namespaces
! colspan="2" style="background-color: #F0F8FF;" | Talk namespaces
|-
|-
| 0
| [[Wikipedia:Wikipedia:Main namespace|Main]]
| [[Wikipedia:Wikipedia:Talk namespace|Talk]]
| 1
| 1
|-
|-
| 2
| [[Wikipedia:Wikipedia:User namespace|User]]
| User talk
| 3
| 3
|-
|-
| 4
| [[Wikipedia:Wikipedia:Wikipedia namespace|Wikipedia]]
| Wikipedia talk
| 5
| 5
|-
|-
| 6
| [[Wikipedia:Wikipedia:File namespace|File]]
| File talk
| 7
| 7
|-
|-
| 8
| [[Wikipedia:Wikipedia:MediaWiki namespace|MediaWiki]]
| MediaWiki talk
| 9
| 9
|-
|-
| 10
| [[Wikipedia:Wikipedia:Template namespace|Template]]
| Template talk
| 11
| 11
|-
|-
| 12
| [[Wikipedia:Wikipedia:Help namespace|Help]]
| Help talk
| 13
| 13
|-
|-
| 14
| [[Wikipedia:Wikipedia:Category namespace|Category]]
| Category talk
| 15
| 15
|-
|-
| 100
| [[Wikipedia:Wikipedia:Portal namespace|Portal]]
| Portal talk
| 101
| 101
|-
|-
| 108
| [[Wikipedia:Wikipedia:Book namespace|Book]]
| Book talk
| 109
| 109
|-
|-
! colspan="4" style="background-color: #F0F8FF;" | Virtual namespaces
!Virtual namespaces
|-
|-
|[[Wikipedia:Wikipedia:Special namespace|Special]]
| -1
| colspan="3" | [[Wikipedia:Wikipedia:Special namespace|Special]]
|-
|-
|[[Wikipedia:Wikipedia:Media namespace|Media]]
| -2
| colspan="3" | [[Wikipedia:Wikipedia:Media namespace|Media]]
|}
|}


Line 181: Line 137:
* <code>==</code> (or <code>=</code>) and <code>!=</code> &mdash; Return true if the left-hand operand is ''equal to/not equal to'' the right-hand operand respectively.
* <code>==</code> (or <code>=</code>) and <code>!=</code> &mdash; Return true if the left-hand operand is ''equal to/not equal to'' the right-hand operand respectively.
* <code>===</code> and <code>!==</code> &mdash; Return true if the left-hand operand is ''equal to/not equal to'' the right-hand operand AND the left-hand operand is ''the same/not the same'' data type to the right-hand operand respectively.
* <code>===</code> and <code>!==</code> &mdash; Return true if the left-hand operand is ''equal to/not equal to'' the right-hand operand AND the left-hand operand is ''the same/not the same'' data type to the right-hand operand respectively.

{| class="wikitable"
! style="width: 50%;"| Example
! Result
|-
|<code>1 == 2</code>|| False
|-
|<code>1 <= 2</code>|| True
|-
|<code>1 >= 2</code>|| False
|-
|<code>1 != 2</code>|| True
|-
|<code>1 < 2</code>|| True
|-
|<code>1 > 2</code>|| False
|-
|<code>0 == False</code>|| True
|-
|<code>0 === False</code>|| False
|}


=== Arithmetic ===
=== Arithmetic ===
Line 211: Line 146:
* <code>**</code> — Raise the left-hand operand to the exponential power specified by the right-hand operand.
* <code>**</code> — Raise the left-hand operand to the exponential power specified by the right-hand operand.
* <code>%</code> — Return the remainder given when the left-hand operand is divided by the right-hand operand.
* <code>%</code> — Return the remainder given when the left-hand operand is divided by the right-hand operand.

{| class="wikitable"
!style="width: 50%;"|Example!!|Result
|-
| <code>1 + 1</code> || 2
|-
| <code>2 * 2</code> || 4
|-
| <code>1 / 2</code> || 0.5
|-
| <code>9 ** 2</code>|| 81
|-
| <code>6 % 5</code> || 1
|}


=== String concatenation ===
=== String concatenation ===
Line 231: Line 152:
=== Keywords ===
=== Keywords ===
The following special keywords are included for often-used functionality:
The following special keywords are included for often-used functionality:
* <code>like</code> (or <code>matches</code>) returns true if the left-hand operand matches the [[w:en:Glob (programming)#Syntax|glob pattern]] in the right-hand operand.
* <code>like</code> (or <code>matches</code>) returns true if the left-hand operand matches the glob pattern in the right-hand operand.
* <code>in</code> returns true if the right-hand operand (a string) contains the left-hand operand.
* <code>in</code> returns true if the right-hand operand (a string) contains the left-hand operand.
* <code>rlike</code> (or <code>regex</code>) and <code>irlike</code> return true if the left-hand operand matches (contains) the [[w:Regular expression|regex]] pattern in the right-hand operand (<code>irlike</code> is case '''i'''nsensitive). The system uses [[w:Perl-compatible regular expressions|PCRE]]. The only PCRE option enabled is <code>PCRE_UTF8</code> (modifier <code>u</code> [//php.net/manual/en/reference.pcre.pattern.modifiers.php in PHP]); for <code>irlike</code> both <code>PCRE_CASELESS</code> and <code>PCRE_UTF8</code> are enabled (modifier <code>iu</code>).
* <code>rlike</code> (or <code>regex</code>) and <code>irlike</code> return true if the left-hand operand matches (contains) the regex pattern in the right-hand operand (<code>irlike</code> is case '''i'''nsensitive). The system uses PCRE. The only PCRE option enabled is <code>PCRE_UTF8</code> (modifier <code>u</code> in PHP); for <code>irlike</code> both <code>PCRE_CASELESS</code> and <code>PCRE_UTF8</code> are enabled (modifier <code>iu</code>).
* <code>contains</code>
* <code>contains</code>
* <code>if ... then ... else ... end</code>
* <code>if ... then ... else ... end</code>
Line 241: Line 162:


'''Examples'''
'''Examples'''
{| class="wikitable"
|-
! Code
! Result
|-
| <code>"1234" like "12?4"</code>
| True
|-
| <code>"1234" like "12*"</code>
| True
|-
| <code>"foo" in "foobar"</code>
| True
|-
| <code>"foo" regex "\w+"</code>
| True
|}

=== Functions ===
=== Functions ===
A number of built-in functions are included to ease some common issues. They are executed in the general format <code>functionName( arg1, arg2, arg3 )</code>, and can be used in place of any literal or variable. Its arguments can be given as literals, variables, or even other functions.
A number of built-in functions are included to ease some common issues. They are executed in the general format <code>functionName( arg1, arg2, arg3 )</code>, and can be used in place of any literal or variable. Its arguments can be given as literals, variables, or even other functions.

{| class="wikitable sortable"
! name !! description
|-
| <code>lcase</code> || Returns the argument converted to lower case.
|-
| <code>ucase</code> || Returns the argument converted to upper case.
|-
| <code>length</code> || Returns the length of the string given as the argument.
|-
| <code>string</code> || Casts to string data type.
|-
| <code>int</code> || Casts to integer data type.
|-
| <code>float</code> || Casts to floating-point data type.
|-
| <code>bool</code> || Casts to boolean data type.
|-
| <code>norm</code> || Equivalent to <code>rmwhitespace(rmspecials(rmdoubles(ccnorm(arg1))))</code>.
|-
| <code>ccnorm</code> || Normalises confusable/similar characters in the argument, and returns a canonical form. A list of characters and their replacements can be found {{git file|project=mediawiki/extensions/AntiSpoof|branch=HEAD|file=equivset.php|text=on git}}, eg. <code>ccnorm( "Eeèéëēĕėęě3ƐƷ" ) === "EEEEEEEEEEEEE"</code>.<ref name="bug27987">Be aware of [[bugzilla:27987|bug 27987]]</ref><ref name="bug25619">Be aware of [[bugzilla:25619|bug 25619]]</ref>
|-
| <code>specialratio</code> || Returns the number of non-alphanumeric characters divided by the total number of characters in the argument.
|-
| <code>rmspecials</code> || Removes any special characters in the argument, and returns the result.
|-
| <code>rmdoubles</code> || Removes repeated characters in the argument, and returns the result.
|-
| <code>rmwhitespace</code> || Removes whitespace (spaces, tabs, newlines).
|-
| <code>count</code> || Returns the number of times the needle (first string) appears in the haystack (second string). If only one argument is given, splits it by commas and returns the number of segments.
|-
| <code>rcount</code> || Similar to <code>count</code> but the needle uses a regular expression instead. Can be made case-insensitive by letting the regular expression start with "(?i)".
|-
| <code>ip_in_range</code> || Returns true if user's IP (first string) matches specified IP ranges (second string). Only works for anonymous users.
|-
| <code>contains_any</code> || Returns true if the first string contains any strings from the following arguments (unlimited number of arguments).
|-
| <code>substr</code> || Returns the portion of the first string, by offset from the second argument (starts at 0) and maximum length from the third argument (optional).
|-
| <code>strlen</code> || Same as <code>length</code>.
|-
| <code>strpos</code> || Returns the numeric position of the first occurrence of needle (second string) in the haystack (first string). This function may return 0 when the needle is found at the begining of the haystack, so it might be misinterpreted as ''false'' value by another comparative operator. The better way is to use <code>===</code> or <code>!==</code> for testing whether it is found.
|-
| <code>str_replace</code> || Replaces all occurrences of the search string with the replacement string. The function takes 3 arguments in the following order: text to perform the search, text to find, replacement text.
|-
| <code>rescape</code> || Returns the argument with some characters preceded with the escape character "\", so that the string can be used in a regular expression without those characters having a special meaning.
|-
| <code>set</code> || Sets a variable (first string) with a given value (second argument) for further use in the filter. Another syntax: <code>''name'' := ''value''</code>.
|-
| <code>set_var</code> || Same as <code>set</code>.
|}


'''Other'''
'''Other'''
* <code>convert</code> returns the second argument converted to variant language specified by the first argument. ONLY apply on wikis with LanguageConverter class. (New func added on [[rev:49399]], need support of MediaWiki after [[rev:49397]])
* <code>convert</code> returns the second argument converted to variant language specified by the first argument. ONLY apply on wikis with LanguageConverter class. (New func added on rev:49399, need support of MediaWiki after rev:49397)


'''Examples'''
'''Examples'''
{| class="wikitable"
|-
| <code>length( "Wikipedia" )</code>
| 9
|-
| <code>lcase( "Wikipedia" )</code>
| wikipedia
|-
| <code>ccnorm( "ωɨƙɩᑭƐƉlα" )</code>
| W1K1PED1A
|-
| <code>ccnorm( "ìíîïĩļǐīĭḷĿї!ľį₤ĺľḷĿΛЛљóòôöõǒōŏǫőόὸὀὁὄὂὅὃọ$śŝşšṣσ" )</code>
| ìíîïĩļǐīĭḷĿї!ľį₤ĺľḷĿΛЛљóòôöõǒōŏǫőόὸὀὁὄὂὅὃọ$śŝşšṣσ<ref name="bug25619" />
<!--
<!--
|-
|-
| <code>convert( "zh-hant", "维基百科" )</code><br />// assume we work on a wiki with Chinese LanguageConverter class
| <code>convert( "zh-hant", "维基百科" )</code><br />// assume we work on a wiki with Chinese LanguageConverter class
| 維基百科
| 維基百科
-->=== Boolean operations ===
-->
|-
| <code>rmdoubles( "foobybboo" )</code>
| fobybo
|-
| <code>specialratio( "Wikipedia!" )</code>
| 0.1
|-
| <code>norm( "!!ω..ɨ..ƙ..ɩ..ᑭᑭ..Ɛ.Ɖ@@l%%α!!" )</code>
| W1K1PED1A
|-
| <code>count( "foo", "foofooboofoo" )</code>
| 3
|-
| <code>count( "foo,bar,baz" )</code>
| 3
|-
| <code>rmspecials( "FOOBAR!!1" )</code>
| FOOBAR1
|-
| <code>rescape( "abc* (def)" )</code>
| abc\* \(def\)
|}

=== Boolean operations ===
You can match if and only if all of a number of conditions are true, one of a number of conditions are true, or one and only one of all conditions are true.
You can match if and only if all of a number of conditions are true, one of a number of conditions are true, or one and only one of all conditions are true.
* <code>x | y</code> &mdash; OR &ndash; returns true if one or more of the conditions is true.
* <code>x | y</code> &mdash; OR &ndash; returns true if one or more of the conditions is true.
Line 366: Line 180:
* <code>!x</code> &mdash; NOT &ndash; returns true if the condition is not true.
* <code>!x</code> &mdash; NOT &ndash; returns true if the condition is not true.


* '''Examples'''<ref>The last 4 conditions are counted due to [[bugzilla:41693|bug 41693]]</ref>
'''Examples'''
{| class="wikitable"
|-
! Code
! Result
|-
| <code>1 | 1</code>
| True
|-
| <code>1 | 0</code>
| True
|-
| <code>0 | 0</code>
| False
|-
| <code>1 & 1</code>
| True
|-
| <code>1 & 0</code>
| False
|-
| <code>0 & 0</code>
| False
|-
| <code>1 ^ 1</code>
| False
|-
| <code>1 ^ 0</code>
| True
|-
| <code>0 ^ 0</code>
| False
|-
| <code>!1</code>
| False
|}

=== Order of operations ===
=== Order of operations ===
Operations are generally done left-to-right, but there is an order to which they are resolved. As soon as the filter fails one of the conditions, it will stop checking the rest of them (due to [[w:short-circuit evaluation|short-circuit evaluation]]) and move on to the next filter (except for [[bugzilla:41693|bug 41693]]). The evaluation order is:
Operations are generally done left-to-right, but there is an order to which they are resolved. As soon as the filter fails one of the conditions, it will stop checking the rest of them (due to short-circuit ev<ref name="bug27987" />aluation) and move on to the next filter (except for bug 41693). The evaluation order is:
# Anything surrounded by parentheses (<code>(</code> and <code>)</code>) is evaluated as a single unit.
# Anything surrounded by parentheses (<code>(</code> and <code>)</code>) is evaluated as a single unit.
# Turning variables/literals into their respective data. (i.e., <code>article_namespace</code> to 0)
# Turning variables/literals into their respective data. (i.e., <code>article_namespace</code> to 0)
Line 422: Line 200:


== Conditions ==
== Conditions ==
<references/>

{| class="wikitable" style="width: 100%;"
|+ Condition counting
! style="width: 30em;" | Rules
! Conditions used
! Notes
|-
|<code>'foo' == 'bar'</code>|| 1 || rowspan=2 | A simple test counts as one condition
|-
|<code>false & false & false & false & false</code>|| 5<ref>The last 4 conditions are counted due to [[bugzilla:41693|bug 41693]]</ref>
|-
|<code>( 'foo' == 'bar' )</code>|| 2 || rowspan=3 | Evaluating parenthesis also counts as conditions
|-
|<code>( 'foo' ) == ( 'bar' )</code>|| 3
|-
|<code>(((( 'foo' == 'bar' ))))</code>|| 5
|-
|<code>false & ( false & false & false & false )</code>|| 2 || rowspan=2 | But they can be used to force a short-circuit
|-
|<code>false & ( true & true & true & true )</code>|| 2
|-
|<code>true & ( false & false & false & false )</code>|| 6 || rowspan=2 | Rearranging and grouping the conditions according to their likelihood of being true might represent a big difference in the total number of conditions used by a complex filter
|-
|<code>true & ( false & ( false & false & false ) )</code>|| 4
|-
|<code>str_replace( 'FooFoo', 'Foo', <nowiki>''</nowiki> ) == 'bar'</code>|| 5 || Each function call and each parameter evaluation also counts as one condition
|-
|<code>&nbsp;&nbsp;str_replace( 'FooFoo', 'Foo', <nowiki>''</nowiki> ) == 'bar' <br /><nowiki>|</nowiki> str_replace( 'FooFoo', 'Foo', <nowiki>''</nowiki> ) == 'baz'</code>|| 9 || 1 from <code>str_replace</code> + 3 from its parameters + 1 from the first <code>==</code> + 3 for the same parameters + 1 for the second <code>==</code>
|-
|<code>str_replace( 'FooFoo', 'Foo', <nowiki>''</nowiki> )</code>|| 5 || equivalent to "<code>str_replace( 'FooFoo', 'Foo', <nowiki>''</nowiki> ) = 1</code>"
|}


== Useful links ==
== Useful links ==


* [http://php.net/manual/en/reference.pcre.pattern.syntax.php PCRE pattern syntax].
* PCRE pattern syntax.


== Notes ==
== Notes ==
<references/>

{{languages|Extension:AbuseFilter/Rules format}}
{{languages|Extension:AbuseFilter/Rules format}}


[[zh:Wikipedia:防滥用过滤器/操作指引]]
[[zh:Wikipedia:防滥用过滤器/操作指引]]
__FORCETOC__
__NOEDITSECTION__
__DISAMBIG__
__INDEX__
__NEWSECTIONLINK__

Revision as of 02:42, 1 January 2015

The rules are formatted much as conditionals in a C/Java/Perl-like language.

Comments

You can specify comments using the following syntax:

/* This is a comment */

Variables

The abuse filter passes various variables by name into the parser. These variables can be accessed by typing their name in, in a place where a literal would work. You can view the variables associated with each request in the abuse log.

Examples

USER_EDITCOUNT
ARTICLE_RECENT_CONTRIBUTORS

Action variable can be 'edit', 'move', 'createaccount', 'autocreateaccount', 'delete' or 'upload'.

You can define more variables for ease of understanding with the assign symbol := in a line (closed by ;) within a condition. Example (from w:en:Special:AbuseFilter/79):

Lists:

[1]

All variables

Variables available
Description Name Data type
Edit count of the user user_editcount string
Name of the user account user_name string
Time email address was confirmed user_emailconfirm string
Age of the user account user_age
Groups (including implicit) the user is in user_groups
Rights that the user has user_rights
⧼abusefilter-edit-builder-vars-article-ns⧽ article_namespace integer
⧼abusefilter-edit-builder-vars-article-text⧽ article_text string
⧼abusefilter-edit-builder-vars-article-prefixedtext⧽ article_prefixedtext string
Edit protection level of the page article_restrictions_edit
Move protection level of the page article_restrictions_move
Last ten users to contribute to the page article_recent_contributors
Action action string
Edit summary/reason summary string
Whether or not the edit is marked as minor (no longer in use) minor_edit string
Old page text, stripped of any markup (no longer in use) old_wikitext
New page text, stripped of any markup new_wikitext
Unified diff of changes made by edit edit_diff
New page size new_size integer
Old page size old_size integer
Size change in edit edit_delta
Lines added in edit added_lines
Lines removed in edit removed_lines
All external links in the new text all_links
Links in the page, before the edit old_links
All external links added in the edit added_links
All external links removed in the edit removed_links
Parsed HTML source of the new revision new_html
New page text, stripped of any markup new_text
Disabled old_html
Disabled old_text
Whether or not the change was made through a tor exit node tor_exit_node
Unix timestamp of change timestamp string

CentralAuth also provides a global_user_groups variable, like user_groups.

When action is move, only the summary, action and timestamp variables, and variables with a name that starts with "user" are available. Variables with a name that starts with "article_" are also available, but the prefix is replaced by "moved_from_" and "moved_to_", that represent the values of the original article name and the destination one respectively. For example, "moved_from_text" and "moved_to_text" instead of "article_text".

Page/Article namespace

See also Manual:Namespace

English Wikipedia namespaces
Talk namespaces
1
3
5
7
9
11
13
15
101
109
Virtual namespaces
Special
Media

Simple comparisons

You can compare variables with other variables and literals with the following syntax:

  • < and > — Return true if the left-hand operand is less than/greater than the right-hand operand respectively.
  • <= and >= — Return true if the left-hand operand is less than or equal to/greater than or equal to the right-hand operand respectively.
  • == (or =) and != — Return true if the left-hand operand is equal to/not equal to the right-hand operand respectively.
  • === and !== — Return true if the left-hand operand is equal to/not equal to the right-hand operand AND the left-hand operand is the same/not the same data type to the right-hand operand respectively.

Arithmetic

You can use basic arithmetic symbols to do arithmetic on variables and literals with the following syntax:

  • - — Subtract the right-hand operand from the left-hand operand.
  • + — Add the right-hand operand to the left-hand operand.
  • * — Multiply the left-hand operand by the right-hand operand.
  • / — Divide the left-hand operand by the right-hand operand.
  • ** — Raise the left-hand operand to the exponential power specified by the right-hand operand.
  • % — Return the remainder given when the left-hand operand is divided by the right-hand operand.

String concatenation

You can use the + (plus) symbol to concatenate two literal strings or the values of two vars with a string value.

Keywords

The following special keywords are included for often-used functionality:

  • like (or matches) returns true if the left-hand operand matches the glob pattern in the right-hand operand.
  • in returns true if the right-hand operand (a string) contains the left-hand operand.
  • rlike (or regex) and irlike return true if the left-hand operand matches (contains) the regex pattern in the right-hand operand (irlike is case insensitive). The system uses PCRE. The only PCRE option enabled is PCRE_UTF8 (modifier u in PHP); for irlike both PCRE_CASELESS and PCRE_UTF8 are enabled (modifier iu).
  • contains
  • if ... then ... else ... end
  • ... ? ... : ...
  • true, false and null


Examples

Functions

A number of built-in functions are included to ease some common issues. They are executed in the general format functionName( arg1, arg2, arg3 ), and can be used in place of any literal or variable. Its arguments can be given as literals, variables, or even other functions.

Other

  • convert returns the second argument converted to variant language specified by the first argument. ONLY apply on wikis with LanguageConverter class. (New func added on rev:49399, need support of MediaWiki after rev:49397)

Examples

Boolean operations

You can match if and only if all of a number of conditions are true, one of a number of conditions are true, or one and only one of all conditions are true.

  • x | y — OR – returns true if one or more of the conditions is true.
  • x & y — AND – returns true if both of the conditions are true.
  • x ^ y — XOR – returns true if one, and only one of the two conditions is true.
  • !x — NOT – returns true if the condition is not true.

Order of operations

Operations are generally done left-to-right, but there is an order to which they are resolved. As soon as the filter fails one of the conditions, it will stop checking the rest of them (due to short-circuit ev[1]aluation) and move on to the next filter (except for bug 41693). The evaluation order is:

  1. Anything surrounded by parentheses (( and )) is evaluated as a single unit.
  2. Turning variables/literals into their respective data. (i.e., article_namespace to 0)
  3. Function calls (norm, lcase, etc.)
  4. Unary + and - (defining positive or negative value, e.g. -1234, +1234)
  5. Keywords
  6. Boolean inversion (!x)
  7. Exponentiation (2**3 → 8)
  8. Multiplication-related (multiplication, division, modulo)
  9. Addition and subtraction (3-2 → 1)
  10. Comparisons. (<, >, ==)
  11. Boolean operations. (&, |, ^, in)

Examples

  • A & B | C is equivalent to (A & B) | C, not to A & (B | C). In particular, both false & true | true and false & false | true evaluates to true.
  • A | B & C is equivalent to (A | B) & C, not to A | (B & C). In particular, both true | true & false and true | false & false evaluates to false.

Conditions

  1. 1.0 1.1 Be aware of bug 27987
  2. The last 4 conditions are counted due to bug 41693

Useful links

  • PCRE pattern syntax.

Notes

Template:Languages

zh:Wikipedia:防滥用过滤器/操作指引