Extension:AbuseFilter/Rules format/zh

規則格式与类C／JAVA／Perl语言的条件式十分相近.

-{zh-hans:字符串;zh-hant:字串;}-
你可以在单引号或双引号中填写字符串常量，亦可直接填写数字变量（包括整型与浮点型）. 你可以使用 来换行， 為制表符，而且你还可以使用半角反斜杠来转义引号字符.

使用 （加號）來串接兩個字符串常量或字符串值的變數.


 * 示例:

用戶自訂變數
你可以定義自訂的變數，只要在一行中使用賦值符號 （以 結尾）並跟隨一個條件. 這些變數可以使用字母、下劃線和數字（除第一個字符外），並且區分大小寫. 例如（來自w:Special:AbuseFilter/79）：

陣列
防濫用過濾器支援非關聯陣列，如以下範例一樣使用.

注釋
您可以使用以下語法來注釋： /* 這是一條注釋 */

算术
你可以使用基本的算术符号对变量和常量进行算术运算，语法规则如下：


 * — 用左操作数减去右操作数.
 * — 将左、右操作数相加.
 * — 将左、右操作数相乘.
 * — 用右操作数去除左操作数.
 * — 对左操作数求幂，幂次由右操作数指定.
 * — 返回左操作数除以右操作数后的余数.

返回結果的類型與PHP返回的結果相同，可以在線上找到大量文檔. 更多詳盡的例子可以在此AF解析器測試中找到.

布尔运算
你可以要求满足所有条件为真，或满足任一条件为真，或只满足任一条件为真.


 * &mdash; OR – 如果两个条件中有一个或多个为真，则返回真值True.
 * &mdash; AND – 如果两个条件中所有条件都为真，则返回真值True.
 * &mdash; XOR – 如果两个条件中当且仅当一个条件为真，则返回真值True.
 * &mdash; NOT – 如果条件为假，则返回真值True.

示例

简单的比较
你可以将变量与其他变量进行比较及運算，以下是语法规则：


 * &mdash; 如果左運算元分別小於／大於右運算元，則返回true. 注意：運算元被轉換為字符串，就如同在PHP發生的一樣， 且.
 * &mdash; 如果左運算元分別小於或等於／大於或等於右運算元，則返回true. 注意：運算元被轉換為字符串，就如同在PHP發生的一樣， 且.
 * （或 ）、 &mdash;如果左運算元分別等於／不等於右運算元，則返回true.
 * &mdash;如果左運算元等於右運算元且左運算元的資料類型等於右運算元的資料類型，則返回true，只要有一個不符合，返回false.

內建變數
防滥用过滤器可以根据变量的名称将各种变量传送给解析器. 你可以输入这些变量的名称来访问它们，就像访问常量一样. 在过滤日志中，你还可以看到各请求的相关变量.

注释
當 ，只有 、 、 和 變數可用. The  variables are also available, but the prefix is replaced by   and , that represent the values of the original article name and the destination one, respectively. 例如 和 ，而不是.

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

Filter authors should use  in filter code when a file can be checked based only on the file contents – for example, to reject low-resolution files – and   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.

效能
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  or   always need a DB query to be computed, while   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  variable from a single filter, and halving the cases when another filter would use   brought the average execution time to 50ms. 進一步來說：

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.
 * Use  variables when you need high accuracy and checking for "http://..." in other variables (for instance,  ) could lead to heavy malfunctioning;
 * Use  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   contains  ;
 * 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.

关键词
常用功能中通常會包含以下特殊关键词：


 * （或 ）— 如果左字符串能够匹配右字符串指定的通配符，则返回真值True.
 * — 如果左字符串包含右字符串，则返回真值True. 注意： 空字符串不被包含在任何其他字串，不包含任何其他字符串（甚至不包含空字符串本身）.
 * 類似 ，但左右操作符交換位置. 注意： 空字符串不被包含在任何其他字串，不包含任何其他字符串（甚至不包含空字符串本身）.
 * （或 ）和 — 如果左字符串能够匹配右字符串指定的正则表达式，则返回真值True（ 是不區分大小寫）.

此系統使用PCRE. 只有 （PHP修飾字 ）選項被啟用. 有 和 選項被啟用（修飾字 ）.



示例

函数
为了简化一些常见的操作，防滥用过滤器提供了一系列内建的函数. 它们具有相同的格式 ，并可以放置在常量或变量的位置使用. 它的参数可以是常量，可以是变量，亦可以是别的函数.

运算符的顺序
运算符通常是按从左向右的顺序运算的，但各运算符的优先级并不相同. As soon as the filter fails one of the conditions, it will stop checking the rest of them (due to short-circuit evaluation) and move on to the next filter (except for T43693). 運算顺序如下：


 * 1) 由半角圆括号（ 与 ）括住的内容会被作为一个独立单元首先运算出.
 * 2) 将变量/常量转换为它们所代表的数据. （如 變成0）
 * 3) 调用函数（ 、 等）
 * 4) 一元运算符 和 （定義正值或負值，例： ， ）
 * 5) 关键词
 * 6) 布尔反向
 * 7) 求幂
 * 8) 乘法相关（乘、除、模）
 * 9) 加减法
 * 10) 比较.
 * 11) 布尔运算

示例

 * is equivalent to, not to  . In particular, both   and   evaluates to.
 * is equivalent to, not to  . In particular, both   and   evaluates to.

條件計數
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.

排除
儘管防濫用過濾器的檢查功能會將「回退」操作標示為編輯，但防濫用過濾器不會評估匹配回退操作.

实用链接

 * PCRE模式語法
 * Edit filters benefiting to various local Wikiprojects