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

 
(139 intermediate revisions by 16 users not shown)
Line 1:
<languages/>
{{languages|Extension:AbuseFilter/Rules format}}
[[zh:Wikipedia:防滥用过滤器/操作指引]]
<translate>
The rules are formatted much as conditionals in a C/Java/Perl-like language.
 
== Strings ==
 
You can specify a literal by placing it in single or double quotes (for strings), or by typing it in as-is (for numbers, both floating-point and integer).</translate>
<translate>
You can get linebreaks with <tvar|1><code>\n</code></>, tab characters with <tvar|2><code>\t</code></>, and you can also escape the quote character with a backslash.
 
You can get linebreaks with <code>\n</code>, tab characters with <code>\t</code>, and you can also escape the quote character with a backslash.
Use the <tvar|1><code>+</code></> (plus) symbol to [[w:concatenation|concatenate]] two [[w:string literal|literal strings]] or the values of two [[w:Variable (computer science)|vars]] with a string value.
</translate>
 
Use the <code>+</code> (plus) symbol to [[w:concatenation|concatenate]] two [[w:string literal|literal strings]] or the values of two [[w:Variable (computer science)|vars]] with a string value.
; <translate>Examples</translate>:
 
; Examples:
<syntaxhighlight lang="perl">
"<translate> This is a string</translate>"
'<translate> This is also a string</translate>'
'<translate> This string shouldn\'t fail</translate>'
"<translate> This string\nHas a linebreak</translate>"
1234
1.234
Line 25 ⟶ 20:
</syntaxhighlight>
 
<translate>
==User-defined variables==
 
You can define custom variables for ease of understanding with the assign symbol <tvar|1><code>:=</code></> in a line (closed by <tvar|2><code>;</code></>) within a condition.</translate>
 
<translate>
Such variables may use letters, underscores, and numbers (apart from the first character) and are case sensitive-insensitive.</translate>
 
<translate>
Example (from <tvar|1>[[w:Special:AbuseFilter/79]]</>):
</translate>
 
<syntaxhighlight lang="c">
Line 44 ⟶ 37:
</syntaxhighlight>
 
<translate>
==Arrays==
 
</translate>
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-arrays.t -->
 
<translate>
AbuseFilter has support for non-associative arrays, which can be used like in the following examples.
 
</translate>
{{Caution|1=Expressions like <code>page_namespace in [14, 15]</code> may not work as expected. This one will evaluate to <samp>true</samp> also if <code>page_namespace</code> is <samp>1</samp>, <samp>4</samp>, or <samp>5</samp>. For more information and possible workarounds, please see [[:phab:T181024|T181024]].}}
 
<syntaxhighlight lang="c">
Line 56 ⟶ 49:
my_array[0] == 5
length(my_array) == 4
int( my_array ) === 4 // Same as length
string(my_array) == "5\n6\n7\n10\n" //<translate>Note: the last linebreak could be removed in the future</translate>
float( my_array ) === 4.0 // Counts the elements
string(my_array) == "5\n6\n7\n10\n" // Note: the last linebreak could be removed in the future
5 in my_array == true
'5' in my_array == true
'5\n6' in my_array == true //<translate> Note: this is due to how arrays are casted to string, i.e. by imploding them with linebreaks</translate>
1 in my_array == true //<translate> Note: this happens because 'in' casts arguments to strings, so the 1 is catchedcaught in '10' and returns true.</translate>
my_array[] := 57 // This appends an element at the end of the array
my_array === [ 5, 6, 7, 10, 57 ]
my_array[2] := 42 // And this is for changing an element in the array
my_array === [ 5, 6, 42, 10, 57 ]
</syntaxhighlight>
 
<translate>
== Comments ==
 
You can specify comments using the following syntax:
</translate>
/* <translate>This is a comment</translate> */
 
/* This is a comment */
<translate>
 
== Arithmetic ==
 
You can use basic arithmetic symbols to do arithmetic on variables and literals with the following syntax:
</translate>
 
* <code>-</code> — <translate>Subtract the right-hand operand from the left-hand operand.</translate>
* <code>+</code> — <translate>Add the right-hand operand to the left-hand operand.</translate>
* <code>*</code> — <translate>Multiply the left-hand operand by the right-hand operand.</translate>
* <code>/</code> — <translate>Divide the left-hand operand by the right-hand operand.</translate>
* <code>**</code> — <translate>Raise the left-hand operand to the exponential power specified by the right-hand operand.</translate>
* <code>%</code> — <translate>Return the remainder given when the left-hand operand is divided by the right-hand operand.</translate>
 
