Extension:Expressions
Expressions Release status: stable 


Implementation  Parser function 
Description  Enhances the parser with boolean algebra. 
Author(s)  Marijn van Wezel (Xxmarijnw^{talk}) 
Latest version  1.5 (20211019) 
Compatibility policy  Master maintains backward compatibility. 
MediaWiki  1.27+ 
PHP  5.6+ 
License  MIT License 
Download  
Translate the Expressions extension if it is available at translatewiki.net  
The Expressions extension enhances the wikitext parser with boolean algebra.
UsageEdit
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. Nonempty strings are converted to true
, zero is converted to false
, and any integers that are not zero get converted to true
.
{{#expression: "foobar" 10}}
→ 1{{#expression: "a" == true 10}}
→ 1{{#expression: 1 == true 10}}
→ 1{{#expression: "foobar" or false 10}}
→ 1{{#expression: "foobar" and false 10}}
→ 0
Two values can be compared before type juggling using the strict equality operator (===
):
{{#expression: "a" === "a" 10}}
→ 1{{#expression: "a" === true 10}}
→ 0
Operator associativityEdit
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 leftassociative. This means that all operators are grouped from left to right.
{{#expression: "a" == "b" == "c" == "d" 10}}
→{{#expression: (("a" == "b") == "c") == "d" 10}}
→{{#expression: (true == "c") == "d" 10}}
→{{#expression: true == "d" 10}}
→ 1{{#expression: "a" === "a" === "a" 10}}
→{{#expression: ("a" === "a") === "a" 10}}
→{{#expression: true === "a" 10}}
→ 0
The resulting behaviour might not be what you expect at first, therefore it is important to keep it in mind.
Type operatorsEdit
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.
OperatorsEdit
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  ==

!=
 
=== <> <=>
 
!== !<> !<=> <>

InstallationEdit
 Download and place the file(s) in a directory called
Expressions
in yourextensions/
folder.  Add the following code at the bottom of your LocalSettings.php:
wfLoadExtension( 'Expressions' );
 Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.
See alsoEdit
 Extension:ParserFunctions  Enhances parser with logical and string functions