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

From TestWiki
Content added Content deleted
No edit summary
No edit summary
Line 331: Line 331:
== String concatenation ==
== String concatenation ==
You can use the <code>+</code> (plus) symbol to [[:en:concatenation|concatenate]] two [[:en:string literal|literal strings]] or the values of two [[:en:Variable (computer science)|vars]] with a string value.
You can use the <code>+</code> (plus) symbol to [[:en:concatenation|concatenate]] two [[:en:string literal|literal strings]] or the values of two [[:en:Variable (computer science)|vars]] with a string value.

== Keywords ==
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>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>contains</code>
* <code>if ... then ... else ... end</code>
* <code>... ? ... : ...</code>
* <code>true</code>, <code>false</code> and <code>null</code>


'''Examples'''
{| class="wikitable"
|-
! Code
! Result
! Comment
|-
| <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
|
|-
| <code>"a\b" regex "a\\\\b"</code>
| True
| rowspan= "2" | To look for the escape character backslash using regex you need<br> to use either four backslashes or two \x5C. (Either works fine.)
|-
| <code>"a\b" regex "a\x5C\x5Cb"</code>
| True
|}

Revision as of 00:37, 6 November 2017

Literals

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). You can get linebreaks with \n, tab characters with \t, and you can also escape the quote character with a backslash.

Examples

"This is a string"
'This is also a string'
'This string shouldn\'t fail'
"This string\nHas a linebreak"
1234
1.234
-123

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.

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):

(
	line1:="(\{\{(r|R)eflist|\{\{(r|R)efs|<references\s?/>|</references\s?>)";
	rcount(line1, removed_lines)
) > (
	rcount(line1, added_lines)
)

Lists:

a_list := [ 5, 6, 7];

All variables

Variables available
Description Name Data type Values
Action action string edit, move, createaccount, autocreateaccount, delete, upload[1], stashupload[2]
Edit count of the user user_editcount string Empty for unregistered users.
Name of the user account user_name string
Time email address was confirmed user_emailconfirm string YYYYMMDDHHMMSS
Age of the user account user_age in seconds; 0 for unregistered users.
Whether the user is blocked user_blocked boolean is 1 for blocked registered users; null for unregistered users.
Whether or not a user is editing through the mobile interface user_mobile boolean is 1 for mobile users.
Groups (including implicit) the user is in user_groups
Rights that the user has user_rights
⧼abusefilter-edit-builder-vars-article-id⧽ (found in the page's HTML source - search for wgArticleId) article_articleid integer In theory this is 0 for new pages, but this is unreliable. Instead, use "old_size==0" to identify new page creation.
⧼abusefilter-edit-builder-vars-article-ns⧽ article_namespace integer refers to namespace index
⧼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
Upload protection of the file article_restrictions_upload
Create protection of the page article_restrictions_create
Last ten users to contribute to the page article_recent_contributors Empty if the user is the only contributor to the page(?), only scans the last 100 revisions
First user to contribute to the page article_first_contributor
Variables available for some actions
Description Name Data type Values
Edit summary/reason summary string
Whether or not the edit is marked as minor (no longer in use) minor_edit string [3]
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
Unified diff of changes made by edit, pre-save transformed edit_diff_pst
New page size new_size integer
Old page size old_size integer
Size change in edit edit_delta
Lines added in edit, pre-save transformed added_lines_pst string
Lines added in edit added_lines array of strings includes all lines in the final diff that begin with +
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
New page wikitext, pre-save transformed new_pst
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 boolean 0, 1 (only available if TorBlock is installed)
Unix timestamp of change timestamp string int(timestamp) gives you a number with which you can calculate the date, time, day of week, etc.
SHA1 hash of file contents file_sha1 [1]
Size of the file in bytes file_size integer The file size in bytes[1]
Page ID of move destination page moved_to_articleid
⧼Abusefilter-edit-builder-vars-movedto-prefixedtext⧽ moved_to_prefixedtext
Namespace of move destination page moved_to_namespace
Namespace of move source page moved_from_namespace
⧼abusefilter-edit-builder-vars-movedfrom-prefixedtext⧽ moved_from_prefixedtext
Page ID of move source page moved_from_articleid
Account name (on account creation) accountname
Content model of the old revision old_content_model string See Help:ChangeContentModel for information about content model changes
Content model of the new revision new_content_model string See Help:ChangeContentModel for information about content model changes
Variables provided by CentralAuth
Description Name Data type Values
Global groups that the user is in global_user_groups
StructuredDiscussions variables
Description Name Data type Values
⧼abusefilter-edit-builder-vars-board-articleid⧽ board_articleid
Namespace of Structured Discussions board board_namespace refers to namespace index
⧼abusefilter-edit-builder-vars-board-text⧽ board_text
⧼abusefilter-edit-builder-vars-board-prefixedtext⧽ board_prefixedtext

Notes

When action='move', only the summary, action, timestamp and user_* variables are available. The article_* variables 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.

Since MediaWiki 1.28 (https://gerrit.wikimedia.org/r/#/c/295254/), action='upload' is only used when publishing an upload, and not for uploads to stash. A new action='stashupload' is introduced, which is used for all uploads, including uploads to stash. This behaves like action='upload' used to, and only provides file metadata variables (file_*). Variables related to the page edit, including summary, new_wikitext and several others, are now available for action='upload'. For every file upload, filters may be called with action='stashupload' (for uploads to stash), and are always called with action='upload'; they are not called with action='edit'.

Filter authors should use action='stashupload' | action='upload' in filter code when a file can be checked based only on the file contents – for example, to reject low-resolution files – and action='upload' 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. UploadWizard or upload dialog) to inform the user of the failure before they spend the time filling in the upload details.

Page/Article namespace

See also Manual:Namespace

English Wikipedia namespaces
Basic namespaces Talk namespaces
0 Main Talk 1
2 User User talk 3
4 Wikipedia Wikipedia talk 5
6 File File talk 7
8 MediaWiki MediaWiki talk 9
10 Template Template talk 11
12 Help Help talk 13
14 Category Category talk 15
100 Portal Portal talk 101
108 Book Book talk 109
Virtual namespaces
-1 Special
-2 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.
Example Result
1 == 2 false
1 <= 2 true
1 >= 2 false
1 != 2 true
1 < 2 true
1 > 2 false
2 = 2 true
'' == false true
'' === false false
1 == true true
1 === true false

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.
Example Result
1 + 1 2
2 * 2 4
1 / 2 0.5
9 ** 2 81
6 % 5 1

String concatenation

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

  1. 1.0 1.1 1.2 The only variables currently available for file uploads (action='upload') are user_*, article_*, 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, gerrit:281503). All the file_* variables are unavailable for other actions (including action='edit').
  2. Since MediaWiki 1.28 (https://gerrit.wikimedia.org/r/#/c/295254/)
  3. Always false since https://gerrit.wikimedia.org/r/#/c/296268/