Extension:Expressions

MediaWiki extensions manual
Expressions
Release status: stable
Implementation Parser function
Description Enhances the parser with boolean algebra.
Author(s) Marijn van Wezel (Xxmarijnwtalk)
Latest version 1.5 (2021-10-19)
Compatibility policy Master maintains backward compatibility.
MediaWiki 1.27+
PHP 5.6+
License MIT License
Download
Quarterly downloads 2 (Ranked 145th)
Translate the Expressions extension if it is available at translatewiki.net

The Expressions extension enhances the wikitext parser with boolean algebra.

Usage edit

This extension introduces the parser function #expression. It evaluates an expression and returns one of two strings depending on the result:

{{#expression: expression | value if true | value if false }}

The available operators are listed below, in order of precedence. The extension makes use of type conversion when comparing two values. Non-empty strings are converted to true, zero is converted to false, and any integers that are not zero get converted to true.

{{#expression: "foobar" |1|0}}1
{{#expression: "a" == true |1|0}}1
{{#expression: 1 == true |1|0}}1
{{#expression: "foobar" or false |1|0}}1
{{#expression: "foobar" and false |1|0}}0

Two values can be compared before type juggling using the strict equality operator (===):

{{#expression: "a" === "a" |1|0}}1
{{#expression: "a" === true |1|0}}0

Operator associativity edit

Operator associativity determines how operators of the same precedence are grouped in the absence of parentheses. If a value (operand) is both preceded and followed by operators of the same precedence (for example == "a" ==), then the value can be applied to two different operations. Operator associativity determines which of those operations gets chosen.

All operators in the Expressions extension are left-associative. This means that all operators are grouped from left to right.

{{#expression: "a" == "b" == "c" == "d" |1|0}}{{#expression: (("a" == "b") == "c") == "d" |1|0}}{{#expression: (true == "c") == "d" |1|0}}{{#expression: true == "d" |1|0}}1
{{#expression: "a" === "a" === "a" |1|0}}{{#expression: ("a" === "a") === "a" |1|0}}{{#expression: true === "a" |1|0}}0

The resulting behaviour might not be what you expect at first, therefore it is important to keep it in mind.

Type operators edit

Some operators are strictly typed and require their operand(s) to be of a certain type or else an error will occur. The table below lists all typed operators with their required type(s).

Operator Required type(s)
>= number >= number
<= number <= number
> number > number
< number < number
~ ! ~boolean !boolean
- -number

When something other than the required type is used, the evaluator will throw an exception.

Operators edit

The table below lists all operators, in order of precedence from highest to lowest. Operands of the same type have identical precedence. Operations applied by operators of higher precedence will be evaluated first.

Type Operators
Grouping (parentheses) ( )
Numbers, strings and booleans 1234.5 10 40298234
"foobar" "lorem ipsum"
true false
Unary ~ !
-
Comparison > < >= <=
Xor xor
Conjunction /\ && and
Disjunction \/ or
Implication => ->
Equality, inequality, strict equality and strict inequality ==
!=
=== <-> <=>
!== !<-> !<=> <>

Installation edit

  • Download and place the file(s) in a directory called Expressions in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'Expressions' );
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

See also edit