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

mNo edit summary
 
(20 intermediate revisions by the same user not shown)
Line 230:
! Notes
|-
| Action || <code>action</code> || string || One of the following:edit, move, createaccount, autocreateaccount, delete, upload<ref name="upload"> The only variables currently available for file uploads (action='upload') are 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 [[mw:gerrit:281503|gerrit:281503]]).All the file_* variables are unavailable for other actions (including action='edit').</ref>, stashupload<ref>Since MediaWiki 1.28 ([[mw:gerrit:295254|gerrit:295254]])</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.
Line 306:
| {{int|abusefilter-edit-builder-vars-summary}} || <code>summary</code> || string || 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.<ref>See [[phabricator:T191722]]</ref>
|-
| <s>{{int|abusefilter-edit-builder-vars-minor-edit}}</s> || <s><code>minor_edit</code></s> || <s>string</s> || Disabled, and set to false for all entries between 2016 and 2018.<ref>Deprecated with [[mw:gerrit:296268|this commit]] and disabled with [[mw:gerrit:481544|this one]].</ref>
|-
| {{int|abusefilter-edit-builder-vars-old-wikitext}} || <code>old_wikitext</code> || string || This variable can be very large.Consider using <code>removed_lines</code> if possible to improve performance.
Line 346:
| <s>{{int|abusefilter-edit-builder-vars-old-text}}</s> || <s><code>old_text</code></s> || <s>string</s> || Disabled for performance reasons.
|-
| {{int|abusefilter-edit-builder-vars-file-sha1}} || <code>file_sha1</code> || <translate><!--T:186--> string</translate> || <ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-size}} || <code>file_size</code> || <translate><!--T:187--> integer</translate> || <translate><!--T:188--> The file size in bytes</translate><ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-width}} || <code>file_width</code> || <translate><!--T:189--> integer</translate> || <translate><!--T:190--> The width in pixels</translate><ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-height}} || <code>file_height</code> || <translate><!--T:191--> integer</translate> || <translate><!--T:192--> The height in pixels</translate><ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-bits-per-channel}} || <code>file_bits_per_channel</code> || <translate><!--T:193--> integer</translate> || <translate><!--T:194--> The amount of bits per color channel</translate><ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-mime}} || <code>file_mime</code> || <translate><!--T:417--> string</translate> || <translate><!--T:418--> The file [[w:MIME|MIME]] type.</translate><ref name=upload/>
|-
| {{int|abusefilter-edit-builder-vars-file-mediatype}} || <code>file_mediatype</code> || <translate><!--T:419--> string</translate> || <translate><!--T:420--> The file media type.</translate><ref><translate><!--T:461--> See [<tvar|1>https://gerrit.wikimedia.org/g/mediawiki/core/+/d887b9ff37a4956f6db15ddeea68e6d933e108c7/includes/libs/mime/defines.php</> the source code] for a list of types.</translate></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><!--T:195--> integer</translate></span> || {{deprecated-inline}} <translate><!--T:196--> Use <tvar|1><code>moved_to_id</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-id}} || <code>moved_to_id</code> || <translate><!--T:197--> integer</translate> ||
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-title}}</span> || <span style="opacity:0.5"><code>moved_to_text</code></span> || <span style="opacity:0.5"><translate><!--T:198--> string</translate></span> || {{deprecated-inline}} <translate><!--T:199--> Use <tvar|1><code>moved_to_title</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-title}} || <code>moved_to_title</code> || <translate><!--T:200--> string</translate> ||
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedto-prefixedtitle}}</span> || <span style="opacity:0.5"><code>moved_to_prefixedtext</code></span> || <span style="opacity:0.5"><translate><!--T:201--> string</translate></span> || {{deprecated-inline}} <translate><!--T:202--> Use <tvar|1><code>moved_to_prefixedtitle</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-prefixedtitle}} || <code>moved_to_prefixedtitle</code> || <translate><!--T:203--> string</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedto-ns}} || <code>moved_to_namespace</code> || <translate><!--T:421--> integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedto-age}} || <code>moved_to_age</code> || <translate><!--T:205--> integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedto-restrictions-edit}} || <code>moved_to_restrictions_edit</code> || <translate><!--T:422--> array of string</translate> || <translate><!--T:448--> Same as <tvar|1><code>page_restrictions_edit</code></>, but for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-restrictions-move}} || <code>moved_to_restrictions_move</code> || <translate><!--T:423--> array of string</translate> || <translate><!--T:449--> Same as <tvar|1><code>page_restrictions_move</code></>, but for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-restrictions-upload}} || <code>moved_to_restrictions_upload</code> || <translate><!--T:424--> array of string</translate> || <translate><!--T:450--> Same as <tvar|1><code>page_restrictions_upload</code></>, but for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-restrictions-create}} || <code>moved_to_restrictions_create</code> || <translate><!--T:425--> array of string</translate> || <translate><!--T:451--> Same as <tvar|1><code>page_restrictions_create</code></>, but for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-recent-contributors}} || <code>moved_to_recent_contributors</code> || <translate><!--T:426--> array of strings</translate> || <translate><!--T:452--> Same as <tvar|1><code>page_recent_contributors</code></>, but for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedto-first-contributor}} || <code>moved_to_first_contributor</code> || <translate><!--T:427--> string</translate> || <translate><!--T:453--> Same as <tvar|1><code>page_first_contributor</code></>, but for the target of the move.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-ns}} || <code>moved_from_namespace</code> || <translate><!--T:428--> integer</translate> ||
|-
| <span style="opacity:0.5">{{int|abusefilter-edit-builder-vars-movedfrom-title}}</span> || <span style="opacity:0.5"><code>moved_from_text</code></span> || <span style="opacity:0.5"><translate><!--T:207--> string</translate></span> || {{deprecated-inline}} <translate><!--T:208--> Use <tvar|1><code>moved_from_title</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-title}} || <code>moved_from_title</code> || <translate><!--T:209--> string</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"><translate><!--T:210--> string</translate></span> || {{deprecated-inline}} <translate><!--T:211--> Use <tvar|1><code>moved_from_prefixedtitle</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-prefixedtitle}} || <code>moved_from_prefixedtitle</code> || <translate><!--T:212--> string</translate> ||
|-
| <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"><translate><!--T:213--> integer</translate></span> || {{deprecated-inline}} <translate><!--T:214--> Use <tvar|1><code>moved_from_id</code></> instead.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-id}} || <code>moved_from_id</code> || <translate><!--T:215--> integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-age}} || <code>moved_from_age</code> || <translate><!--T:216--> integer</translate> ||
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-edit}} || <code>moved_from_restrictions_edit</code> || <translate><!--T:429--> array of string</translate> || <translate><!--T:454--> Same as <tvar|1><code>page_restrictions_edit</code></>, but for the page being moved.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-move}} || <code>moved_from_restrictions_move</code> || <translate><!--T:430--> array of string</translate> || <translate><!--T:455--> Same as <tvar|1><code>page_restrictions_move</code></>, but for the page being moved.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-upload}} || <code>moved_from_restrictions_upload</code> || <translate><!--T:431--> array of string</translate> || <translate><!--T:456--> Same as <tvar|1><code>page_restrictions_upload</code></>, but for the page being moved.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-restrictions-create}} || <code>moved_from_restrictions_create</code> || <translate><!--T:432--> array of string</translate> || <translate><!--T:457--> Same as <tvar|1><code>page_restrictions_create</code></>, but for the page being moved.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-recent-contributors}} || <code>moved_from_recent_contributors</code> || <translate><!--T:433--> array of strings</translate> || <translate><!--T:458--> Same as <tvar|1><code>page_recent_contributors</code></>, but for the page being moved.</translate>
|-
| {{int|abusefilter-edit-builder-vars-movedfrom-first-contributor}} || <code>moved_from_first_contributor</code> || <translate><!--T:434--> string</translate> || <translate><!--T:459--> Same as <tvar|1><code>page_first_contributor</code></>, but for the page being moved.</translate>
|-
| {{int|abusefilter-edit-builder-vars-accountname}} || <code>accountname</code> || <translate><!--T:217--> string</translate> ||
|-
|<translate><!--T:218--> Content model of the old revision</translate>
|<code>old_content_model</code>
| string
|<translate><!--T:219--> string</translate>
|<translate><!--T:220--> See <tvar|1>{{ll|Help:ChangeContentModel}}</> for information about content model changes</translate>
|-
|<translate><!--T:221--> Content model of the new revision</translate>
|<code>new_content_model</code>
| string
|<translate><!--T:222--> string</translate>
|<translate><!--T:223--> See <tvar|1>{{ll|Help:ChangeContentModel}}</> for information about content model changes</translate>
|-
|}
Line 432:
===Variables from other extensions===
 