The type of the returned result is the same that would be returned by PHP, for which a lot of documentation may be found [https://php.net/language.operators.arithmetic online].
<translate>
 
The type of the returned result is the same that would be returned by PHP, for which a lot of documentation may be found [<tvar|url>https://secure.php.net/language.operators.arithmetic</> online].</translate>
More exhaustive examples may be found in [https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/arith.t this AF parser test].
<translate>
More exhaustive examples may be found in [<tvar|url>https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/arith.t</> this AF parser test].
</translate>
 
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-arithmetic.t -->
{| class="wikitable"
!style="width: 50%;"|<translate>Example</translate>
!<translate>Result</translate>
|-
| <code>1 + 1</code> || 2
Line 105 ⟶ 99:
|}
 
<translate>
== 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.
</translate>
 
* <translate><tvar|1><code>x | y</code></> &mdash; OR &ndash; returns true if one or more of the conditions is true.</translate>
* <translate><tvar|1><code>x & y</code></> &mdash; AND &ndash; returns true if both of the conditions are true.</translate>
* <translate><tvar|1><code>x ^ y</code></> &mdash; XOR &ndash; returns true if one, and only one of the two conditions is true.</translate>
* <translate><tvar|1><code>!x</code></> &mdash; NOT &ndash; returns true if the condition is not true.</translate>
 
'''<translate>Examples</translate>'''
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-bools.t -->
 
{| class="wikitable"
|-
!Code
! <translate>Code</translate>
! <translate>Result</translate>
|-
| <code>1 <nowiki>|</nowiki> 1</code>
Line 153 ⟶ 145:
| <code>!1</code>
| false
|-
| <code>!0</code>
| true
|}
 
{{anchor|Simple comparisons}}
 
<translate>
== Simple comparisons ==
 
You can compare [[w:Variable (computer science)|variables]] with other variables and [[w:Operand|literals]] with the following [[w:syntax|syntax]]:
 
</translate>
* <translate><tvar|1><code>&lt;</code></>, <tvar{{int|2>comma-separator}}<code>&gt;</code></> &mdash; Return true if the left-hand [[w:Operand|operand]] is ''less than/greater than'' the right-hand operand respectively.</translate> <translate>Watch out: operands are casted to strings and, like it happens in PHP, <code><tvar|1>null <</> any number <tvar|2>=== true</></code> and <code><tvar|3>null ></> any number <tvar|4>=== false</></code>.</translate>
* <translate><tvar|1><code>&lt;=</code></>, <tvar{{int|2>comma-separator}}<code>&gt;=</code></> &mdash; Return true if the left-hand operand is ''less than or equal to/greater than or equal to'' the right-hand operand respectively.</translate> <translate>Watch out: operands are casted to strings and, like it happens in PHP, <code><tvar|1>null <=</> any number <tvar|2>=== true</></code> and <code><tvar|3>null >=</> any number <tvar|4>=== false</></code>.</translate>
* <translate><tvar|1><code>==</code></> (or <tvar|2><code>=</code></>), <tvar|3><code>!=</code></> &mdash; Return true if the left-hand operand is ''equal to/not equal to'' the right-hand operand respectively.</translate>
* <translate><tvar|1><code>===</code></>, <tvar{{int|2>comma-separator}}<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.</translate>
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-comparisons.t -->
 
{| class="wikitable"
! style="width: 50%;"| <translate>Example</translate>
! <translate>Result</translate>
|-
|<code>1 == 2</code>|| false
Line 216 ⟶ 211:
|-
|<code>['1'] == '1'</code>
|false<ref><translate>Comparing arrays to other types will always return false, except for the example above</translate></ref>
|}
 
== Built-in variables ==
<translate>
== Built-in variables ==
 
The abuse filter passes various variables by name into the parser.</translate>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.
 
<translate>
===Variables from AbuseFilter===
These variables can be accessed by typing their name in, in a place where a literal would work.</translate>
 
<translate>
====Variables always available====
You can view the variables associated with each request in the abuse log.
 
{{Caution|1=User-related variables are always available, except for one case: account creation when the creator is not logged in.All variables starting with =<code>user_</code> are affected.}}
 
===Variables from AbuseFilter===
</translate>
{| class="wikitable sortable"
! Description
|+ <translate>Variables available</translate>
! Name
! <translate>Description</translate>
! Data type
! <translate>Name</translate>
! Notes
! <translate>Data type</translate>
! <translate>Notes</translate>
|-
| <translate>Action</translate> || <code>action</code> || <translate>string</translate> || <translate>One of the following: <tvar|1>edit, move, createaccount, autocreateaccount, delete, upload</></translate><ref name="upload"><translate> The only variables currently available for file uploads <tvar|1>(action='upload')</> are <tvar|2>user_*, page_*, file_sha1, file_size, file_mime, file_mediatype, file_width, file_height, file_bits_per_channel</> (the last five were only added since the release for MediaWiki 1.27, <tvar|3>[[mw:gerrit:281503|gerrit:281503]]</>). All the <tvar|4>file_*</> variables are unavailable for other actions (including <tvar|5>action='edit'</>).</ref>, <tvar|6>stashupload</><ref>Since MediaWiki 1.28 (<tvar|7>https[[mw://gerrit.wikimedia.org/r/#/c/:295254/</>|gerrit:295254]])</translate></ref>
|-
| 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.
| {{int|abusefilter-edit-builder-vars-user-editcount}} || <code>user_editcount</code> || <translate>integer/null</translate> || <translate>Null only for unregistered users.</translate>
|-
| {{int|abusefilter-edit-builder-vars-userwiki-name}} || <code>user_namewiki_name</code> || <translate> string</translate> || {{note|1=<translate>For instance, this is empty"enwiki" foron the English Wikipedia, and <tvar|1>"createaccountitwikiquote"</> action,on usethe <tvar|2><code>accountname</code></>Italian insteadWikiquote.</translate>}}
|-
| {{int|abusefilter-edit-builder-vars-userwiki-emailconfirmlanguage}} || <code>user_emailconfirmwiki_language</code> || <translate>string</translate> || <translate>InFor instance, this is "en" on the format:English YYYYMMDDHHMMSS.</translate>Wikipedia, <translate>Nulland if"it" on the emailItalian wasn'tWikiquote. confirmedMulti-lingual wikis like Commons, Meta, and Wikidata will also report as "en".</translate>
|-
| {{int|abusefilter-edit-builder-vars-user-ageeditcount}} || <code>user_ageuser_editcount</code> || <translate>integer</translate>null || <translate>InNull seconds.</translate> <translate>0only for unregistered users.</translate>
|-
| {{int|abusefilter-edit-builder-vars-user-blockedname}} || <code>user_blockeduser_name</code> || booleanstring || <translate><tvar{{note|1>true</>=For for"createaccount" blockedand registered"autocreateaccount" usersactions, use <tvar|2code>falseaccountname</code> forif unregisteredyou users.</translate>want {{note|1=<translate>Thisthe doesn'tname differentiateof betweenthe partialaccount andbeing total blockscreated.</translate>}}
|-
| {{int|abusefilter-edit-builder-vars-user-groupsemailconfirm}} || <code>user_groupsuser_emailconfirm</code> || <translate>array of strings<string/translate>null || <translate>seeIn <tvar|1>[[Specialthe format:ListGroupRights]]</></translate> YYYYMMDDHHMMSS.Null if the email wasn't confirmed.
|-
| {{int|abusefilter-edit-builder-vars-user-rightsage}} || <code>user_rightsuser_age</code> || <translate>array of strings</translate>integer || <translate>seeIn <tvar|1>[[Special:ListGroupRights]]</></translate>seconds.0 for unregistered users.
|-
| {{int|abusefilter-edit-builder-vars-user-blocked}} || <code>user_blocked</code> || boolean || True for blocked registered users.Also true for edits from blocked IP addresses, even if the editor is a registered user who is not blocked.False otherwise. {{note|1=This doesn't differentiate between partial and total blocks.}}
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-page-id}}</span> || <span style="opacity:0.5"><code>article_articleid</code></span> || <span style="opacity:0.5"><translate>integer</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_id</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-user-groups}} || <code>user_groups</code> || array of strings || see [[Special:ListGroupRights]]
| {{ll|Manual:Page_table#page_id|{{int|abusefilter-edit-builder-vars-page-id}}}} (found in the page's HTML source - search for wgArticleId) || <code>page_id</code> || <translate>integer</translate> || <translate>In theory this is 0 for new pages, but this is unreliable. Instead, use "<tvar|1>page_age == 0</>" to identify new page creation. </translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-pageuser-nsrights}}</span> || <span style="opacity:0.5"><code>article_namespaceuser_rights</code></span> || <spanarray style="opacity:0.5"><translate>integer</translate></span>of strings || {{deprecated-inline}}see <translate>Use <tvar|1><code>page_namespace</code></> instead.</translate>[[Special:ListGroupRights]]
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-page-nsid}}</span> || <span style="opacity:0.5"><code>page_namespacearticle_articleid</code></span> || <translatespan style="opacity:0.5"> integer</translatespan> || <translate>refers to {{<tvar|1>ll|Manual:Namespace#Builtdeprecated-in_namespaces</>|namespace indexinline}} Use <code>page_id</translatecode> instead.
|-
| {{ll|Manual:Page_table#page_id|{{int|abusefilter-edit-builder-vars-page-ageid}}}} (found in the page's HTML source - search for wgArticleId) || <code>page_agepage_id</code> || <translate>integer</translate> || <translate>theIn numbertheory ofthis secondsis since0 thefor firstnew editpages, (orbut this is unreliable.If you need an exact result, use "page_age == 0" forto identify new pagespage creation. (note that it is slower, though.)</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-page-titlens}}</span> || <span style="opacity:0.5"><code>article_textarticle_namespace</code></span> || <span style="opacity:0.5"><translate>string</translate> integer</span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_titlepage_namespace</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-page-titlens}} || <code>page_titlepage_namespace</code> || <translate>string</translate>integer || refers to {{ll|Manual:Namespace#Built-in_namespaces|namespace index}}
|-
| {{int|abusefilter-edit-builder-vars-page-age}} || <code>page_age</code> || integer || the number of seconds since the first edit (or 0 for new pages).This is reliable, but it tends to be slow; consider using <code>page_id</code> if you don't need much precision.
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-page-prefixedtitle}}</span> || <span style="opacity:0.5"><code>article_prefixedtext</code></span> || <span style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_prefixedtitle</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-page-prefixedtitletitle}}</span> || <span style="opacity:0.5"><code>page_prefixedtitlearticle_text</code></span> || <translatespan style="opacity:0.5"> string</translatespan> || {{deprecated-inline}} Use <code>page_title</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictionspage-edittitle}}</span> || <span style="opacity:0.5"><code>article_restrictions_editpage_title</code></span> || <span style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_restrictions_edit</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictionspage-editprefixedtitle}}</span> || <span style="opacity:0.5"><code>page_restrictions_editarticle_prefixedtext</code></span> || <translate>arrayspan ofstyle="opacity:0.5"> stringsstring</translatespan> || {{deprecated-inline}} Use <code>page_prefixedtitle</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictionspage-moveprefixedtitle}}</span> || <span style="opacity:0.5"><code>article_restrictions_movepage_prefixedtitle</code></span> || <span style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_restrictions_move</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictions-moveedit}}</span> || <span style="opacity:0.5"><code>page_restrictions_movearticle_restrictions_edit</code></span> || <translate>arrayspan ofstyle="opacity:0.5"> stringsstring</translatespan> || {{deprecated-inline}} Use <code>page_restrictions_edit</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictions-uploadedit}}</span> || <span style="opacity:0.5"><code>article_restrictions_uploadpage_restrictions_edit</code></span> || <spanarray style="opacity:0.5"><translate>string</translate></span>of strings || {{deprecated-inline}} <translate>Use <tvar|1><code>page_restrictions_upload</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictions-uploadmove}}</span> || <span style="opacity:0.5"><code>page_restrictions_uploadarticle_restrictions_move</code></span> || <translate>arrayspan ofstyle="opacity:0.5"> stringsstring</translatespan> || {{deprecated-inline}} Use <code>page_restrictions_move</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictions-createmove}}</span> || <span style="opacity:0.5"><code>article_restrictions_createpage_restrictions_move</code></span> || <spanarray style="opacity:0.5"><translate>string</translate></span>of strings || {{deprecated-inline}} <translate>Use <tvar|1><code>page_restrictions_create</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictions-createupload}}</span> || <span style="opacity:0.5"><code>page_restrictions_createarticle_restrictions_upload</code></span> || <translate>arrayspan ofstyle="opacity:0.5"> stringsstring</translatespan> || {{deprecated-inline}} Use <code>page_restrictions_upload</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-recentrestrictions-contributorsupload}}</span> || <span style="opacity:0.5"><code>article_recent_contributorspage_restrictions_upload</code></span> || <span style="opacity:0.5">array of strings</span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_recent_contributors</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-restrictions-create}}</span> || <span style="opacity:0.5"><code>article_restrictions_create</code></span> || <span style="opacity:0.5"> string</span> || {{deprecated-inline}} Use <code>page_restrictions_create</code> instead.
| {{int|abusefilter-edit-builder-vars-recent-contributors}} || <code>page_recent_contributors</code> || <translate>array of strings</translate> || <translate>This tends to be '''slow''' (see [[<tvar|1>#Performance</>|#Performance]]).</translate> <translate>Try to put conditions more likely evaluate to false before this one, to avoid unnecessarily running the query.</translate> <translate>This value is empty if the user is the only contributor to the page(?), and only scans the last 100 revisions</translate>
|-
| {{int|abusefilter-edit-builder-vars-restrictions-create}} || <code>page_restrictions_create</code> || array of strings ||
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-first-contributor}}</span> || <span style="opacity:0.5"><code>article_first_contributor</code></span> || <span style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>page_first_contributor</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-recent-contributors}}</span> || <span style="opacity:0.5"><code>article_recent_contributors</code></span> || <span style="opacity:0.5">array of strings</span> || {{deprecated-inline}} Use <code>page_recent_contributors</code> instead.
| {{int|abusefilter-edit-builder-vars-first-contributor}} || <code>page_first_contributor</code> || <translate>string</translate> || <translate>This tends to be '''slow''' (see [[<tvar|1>#Performance</>|#Performance]]).</translate><ref><translate>Several filters (<tvar|1>[https://logstash.wikimedia.org/app/kibana#/doc/logstash-*/logstash-2017.11.22/mediawiki?id=AV_k0nzBSUnOz-leiQ1Q 1][https://logstash.wikimedia.org/app/kibana#/doc/logstash-*/logstash-2017.11.22/mediawiki?id=AV_kvnragaOKEclNGlrc 2]</>) that use this variable have showed up in the [<tvar|url>https://logstash.wikimedia.org/app/kibana#/dashboard/941a0560-b9b2-11e7-a3f7-cb9288bcae84</> AbuseFilterSlow Grafana dashboard] (requires logstash access to view). Moving this variable to towards the end of the filter seemed to help.</translate></ref> <translate>Try to put conditions more likely evaluate to false before this one, to avoid unnecessarily running the query.</translate>
|-
| {{int|abusefilter-edit-builder-vars-recent-contributors}} || <code>page_recent_contributors</code> || array of strings || This tends to be '''slow''' (see [[#Performance]]).Try to put conditions more likely evaluate to false before this one, to avoid unnecessarily running the query.This value is empty if the user is the only contributor to the page(?), and only scans the last 100 revisions
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-first-contributor}}</span> || <span style="opacity:0.5"><code>article_first_contributor</code></span> || <span style="opacity:0.5"> string</span> || {{deprecated-inline}} Use <code>page_first_contributor</code> instead.
|-
| {{int|abusefilter-edit-builder-vars-first-contributor}} || <code>page_first_contributor</code> || string || This tends to be '''slow''' (see [[#Performance]]).<ref>Several filters ([https://logstash.wikimedia.org/app/kibana#/doc/logstash-*/logstash-2017.11.22/mediawiki?id=AV_k0nzBSUnOz-leiQ1Q 1][https://logstash.wikimedia.org/app/kibana#/doc/logstash-*/logstash-2017.11.22/mediawiki?id=AV_kvnragaOKEclNGlrc 2]) that use this variable have showed up in the [https://logstash.wikimedia.org/app/kibana#/dashboard/941a0560-b9b2-11e7-a3f7-cb9288bcae84 AbuseFilterSlow Grafana dashboard] (requires logstash access to view).Moving this variable to towards the end of the filter seemed to help.</ref> Try to put conditions more likely evaluate to false before this one, to avoid unnecessarily running the query.
|}
 
==== Variables available for some actions ====
{{Caution|1=Always check that the variables you want to use are available for the current action being filtered, e.g. by using the <code>action</code> variable.Failing to do so (for instance using <code>accountname</code> for an edit, or <code>edit_delta</code> for a deletion) will make any code using the variable in question return false.}}
 
{| class="wikitable sortable"
! Description
|+ <translate>Variables available for some actions</translate>
! Name
! <translate>Description</translate>
! Data type
! <translate>Name</translate>
! Notes
! <translate>Data type</translate>
! <translate>Notes</translate>
|-
| {{int|abusefilter-edit-builder-vars-summary}} || <code>summary</code> || <translate> string</translate> || <translate>Summaries automatically created by MediaWiki ("New section", "Blanked the page", etc.) are created ''after'' the filter checks the edit, so they will never actually catch, even if the debugger shows that they should.</translate><ref><translate>See <tvar|1>[[phabricator:T191722]]</></translate></ref>
|-
| <s>{{int|abusefilter-edit-builder-vars-minor-edit}}</s> || <s><code>minor_edit</code></s> || <s><translate>string</translate></s> || <translate>Disabled, and set to false for all entries between 2016 and 2018.</translate><ref><translate>Deprecated with [<tvar|1>https[mw://gerrit.wikimedia.org/r/#/c/:296268/</> |this commit]] and disabled with [<tvar|2>https[mw://gerrit.wikimedia.org/r/#/c/mediawiki/extensions/AbuseFilter/+/:481544/</> |this one]].</translate></ref>
|-
| {{int|abusefilter-edit-builder-vars-old-textwikitext}} || <code>old_wikitext</code> || <translate>string</translate> ||<translate> This variable can be very large. Consider using <tvar|1><code>removed_lines</code></> if possible to improve performance.</translate>
|-
| {{int|abusefilter-edit-builder-vars-new-textwikitext}} || <code>new_wikitext</code> || <translate>string</translate> || <translate>This variable can be very large. Consider using <tvar|1><code>added_lines</code></> if possible to improve performance.</translate>
|-
| {{int|abusefilter-edit-builder-vars-diff}} || <code>edit_diff</code> || <translate> string</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-diff-pst}} || <code>edit_diff_pst</code> || <translate>string</translate> || <translate>This tends to be '''slow''' (see [[<tvar|1>#Performance</>|#Performance]]).</translate> <translate>Checking both <tvar|1><code>added_lines</code></> and <tvar|2><code>removed_lines</code></> is probably more efficient.</translate><ref><translate> Some filters using this variable have showed up in the AbuseFilterSlow Grafana dashboard ([<tvar|url>https://logstash.wikimedia.org/app/kibana#/doc/logstash-*/logstash-2017.11.22/mediawiki?id=AV_j4TTs-DyidI0FMJNq</> example], requires logstash access).</translate> <translate>For instance, instead of using <tvar|1><code>"text" in edit_diff_pst</code></> (or even <tvar|2><code>edit_diff</code></>), consider something like <tvar|3><code>"text" in added_lines & !("text" in removed_lines)</code></></translate></ref>
|-
| {{int|abusefilter-edit-builder-vars-newsize}} || <code>new_size</code> || <translate>integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-oldsize}} || <code>old_size</code> || <translate> integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-delta}} || <code>edit_delta</code> || integer ||
| {{int|abusefilter-edit-builder-vars-delta}} || <code>edit_delta</code> || <translate>integer</translate> || {{tip|1=<translate>Ensure that the action being filtered is an edit, either by checking for <tvar|1><code>action === 'edit'</code></> or checking other edit-specific variables.</translate> <translate>Otherwise, <tvar|2><code>edit_delta</code></> will be <tvar|3><code>null</code></>, thus being considered less than every other number (see [[<tvar|4>#Simple comparisons</>|#Simple comparisons]]).</translate>
|-
| {{int|abusefilter-edit-builder-vars-addedlines-pst}} || <code>added_lines_pst</code> || <translate>array of strings</translate> || <translate>Use <tvar|1><code>added_lines</code></> if possible, which is more efficient.</translate>
|-
| {{int|abusefilter-edit-builder-vars-addedlines}} || <code>added_lines</code> || <translate>array of strings</translate> || <translate>includes all lines in the final diff that begin with +</translate>
|-
| {{int|abusefilter-edit-builder-vars-removedlines}} || <code>removed_lines</code> || <translate>array of strings</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-all-links}} || <code>all_links</code> || array of strings ||
|-
| {{int|abusefilter-edit-builder-vars-old-links}} || <code>old_links</code> || array of strings ||
|-
| {{int|abusefilter-edit-builder-vars-added-links}} || <code>added_links</code> || <translate> array of strings</translate> || <translate>This tends to be '''slow''' (see [[<tvar|1>#Performance</>|#Performance]]).</translate> <translate>Consider checking against <tvar|1><code>added_lines</code></> first, then check <tvar|2><code>added_links</code></> so that fewer edits are slowed down.</translate> <translate>This follows {{<tvar|1>ll|Help:Links#External_links</>|MediaWiki's rules for external links}}.</translate> <translate>Only unique links are added to the array.</translate> <translate>Changing a link will count as 1 added and 1 removed link.</translate>
|-
| {{int|abusefilter-edit-builder-vars-removed-links}} || <code>removed_links</code> || <translate>array of strings</translate> || <translate>This tends to be '''slow''' (see [[<tvar|1>#Performance</>|#Performance]]).</translate> <translate>Consider checking against <tvar|1><code>removed_lines</code></> first, then check <tvar|2><code>removed_links</code></> so that fewer edits are slowed down.</translate> <translate>This follows {{<tvar|1>ll|Help:Links#External_links</>|MediaWiki's rules for external links}}.</translate> <translate>Only unique links are added to the array.</translate> <translate>Changing a link will count as 1 added and 1 removed link.</translate>
|-
| {{int|abusefilter-edit-builder-vars-new-pst}} || <code>new_pst</code> || <translate>string</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-new-html}} || <code>new_html</code> || <translate>string</translate> ||<translate> This variable can be very large. Consider using <tvar|1><code>added_lines</code></> if possible to improve performance.</translate>
|-
| {{int|abusefilter-edit-builder-vars-new-text}} || <code>new_text</code> || <translate> string</translate> ||<translate> This variable can be very large. Consider using <tvar|1><code>added_lines</code></> if possible to improve performance.</translate>
|-
| <s>{{int|abusefilter-edit-builder-vars-old-html}}</s> || <s><code>old_html</code></s> || <s><translate>string</translate></s> || <translate>Disabled for performance reasons.</translate>
|-
| <s>{{int|abusefilter-edit-builder-vars-old-text}}</s> || <s><code>old_text</code></s> || <s><translate>string</translate></s> || <translate>Disabled for performance reasons.</translate>
|-
| {{int|abusefilter-edit-builder-vars-file-sha1}} || <code>file_sha1</code> || string || <ref name=upload/>
| Unix timestamp of change || <code>timestamp</code> || <translate>string</translate> || <translate><tvar|1>int(timestamp)</> gives you a number with which you can calculate the date, time, day of week, etc.</translate>
|-
| {{int|abusefilter-edit-builder-vars-file-sha1size}} || <code>file_sha1file_size</code> || <translate>string</translate>integer || The file size in bytes<ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-sizewidth}} || <code>file_sizefile_width</code> || <translate>integer</translate> || <translate>The file sizewidth in bytes</translate>pixels<ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-widthheight}} || <code>file_widthfile_height</code> || <translate>integer</translate> || <translate>The widthheight in pixels</translate><ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-heightbits-per-channel}} || <code>file_heightfile_bits_per_channel</code> || <translate>integer</translate> || <translate>The heightamount inof pixels</translate>bits per color channel<ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-bits-per-channelmime}} || <code>file_bits_per_channelfile_mime</code> || <translate>integer</translate>string || <translate>The amountfile of[[w:MIME|MIME]] bits per color channel</translate>type.<ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-mediatype}} || <code>file_mediatype</code> || string || The file media type.<ref> See [https://gerrit.wikimedia.org/g/mediawiki/core/+/d887b9ff37a4956f6db15ddeea68e6d933e108c7/includes/libs/mime/defines.php the source code] for a list of types.</ref><ref name=upload/>
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-id}}</span> || <span style="opacity:0.5"><code>moved_to_articleid</code></span> || <span style="opacity:0.5"><translate>integer</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>moved_to_id</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-id}}</span> || <span style="opacity:0.5"><code>moved_to_idmoved_to_articleid</code></span> || <translatespan style="opacity:0.5">integer</translatespan> || {{deprecated-inline}} Use <code>moved_to_id</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-titleid}}</span> || <span style="opacity:0.5"><code>moved_to_textmoved_to_id</code></span> || <span style="opacity:0.5"><translate>string</translate></span>integer || {{deprecated-inline}} <translate>Use <tvar|1><code>moved_to_title</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-title}}</span> || <span style="opacity:0.5"><code>moved_to_titlemoved_to_text</code></span> || <translatespan style="opacity:0.5">string</translatespan> || {{deprecated-inline}} Use <code>moved_to_title</code> instead.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-prefixedtitletitle}}</span> || <span style="opacity:0.5"><code>moved_to_prefixedtextmoved_to_title</code></span> || <span style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}} <translate>Use <tvar|1><code>moved_to_prefixedtitle</code></> instead.</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-prefixedtitle}}</span> || <span style="opacity:0.5"><code>moved_to_prefixedtitlemoved_to_prefixedtext</code></span> || <translatespan style="opacity:0.5">string</translatespan> || {{deprecated-inline}} Use <code>moved_to_prefixedtitle</code> instead.
|-
| {{int|abusefilter-edit-builder-vars-movedto-nsprefixedtitle}} || <code>moved_to_namespacemoved_to_prefixedtitle</code> || <translate>string</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedto-agens}} || <code>moved_to_agemoved_to_namespace</code> || <translate>integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedfrommovedto-nsage}} || <code>moved_from_namespacemoved_to_age</code> || <translate>string</translate>integer ||
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedfrommovedto-titlerestrictions-edit}}</span> || <span style="opacity:0.5"><code>moved_from_textmoved_to_restrictions_edit</code></span> || <spanarray of style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}}Same <translate>Useas <tvar|1><code>moved_from_titlepage_restrictions_edit</code></>, insteadbut for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrommovedto-titlerestrictions-move}} || <code>moved_from_titlemoved_to_restrictions_move</code> || array of string || Same as <translatecode>stringpage_restrictions_move</translatecode>, ||but for the target of the move.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedfrommovedto-prefixedtitlerestrictions-upload}}</span> || <span style="opacity:0.5"><code>moved_from_prefixedtextmoved_to_restrictions_upload</code></span> || <spanarray of style="opacity:0.5"><translate>string</translate></span> || {{deprecated-inline}}Same <translate>Useas <tvar|1><code>moved_from_prefixedtitlepage_restrictions_upload</code></>, but insteadfor the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrommovedto-prefixedtitlerestrictions-create}} || <code>moved_from_prefixedtitlemoved_to_restrictions_create</code> || array of string || Same as <translatecode>stringpage_restrictions_create</translatecode>, ||but for the target of the move.
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedfrommovedto-idrecent-contributors}}</span> || <span style="opacity:0.5"><code>moved_from_articleidmoved_to_recent_contributors</code></span> || <spanarray style="opacity:0.5"><translate>integer</translate></span>of strings || {{deprecated-inline}}Same <translate>Useas <tvar|1><code>moved_from_idpage_recent_contributors</code></>, but insteadfor the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrommovedto-idfirst-contributor}} || <code>moved_from_idmoved_to_first_contributor</code> || string || Same as <translatecode>integerpage_first_contributor</translatecode>, ||but for the target of the move.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-agens}} || <code>moved_from_agemoved_from_namespace</code> || <translate>integer</translate> ||
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-accountnamemovedfrom-title}}</span> || <span style="opacity:0.5"><code>accountnamemoved_from_text</code></span> || <translatespan style="opacity:0.5">string</translatespan> || {{deprecated-inline}} Use <code>moved_from_title</code> instead.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-title}} || <code>moved_from_title</code> || string ||
|<translate>Content model of the old revision</translate>
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedfrom-prefixedtitle}}</span> || <span style="opacity:0.5"><code>moved_from_prefixedtext</code></span> || <span style="opacity:0.5">string</span> || {{deprecated-inline}} Use <code>moved_from_prefixedtitle</code> instead.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-prefixedtitle}} || <code>moved_from_prefixedtitle</code> || string ||
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedfrom-id}}</span> || <span style="opacity:0.5"><code>moved_from_articleid</code></span> || <span style="opacity:0.5">integer</span> || {{deprecated-inline}} Use <code>moved_from_id</code> instead.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-id}} || <code>moved_from_id</code> || integer ||
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-age}} || <code>moved_from_age</code> || integer ||
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-edit}} || <code>moved_from_restrictions_edit</code> || array of string || Same as <code>page_restrictions_edit</code>, but for the page being moved.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-move}} || <code>moved_from_restrictions_move</code> || array of string || Same as <code>page_restrictions_move</code>, but for the page being moved.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-upload}} || <code>moved_from_restrictions_upload</code> || array of string || Same as <code>page_restrictions_upload</code>, but for the page being moved.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-create}} || <code>moved_from_restrictions_create</code> || array of string || Same as <code>page_restrictions_create</code>, but for the page being moved.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-recent-contributors}} || <code>moved_from_recent_contributors</code> || array of strings || Same as <code>page_recent_contributors</code>, but for the page being moved.
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-first-contributor}} || <code>moved_from_first_contributor</code> || string || Same as <code>page_first_contributor</code>, but for the page being moved.
|-
| {{int|abusefilter-edit-builder-vars-accountname}} || <code>accountname</code> || string ||
|-
| Content model of the old revision
|<code>old_content_model</code>
|<translate> string</translate>
|<translate> See <tvar|1>{{ll|Help:ChangeContentModel}}</> for information about content model changes</translate>
|-
|<translate> Content model of the new revision</translate>
|<code>new_content_model</code>
|<translate> string</translate>
|<translate> See <tvar|1>{{ll|Help:ChangeContentModel}}</> for information about content model changes</translate>
|-
|}
<translate>
 
