Extension:Variables

From mediawiki.org
This page is a translated version of the page Extension:Variables and the translation is 72% complete.
Outdated translations are marked like this.
MediaWiki 拡張機能マニュアル
Variables
リリースの状態: 安定
実装 パーサー関数
説明 ページ上の変数をさらに活用できるパーサー機能を提供します。
作者
メンテナー MGChecker
最新バージョン 2.5.1 (2019-07-11)
MediaWiki 1.29+
PHP 5.5+
データベースの変更 いいえ
Composer mediawiki/variables
ライセンス ISC ライセンス
ダウンロード
README
RELEASE-NOTES
  • $wgVariablesAreVolatile
  • $wgVariablesDisabledFunctions
Quarterly downloads 201 (Ranked 41st)
Public wikis using 1,808 (Ranked 201st)
translatewiki.net で翻訳を利用できる場合は、Variables 拡張機能の翻訳にご協力ください
Vagrant role 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.

Variables 拡張機能では、ページ上の変数を定義し、同じページ、あるいは参照読み込みされたテンプレートでその変数を使用することを可能にします。式によっては、古い値などを指定された値に変更することもできます。

たったそれだけの機能ですが、非常に軽量な拡張機能なのでウィキへの負荷も少なく、1つのページ上で多くの変数を扱うことができます。最良の結果を得るためには、ParserFunctions 拡張機能も揃えて使うといいでしょう。

The #var_final parser function is shown as deprecated in MediaWiki 1.35 or above, due to changes in the parser. However, it will continue to work in this versions. See タスク T276627 and タスク T236809 for further information.
Wikimedia Foundation (WMF)によって運営されているwikiはこの拡張機能を有効にしていません。[1][2][3][4] 代替を見てください。


変数への値の代入

#vardefine

  • {{#vardefine:変数名|指定した値}}

(既存またはここに記入された) 変数名 の変数に 指定した値 の値が割り当てられます。

  • 例: {{#vardefine:iconwidth|25}}iconwidth = 25 にする

#vardefineecho

  • {{#vardefineecho:変数名|指定した値}}

#vardefine とほぼ同様に機能しますが、影響を受けた値が出力されます。

  • 例: making iconwidth = {{#vardefineecho:iconwidth|25}}

変数の値の取得

#var

変数 変数名 の値は以下のようにして取得できます

  • {{#var:変数名}}

未定義の場合、結果は空の文字列になります。エラーメッセージは表示されません。

変数が未定義の場合や空 (void) の場合に使用される値 (既定値) は以下のようにして定義できます:

  • {{#var:変数名 | 既定値 }}

これは以下と等価です:

  • {{#if: {{#var:変数名}} | {{#var:変数名 }} | 既定値 }}

これは最短のきれいな配列です。しかしバージョン2.0以前では、既定値が常時展開されるようになっています。バージョン2.0からは、実際に使用された場合のみに展開されます。

値はパーサー関数などで使用できます。

#varexists

  • {{#varexists:変数名 }} 変数が既に定義されている場合は 1 を返します。変数が定義されていない場合は、戻り値が空 (void) となります。

It supports a second and a third parameter to replace these values.

  • {{#varexists:変数名 | if-値 | else-値 }}

これは以下と等価です:

  • {{#if: {{#varexists: 変数名 }} | if-値 | else-値 }}

but it is much shorter and better arranged. Before version 2.5 though, both were always expanded. From version 2.5 on the if and else values only get expanded when their cases are actually entered.

#var_final

Variables 2.0で新しく導入された実験的な関数です。 この関数では、ページの表示が終了した時点での変数の値を、最終的に出力します。 パーサーがすべてのウィキマークアップを検証した上で値が挿入されるため、正確な値が示されるはずです。ただし、他の関数と併用することはできません。 例:

  • {{#var_final:変数名 | 既定値}}

最終的なページの表示段階で変数が存在しない場合、またはその値が空の文字列である場合に default の値が使用されます。 既定では、関数がどこで使用されていても展開するため、パラメーターはそれが必要としていなくとも展開されます。


#expr を使用するには ParserFunctions 拡張機能をインストールする必要があります。

x = 2*a + b を計算する:

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

n に 1 を足す:

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

インストール

  • ダウンロードして、ファイルをextensions/フォルダー内のVariablesという名前のディレクトリ内に配置します。
  • 以下のコードを LocalSettings.php ファイルの末尾に追加します:
    wfLoadExtension( 'Variables' );
    
  • 必要に応じて設定します
  • Yes 完了 – ウィキの「Special:Version」に移動して、拡張機能が正しくインストールされたことを確認します。

MediaWiki 1.30以前を稼働させている利用者へ:

上記の手順では、wfLoadExtension()を使用してこの拡張機能をインストールする新しい方法を記載しています。 この拡張機能をこれらの過去のバージョン (MediaWiki 1.30以前) にインストールする必要がある場合は、wfLoadExtension( 'Variables' );の代わりに以下を使用する必要があります:

require_once "$IP/extensions/Variables/Variables.php";

設定

This extension provides two configuration parameters:

$egVariablesAreVolatile
Allows to disable the parser frame being marked as volatile by the extension, i.e. disable template caching.
既定値: true;
$egVariablesDisabledFunctions
Allows to disable specified parser functions provided by this extension.
既定値: [];
例: [ 'var_final', 'vardefineecho' ];

互換性

The recommended version of the Variables extension for recent MediaWiki releases is listed below. Older versions of the extensions might work as well, but aren't tested for new MediaWiki releases.

代替

As this extension will not be enabled for wikis run by the Wikimedia Foundation (WMF), here are some alternatives:[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:ParserFunctions , 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 .

関連項目

脚注