{{Note|1=<translate><!--T:470--> 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.</translate> <translate><!--T:471--> See <tvar|1>[[:phab:T102944|T102944]]</>.</translate>|2=warn}}
 
{| class="wikitable sortable"
|+
! <translate><!--T:225--> Description</translate>
! Name
! <translate><!--T:226--> Name</translate>
! <translate><!--T:227--> Data type</translate>
! Values
! <translate><!--T:228--> Values</translate>
! <translate><!--T:229--> Added by</translate>
|-
| {{int|abusefilter-edit-builder-vars-global-user-groups}}
|<code>global_user_groups</code>
| array
|<translate><!--T:230--> array</translate>
|
|{{ll|Extension:CentralAuth|nsp=0}}
Line 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><!--T:231--> integer</translate></span>
| {{deprecated-inline}} <translate><!--T:232--> Use <tvar|1><code>board_id</code></> instead.</translate>
|{{ll|Extension:StructuredDiscussions|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-board-id}}
|<code>board_id</code>
|<translate><!--T:233--> integer</translate>
|
|{{ll|Extension:StructuredDiscussions|nsp=0}}
Line 462:
|{{int|abusefilter-edit-builder-vars-board-namespace}}
|<code>board_namespace</code>
|<translate><!--T:234--> integer</translate>
|<translate><!--T:235--> 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><!--T:236--> string</translate></span>
| {{deprecated-inline}} <translate><!--T:237--> Use <tvar|1><code>board_title</code></> instead.</translate>
|{{ll|Extension:StructuredDiscussions|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-board-title}}
|<code>board_title</code>
| string
|<translate><!--T:238--> string</translate>
|
|{{ll|Extension:StructuredDiscussions|nsp=0}}
Line 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><!--T:239--> string</translate></span>
| {{deprecated-inline}} <translate><!--T:240--> 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>
| string
|<translate><!--T:241--> string</translate>
|
|{{ll|Extension:StructuredDiscussions|nsp=0}}
Line 492:
| {{int|abusefilter-edit-builder-vars-translate-source-text}}
|<code>translate_source_text</code>
| string
|<translate><!--T:242--> string</translate>
|
|{{ll|Extension:Translate|nsp=0}}
Line 498:
| {{int|abusefilter-edit-builder-vars-translate-target-language}}
|<code>translate_target_language</code>
| string
| <translate><!--T:462--> string</translate>
| <translate><!--T:463--> This is the language code, like <tvar|1><code>en</code></> for English.</translate>
|{{ll|Extension:Translate|nsp=0}}
|-
| {{int|abusefilter-edit-builder-vars-tor-exit-node}}
|<code>tor_exit_node</code>
| boolean
| <translate><!--T:243--> boolean</translate>
| <translate><!--T:244--> 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><!--T:245--> boolean</translate>
| <translate><!--T:246--> true for mobile users, false otherwise.</translate>
|{{ll|Extension:MobileFrontend|nsp=0}}
|-
|{{int|abusefilter-edit-builder-vars-user-app}}
| <code>user_app</code>
| boolean
| <translate><!--T:247--> boolean</translate>
| <translate><!--T:248--> true if the user is editing from the mobile app, false otherwise.</translate>
|{{ll|Extension:MobileApp|nsp=0}}
|-
| <span style="opacity:0.5"><translate><!--T:464--> Page views</translate>[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-page-views/&namespace=0]</span>
| <span style="opacity:0.5"><code>article_views</code></span>
| <span style="opacity:0.5"><translate><!--T:252--> integer</translate></span>
| {{deprecated-inline}} <translate><!--T:253--> Use <tvar|1><code>page_views</code></> instead.</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
|<translate><!--T:465--> Page views</translate>[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-page-views/&namespace=0]
| <code>page_views</code>
| integer
| <translate><!--T:254--> integer</translate>
| <translate><!--T:255--> the amount of page views</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
|<translate><!--T:466--> Source page views</translate>[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-movedfrom-views/&namespace=0]
| <code>moved_from_views</code>
| integer
| <translate><!--T:256--> integer</translate>
| <translate><!--T:257--> the amount of page views of the source page</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
|<translate><!--T:467--> Target page views</translate>[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-movedto-views/&namespace=0]
| <code>moved_to_views</code>
| integer
| <translate><!--T:258--> integer</translate>
| <translate><!--T:259--> the amount of page views of the target page</translate>
|{{ll|Extension:HitCounters|nsp=0}}
|-
| <translate><!--T:468--> True if this action was performed through a proxy</translate>[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-is-proxy/&namespace=0]
| <code>is_proxy</code>
| integer
| <translate><!--T:260--> integer</translate>
| <translate><!--T:261--> Whether this action was performed through a proxy</translate>
| {{ll|Extension:AutoProxyBlock|nsp=0}}
|-
|<translate><!--T:469--> Whether the IP address is blocked using the stopforumspam.com list</translate>[https://translatewiki.net/wiki/Special:PrefixIndex?prefix=MediaWiki:abusefilter-edit-builder-vars-sfs-blocked/&namespace=0]
| <code>sfs_blocked</code>
| boolean
| <translate><!--T:262--> boolean</translate>
| <translate><!--T:263--> Whether the IP address is blocked using the <tvar|1>stopforumspam.com</> list</translate>
| {{ll|Extension:StopForumSpam|nsp=0}}
|}
Line 559:
=== Notes ===
 
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>
<!--T:266-->
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>
<!--T:267-->
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></>.
 
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>.
<!--T:268-->
Since MediaWiki 1.28 (<tvar|1>[[gerrit:295254]]</>), <tvar|2><code>action='upload'</code></> is only used when publishing an upload, and not for uploads to stash.</translate>
<translate>
<!--T:269-->
A new <tvar|1><code>action='stashupload'</code></> is introduced, which is used for all uploads, including uploads to stash.</translate>
<translate>
<!--T:270-->
This behaves like <tvar|1><code>action='upload'</code></> used to, and only provides file metadata variables (<tvar|2><code>file_*</code></>).</translate>
<translate>
<!--T:271-->
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>
<!--T:272-->
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></>.
 
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.
<!--T:273-->
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>
<!--T:274-->
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>
 
{{anchor|Performance}}
Line 593 ⟶ 569:
=== Performance ===
 
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.For instance, variables like <code>*_recent_contributors</code> or <code>*_links</code> always need a DB query to be computed, while <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.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.Removing the <code>added_links</code> variable from a single filter, and halving the cases when another filter would use <code>added_lines_pst</code> brought the average execution time to 50ms.More specifically:
As noted in the table above, some of these variables can be very slow.
 
<translate>
*Use <code>_links</code> variables when you need high accuracy and checking for "http://..." in other variables (for instance, <code>added_lines</code>) could lead to heavy malfunctioning;
<!--T:277-->
*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>;
While writing filters, remember that the condition limit is '''not''' a good metric of how heavy filters are.</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>
<!--T:278-->
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>
<!--T:279-->
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>
<!--T:280-->
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.</translate>
<translate>
<!--T:281-->
More specifically:
 
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.
<!--T:282-->
* 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>
<!--T:283-->
* Use <tvar|1><code>_pst</code></> variables when you're really sure that non-PST variables aren't enough.</translate> <translate><!--T:284--> You may also conditionally decide which one to check: if, for instance, you want to examine a signature, check first if <tvar|1><code>added_lines</code></> contains <tvar|2><code><nowiki>~~~</nowiki></code></>;</translate>
<translate>
<!--T:285-->
* In general, when dealing with these variables, it's always much better to consume further conditions but avoid computing heavy stuff.</translate> <translate><!--T:286--> In order to achieve this, always put heavy variables as last conditions.</translate>
<translate><!--T:287--> 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.</translate>This means that one single filter computing this variable counts more or less as dozens of filters using it.
 
== Keywords ==
 
{{note|1=<translate><!--T:290--> Where not specifically stated, keywords cast their operands to strings</translate>}}
<translate>
<!--T:291-->
The following special keywords are included for often-used functionality:
</translate>
 
* <translate><!--T:292--> <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><!--T:293--> <tvar|1><code>in</code></> returns true if the right-hand operand (a string) contains the left-hand operand.</translate> '''<translate><!--T:294--> Note:</translate>''' <translate><!--T:295--> empty strings are not contained in, nor contain, any other string (not even the empty string itself).</translate>
* <translate><!--T:296--> <tvar|1><code>contains</code></> works like <tvar|2><code>in</code></>, but with the left and right-hand operands switched.</translate> '''<translate><!--T:297--> Note:</translate>''' <translate><!--T:298--> empty strings are not contained in, nor contain, any other string (not even the empty string itself).</translate>
* <translate><!--T:299--> <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>
 
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>).
<translate>
<!--T:300-->
The system uses [[w:Perl Compatible Regular Expressions|PCRE]].</translate>
<translate>
<!--T:301-->
The only PCRE option enabled is <tvar|1><code>PCRE_UTF8</code></> (modifier <tvar|2><code>u</code></> [<tvar|3>https://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 ... end</code>
* <code>if ... then ... else ... end</code>
* <code>... ? ... : ...</code>
* <code>true</code>, <code>false</code>, <code>null</code>
 
'''<translate><!--T:302--> Examples</translate>'''
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-keywords.t -->
{| class="wikitable"
|-
! Code
! <translate><!--T:400--> Code</translate>
! Result
! <translate><!--T:401--> Result</translate>
! Comment
! <translate><!--T:402--> Comment</translate>
|-
| <code>"1234" like "12?4"</code>
Line 672 ⟶ 620:
| <code> "o" in ["foo", "bar"]</code>
| True
| <translate><!--T:303--> Due to the string cast</translate>
|-
| <code>"foo" regex "\w+"</code>
Line 680 ⟶ 628:
| <code>"a\b" regex "a\\\\b"</code>
| True
| rowspan= "2" | <translate><!--T:304--> To look for the escape character backslash using regex you need to use either four backslashes or two <tvar|1><code>\x5C</code></>.</translate> <translate><!--T:305--> (Either works fine.)</translate>
|-
| <code>"a\b" regex "a\x5C\x5Cb"</code>
Line 693 ⟶ 641:
! name !! description
|-
| <code>lcase</code> || <translate><!--T:312--> Returns the argument converted to lower case.</translate>
|-
| <code>ucase</code> || <translate><!--T:313--> Returns the argument converted to upper case.</translate>
|-
| <code>length</code> || <translate><!--T:314--> Returns the length of the string given as the argument.</translate> <translate><!--T:315--> If the argument is an array, returns its number of elements.</translate>
|-
| <code>string</code> || <translate><!--T:316--> Casts to string data type.</translate> <translate><!--T:317--> If the argument is an array, implodes it with linebreaks.</translate>
|-
| <code>int</code> || <translate><!--T:318--> Casts to integer data type.</translate>
|-
| <code>float</code> || <translate><!--T:319--> Casts to floating-point data type.</translate>
|-
| <code>bool</code> || <translate><!--T:320--> Casts to boolean data type.</translate>
|-
| <code>norm</code> || <translate><!--T:321--> Equivalent to <tvar|1> <code>rmwhitespace(rmspecials(rmdoubles(ccnorm(arg1))))</code></>.</translate>
|-
| <code>ccnorm</code> || <translate><!--T:322--> Normalises confusable/similar characters in the argument, and returns a canonical form.</translate> <translate><!--T:323--> A list of characters and their replacements can be found [[<tvar|1>phab:source/Equivset/browse/master/data/equivset.in</>|on git]], e.g. <tvar|2><code>ccnorm( "Eeèéëēĕėęě3ƐƷ" ) === "EEEEEEEEEEEEE"</code></>.</translate><ref name="T27619"><translate><!--T:324--> Be aware of <tvar|1>[[phab:T27619]]</>.</translate> <translate><!--T:325--> You can use <tvar|1>[[Special:AbuseFilter/tools]]</> to evaluate <tvar|2><code>ccnorm( "your string" )</code></> to see which characters are transformed.</translate></ref> <translate><!--T:326--> The output of this function is always uppercase.</translate>
|-
| <code>ccnorm_contains_any</code> || <translate><!--T:327--> Normalises confusable/similar characters in the arguments, and returns true if the first string contains '''any''' string from the following arguments (unlimited number of arguments, logic OR mode).</translate> <translate><!--T:328--> 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><!--T:329--> Normalises confusable/similar characters in the arguments, and returns true if the first string contains '''every''' string from the following arguments (unlimited number of arguments, logic AND mode).</translate> <translate><!--T:330--> 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><!--T:331--> Returns the number of non-alphanumeric characters divided by the total number of characters in the argument.</translate>
|-
| <code>rmspecials</code> || <translate><!--T:332--> Removes any special characters in the argument, and returns the result.</translate> <translate><!--T:333--> (Equivalent to <tvar|1>s/[^\p{L}\p{N}]//g</>.)</translate>
|-
| <code>rmdoubles</code> || <translate><!--T:334--> Removes repeated characters in the argument, and returns the result.</translate>
|-
| <code>rmwhitespace</code> || <translate><!--T:335--> Removes whitespace (spaces, tabs, newlines).</translate>
|-
| <code>count</code> || <translate><!--T:336--> Returns the number of times the needle (first string) appears in the haystack (second string).</translate> <translate><!--T:337--> If only one argument is given, splits it by commas and returns the number of segments.</translate>
|-
| <code>rcount</code> || <translate><!--T:338--> Similar to <tvar|1><code>count</code></> but the needle uses a regular expression instead.</translate> <translate><!--T:339--> Can be made case-insensitive by letting the regular expression start with "<tvar|2>(?i)</>".</translate> <translate><!--T:405--> Please note that, for plain strings, this function can be up to 50 times slower than <tvar|1><code>count</code><ref>https://3v4l.org/S6IGP</ref></>, so use that one when possible.</translate>
|-
| <code>get_matches</code> || {{MW version-inline|MW 1.31+}} <translate><!--T:340--> Looks for matches of the regex needle (first string) in the haystack (second string).</translate> <translate><!--T:341--> 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.</translate> <translate><!--T:342--> Can be made case-insensitive by letting the regular expression start with "<tvar|1>(?i)</>".</translate> <translate><!--T:343--> If a capturing group didn't match, that array position will take value of ''false''.</translate>
|-
| <code>ip_in_range</code> || <translate><!--T:344--> Returns true if user's IP (first string) matches the specified IP range (second string, in [[:enw:CIDR notation|CIDR notation]]).</translate> <translate><!--T:345--> Only works for anonymous users.</translate> <translate><!--T:346--> Supports both IPv4 and IPv6 addresses.</translate>
|-
| <code>contains_any</code> || <translate><!--T:347--> Returns true if the first string contains '''any''' string from the following arguments (unlimited number of arguments in logic OR mode).</translate> <translate><!--T:348--> If the first argument is an array, it gets casted to string.</translate>
|-
| <code>contains_all</code> || <translate><!--T:349--> Returns true if the first string contains '''every''' string from the following arguments (unlimited number of arguments in logic AND mode).</translate> <translate><!--T:350--> If the first argument is an array, it gets casted to string.</translate>
|-
| <code>equals_to_any</code> || <translate><!--T:351--> Returns true if the first argument is identical (<tvar|1><code>===</code></>) to any of the following ones (unlimited number of arguments).</translate> <translate><!--T:352--> Basically, <tvar|1><code>equals_to_any(a, b, c)</code></> is the same as <tvar|2><code><nowiki>a===b | a===c</nowiki></code></>, but more compact and saves conditions.</translate>
|-
| <code>substr</code> || <translate><!--T:353--> 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><!--T:354--> Same as <tvar|1><code>length</code></>.</translate>
|-
| <code>strpos</code> || <translate><!--T:355--> 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).</translate> <translate><!--T:356--> 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.</translate> <translate><!--T:357--> 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><!--T:358--> Replaces all occurrences of the search string with the replacement string.</translate> <translate><!--T:359--> 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><!--T:360--> 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><!--T:361--> Sets a variable (first string) with a given value (second argument) for further use in the filter.</translate> <translate><!--T:362--> Another syntax: <tvar|1><code>''name'' := ''value''</code></>.</translate>
|-
| <code>set_var</code> || <translate><!--T:363--> Same as <tvar|1><code>set</code></>.</translate>
|}
 
Line 756 ⟶ 704:
<!--Note: these examples are also used in https://phabricator.wikimedia.org/diffusion/EABF/browse/master/tests/parserTests/mwexamples-functions.t -->
{| class="wikitable"
! Code
!<translate><!--T:365--> Code</translate>
! Result
!<translate><!--T:366--> Result</translate>
!<translate><!--T:367--> Comment</translate>
|-
| <code>length( "Wikipedia" )</code>
Line 770 ⟶ 718:
| <code>ccnorm( "w1k1p3d14" )</code>
| WIKIPEDIA
|<translate><!--T:368--> <tvar|1><code>ccnorm</code></> output is always uppercase</translate>
|-
| <code>ccnorm( "ωɨƙɩᑭƐƉ1α" )</code>
Line 798 ⟶ 746:
|<code>norm( "F00 B@rr" )</code>
|FOBAR
|<translate><!--T:369--> <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
222

edits