===Variables from other extensions===
 
</translate>
{{Note|1=Most of these variables are always set to false when examinating past edits, and may not reflect their actual value at the time the edit was made.See [[:phab:T102944|T102944]].|2=warn}}
 
{| class="wikitable sortable"
|+
! <translate>Description</translate>
! <translate>Name</translate>
! <translate>Data type</translate>
! <translate>Values</translate>
! <translate>Added by</translate>
|-
| {{int|abusefilter-edit-builder-vars-global-user-groups}}
|<code>global_user_groups</code>
|<translate> array</translate>
|
|{{ll|Extension:CentralAuth|nsp=0}}
Line 422 ⟶ 450:
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-board-id}}</span>
| <span style="opacity:0.5"><code>board_articleid</code></span>
| <span style="opacity:0.5"><translate>integer</translate></span>
| {{deprecated-inline}} <translate>Use <code>board_id</code> instead.</translate>
|{{ll|Extension:StructuredDiscussions|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-board-id}}
|<code>board_id</code>
|<translate> integer</translate>
|
|{{ll|Extension:StructuredDiscussions|nsp=0}}
Line 434 ⟶ 462:
|{{int|abusefilter-edit-builder-vars-board-namespace}}
|<code>board_namespace</code>
|<translate> integer</translate>
|<translate> refers to {{<tvar|1>ll|Manual:Namespace#Built-in_namespaces</>|namespace index}}</translate>
|{{ll|Extension:StructuredDiscussions|nsp=0}}
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-board-title}}</span>
| <span style="opacity:0.5"><code>board_text</code></span>
| <span style="opacity:0.5"><translate>string</translate></span>
| {{deprecated-inline}} <translate>Use <code>board_title</code> instead.</translate>
|{{ll|Extension:StructuredDiscussions|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-board-title}}
|<code>board_title</code>
|<translate> string</translate>
|
|{{ll|Extension:StructuredDiscussions|nsp=0}}
Line 452 ⟶ 480:
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-board-prefixedtitle}}</span>
| <span style="opacity:0.5"><code>board_prefixedtext</code></span>
| <span style="opacity:0.5"><translate>string</translate></span>
| {{deprecated-inline}} <translate>Use <tvar|1><code>board_prefixedtitle</code></> instead.</translate>
|{{ll|Extension:StructuredDiscussions|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-board-prefixedtitle}}
|<code>board_prefixedtitle</code>
|<translate> string</translate>
|
|{{ll|Extension:StructuredDiscussions|nsp=0}}
Line 464 ⟶ 492:
| {{int|abusefilter-edit-builder-vars-translate-source-text}}
|<code>translate_source_text</code>
|<translate> string</translate>
|
|{{ll|Extension:Translate|nsp=0}}
|-
| {{int|abusefilter-edit-builder-vars-translate-target-language}}
|<code>translate_target_language</code>
| string
| This is the language code, like <code>en</code> for English.
|{{ll|Extension:Translate|nsp=0}}
|-
| {{int|abusefilter-edit-builder-vars-tor-exit-node}}
|<code>tor_exit_node</code>
| <translate>boolean</translate>
| <translate>true if the action comes from a tor exit node.</translate>
|{{ll|Extension:TorBlock|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-user-mobile}}
| <code>user_mobile</code>
| <translate>boolean</translate>
| <translate>true for mobile users, false otherwise.</translate>
|{{ll|Extension:MobileFrontend|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-user-app}}
| <code>user_app</code>
| <translate>boolean</translate>
| <translate>true if the user is editing from the mobile app, false otherwise.</translate>
|{{ll|Extension:MobileApp|nsp=0}}
|-
| <span style="opacity:0.5">Page views[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-page-views/&namespace=0]</span>
|{{int|abusefilter-edit-builder-vars-user-wpzero}}
| <code>user_wpzero</code>
| <translate>boolean</translate>
| <translate>'''Note''': This variable is only valid when filtering an action. When examining a past edit or batch testing, it'll always be null.</translate>
|{{ll|Extension:WikimediaEvents|nsp=0}}
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-page-views}}</span>
| <span style="opacity:0.5"><code>article_views</code></span>
| <span style="opacity:0.5"><translate>integer</translate></span>
| {{deprecated-inline}} <translate>Use <tvar|1><code>page_views</code></> instead.</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
|{{int| Page views[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-page-views}}/&namespace=0]
| <code>page_views</code>
| <translate>integer</translate>
| <translate>the amount of page views</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
|{{int| Source page views[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-movedfrom-views}}/&namespace=0]
| <code>moved_from_views</code>
| <translate>integer</translate>
| <translate>the amount of page views of the source page</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
|{{int| Target page views[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-movedto-views}}/&namespace=0]
| <code>moved_to_views</code>
| <translate>integer</translate>
| <translate>the amount of page views of the target page</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
| {{int|True if this action was performed through a proxy[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-is-proxy}}/&namespace=0]
| <code>is_proxy</code>
| <translate>integer</translate>
| <translate>Whether this action was performed through a proxy</translate>
| {{ll|Extension:AutoProxyBlock|nsp=0}}
|-
|{{int| Whether the IP address is blocked using the stopforumspam.com list[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-sfs-blocked}}/&namespace=0]
| <code>sfs_blocked</code>
| <translate>boolean</translate>
| <translate>Whether the IP address is blocked using the <tvar|1>stopforumspam.com</> list</translate>
| {{ll|Extension:StopForumSpam|nsp=0}}
|}
<translate>
 
