Extension:变量

本頁使用了標題或全文手工轉換
From mediawiki.org
This page is a translated version of the page Extension:Variables and the translation is 90% complete.
MediaWiki扩展手册
Variables
发行状态: 稳定版
实现 解析器函数
描述 介绍页面作用的变量函数。
作者
维护者 MGChecker
最新版本 2.5.1 (2019-07-11)
MediaWiki 1.29+
PHP 5.5+
数据库更改
Composer mediawiki/variables
许可协议 ISC授權條款
下載
README
RELEASE-NOTES
  • $wgVariablesAreVolatile
  • $wgVariablesDisabledFunctions
季度下載量 145 (Ranked 49th)
正在使用的公开wiki数 1,808 (Ranked 201st)
前往translatewiki.net翻譯Variables扩展
Vagrant角色 variables
問題 开启的任务 · 报告错误
警告 警告: This extension is incompatible with plans to parallelize parsing, as is intended by the use of Parsoid . Therefore, the future of this extension is uncertain, and it is expected to become incompatible with the standard MediaWiki parser within a few years. For further information, see 任务T250963 and Parsoid/Extension API § No support for sequential, in-order processing of extension tags.
为避免MediaWiki 1.35 - MediaWiki 1.38 中关于InternalParseBeforeSanitize钩子的弃用警告,或在MediaWiki 1.39 +中#var_final解析器函数失效,请更新到最新版本(目前为master分支)。 参见任务T276627任务T250963以获取更多信息。

变量(Variables)扩展允许您在页面中定义变量,然后在同一页面或包含的模板中使用、改变它的值,可以是按照表达式根据旧值计算得来等。

它很像模板,只是占用内存非常少并且作用只能在1个页面,所以你可以在1个页面使用很多个变量,不会出现因使用大量的模板导致维基运行缓慢的问题。结合解析器函數 扩展以达到最好的结果。

維基媒體基金會(WMF)運行的維基將不會啟用此擴充功能。[1][2][3][4] 參見替代選項

给变量赋值

#vardefine

  • {{#vardefine:变量名称 | 指定值 }}

将值指定值分配给(已存在或由此引入的)变量变量名

  • 示例:{{#vardefine:iconwidth|25}}使iconwidth = 25

#vardefineecho

  • {{#vardefineecho:变量名称 | 指定值 }}

#vardefine完全相同,但会打印受影响的值。

  • 例如: 使得iconwidth = {{#vardefineecho:iconwidth|25}}

检索变量的值

#var

变量“变量名称“的值的产生:

  • {{#var:变量名称}}

如果未定义,会产生空字符串,不会抛出错误。

变量未定义或为空时可以取得默认值:

  • {{#var:变量名称 | 默认值 }}

相当于:

  • {{#if: {{#var:变量名称 }} | {{#var:变量名称 }} | 默认值 }}

但这样更加简短。在版本2.0之前,默认值会自动展开。从版本2.0开始,默认值只会在真正用到时展开。

值可用于解析器函数。

#varexists

  • {{#varexists:变量名称 }} 如果变量已经定义返回1。如果没有定义则返回空。

它支持第二个和第三个参数来替换这些值。

  • {{#varexists:变量名称 | if值 | else-value }}

这相当于:

  • {{#if: {{#varexists: 变量名称 }} | if值 | else-value }}

但它更短,更好地安排。在2.5版之前,两者都是扩展的。 从2.5以后的if和else值只有在实际输入案例时才会展开。

#var_final

变量2.0中引入了实验新功能。 此函数将输出变量在页面渲染结束时具有的最后一个最后一个值。 当然,在解析器遍历整个wiki标记之后将插入该值,因此该函数不能用于其他函数,期望正在使用正确的值。 例:

  • {{#var_final:变量名称 | 默认值 }}

如果变量在最终页面呈现阶段不存在或者其值为空字符串,则将使用默认值。 默认值将在使用函数的位置展开,因此即使不需要参数,也会扩展参数。

示例

解析器函數 扩展必须已安装以使用#expr

计算x = 2*a + b

  • {{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}}

令变量n加1:

  • {{#vardefine:n|{{#expr:{{#var:n}}+1}}}}

安裝

  • 下载文件,并将其放置在您extensions/文件夹中的Variables目录内。
    开发者和代码贡献人员应从Git安装扩展,输入:cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Variables
  • 将下列代码放置在您的LocalSettings.php 的底部:
    wfLoadExtension( 'Variables' );
    
  • Configure as required
  • Yes 完成 – 在您的wiki上导航至Special:Version,以验证已成功安装扩展。

配置

該擴充功能提供了兩個配置參數:

$egVariablesAreVolatile
Allows to disable the parser frame being marked as volatile by the extension, i.e. disable template caching.
默认值: true;
$egVariablesDisabledFunctions
允許指定停用此擴充功能提供的解析器功能。
默认值: [];
示例: [ 'var_final', 'vardefineecho' ];

兼容性

下面列出了最近MediaWiki版本的Variables扩展的推荐版本。较旧版本的扩展可能也可以正常工作,但未针对新的MediaWiki版本进行测试。

替代選項

由于这个扩展不被维基媒体基金会(WMF)启用,以下是一些替代选择:[1][2][3][4]

  • If you use Variables as cache for expensive operations, you can transform the section where you need them into a template and pass the required information as template parameters instead. This will work until you reach the expansion depth limit, which can be increased if necessary.
  • If you use Variables to do more complex template operations than possible with simple Extension:解析器函數 , and if you have sufficient permissions on the server, you can use Scribunto's Lua functionality instead. You may not be able to install Scribunto on shared hosting. Note that this does not add support for global Variables. However, Extension:VariablesLua adds a Scribunto interface for the Variables store.
  • Don't use variables, instead duplicate the information you need as a variable. If that information is acquired by an expensive template call, performance may suffer. This will work until you reach the node count limit.
  • If you require variables just for autonumbering, you could look into Extension:NumerAlpha .

参见

参考资料