=== Notes ===
</translate>
<translate>When <tvar|1><code>action='move'</code></>, only the <tvar|2><code>summary</code></>, <tvar|3><code>action</code></>,<tvar|4> <code>timestamp</code></> and <tvar|5><code>user_*</code></> variables are available.</translate><translate> The <tvar|1><code>page_*</code></> variables are also available, but the prefix is replaced by <tvar|2><code>moved_from_</code></> and <tvar|3><code>moved_to_</code></>, that represent the values of the original article name and the destination one, respectively.</translate><translate> For example, <tvar|1><code>moved_from_title</code></> and <tvar|2><code>moved_to_title</code></> instead of <tvar|3><code>page_title</code></>.</translate>
 
When <code>action='move'</code>,only the <code>summary</code>,<code>action</code>,<code>timestamp</code> and <code>user_*</code> variables are available.The <code>page_*</code> variables are also available, but the prefix is replaced by <code>moved_from_</code> and <code>moved_to_</code>, that represent the values of the original article name and the destination one, respectively.For example, <code>moved_from_title</code> and <code>moved_to_title</code> instead of <code>page_title</code>.
<translate>Since MediaWiki 1.28 (<tvar|1>https://gerrit.wikimedia.org/r/#/c/295254/</>), <tvar|2><code>action='upload'</code></> is only used when publishing an upload, and not for uploads to stash.</translate><translate> A new <tvar|1><code>action='stashupload'</code></> is introduced, which is used for all uploads, including uploads to stash. This behaves like <tvar|2><code>action='upload'</code></> used to, and only provides file metadata variables (<tvar|3><code>file_*</code></>).</translate><translate> Variables related to the page edit, including <tvar|1><code>summary</code></>, <tvar|2><code>new_wikitext</code></> and several others, are now available for <tvar|3><code>action='upload'</code></>.</translate><translate> For every file upload, filters may be called with <tvar|1><code>action='stashupload'</code></> (for uploads to stash), and are always called with <tvar|2><code>action='upload'</code></>; they are not called with <tvar|3><code>action='edit'</code></>.</translate>
 
Since MediaWiki 1.28 ([[mw:gerrit:295254|gerrit:295254]]), <code>action='upload'</code> is only used when publishing an upload, and not for uploads to stash.A new <code>action='stashupload'</code> is introduced, which is used for all uploads, including uploads to stash.This behaves like <code>action='upload'</code> used to, and only provides file metadata variables (<code>file_*</code>).Variables related to the page edit, including <code>summary</code>, <code>new_wikitext</code> and several others, are now available for <code>action='upload'</code>.For every file upload, filters may be called with <code>action='stashupload'</code> (for uploads to stash), and are always called with <code>action='upload'</code>; they are not called with <code>action='edit'</code>.
<translate>Filter authors should use <tvar|1><code>action='stashupload' | action='upload'</code></> in filter code when a file can be checked based only on the file contents – for example, to reject low-resolution files – and <tvar|2><code>action='upload'</code></> only when the wikitext parts of the edit need to be examined too – for example, to reject files with no description.</translate><translate> This will allow tools that separate uploading the file and publishing the file (e.g. [[UploadWizard]] or [[upload dialog]]) to inform the user of the failure before they spend the time filling in the upload details.</translate>
 
Filter authors should use <code>action='stashupload' | action='upload'</code> in filter code when a file can be checked based only on the file contents – for example, to reject low-resolution files – and <code>action='upload'</code> only when the wikitext parts of the edit need to be examined too – for example, to reject files with no description.This will allow tools that separate uploading the file and publishing the file (e.g. [[mw:UploadWizard|UploadWizard]] or [[mw:upload dialog|upload dialog]]) to inform the user of the failure before they spend the time filling in the upload details.
 
{{anchor|Performance}}
 
<translate>
=== Performance ===
 
</translate>
<translate>As noted in the table above, some of these variables can be very slow. While writing filters, remember that the condition limit is '''not''' a good metric of how heavy filters are.</translate><translate> For instance, variables like <tvar|1><code>*_recent_contributors</code></> or <tvar|2><code>*_links</code></> always need a DB query to be computed, while <tvar|3><code>*_pst</code></> variables will have to perform parsing of the text, which again is a heavy operation; all these variables should be used very, very carefully.</translate><translate> For instance, on Italian Wikipedia it's been observed that, with 135 active filters and an average of 450 used conditions, filters execution time was around 500ms, with peaks reaching 15 seconds.</translate><translate> Removing the <tvar|1><code>added_links</code></> variable from a single filter, and halving the cases when another filter would use <tvar|2><code>added_lines_pst</code></> brought the average execution time to 50ms. More specifically:</translate>
 
*<translate>Use <tvar|1><code>_links</code></> variables when you need high accuracy and checking for "<tvar|2>http://...</>" in other variables (for instance, <tvar|3><code>added_lines</code></>) could lead to heavy malfunctioning;</translate>
*<translate>Use <tvar|1><code>_pst_links</code></> variables when you're reallyneed surehigh thataccuracy non-PSTand variableschecking aren'tfor enough"http://..." Youin mayother also conditionally decide which one to check: if,variables (for instance, you want to examine a signature, check first if <tvar|2><code>added_lines</code></>) containscould <tvar|3><code><nowiki>~~~</nowiki></code></>lead to heavy malfunctioning;</translate>
*Use <code>_pst</code> variables when you're really sure that non-PST variables aren't enough.You may also conditionally decide which one to check: if, for instance, you want to examine a signature, check first if <code>added_lines</code> contains <code><nowiki>~~~</nowiki></code>;
*<translate>In general, when dealing with these variables, it's always much better to consume further conditions but avoid computing heavy stuff. In order to achieve this, always put heavy variables as last conditions.</translate>
*In general, when dealing with these variables, it's always much better to consume further conditions but avoid computing heavy stuff.In order to achieve this, always put heavy variables as last conditions.
<translate>Last but not least, note that whenever a variable is computed for a given filter, it'll be saved and any other filter will immediately retrieve it. This means that one single filter computing this variable counts more or less as dozens of filters using it.</translate>
 
<translate>
Last but not least, note that whenever a variable is computed for a given filter, it'll be saved and any other filter will immediately retrieve it.This means that one single filter computing this variable counts more or less as dozens of filters using it.
 
== Keywords ==
</translate>
{{note|1=<translate>Where not specifically stated, keywords cast their operands to strings</translate>}}
<translate>The following special keywords are included for often-used functionality:</translate>
* <translate><tvar|1><code>like</code></> (or <tvar|2><code>matches</code></>) returns true if the left-hand operand matches the [[w:Glob (programming)#Syntax|glob pattern]] in the right-hand operand.</translate>
* <translate><tvar|1><code>in</code></> returns true if the right-hand operand (a string) contains the left-hand operand. '''Note''': empty strings are not contained in, nor contain, any other string (not even the empty string itself).</translate>
* <translate><tvar|1><code>contains</code></> works like <tvar|2><code>in</code></>, but with the left and right-hand operands switched. '''Note''': empty strings are not contained in, nor contain, any other string (not even the empty string itself).</translate>
* <translate><tvar|1><code>rlike</code></> (or <tvar|2><code>regex</code></>) and <tvar|3><code>irlike</code></> return true if the left-hand operand matches (contains) the [[w:Regular expression|regex]] pattern in the right-hand operand (<tvar|4><code>irlike</code></> is case '''i'''nsensitive).</translate><translate> The system uses [[w:Perl Compatible Regular Expressions|PCRE]]. The only PCRE option enabled is <tvar|1><code>PCRE_UTF8</code></> (modifier <tvar|2><code>u</code></> [<tvar|3>https://secure.php.net/reference.pcre.pattern.modifiers</> in PHP]); for <tvar|4><code>irlike</code></> both <tvar|5><code>PCRE_CASELESS</code></> and <tvar|6><code>PCRE_UTF8</code></> are enabled (modifier <tvar|7><code>iu</code></>).</translate>
* <code>if ... then ... else ... end</code>
* <code>... ? ... : ...</code>
* <translate><tvar|1><code>true</code></>, <tvar|2><code>false</code></> and <tvar|3><code>null</code></></translate>
 
{{note|1=Where not specifically stated, keywords cast their operands to strings}}
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:Glob (programming)#Syntax|glob pattern]] in the right-hand operand.
*<code>in</code> returns true if the right-hand operand (a string) contains the left-hand operand.'''Note:'''empty strings are not contained in, nor contain, any other string (not even the empty string itself).
*<code>contains</code> works like <code>in</code>, but with the left and right-hand operands switched.'''Note:'''empty strings are not contained in, nor contain, any other string (not even the empty string itself).
*<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> [https://php.net/reference.pcre.pattern.modifiers in PHP]); for <code>irlike</code> both <code>PCRE_CASELESS</code> and <code>PCRE_UTF8</code> are enabled (modifier <code>iu</code>).
 
*<code>if ... then ... end</code>
*<code>if ... then ... else ... end</code>
*<code>... ? ... : ...</code>
*<code>true</code>,<code>false</code>,<code>null</code>
 
<translate>'''Examples'''</translate>
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-keywords.t -->
{| class="wikitable"
Line 587 ⟶ 620:
| <code> "o" in ["foo", "bar"]</code>
| True
| <translate>Due to the string cast</translate>
|-
| <code>"foo" regex "\w+"</code>
Line 595 ⟶ 628:
| <code>"a\b" regex "a\\\\b"</code>
| True
| rowspan= "2" | <translate>To look for the escape character backslash using regex you need<br /> to use either four backslashes or two <tvar|1><code>\x5C</code></>. (Either works fine.)</translate>
|-
| <code>"a\b" regex "a\x5C\x5Cb"</code>
| True
|}
 
<translate>
== Functions ==
 
</translate>
<translate>A number of built-in functions are included to ease some common issues. They are executed in the general format <tvar|1><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.</translate>
 
{| class="wikitable sortable"
! <translate>name</translate> !! <translate>description</translate>
|-
| <code>lcase</code> || <translate>Returns the argument converted to lower case.</translate>
|-
| <code>ucase</code> || <translate>Returns the argument converted to upper case.</translate>
|-
| <code>length</code> || <translate>Returns the length of the string given as the argument. If the argument is an array, returns its number of elements.</translate>
|-
| <code>string</code> || <translate>Casts to string data type. If the argument is an array, implodes it with linebreaks.</translate>
|-
| <code>int</code> || <translate>Casts to integer data type.</translate>
|-
| <code>float</code> || <translate>Casts to floating-point data type.</translate>
|-
| <code>bool</code> || <translate>Casts to boolean data type.</translate>
|-
| <code>norm</code> || <translate>Equivalent to <tvar|1> <code>rmwhitespace(rmspecials(rmdoubles(ccnorm(arg1))))</code></>.</translate>
|-
| <code>ccnorm</code> || <translate>Normalises confusable/similar characters in the argument, and returns a canonical form. A list of characters and their replacements can be found [[<tvar|1>phab:source/Equivset/browse/master/data/equivset.in</>|on git]], ege.g. <tvar|2><code>ccnorm( "Eeèéëēĕėęě3ƐƷ" ) === "EEEEEEEEEEEEE"</code></>.</translate><ref name="T27619"><translate>Be aware of <tvar|1>[[phab:T27619]]</>. You can use <tvar|2>[[Special:AbuseFilter/tools]]</> to evaluate <tvar|3><code>ccnorm( "your string" )</code></> to see which characters are transformed.</translate></ref> <translate>The output of this function is always uppercase.</translate>
|-
| <code>ccnorm_contains_any</code> || <translate>Normalises confusable/similar characters in the arguments, and returns true if the first string contains '''any''' stringsstring from the following arguments (unlimited number of arguments, logic OR mode). A list of characters and their replacements can be found [[<tvar|1>phab:source/Equivset/browse/master/data/equivset.in</>|on git]].</translate>
|-
| <code>ccnorm_contains_all</code> || <translate>Normalises confusable/similar characters in the arguments, and returns true if the first string contains '''every''' stringsstring from the following arguments (unlimited number of arguments, logic AND mode). A list of characters and their replacements can be found [[<tvar|1>phab:source/Equivset/browse/master/data/equivset.in</>|on git]].</translate>
|-
| <code>specialratio</code> || <translate>Returns the number of non-alphanumeric characters divided by the total number of characters in the argument.</translate>
|-
| <code>rmspecials</code> || <translate>Removes any special characters in the argument, and returns the result. (Equivalent to <tvar|1>s/[^\p{L}\p{N}]//g</>.)</translate>
|-
| <code>rmdoubles</code> || <translate>Removes repeated characters in the argument, and returns the result.</translate>
|-
| <code>rmwhitespace</code> || <translate>Removes whitespace (spaces, tabs, newlines).</translate>
|-
| <code>count</code> || <translate>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.</translate>
|-
| <code>rcount</code> || <translate>Similar to <tvar|1><code>count</code></> but the needle uses a regular expression instead. Can be made case-insensitive by letting the regular expression start with "<tvar|2>(?i)".Please note that, for plain strings, this function can be up to 50 times slower than <code>count</code>"<ref>https://3v4l.org/S6IGP</translateref>, so use that one when possible.
|-
| <code>get_matches</code> || {{MW version-inline|MW 1.31+}} <translate>Looks for matches of the regex needle (first string) in the haystack (second string). Returns an array where the 0 element is the whole match and every <tvar|1><code>[n]</code></> element is the match of the n'th capturing group of the needle. Can be made case-insensitive by letting the regular expression start with "<tvar|2>(?i)</>". If a capturing group didn't match, that array position will take value of ''false''.</translate>
|-
| <code>ip_in_range</code> || <translate>Returns true if user's IP (first string) matches the specified IP rangesrange (second string, in [[:w:CIDR notation|CIDR notation]]). Only works for anonymous users. Supports both IPv4 and IPv6 addresses.</translate>
|-
| <code>contains_any</code> || <translate>Returns true if the first string contains '''any''' stringsstring from the following arguments (unlimited number of arguments in logic OR mode). If the first argument is an array, it gets casted to string.</translate>
|-
| <code>contains_all</code> || <translate>Returns true if the first string contains '''every''' stringsstring from the following arguments (unlimited number of arguments in logic AND mode). If the first argument is an array, it gets casted to string.</translate>
|-
| <code>equals_to_any</code> || <translate>Returns true if the first argument is identical (<tvar|1><code>===</code></>) to any of the following ones (unlimited number of arguments). Basically, <tvar|2><code>equals_to_any(a, b, c)</code></> is the same as <tvar|3><code><nowiki>a===b | a===c</nowiki></code></>, but more compact and saves conditions.</translate>
|-
| <code>substr</code> || <translate>Returns the portion of the first string, by offset from the second argument (starts at 0) and maximum length from the third argument (optional).</translate>
|-
| <code>strlen</code> || <translate>Same as <tvar|1><code>length</code></>.</translate>
|-
| <code>strpos</code> || <translate>Returns the numeric position of the first occurrence of needle (second string) in the haystack (first string), starting from offset from the third argument (optional, default is 0). 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 <tvar|1><code>===</code></> or <tvar|2><code>!==</code></> for testing whether it is found.</translate>
|-
| <code>str_replace</code> || <translate>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 on, text to find, replacement text.</translate>
|-
| <code>rescape</code> || <translate>Returns the argument with some characters preceded with the escape character <tvar|1>"\"</>, so that the string can be used in a regular expression without those characters having a special meaning.</translate>
|-
| <code>set</code> || <translate>Sets a variable (first string) with a given value (second argument) for further use in the filter. Another syntax: <tvar|1><code>''name'' := ''value''</code></>.</translate>
|-
| <code>set_var</code> || <translate>Same as <tvar|1><code>set</code></>.</translate>
|}
 
<translate>
=== Examples ===
 
</translate>
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-functions.t -->
{| class="wikitable"
! Code
! Result
! Comment
|-
| <code>length( "Wikipedia" )</code>
Line 685 ⟶ 718:
| <code>ccnorm( "w1k1p3d14" )</code>
| WIKIPEDIA
|<translate><tvar|1> <code>ccnorm</code></> output is always uppercase</translate>
|-
| <code>ccnorm( "ωɨƙɩᑭƐƉ1α" )</code>
Line 713 ⟶ 746:
|<code>norm( "F00 B@rr" )</code>
|FOBAR
|<translate><tvar|1> <code>norm</code></> removes whitespace, special characters and duplicates, then uses <tvar|2><code>ccnorm</code></></translate><!--
|-
| <code>convert( "zh-hant", "维基百科" )</code><br />// assume we work on a wiki with Chinese LanguageConverter class
Line 759 ⟶ 792:
|
|}
 
<translate>
== Order of operations ==
</translate>
<translate>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 [[phab:T43693]]). The evaluation order is:</translate>
# <translate>Anything surrounded by parentheses (<tvar|1><code>(</code></> and <tvar|2><code>)</code></>) is evaluated as a single unit.</translate>
# <translate>Turning variables/literals into their respective data. (i.e., <tvar|1><code>page_namespace</code></> to 0)</translate>
# <translate>Function calls (<tvar|1><code>norm</code></>, <tvar|2><code>lcase</code></>, etc.)</translate>
# <translate>Unary <tvar|1><code>+</code></> and <tvar|2><code>-</code></> (defining positive or negative value, e.g. <tvar|3><code>-1234</code></>, <tvar|4><code>+1234</code></>)</translate>
# <translate>Keywords</translate>
# <translate>Boolean inversion (<tvar|1><code>!x</code></>)</translate>
# <translate>Exponentiation (<tvar|1><code>2**3 → 8</code></>)</translate>
# <translate>Multiplication-related (multiplication, division, modulo)</translate>
# <translate>Addition and subtraction (<tvar|1><code>3-2 → 1</code></>)</translate>
# <translate>Comparisons. (<tvar|1><code><</code></>, <tvar|2><code>></code></>, <tvar|3><code>==</code></>)</translate>
# <translate>Boolean operations. (<tvar|1><code>&</code></>, <tvar|2><code>|</code></>, <tvar|3><code>^</code></>)</translate>
<translate>
=== Examples ===
</translate>
* <translate><tvar|1><code>A & B | C</code></> is equivalent to <tvar|2><code>(A & B) | C</code></>, not to <tvar|3><code>A & (B | C)</code></>. In particular, both <tvar|4><code>false & true '''| true'''</code></> and <tvar|5><code>false & false '''| true'''</code></> evaluates to <tvar|6><code>true</code></>.</translate>
* <translate><tvar|1><code>A | B & C</code></> is equivalent to <tvar|2><code>(A | B) & C</code></>, not to <tvar|3><code>A | (B & C)</code></>. In particular, both <tvar|4><code>true | true '''& false'''</code></> and <tvar|5><code>true | false '''& false'''</code></> evaluates to <tvar|6><code>false</code></>.</translate>
<translate>
== Condition counting ==
</translate>
<translate>The condition limit is (more or less) tracking the number of comparison operators + number of function calls entered.</translate>
 
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.The evaluation order is:
<translate>Further explanation on how to reduce conditions used can be found at {{ll|Extension:AbuseFilter/Conditions}}.</translate>
 
<translate>
#Anything surrounded by parentheses (<code>(</code> and <code>)</code>) is evaluated as a single unit.
== Exclusions ==
#Turning variables/literals into their respective data. (e.g.,<code>page_namespace</code> to 0)
</translate>
#Function calls (<code>norm</code>,<code>lcase</code>, etc.)
<translate>Although the AbuseFilter examine function will identify "rollback" actions as edits, the AbuseFilter will not evaluate rollback actions for matching.</translate> <ref>[[phab:T24713|<translate>T24713 - rollback not matched by AF</translate>]]</ref>
#Unary <code>+</code> and <code>-</code> (defining positive or negative value, e.g.<code>-1234</code>, <code>+1234</code>)
#Keywords (<code>in</code>, <code>rlike</code>, etc.)
#Boolean inversion (<code>!x</code>)
#Exponentiation (<code>2**3 → 8</code>)
#Multiplication-related (multiplication, division, modulo)
#Addition and subtraction (<code>3-2 → 1</code>)
#Comparisons. (<code><</code>, <code>></code>, <code>==</code>)
#Boolean operations. (<code>&</code>, <code>|</code>, <code>^</code>)
 
=== Examples ===
 
*<code>A & B | C</code> is equivalent to <code>(A & B) | C</code>, not to <code>A & (B | C)</code>.In particular, both <code>false & true '''| true'''</code> and <code>false & false '''| true'''</code> evaluates to <code>true</code>.
*<code>A | B & C</code> is equivalent to <code>(A | B) & C</code>, not to <code>A | (B & C)</code>.In particular, both <code>true | true '''& false'''</code> and <code>true | false '''& false'''</code> evaluates to <code>false</code>.
 
== Condition counting ==
 
The condition limit is (more or less) tracking the number of comparison operators + number of function calls entered.
 
Further explanation on how to reduce conditions used can be found at {{ll|Extension:AbuseFilter/Conditions}}.
 
== Exclusions ==
 
Although the AbuseFilter examine function will identify "rollback" actions as edits, the AbuseFilter will not evaluate rollback actions for matching.<ref>[[phab:T24713|T24713 - rollback not matched by AF]]</ref>
 
== Useful links ==
 
* [https://php.net/reference.pcre.pattern.syntax PCRE pattern syntax]
<translate>
* [[m:Edit filters benefiting to various local Wikiprojects|Edit filters benefiting to various local Wikiprojects]]
== Useful links ==
</translate>
* [https://secure.php.net/reference.pcre.pattern.syntax <translate>PCRE pattern syntax</translate>]
* [[m:Edit filters benefiting to various local Wikiprojects|<translate>Edit filters benefiting to various local Wikiprojects</translate>]]
* {{ll|Extension:AbuseFilter/Conditions}}
 
== Notes ==
<references/>
222

edits