Jump to content

Extension:ArrayFunctions: Difference between revisions

From mediawiki.org
Content deleted Content added
m simplify translation units
ย 
(162 intermediate revisions by 20 users not shown)
Line 1: Line 1:
<languages/>
{{Extension
{{Extension
|name = ArrayFunctions
|name = ArrayFunctions
Line 7: Line 8:
|hook3 = GetMagicVariableIDs
|hook3 = GetMagicVariableIDs
|hook4 = ScribuntoExternalLibraries
|hook4 = ScribuntoExternalLibraries
|hook5 = CargoSetFormatClasses
|author = Marijn van Wezel ([https://www.wikibase-solutions.com Wikibase Solutions])
|author = Marijn van Wezel ([https://www.wikibase-solutions.com Wikibase Solutions])
|description = Provides a set of pure parser functions that operate on arrays
|description = <translate><!--T:1--> Provides a set of pure parser functions that operate on arrays</translate>
|version = 1.1.0
|version = 2.0.1
|update = 2023-02-03
|update = 2026-06-18
|compatibility policy = master
|compatibility policy = master
|mediawiki = >=1.35.6
|mediawiki = >=1.40.0
|php = >=7.4
|php = >=7.4
|needs-updatephp = no
|needs-updatephp = no
|license = GPL-2.0-or-later
|license = GPL-2.0-or-later
|download = {{WikimediaDownload}}
|download = {{WikimediaDownload|ArrayFunctions|phab=EARF}}
|phabricator = mediawiki-extensions-arrayfunctions
|phabricator = mediawiki-extensions-arrayfunctions
}}
}}


<span class="plainlinks" style="font-size: 88%;"><translate><!--T:8--> Documentation for other releases:</translate> [{{fullurl: {{FULLPAGENAME}} | oldid=5754125 }} 1.0] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=5767381 }} 1.1] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=5839898 }} 1.2] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=5898646 }} 1.3] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6016840 }} 1.4] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6095267 }} 1.5] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6167475 }} 1.6] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6266318 }} 1.7] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6500031 }} 1.8] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6865274 }} 1.9] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6883674 }} 1.10] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=6951244 }} 1.11] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=7038728 }} 1.12] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=7483863 }} 1.13] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=7567670 }} 1.14] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=7674757 }} 1.15] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=7734636 }} 1.16] ยท [{{fullurl: {{FULLPAGENAME}} | oldid=8171683 }} 1.17] ยท [[Special:MyLanguage/Extension:ArrayFunctions|2.0]].</span>
The '''ArrayFunctions''' extension creates an additional set of pure, [[Parsoid]]-compatible (see [[Parsoid/Extension_API#No_support_for_sequential,_in-order_processing_of_extension_tags|here]]) [[Parser functions|parser functions]] that perform operations on [[:en:arrays|arrays]]. These parser functions are pure, meaning they do not modify any previously defined arrays and only return a result based on their input arguments.


<translate>
This extension defines the following parser functions, Lua functions and magic words:
<!--T:9-->
The <tvar name=1>'''ArrayFunctions'''</tvar> extension creates a set of pure, <tvar name=2>{{ll|Parsoid}}</tvar>-compatible (see [[<tvar name=3>Special:MyLanguage/Parsoid/Extension_API#No_support_for_sequential,_in-order_processing_of_extension_tags</tvar>|here]]) [[<tvar name=4>Special:MyLanguage/Parser functions</tvar>|parser functions]] that perform operations on [[w:arrays|arrays]].</translate>

{{TOC|limit=2}}

<translate>
== Installation == <!--T:3-->
</translate>
{{ExtensionInstall
|ArrayFunctions
|localsettings=// <translate nowrap><!--T:148--> Increase <tvar name=1>$wgMaxArticleSize</tvar> to allow for larger arrays (default: 2048)</translate>
$wgMaxArticleSize = 8192;
|custom-steps=
* Configure [[#Performance limits|performance limits]] as necessary.
|registration=required
}}

<translate>
== TL;DR == <!--T:883-->
</translate>
<translate><!--T:10--> <tvar name=1>ArrayFunctions</tvar> defines a collection of parser functions that allow you to work with immutable lists and objects, collectively "arrays".</translate>
<translate><!--T:11--> Instead of storing these arrays in memory, they are outputted by the parser function directly, and can be passed around to templates or to other parser functions.</translate>
<translate><!--T:12--> While it is possible to create and work with arrays entirely through wikitext, the recommended approach is to use [[<tvar name=1>Special:MyLanguage/Extension:Scribunto</tvar>|Scribunto (Lua)]], [[<tvar name=2>Special:MyLanguage/Extension:Semantic MediaWiki</tvar>|Semantic MediaWiki]] or [[<tvar name=3>Special:MyLanguage/Extension:Cargo</tvar>|Cargo]] to create an array that contains all the necessary data, and use <tvar name=4>ArrayFunctions</tvar> only for formatting the array.</translate>
<translate><!--T:13--> The <tvar name=1>[[#af_list]]</tvar> and <tvar name=2>[[#af_object]]</tvar> parser functions should primarily be used for simple, one-off arrays.</translate>

<translate><!--T:14--> <tvar name=1>ArrayFunctions</tvar> defines many (over 30) parser functions, but the most useful ones are:</translate>

* [[#af_get]] โ€“ <translate><!--T:15--> to retrieve a value, or perform some operation succincly;</translate>
* [[#af_pipeline]] โ€“ <translate><!--T:16--> to create a pipeline of operations to prevent deep nesting;</translate>
* [[#af_foreach]] โ€“ <translate><!--T:17--> to iterate over an array;</translate>
* [[#af_show]] โ€“ <translate><!--T:18--> to output a value stored in an array;</translate>
* [[#af_print]] โ€“ <translate><!--T:19--> to print an array for debug purposes;</translate>
* [[#af_map]] โ€“ <translate><!--T:20--> to modify all elements of an array;</translate>
* [[#af_template]] โ€“ <translate><!--T:21--> to invoke a template with data stored in an array;</translate>
* [[#af_list]] โ€“ <translate><!--T:22--> to create a list;</translate>
* [[#af_object]] &mdash; <translate><!--T:23--> to create an object;</translate>
* [[#af_zip]] โ€“ <translate><!--T:24--> to zip two or more arrays together.</translate>

<translate>
== Compatibility == <!--T:25-->
</translate>
<translate><!--T:26--> <tvar name=1>ArrayFunctions</tvar> has a commitment to not breaking backwards-compatibility.</translate>
<translate><!--T:27--> This means that user-facing code (e.g. parser functions, Lua fuctions and magic words) will '''keep working indefinitely''', and changes will always be implemented in a backwards-compatible manner.</translate>

<translate><!--T:28--> <tvar name=1>ArrayFunctions</tvar> also tries to keep compatibility with older versions of MediaWiki for as long as possible, but due to the nature of extensions, this may not be possible indefinitely.</translate>
<translate><!--T:29--> In case an update of MediaWiki breaks a feature that <tvar name=1>ArrayFunctions</tvar> relies on, it will be patched in a backwards compatible manner if this is possible.</translate>
<translate><!--T:30--> Otherwise, a new MAJOR version of <tvar name=1>ArrayFunctions</tvar> will be released.</translate>

{| class="wikitable"
|+ <translate><!--T:31--> Compatibility Matrix</translate>
! ArrayFunctions
! MediaWiki
! PHP
! Scribunto<sup>1</sup>
! Semantic MediaWiki<sup>1</sup>
! Cargo<sup>1</sup>
|-
| 2.0.0+
| {{MW version|version=1.40|comment=and after}}
| {{Software version|software=PHP|version=7.4.3|comment=and after}}
| {{Software version|software=Scribunto|version=1.40|comment=and after}}
| {{Software version|software=Semantic MediaWiki|version=5.0|comment=and after}}
| {{Software version|software=Cargo|version=3.8|comment=and after}}
|-
| 1.15.0+
| {{MW version|version=1.35|comment=and after}}
| {{Software version|software=PHP|version=7.4.0|comment=and after}}
| {{Software version|software=Scribunto|version=1.35|comment=and after}}
| {{Software version|software=Semantic MediaWiki|version=4.0|comment=and after}}
| {{Software version|software=Cargo|version=3.0|comment=and after}}
|-
| 1.14+
| {{MW version|version=1.35|comment=and after}}
| {{Software version|software=PHP|version=7.4.0|comment=and after}}
| {{Software version|software=Scribunto|version=1.35|comment=and after}}
| {{Software version|software=Semantic MediaWiki|version=4.0|comment=and after}}
| ''N/A''
|-
| 1.0-1.13
| {{MW version|version=1.35|version2=1.43}}
| {{Software version|software=PHP|version=7.4.0|comment=and after}}
| {{Software version|software=Scribunto|version=1.35|comment=and after}}
| ''N/A''
| ''N/A''
|}

: <sup>1</sup> <translate><!--T:32--> Soft dependency; version constraint only applicable when the software is installed.</translate>

<translate>
== Performance limits == <!--T:33-->
</translate>

{{Software version|software=ArrayFunctions|version=1.16|version-status=stable|comment=+}}
{{Note|text=<translate><!--T:34--> MediaWiki already limits the total size of arrays through <tvar name=1>{{Setting|MaxArticleSize}}</tvar>.</translate>}}

<translate><!--T:35--> <tvar name=1>ArrayFunctions</tvar> provides a number of configuration parameters to limit the computation time of certain functions and prevent users from performing a [[w:Denial-of-service attack|denial-of-service attack]] on your wiki.</translate>
'''<translate><!--T:36--> For backwards-compatibility reasons, these limits are off by default.</translate>''' <translate><!--T:37--> The available configuration parameters are:</translate>


{| class="wikitable"
{| class="wikitable"
! {{tunit|74|Name}}
! Group
! <translate><!--T:257--> Default</translate>
! Functions
! <translate><!--T:258--> Recommended</translate>
! {{tunit|75|Description}}
|-
|-
| <code>$wgArrayFunctionsForeachIterationLimit</code> || <code>-1</code> || ~<code>1024</code>
| Construct an array or value
| <translate><!--T:38--> This configuration parameter limits the maximum number of <tvar name=1>[[#af_foreach]]</tvar> iterations a page is allowed to perform.</translate> <translate><!--T:39--> Setting it to anything below zero will impose no restrictions on the number of iterations.</translate>
| <code>[[#af_bool]]</code>, <code>[[#af_float]]</code>, <code>[[#af_int]]</code>, <code>[[#af_list]]</code>, <code>[[#af_object]]</code>, <code>[[#af_split]]</code>, <code>[[#AF_EMPTY|AF_EMPTY]]</code>, <code>[[#Scribunto|mw.af.export]]</code>
|-
|-
| <code>$wgArrayFunctionsMaxRangeSize</code> || <code>-1</code> || ~<code>4096</code>
| Extract information from an array
| <translate><!--T:149--> This configuration parameter limits the maximum size of an array constructed with the <tvar name=1>[[#af_range]]</tvar> parser function.</translate> <translate><!--T:150--> Setting it to anything below zero will impose no restrictions on the size of arrays constructed with <tvar name=1>[[#af_range]]</tvar>.</translate>
| <code>[[#af_count]]</code>, <code>[[#af_exists]]</code>, <code>[[#af_get]]</code>, <code>[[#af_isarray]]</code>, <code>[[#af_print]]</code>
|-
|-
| <code>$wgArrayFunctionsMaxPipelineLength</code> || <code>-1</code> || ~<code>$wgMaxTemplateDepth / 4</code>
| Create an array from an existing array
| <translate><!--T:151--> This configuration parameter limits the maximum length of a pipeline <tvar name=1>([[#af_pipeline]])</tvar>.</translate> <translate><!--T:152--> Setting it to anything below zero will impose no restrictions on the number of steps in a pipeline.</translate>
| <code>[[#af_keysort]]</code>, <code>[[#af_push]]</code>, <code>[[#af_set]]</code>, <code>[[#af_slice]]</code>, <code>[[#af_sort]]</code>, <code>[[#af_unique]]</code>, <code>[[#af_unset]]</code>
|}

<translate>
<!--T:153-->
These limits are reported in the parser limit report, which can be used to tweak the settings or debug a page.

== FAQ == <!--T:4-->

=== How can I define an array to be used throughout a page? === <!--T:5-->
</translate>
<translate><!--T:46--> It is not possible to directly define an array to be used throughout a page, because this would require sequential processing of extension tags, which is not supported by Parsoid (see [[<tvar name=1>Special:MyLanguage/Parsoid/Extension API#No support for sequential, in-order processing of extension tags</tvar>|Extension API#No support for sequential, in-order processing of extension tags]]).</translate>
<translate><!--T:6--> Instead, you can pass arrays around as template parameters:</translate>

<pre>
{{My template|{{#af_list:a|b|c}}}}
</pre>

<translate>
<!--T:47-->
This way, the array is available in <tvar name=1><code>Template:My template</code></tvar> as <tvar name=2><code>{{{1}}}</code></tvar>.

=== How to iterate over an array? === <!--T:7-->

<!--T:48-->
It is possible to iteratively access elements of an array using <tvar name=1>[[#af_foreach]]</tvar>:
</translate>

<pre>
{{#af_foreach:{{#af_list:red|green|blue}}||color|<nowiki/>
* {{{color}}} is my favourite.
}}
</pre>

<translate><!--T:49--> The expected output from the snipped above is:</translate>

* red is my favourite.
* green is my favourite.
* blue is my favourite.

<translate>
=== Why are values not recognized as arrays? === <!--T:50-->
</translate>
<translate><!--T:51--> This may happen because your arrays are too large.</translate>
<translate><!--T:52--> MediaWiki internally keeps a counter on the total size of template arguments, which can be increased by increasing <tvar name=1>[[Special:MyLanguage/Manual:$wgMaxArticleSize|$wgMaxArticleSize]]</tvar>.</translate>

<translate>
=== How is this different from extensions such as <tvar name=1>[[Special:MyLanguage/Extension:Arrays|Arrays]]</tvar> or <tvar name=2>[[Special:MyLanguage/Extension:Variables|Variables]]</tvar>? === <!--T:53-->
</translate>

<translate><!--T:54--> The main difference between ArrayFunctions and those extensions is that <tvar name=1>ArrayFunctions</tvar> parser functions are '''pure'''.</translate>
<translate><!--T:55--> This means that instead of modifying or declaring a variable, the parser function directly outputs its result.</translate>

<translate><!--T:56--> For example with <tvar name=1><code>#af_map</code></tvar>, the given array is not modified; instead, a copy is created, modified and then outputted.</translate>
<translate><!--T:57--> No parser function in <tvar name=1>ArrayFunctions</tvar> modifies global state: all computation happens solely with the invocation of the parser function.</translate>
<translate><!--T:58--> This makes working with <tvar name=3>ArrayFunctions</tvar> very different from working with other extensions such as <tvar name=1>[[Special:MyLanguage/Extension:Arrays|Arrays]]</tvar> or <tvar name=2>[[Special:MyLanguage/Extension:Variables|Variables]]</tvar>.</translate>
<translate><!--T:59--> Instead of imperatively modifying an array stored in a variable, [[w:function composition|function composition]] must be used to perform more complex operations.</translate>
<translate><!--T:60--> For example:</translate>

;[[Special:MyLanguage/Extension:Arrays|Arrays]]
<pre>
{{#arraydefine: fruits | orange, banana, strawberry, apple }}
{{#arraysort: fruits | asc }}
{{#arrayprint: fruits }}
</pre>
;ArrayFunctions
<pre>
{{#af_pipeline: {{#af_list: orange | banana | strawberry | apple }}
| {{#af_sort: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}
</pre>

<translate>
=== Why is whitespace trimmed from array values? === <!--T:61-->
</translate>
<translate><!--T:62--> Whitespace is inherently implicit in wikitext.</translate>
<translate><!--T:63--> In order to be consistent with this behaviour, whitespace is recursively trimmed from array values.</translate>

<translate>
== Functions == <!--T:64-->
</translate>
* <translate><!--T:65--> Parameters prefixed with a single asterisk (<tvar name=1>'''*'''</tvar>) denote that a variable number of positional arguments can be passed.</translate>
* <translate><!--T:66--> Parameters prefixed with a double asterisk (<tvar name=1>'''**'''</tvar>) denote that a variable number of named arguments can be passed.</translate>
* <translate><!--T:67--> All keyword argument names are '''case-sensitive'''.</translate>
* <translate><!--T:68--> All '''string''' arguments support the following escape sequences:</translate>
: <code>\s</code> <translate><!--T:69--> for spaces</translate>
: <code>\n</code> <translate><!--T:70--> for newlines</translate>
: <code>\\</code> <translate><!--T:71--> for backslashes</translate>

<translate><!--T:72--> The extension defines the following parser functions, Lua functions, Semantic MediaWiki result formats, Cargo display formats and magic words:</translate>

{| class="wikitable"
|+<translate><!--T:73--> Construct an array or value</translate>
! <translate><!--T:74--> Name</translate>
! <translate><!--T:75--> Description</translate>
|-
|-
| <code>[[#af_bool]]</code>
| Iterate over an array
| <translate><!--T:76--> Cast a string to a boolean.</translate>
| <code>[[#af_foreach]]</code>, <code>[[#af_join]]</code>, <code>[[#af_map]]</code>
|-
|-
| <code>[[#af_float]]</code>
| Miscellaneous functions
| <translate><!--T:77--> Cast a string to a float.</translate>
| <code>[[#af_if]]</code>, <code>[[#af_template]]</code>
|-
| <code>[[#af_int]]</code>
| <translate><!--T:78--> Cast a string to an integer.</translate>
|-
| <code>[[#af_list]]</code>
| <translate><!--T:79--> Create a new list from values.</translate>
|-
| <code>[[#af_object]]</code>
| <translate><!--T:80--> Create a new object from values.</translate>
|-
| <code>[[#af_range]]</code>
| <translate><!--T:81--> Create a finite range of integers.</translate>
|-
| <code>[[#af_split]]</code>
| <translate><!--T:82--> Split a string based on a delimiter.</translate>
|-
| <code>[[#AF_EMPTY|AF_EMPTY]]</code>
| <translate><!--T:83--> The empty array.</translate>
|-
| <code>[[#mw.af.export|mw.af.export]]</code>
| <translate><!--T:84--> Create a new array from Lua.</translate>
|-
| <code>[[#arrayfunctions|arrayfunctions]]</code>
| <translate><!--T:85--> Format a Semantic MediaWiki query result as an <tvar name=1>ArrayFunctions</tvar> array.</translate>
|-
| <code>[[#arrayfunctions_2|arrayfunctions]]</code>
| <translate><!--T:86--> Display a Cargo query result as an <tvar name=1>ArrayFunctions</tvar> array.</translate>
|}
|}


{| class="wikitable"
{{TOC|limit=3}}
|+<translate><!--T:87--> Extract information from an array</translate>
! {{tunit|74|Name}} !! {{tunit|75|Description}}
|-
| <code>[[#af_count]]</code>
| <translate><!--T:90--> Count the number of values in an array.</translate>
|-
| <code>[[#af_exists]]</code>
| <translate><!--T:91--> Check whether a key or index exists in an array.</translate>
|-
| <code>[[#af_get]]</code>
| <translate><!--T:92--> Retrieve an element from an array by index.</translate>
|-
| <code>[[#af_isarray]]</code>
| <translate><!--T:93--> Check if a value is an array.</translate>
|-
| <code>[[#af_print]]</code>
| <translate><!--T:94--> Print an array for debug purposes.</translate>
|-
| <code>[[#af_search]]</code>
| <translate><!--T:95--> Searches an array for a value.</translate>
|-
| <code>[[#af_show]]</code>
| <translate><!--T:96--> Show a value in a human-readable format.</translate>
|-
| <code>[[#mw.af.import|mw.af.import]]</code>
| <translate><!--T:97--> Create a new table from an ArrayFunctions array.</translate>
|}


{| class="wikitable"
== Functions ==
|+<translate><!--T:98--> Create an array from an existing array</translate>
! {{tunit|74|Name}} !! {{tunit|75|Description}}
|-
| <code>[[#af_difference]]</code>
| <translate><!--T:101--> Compute the difference between arrays.</translate>
|-
| <code>[[#af_filter]]</code>
| Filter element from an array.
|-
| <code>[[#af_flatten]]</code>
| <translate><!--T:102--> Flatten an array.</translate>
|-
| <code>[[#af_group]]</code>
| <translate><!--T:103--> Compute an array that when indexed yields all elements of a key from all subarrays.</translate>
|-
| <code>[[#af_intersect]]</code>
| <translate><!--T:104--> Compute the intersection of arrays.</translate>
|-
| <code>[[#af_keysort]]</code>
| <translate><!--T:105--> Sort a list of objects based on the values of a key.</translate>
|-
| <code>[[#af_ksort]]</code>
| <translate><!--T:106--> Sort an array by key.</translate>
|-
| <code>[[#af_merge]]</code>
| <translate><!--T:107--> Compute the union of arrays.</translate>
|-
| <code>[[#af_push]]</code>
| <translate><!--T:108--> Add a value to the end of a list.</translate>
|-
| <code>[[#af_put]]</code>
| <translate><!--T:109--> Set a value at an index.</translate>
|-
| <code>[[#af_reverse]]</code>
| <translate><!--T:110--> Reverse an array.</translate>
|-
| <code>[[#af_set]]</code>
| <translate><!--T:111--> Set a value at an index <tvar name=1>{{Deprecated-inline|1.13.0}}</tvar>.</translate>
|-
| <code>[[#af_slice]]</code>
| <translate><!--T:112--> Extract a slice from an array.</translate>
|-
| <code>[[#af_sort]]</code>
| <translate><!--T:113--> Sort a list.</translate>
|-
| <code>[[#af_unique]]</code>
| <translate><!--T:114--> Remove duplicates from an array.</translate>
|-
| <code>[[#af_unset]]</code>
| <translate><!--T:115--> Remove a value from an array by index.</translate>
|-
| <code>[[#af_zip]]</code>
| <translate><!--T:116--> Zip two or more arrays together.</translate>
|}

{| class="wikitable"
|+<translate><!--T:117--> Iterate over an array</translate>
! {{tunit|74|Name}} !! {{tunit|75|Description}}
|-
| <code>[[#af_foreach]]</code>
| <translate><!--T:120--> Iterate over an array.</translate>
|-
| <code>[[#af_join]]</code>
| <translate><!--T:121--> Recursively join the items of an array together with a separator.</translate>
|-
| <code>[[#af_map]]</code>
| <translate><!--T:122--> Apply a callback to each element of a list.</translate>
|-
| <code>[[#af_reduce]]</code>
| <translate><!--T:123--> Iteratively reduce the array to a single value using a callback.</translate>
|}

{| class="wikitable"
|+<translate><!--T:124--> Miscellaneous functions</translate>
! {{tunit|74|Name}} !! {{tunit|75|Description}}
|-
| <code>[[#af_if]]</code>
| <translate><!--T:127--> Select one of two alternatives based on a predicate.</translate>
|-
| <code>[[#af_pipeline]]</code>
| <translate><!--T:128--> Create a pipeline of functions.</translate>
|-
| <code>[[#af_stringmap]]</code>
| <translate><!--T:129--> Apply a callback to each value in a delimited string.</translate>
|-
| <code>[[#af_template]]</code>
| <translate><!--T:130--> Invoke a template with the values in an array.</translate>
|-
| <code>[[#af_trim]]</code>
| <translate><!--T:131--> Trim characters at the start and end of a string.</translate>
|}


=== af_bool ===
=== af_bool ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function casts a string to a boolean. This is useful for creating an array containing a boolean.


<translate><!--T:132--> This parser function casts a string to a boolean.</translate>
==== Description ====
<translate><!--T:133--> This is useful for creating an array containing a boolean.</translate>
<translate><!--T:134--> The values that are considered as <tvar name=1><code>true</code></tvar> are "1", "true", "on", and "yes".</translate>
<translate><!--T:135--> The values that are considered as <tvar name=1><code>false</code></tvar> are "0", "false", "off", and "no".</translate>
<translate><!--T:136--> If any other value is given, an error will be returned.</translate>


<translate>
==== Description ==== <!--T:137-->
</translate>
<pre>
<pre>
{{#af_bool: value }}
{{#af_bool: value }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:138-->
</translate>


{| class="wikitable"
;value <nowiki>:</nowiki> string or boolean
! {{tunit|74|Name}}
: The value to cast to a boolean.
! <translate><!--T:259--> Type</translate>
! {{tunit|75|Description}}
|-
| value || <translate><!--T:154--> string or boolean</translate> || <translate><!--T:155--> The value to cast to a boolean.</translate>
|}


<translate>
==== Return values ====
==== Return values ==== <!--T:156-->


<!--T:157-->
Returns the casted boolean.
Returns the casted boolean.


==== Examples ====
==== Examples ==== <!--T:158-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Create an opaque representation of a boolean
| <translate><!--T:159--> Create an opaque representation of a boolean</translate>
| <pre>{{#af_bool: yes }}, {{#af_bool: no }}, {{#af_bool: true }}</pre>
| <pre>{{#af_bool: yes }}, {{#af_bool: no }}, {{#af_bool: true }}</pre>
| boolean__^__1, boolean__^__0, boolean__^__1
| boolean__^__1, boolean__^__0, boolean__^__1
|-
|-
| Create an array containing a boolean
| <translate><!--T:160--> Create an array containing a boolean</translate>
| <pre>{{#af_print: {{#af_list: {{#af_bool: yes}} }} }}</pre>
| <pre>{{#af_print: {{#af_list: {{#af_bool: yes}} }} }}</pre>
|
|
Line 83: Line 440:
=== af_count ===
=== af_count ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser functions counts the number of values in an array.


<translate><!--T:161--> This parser functions counts the number of values in an array.</translate>
==== Description ====
<translate><!--T:162--> By default, this parser function only counts the number of top-level elements.</translate>
<translate><!--T:163--> However, if the keyword argument <tvar name=1><code>recursive</code></tvar> is given a <tvar name=2><code>true</code></tvar> value, it will also count the number of items in any sub-arrays.</translate>
<translate><!--T:164--> Note that in case of <tvar name=1><code>recursive=true</code></tvar>, both the element containing an array, as well as all values inside the array are counted.</translate>


<translate>
==== Description ==== <!--T:165-->
</translate>
<pre>
<pre>
{{#af_count: array | recursive=recursive }}
{{#af_count: array | recursive=recursive }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:166-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:167--> array</translate> || || <translate><!--T:168--> The array to count.</translate>
|-
| recursive || <translate><!--T:169--> boolean</translate> || {{phpi|false}} || <translate><!--T:170--> Whether to count items recursively.</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:171-->
: The array to count.
;recursive <nowiki>:</nowiki> boolean, default=false
: Whether to count items recursively. Note that elements containing a list are also counted (see examples below).

==== Return values ====


<!--T:172-->
The number of items in the array.
The number of items in the array.


==== Examples ====
==== Examples ==== <!--T:173-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Count the number of items in a one-dimensional list
| <translate><!--T:174--> Count the number of items in a one-dimensional list</translate>
| <pre>{{#af_print: {{#af_count: {{#af_list: a | b | c }} }} }}</pre>
| <pre>{{#af_print: {{#af_count: {{#af_list: a | b | c }} }} }}</pre>
| 3
| 3
|-
|-
| Count the number of items in a multi-dimensional list
| <translate><!--T:175--> Count the number of items in a multi-dimensional list</translate>
| <pre>{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }} }}</pre>
| <pre>{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }} }}</pre>
| 2
| 2
|-
|-
| Recursively count the number of items in a multi-dimensional list
| <translate><!--T:176--> Recursively count the number of items in a multi-dimensional list</translate>
| <pre>{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} | recursive=true }} }}</pre>
| <pre>{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} | recursive=true }} }}</pre>
| 6
| 6
|}
|}


=== af_exists ===
=== af_difference ===


{{Software version|software=ArrayFunctions|version=1.5|version-status=stable|comment=+}}
This parser function checks whether the given key or index exists in the given array.


[[File:Venn 0100 0000.svg|150px|thumb|<translate><!--T:177--> The difference between three arrays</translate>]]
==== Description ====


<translate><!--T:178--> This parser function computes the difference between arrays.</translate>
<translate><!--T:179--> This function preserves keys.</translate>

<translate>
==== Description ==== <!--T:180-->
</translate>
<pre>
<pre>
{{#af_exists: array | key }}
{{#af_difference: array | *arrays }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:181-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:182--> array</translate> || <translate><!--T:183--> The first array.</translate>
|-
| <nowiki>*</nowiki>arrays || <translate><!--T:184--> array</translate> || <translate><!--T:185--> The other arrays.</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:186-->
: The array to check.
</translate>
;key <nowiki>:</nowiki> string or int
<translate><!--T:187--> Returns an array containing all values from <tvar name=1><code>array</code></tvar> that are not present in any of the arrays in <tvar name=2><code>arrays</code></tvar>.</translate>
: The key to check.
<translate><!--T:188--> The keys in <tvar name=1><code>array</code></tvar> are preserved.</translate>


<translate>
==== Return values ====
==== Examples ==== <!--T:189-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:190--> Compute the difference of three arrays</translate>
| <pre>{{#af_print: {{#af_difference: {{#af_list: a | b | c }} | {{#af_list: a }} | {{#af_list: b}} }} }}</pre>
|
* 2: c
|}


=== af_exists ===
Returns true if <code>array</code> contains <code>key</code>, false otherwise.


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
==== Examples ====

<translate><!--T:191--> This parser function checks whether the given key or index exists in the given array.</translate>
<translate><!--T:192--> Multiple keys can be given to check nested arrays.</translate>

<translate>
==== Description ==== <!--T:193-->
</translate>
<pre>
{{#af_exists: array | *keys }}
</pre>

<translate>
==== Parameters ==== <!--T:194-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:195--> array</translate> || <translate><!--T:196--> The array to check.</translate>
|-
| keys || <translate><!--T:197--> string or int</translate>
| <translate><!--T:198--> The key to check.</translate> <translate><!--T:199--> Multiple keys can be given to check if a nested key exists.</translate>
|}

<translate>
==== Return values ==== <!--T:200-->

<!--T:201-->
Returns <tvar name=1>{{phpi|true}}</tvar> if <tvar name=2><code>array</code></tvar> contains the key, <tvar name=3>{{phpi|false}}</tvar> otherwise.

==== Examples ==== <!--T:202-->
</translate>


{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Check if a key exists
| <translate><!--T:203--> Check if a key exists</translate>
| <pre>{{#af_print: {{#af_exists: {{#af_object: hello=world }} | hello }} }}</pre>
| <pre>{{#af_print: {{#af_exists: {{#af_object: hello=world }} | hello }} }}</pre>
| true
| true
|-
|-
| Check if an index exists
| <translate><!--T:204--> Check if an index exists</translate>
| <pre>{{#af_print: {{#af_exists: {{#af_list: a | b | c }} | 2 }} }}</pre>
| <pre>{{#af_print: {{#af_exists: {{#af_list: a | b | c }} | 2 }} }}</pre>
| true
| true
|-
|-
| Check if a nested key exists
| <translate><!--T:205--> Check if a nested key exists</translate>
| <pre>{{#af_print: {{#af_exists: {{#af_get: {{#af_list: a | {{#af_list: b | c }} }} | 1 }} | 2 }} }}</pre>
| <pre>{{#af_print: {{#af_exists: {{#af_list: a | {{#af_list: b | c }} }} | 0 | 3 }} }}</pre>
| false
| false
|}
|}


=== af_float ===
=== af_filter ===


{{Software version|software=ArrayFunctions|version=2.0|version-status=stable|comment=+}}
This parser function casts a string to a float. This is useful for creating an array containing a float.

This parser function filters elements from an array based on a condition. The condition is evaluated for each element in the array, and if the condition returns a "falsy" value, the element will be removed. Otherwise, the element will be kept. The values that are considered "falsy" are:

* The empty string;
* The boolean <code>false</code>;
* The strings "no", "off", "false" and "0".

If no condition is given, all empty elements are removed. Indices are not reset after calling this function.


==== Description ====
==== Description ====


<pre>
<pre>
{{#af_filter: array | value_name | condition }}
{{#af_float: value }}
</pre>
</pre>


==== Parameters ====
==== Parameters ====


{| class="wikitable"
;value <nowiki>:</nowiki> string or float
! Name !! Type !! Default !! Description
: The value to cast to a float.
|-
| array || array ||
| The array to filter.
|-
| value_name || string || {{phpi|null}}
| The name to give to the value in the condition.
|-
| condition || string || {{phpi|null}}
| The condition to filter on.
|}


==== Return values ====
==== Return values ====


Returns the array with all values for which the condition returned a falsy value removed.
Returns the casted float.


==== Examples ====
==== Examples ====
Line 180: Line 623:
{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Remove all empty values
| Create an opaque representation of a float
| <pre>{{#af_print: {{#af_filter: {{#af_list: a | | c }} }} }}</pre>
|
* 0: a
* 2: c
|-
| Remove the element "b"
| <pre>{{#af_print: {{#af_filter: {{#af_list: a | b | c }} | v | {{#ifeq: {{{v}}} | b | yes }} }} }}</pre>
|
* 0: a
* 2: c
|}

=== af_flatten ===

{{Software version|software=ArrayFunctions|version=1.11|version-status=stable|comment=+}}

<translate>
<!--T:206-->
This parser function creates a new array with all sub-array elements concatenated into it recursively up to the specified depth, or until it is completely flattened if no depth is specified.

==== Description ==== <!--T:207-->
</translate>
<pre>
{{#af_flatten: array | depth }}
</pre>

<translate>
==== Parameters ==== <!--T:208-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:209--> array</translate> ||
| <translate><!--T:210--> The array to flatten.</translate>
|-
| depth || <translate><!--T:211--> integer</translate> || {{phpi|null}}
| <translate><!--T:212--> The depth to flatten to, or nothing to flatten until the array is completely flat.</translate>
|}

<translate>
==== Return values ==== <!--T:213-->

<!--T:214-->
Returns an array with all sub-array elements concatenated into it recursively up to the specified depth, or a completely flattened array if no depth is specified.

==== Examples ==== <!--T:215-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:216--> Flatten an array one level deep</translate>
| <pre>{{#af_print: {{#af_flatten: {{#af_list: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }} }}</pre>
|
* 0: a
* 1: b
* 2: c
* 3: d
* 4: e
* 5: f
|}

=== af_float ===

{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate><!--T:217--> This parser function casts a string to a float.</translate>
<translate><!--T:218--> This is useful for creating an array containing a float.</translate>

<translate>
==== Description ==== <!--T:219-->
</translate>
<pre>
{{#af_float: value }}
</pre>

<translate>
==== Parameters ==== <!--T:220-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| value || <translate><!--T:221--> string or float</translate> || <translate><!--T:222--> The value to cast to a float.</translate>
|}

<translate>
==== Return values ==== <!--T:223-->

<!--T:224-->
Returns the casted float.

==== Examples ==== <!--T:225-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:226--> Create an opaque representation of a float</translate>
| <pre>{{#af_float: 1.298 }}, {{#af_float: 0 }}</pre>
| <pre>{{#af_float: 1.298 }}, {{#af_float: 0 }}</pre>
| float__^__1.298, float__^__0
| float__^__1.298, float__^__0
|-
|-
| Create an array containing a float
| <translate><!--T:227--> Create an array containing a float</translate>
| <pre>{{#af_print: {{#af_list: {{#af_float: 1.298 }} }} }}</pre>
| <pre>{{#af_print: {{#af_list: {{#af_float: 1.298 }} }} }}</pre>
|
|
Line 192: Line 729:
=== af_foreach ===
=== af_foreach ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function provides a way to iterate over arrays.


<translate><!--T:228--> This parser function provides a way to iterate over arrays.</translate>
==== Description ====
<translate><!--T:229--> For each of the elements in the given input array, it will evaluate its body, replacing any occurences of the <tvar name=1><code>key_name</code></tvar> and <tvar name=2><code>value_name</code></tvar> variables.</translate>
<translate><!--T:230--> These variables can be accessed in the same manner as template parameters, by surrounding them with triple braces (e.g. <tvar name=1><nowiki>{{{value_name}}}</nowiki></tvar>).</translate>

<translate>
<!--T:231-->
The behaviour of this parser function might be influenced by the <tvar name=1>[[#Performance limits|<code>$wgArrayFunctionsForeachIterationLimit</code>]]</tvar> configuration parameter.


==== Description ==== <!--T:232-->
</translate>
<pre>
<pre>
{{#af_foreach: array | key_name | value_name | body }}
{{#af_foreach: array | key_name | value_name | body | delimiter=delimiter }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:233-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:234--> array</translate> || || <translate><!--T:235--> The array over which to iterate.</translate>
|-
| key_name || {{tunit|240|string}} || {{phpi|null}} || <translate><!--T:237--> The name to use for the key.</translate>
|-
| value_name || {{tunit|240|string}} || {{phpi|null}} || <translate><!--T:239--> The name to use for the value.</translate>
|-
| body || <translate><!--T:240--> string</translate> || || <translate><!--T:241--> The body to return for each iteration.</translate>
|-
| delimiter || {{tunit|240|string}} || {{phpi|""}} || <translate><!--T:243--> The delimiter to put between results (available since version 1.9.0).</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:244-->
: The array over which to iterate.
;key_name <nowiki>:</nowiki> string, default=null
: The name to use for the key.
;value_name <nowiki>:</nowiki> string, default=null
: The name to use for the value.
;body <nowiki>:</nowiki> string
: The body to return for each iteration.

==== Return values ====


<!--T:245-->
Returns the resulting wikitext.
Returns the resulting wikitext.


==== Examples ====
==== Examples ==== <!--T:246-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Iterate over a list
| <translate><!--T:247--> Iterate over a list</translate>
| <pre>{{#af_foreach: {{#af_list: John | Steve | Harry }} | | name | Hello, {{{name}}}!<br/> }}</pre>
| <pre>{{#af_foreach: {{#af_list: John | Steve | Harry }} | | name | Hello, {{{name}}}!<br/> }}</pre>
| Hello, John!<br/>Hello, Steve!<br/>Hello, Harry!
| Hello, John!<br/>Hello, Steve!<br/>Hello, Harry!<br/>
|-
|-
| Iterate over an object
| <translate><!--T:248--> Iterate over an object</translate>
| <pre>{{#af_foreach: {{#af_object: Hello=John | Hi=Steve | Welcome=Harry }} | greeting | name | {{{greeting}}}, {{{name}}}!<br/> }}</pre>
| <pre>{{#af_foreach: {{#af_object: Hello=John | Hi=Steve | Welcome=Harry }} | greeting | name | {{{greeting}}}, {{{name}}}!<br/> }}</pre>
| Hello, John!<br/>Hi, Steve!<br/>Welcome, Harry!
| Hello, John!<br/>Hi, Steve!<br/>Welcome, Harry!
|-
| <translate><!--T:249--> Iterate over a list and add a delimiter</translate>
| <pre>{{#af_foreach: {{#af_list: John | Steve | Harry }} | | name | Hello, {{{name}}}! | delimiter=<br/> }}</pre>
| Hello, John!<br/>Hello, Steve!<br/>Hello, Harry!
|}
|}


=== af_get ===
=== af_get ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function retrieves the element with the given index from the given array. If the index does not exist, the empty string is returned.


<translate><!--T:262--> This parser function retrieves the element with the given index from the given array, or performs some operation if the index is overloaded.</translate>
==== Description ====
<translate><!--T:263--> If the index does not exist and it is not an overloaded index, the empty string is returned.</translate>


<translate><!--T:264--> An overloaded index allows the user to succinctly perform certain simple operations on arrays during indexing.</translate>
<translate><!--T:265--> Using an overloaded index is identical to first performing an <tvar name=1><code>#af_get</code></tvar> with all indices before the overloaded index, then performing the function specified by the overloaded index, and then performing another <tvar name=1><code>#af_get</code></tvar> with the remaining indices.</translate>
<translate><!--T:266--> For example, <tvar name=1><code>{{#af_get: {{{1}}} | a | * | b }}</code></tvar> is equivalent to <tvar name=2><code>{{#af_get: {{#af_group: {{#af_get: {{{1}}} | a }} }} | b }}</code></tvar>.</translate>

<translate><!--T:267--> If the array contains a key that clashes with the symbol for an overloaded index, the value under the key will be retrieved instead.</translate>
<translate><!--T:268--> This was done for backwards compatibility reasons.</translate>

<translate>
==== Description ==== <!--T:269-->
</translate>
<pre>
<pre>
{{#af_get: array | *indices }}
{{#af_get: array | *indices }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:270-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:274--> array</translate> || <translate><!--T:275--> The array in which to index.</translate>
|-
| <nowiki>*</nowiki>indices || <translate><!--T:276--> string</translate> || <translate><!--T:277--> The index. Multiple indices can be given to index nested arrays.</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
===== Overloaded indices (available since version 1.11.0) ===== <!--T:278-->
: The array in which to index.
</translate>
;<nowiki>*</nowiki>indices <nowiki>:</nowiki> string
{| class="wikitable"
: The index. Multiple indices can be given to index nested arrays.
! <translate><!--T:279--> Syntax</translate>
! <translate><!--T:280--> Equivalent function</translate>
! {{tunit|75|Description}}
|-
| <code>*</code>
| [[#af_group]]
| <translate><!--T:282--> Group subarrays together by key.</translate>
|-
| <code><-</code>
| [[#af_reverse]]
| <translate><!--T:283--> Reverse the array.</translate>
|-
| <code>><</code>
| [[#af_flatten]]
| <translate><!--T:284--> Flatten the array once.</translate>
|-
| <code>>><<</code>
| [[#af_flatten]]
| <translate><!--T:285--> Flatten the array until it is completely flat.</translate>
|-
| <code>#</code>
| [[#af_unique]]
| <translate><!--T:286--> Remove duplicates from the array.</translate>
|-
| <code>n..m</code>
| [[#af_slice]]
| <translate><!--T:287--> Retrieve a slice of the array (either <tvar name=1><code>n</code></tvar> or <tvar name=2><code>m</code></tvar> can be omitted to index from the start up to the end of the array respectively).</translate>
|-
| <code>!</code> (<translate><!--T:288--> available since version 1.16.0</translate>)
| [[#af_show]]
| <translate><!--T:289--> Show the value in a human-readable way using format <tvar name=1><code>table, simple</code></tvar>.</translate>
|-
| <code>//</code> (available since version 2.0.0)
| [[#af_filter]]
| Remove all empty elements from the array.
|}


<translate>
==== Return values ====
==== Return values ==== <!--T:290-->


<!--T:291-->
Returns the indexed value, or the empty string if the index does not exist.
Returns the indexed value, or the empty string if the index does not exist.


==== Examples ====
==== Examples ==== <!--T:292-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Get a top-level element
| <translate><!--T:293--> Get a top-level element</translate>
| <pre>{{#af_get: {{#af_list: a | b | c }} | 1 }}</pre>
| <pre>{{#af_get: {{#af_list: a | b | c }} | 1 }}</pre>
| b
| b
|-
|-
| Get a subarray
| <translate><!--T:294--> Get a subarray</translate>
| <pre>{{#af_print: {{#af_get: {{#af_list: a | {{#af_list: b | c }} }} | 1 }} }}</pre>
| <pre>{{#af_print: {{#af_get: {{#af_list: a | {{#af_list: b | c }} }} | 1 }} }}</pre>
|
|
Line 263: Line 878:
* 1: c
* 1: c
|-
|-
| Get a nested element
| <translate><!--T:295--> Get a nested element</translate>
| <pre>{{#af_get: {{#af_list: a | {{#af_object: hello=world }} }} | 1 | hello }}
| <pre>{{#af_get: {{#af_list: a | {{#af_object: hello=world }} }} | 1 | hello }}
</pre>
</pre>
| world
| world
|-
| <translate><!--T:296--> Reverse the array</translate>
| <pre>{{#af_print: {{#af_get: {{#af_list: a | b | c }} | <- }} }}</pre>
|
* 0: c
* 1: b
* 2: a
|-
| <translate><!--T:297--> Retrieve the last element of the array</translate>
| <pre>{{#af_print: {{#af_get: {{#af_list: a | b | c }} | <- | 0 }} }}</pre>
| c
|}

=== af_group ===

{{Software version|software=ArrayFunctions|version=1.11|version-status=stable|comment=+}}

<translate>
<!--T:298-->
This parser function computes an array where subarrays are recursively grouped together by key.

==== Description ==== <!--T:299-->
</translate>
<pre>
{{#af_group: array }}
</pre>

<translate>
==== Parameters ==== <!--T:300-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:304--> array</translate> || <translate><!--T:305--> The array for which to group subarrays together.</translate>
|}

<translate>
==== Return values ==== <!--T:306-->

<!--T:307-->
Returns an array where subarrays are recursively grouped together by key.

==== Examples ==== <!--T:308-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:309--> Group subarrays by key</translate>
| <pre>{{#af_print: {{#af_group: {{#af_list: {{#af_object: a=1 | b=2 }} | {{#af_object: a=3 | b=4 }} | {{#af_object: a=5 | b=5 }} }} }} }}</pre>
|
* a
** 0: 1
** 1: 3
** 2: 5
* b
** 0: 2
** 1: 4
** 2: 5
|}
|}


=== af_if ===
=== af_if ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function selects one of two alternatives based on the given predicate.


<translate>
==== Description ====
<!--T:310-->
This parser function selects one of two alternatives based on the given predicate.


==== Description ==== <!--T:311-->
</translate>
<pre>
<pre>
{{#af_if: predicate | consequent | alternative }}
{{#af_if: predicate | consequent | alternative }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:312-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| predicate || <translate><!--T:317--> boolean</translate> || || <translate><!--T:318--> The predicate.</translate>
|-
| consequent || <translate><!--T:319--> string</translate> || || <translate><!--T:320--> The value to return if the predicate holds (i.e. is <tvar name=1>{{phpi|true}}</tvar>).</translate>
|-
| alternative || <translate><!--T:321--> string</translate> || {{phpi|""}}
| <translate><!--T:322--> The value to return if the predicate does not hold (i.e. is <tvar name=1>{{phpi|false}}</tvar>).</translate>
|}


<translate>
;predicate <nowiki>:</nowiki> boolean
==== Return values ==== <!--T:323-->
: The predicate.
;consequent <nowiki>:</nowiki> string
: The value to return if the predicate holds (i.e. is true).
;alternative <nowiki>:</nowiki> string, default=""
: The value to return if the predicate does not hold (i.e. is false).

==== Return values ====


<!--T:324-->
Returns the consequent if the predicate holds, or the alternative if it is given and the predicate does not hold.
Returns the consequent if the predicate holds, or the alternative if it is given and the predicate does not hold.


==== Examples ====
==== Examples ==== <!--T:325-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Check if a value is an array
| <translate><!--T:326--> Check if a value is an array</translate>
| <pre>{{#af_if: {{#af_isarray: not an array }} | A beautiful array! | Not an array! }}</pre>
| <pre>{{#af_if: {{#af_isarray: not an array }} | A beautiful array! | Not an array! }}</pre>
| Not an array!
| Not an array!
Line 303: Line 986:
=== af_int ===
=== af_int ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function casts a string to an integer. This is useful for creating an array containing an integer.


<translate><!--T:327--> This parser function casts a string to an integer.</translate>
==== Description ====
<translate><!--T:328--> This is useful for creating an array containing an integer.</translate>


<translate>
==== Description ==== <!--T:329-->
</translate>
<pre>
<pre>
{{#af_int: value }}
{{#af_int: value }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:330-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| value || <translate><!--T:334--> string or int</translate> || <translate><!--T:335--> The value to cast to an integer.</translate>
|}


<translate>
;value <nowiki>:</nowiki> string or int
==== Return values ==== <!--T:336-->
: The value to cast to an integer.

==== Return values ====


<!--T:337-->
Returns the casted integer.
Returns the casted integer.


==== Examples ====
==== Examples ==== <!--T:338-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Create an opaque representation of an integer
| <translate><!--T:339--> Create an opaque representation of an integer</translate>
| <pre>{{#af_int: 42 }}, {{#af_int: -12 }}</pre>
| <pre>{{#af_int: 42 }}, {{#af_int: -12 }}</pre>
| integer__^__42, integer__^__-12
| integer__^__42, integer__^__-12
|-
|-
| Create an array containing an integer
| <translate><!--T:340--> Create an array containing an integer</translate>
| <pre>{{#af_print: {{#af_list: {{#af_int: -129}} }} }}</pre>
| <pre>{{#af_print: {{#af_list: {{#af_int: -129}} }} }}</pre>
|
|
Line 334: Line 1,027:
|}
|}


=== af_isarray ===
=== af_intersect ===


{{Software version|software=ArrayFunctions|version=1.2|version-status=stable|comment=+}}
This parser function checks if the given value is an array.


[[File:Venn 0000 0001.svg|150px|thumb|<translate><!--T:250--> The intersection of three arrays</translate>]]
==== Description ====


<translate><!--T:251--> This parser function computes the intersection of arrays.</translate>
<translate><!--T:252--> This function preserves keys.</translate>

<translate>
==== Description ==== <!--T:253-->
</translate>
<pre>
<pre>
{{#af_isarray: value }}
{{#af_intersect: array | *arrays }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:254-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:255--> array</translate> || <translate><!--T:256--> The first array.</translate>
|-
| <nowiki>*</nowiki>arrays || <translate><!--T:344--> array</translate> || <translate><!--T:345--> The other arrays.</translate>
|}


<translate>
;value <nowiki>:</nowiki> mixed
==== Return values ==== <!--T:346-->
: The value to check.


<!--T:347-->
==== Return values ====
Returns the intersection of the given arrays.


==== Examples ==== <!--T:348-->
Returns true if <code>value</code> is an array, false otherwise.
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:349--> Compute the intersection of two identical arrays</translate>
| <pre>{{#af_print: {{#af_intersect: {{#af_list: a | b | c }} | {{#af_list: a | b | c }} }} }}</pre>
|
* 0: a
* 1: b
* 2: c
|-
| <translate><!--T:350--> Compute the intersection of two partially overlapping arrays</translate>
| <pre>{{#af_print: {{#af_intersect: {{#af_list: a | b | c }} | {{#af_list: c | d | e }} }} }}</pre>
|
* 2: c
|}


==== Examples ====
=== af_isarray ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate>
<!--T:351-->
This parser function checks if the given value is an array.

==== Description ==== <!--T:352-->
</translate>
<pre>
{{#af_isarray: value }}
</pre>

<translate>
==== Parameters ==== <!--T:353-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|75|Description}}
|-
| value || <translate><!--T:357--> mixed</translate> || <translate><!--T:358--> The value to check.</translate>
|}

<translate>
==== Return values ==== <!--T:359-->

<!--T:360-->
Returns <tvar name=1>{{phpi|true}}</tvar> if <tvar name=2><code>value</code></tvar> is an array, <tvar name=3>{{phpi|false}}</tvar> otherwise.

==== Examples ==== <!--T:361-->
</translate>
{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Check if an array is an array
| <translate><!--T:362--> Check if an array is an array</translate>
| <pre>{{#af_print: {{#af_isarray: {{#af_list: a | b | c }} }} }}</pre>
| <pre>{{#af_print: {{#af_isarray: {{#af_list: a | b | c }} }} }}</pre>
| true
| {{phpi|true}}
|-
|-
| Check if a string is an array
| <translate><!--T:363--> Check if a string is an array</translate>
| <pre>{{#af_print: {{#af_isarray: Hello, World! }} }}</pre>
| <pre>{{#af_print: {{#af_isarray: Hello, World! }} }}</pre>
| false
| {{phpi|false}}
|}
|}


=== af_join ===
=== af_join ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate>
<!--T:364-->
This parser function recursively joins the items of an array together with a given separator.
This parser function recursively joins the items of an array together with a given separator.


==== Description ====
==== Description ==== <!--T:365-->
</translate>

<pre>
<pre>
{{#af_join: array | glue }}
{{#af_join: array | glue }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:366-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:371--> array</translate> || || <translate><!--T:372--> The array to join.</translate>
|-
| glue || <translate><!--T:373--> string</translate> || {{phpi|""}} || <translate><!--T:374--> The string used to join each item.</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:375-->
: The array to join.
;glue <nowiki>:</nowiki> string, default=""
: The string used to join each item. This parameter recognises the following escape sequences:
:: <code>\s</code> for spaces
:: <code>\n</code> for newlines
:: <code>\\</code> for backslashes

==== Return values ====


<!--T:376-->
Returns the joined array.
Returns the joined array.


==== Examples ====
==== Examples ==== <!--T:377-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Join a one-dimensional array
| <translate><!--T:378--> Join a one-dimensional array</translate>
| <pre>{{#af_join: {{#af_list: a | b | c }} }}</pre>
| <pre>{{#af_join: {{#af_list: a | b | c }} }}</pre>
| abc
| abc
|-
|-
| Join a one-dimensional array using a separator
| <translate><!--T:379--> Join a one-dimensional array using a separator</translate>
| <pre>{{#af_join: {{#af_list: a | b | c }} | \s-\s }}</pre>
| <pre>{{#af_join: {{#af_list: a | b | c }} | \s-\s }}</pre>
| a - b - c
| a - b - c
|-
|-
| Join a multi-dimensional array using a separator
| <translate><!--T:380--> Join a multi-dimensional array using a separator</translate>
| <pre>{{#af_join: {{#af_list: a | b | {{#af_list: c | d }} }} | \s-\s }}</pre>
| <pre>{{#af_join: {{#af_list: a | b | {{#af_list: c | d }} }} | \s-\s }}</pre>
| a - b - c - d
| a - b - c - d
Line 409: Line 1,169:
=== af_keysort ===
=== af_keysort ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function sorts a multidimensional array based on the values of a key.


<translate><!--T:381--> This parser function sorts a list of objects based on the values of the specified key.</translate>
==== Description ====
<translate><!--T:382--> To sort an array by key, use <tvar name=1>[[#af_ksort]]</tvar>.</translate>


<translate>
==== Description ==== <!--T:383-->
</translate>
<pre>
<pre>
{{#af_keysort: array | key | descending=descending }}
{{#af_keysort: array | key | descending=descending | caseinsensitive=caseinsensitive }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:384-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:389--> array</translate> || || <translate><!--T:390--> The array to sort.</translate>
|-
| key || <translate><!--T:391--> string</translate> || || <translate><!--T:392--> The key of the values on which the sort should be based.</translate>
|-
| descending || <translate><!--T:393--> boolean</translate> || {{phpi|false}} || <translate><!--T:394--> Whether to sort in a descending order.</translate>
|-
| caseinsensitive || <translate><!--T:395--> boolean</translate> || {{phpi|false}} || <translate><!--T:396--> Whether to ignore case when sorting (available since version 1.7.0).</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:397-->
: The array to sort.
;key <nowiki>:</nowiki> string
: The key of the values on which the sort should be based.
;descending <nowiki>:</nowiki> boolean, default=false
: Whether to sort in a descending order.

==== Return values ====


<!--T:398-->
Returns the sorted array.
Returns the sorted array.


==== Examples ====
==== Examples ==== <!--T:399-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Sort based on age
| <translate><!--T:400--> Sort based on age</translate>
| <pre>{{#af_print: {{#af_keysort: {{#af_list:
| <pre>{{#af_print: {{#af_keysort: {{#af_list:
{{#af_object: name=John | age=56 }} |
{{#af_object: name=John | age=56 }} |
Line 451: Line 1,223:
** age: 56
** age: 56
|-
|-
| Sort based on age, in descending order
| <translate><!--T:401--> Sort based on age, in descending order</translate>
| <pre>{{#af_print: {{#af_keysort: {{#af_list:
| <pre>{{#af_print: {{#af_keysort: {{#af_list:
{{#af_object: name=John | age=56 }} |
{{#af_object: name=John | age=56 }} |
Line 467: Line 1,239:
** name: Harry
** name: Harry
** age: 12
** age: 12
|}

=== af_ksort ===

{{Software version|software=ArrayFunctions|version=1.7|version-status=stable|comment=+}}

<translate>
<!--T:402-->
This parser function sorts an array by key.

==== Description ==== <!--T:403-->
</translate>
<pre>
{{#af_ksort: array | descending=descending | caseinsensitive=caseinsensitive }}
</pre>

<translate>
==== Parameters ==== <!--T:404-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:409--> array</translate> || || <translate><!--T:410--> The array to sort.</translate>
|-
| descending || <translate><!--T:411--> boolean</translate> || {{phpi|false}} || <translate><!--T:412--> Whether to sort in a descending order.</translate>
|-
| caseinsensitive || <translate><!--T:413--> boolean</translate> || {{phpi|false}} || <translate><!--T:414--> Whether to ignore case when sorting (available since version 1.7.0).</translate>
|}

<translate>
==== Return values ==== <!--T:415-->

<!--T:416-->
Returns the sorted array.

==== Examples ==== <!--T:417-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:418--> Sort by key</translate>
| <pre>{{#af_print: {{#af_ksort: {{#af_object: c=banana | a=orange | b=apple }} }} }}</pre>
|
* a: orange
* b: apple
* c: banana
|}
|}


=== af_list ===
=== af_list ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate>
<!--T:419-->
This parser function creates a new list from the given parameters.
This parser function creates a new list from the given parameters.


==== Description ====
==== Description ==== <!--T:420-->
</translate>

<pre>
<pre>
{{#af_list: *values }}
{{#af_list: *values }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:421-->
</translate>
;<nowiki>*</nowiki>values <nowiki>:</nowiki> <translate><!--T:422--> mixed</translate>
: <translate><!--T:423--> The values for the list.</translate>


<translate>
;<nowiki>*</nowiki>values <nowiki>:</nowiki> mixed
: The values for the list.
==== Return values ==== <!--T:424-->

==== Return values ====


<!--T:425-->
Returns the resulting list.
Returns the resulting list.


==== Examples ====
==== Examples ==== <!--T:426-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Create a simple one-dimensional list
| <translate><!--T:427--> Create a simple one-dimensional list</translate>
| <pre>{{#af_print: {{#af_list: a | b | c }} }}</pre>
| <pre>{{#af_print: {{#af_list: a | b | c }} }}</pre>
|
|
Line 499: Line 1,323:
* 2: c
* 2: c
|-
|-
| Create a multi-dimensional list
| <translate><!--T:428--> Create a multi-dimensional list</translate>
| <pre>{{#af_print: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }}</pre>
| <pre>{{#af_print: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }}</pre>
|
|
Line 509: Line 1,333:
** 1: d
** 1: d
|-
|-
| Create a list of objects
| <translate><!--T:429--> Create a list of objects</translate>
| <pre>{{#af_print: {{#af_list:
| <pre>{{#af_print: {{#af_list:
{{#af_object: name=Harry | age=22 }} |
{{#af_object: name=Harry | age=22 }} |
Line 525: Line 1,349:
=== af_map ===
=== af_map ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate>
<!--T:430-->
This parser function applies a callback to each element of a list.
This parser function applies a callback to each element of a list.


==== Description ====
==== Description ==== <!--T:431-->
</translate>

<pre>
<pre>
{{#af_map: array | value_name | callback }}
{{#af_map: array | value_name | callback }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:432-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:433--> array</translate>
: <translate><!--T:434--> The array to run through the callback.</translate>
;value_name <nowiki>:</nowiki> <translate><!--T:435--> string</translate>
: <translate><!--T:436--> The name to give to the value in the callback.</translate>
;callback <nowiki>:</nowiki> <translate><!--T:437--> string</translate>
: <translate><!--T:438--> The callback to apply to each element of the array.</translate>


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:439-->
: The array to run through the callback.
;value_name <nowiki>:</nowiki> string
: The name to give to the value in the callback.
;callback <nowiki>:</nowiki> string
: The callback to apply to each element of the array.

==== Return values ====


<!--T:440-->
Returns the resulting mapped array.
Returns the resulting mapped array.


==== Examples ====
==== Examples ==== <!--T:441-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Appending a string to each element
| <translate><!--T:442--> Appending a string to each element</translate>
| <pre>{{#af_print: {{#af_map: {{#af_list: a | b | c }} | v | {{{v}}}-appended }} }}</pre>
| <pre>{{#af_print: {{#af_map: {{#af_list: a | b | c }} | v | {{{v}}}-appended }} }}</pre>
|
|
Line 557: Line 1,388:
* 2: c-appended
* 2: c-appended
|-
|-
| Altering list elements
| <translate><!--T:443--> Altering list elements</translate>
| <pre>{{#af_print: {{#af_map: {{#af_list: {{#af_list: a }} | {{#af_list: b }} }} | v | {{#af_push: {{{v}}} | c }} }} }}</pre>
| <pre>{{#af_print: {{#af_map: {{#af_list: {{#af_list: a }} | {{#af_list: b }} }} | v | {{#af_push: {{{v}}} | c }} }} }}</pre>
|
|
Line 566: Line 1,397:
** 0: b
** 0: b
** 1: c
** 1: c
|}

=== af_merge ===

{{Software version|software=ArrayFunctions|version=1.2|version-status=stable|comment=+}}

<translate><!--T:444--> This parser function computes the merges two or more arrays.</translate>
<translate><!--T:445--> It merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.</translate>
<translate><!--T:446--> It does not remove duplicates.</translate>

<translate><!--T:447--> If the input arrays have the same ''string'' key, then the later value for that key will overwrite the previous one.</translate>
<translate><!--T:448--> If the arrays contain numeric keys, later values will be appended instead and the later keys will be renumbered.</translate>

<translate>
==== Description ==== <!--T:449-->
</translate>
<pre>
{{#af_merge: array | *arrays }}
</pre>

<translate>
==== Parameters ==== <!--T:450-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:451--> array</translate>
: <translate><!--T:452--> The first array.</translate>
;<nowiki>*</nowiki>arrays <nowiki>:</nowiki> <translate><!--T:453--> array</translate>
: <translate><!--T:454--> The other arrays.</translate>

<translate>
==== Return values ==== <!--T:455-->

<!--T:456-->
Returns the union of the given arrays.

==== Examples ==== <!--T:457-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:458--> Appending a string to each element</translate>
| <pre>{{#af_print: {{#af_merge: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }}</pre>
|
* 0: a
* 1: b
* 2: c
* 3: d
* 4: e
* 5: f
|}
|}


=== af_object ===
=== af_object ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate>
<!--T:459-->
This parser function creates a new object from the given parameters.
This parser function creates a new object from the given parameters.


==== Description ====
==== Description ==== <!--T:460-->
</translate>

<pre>
<pre>
{{#af_object: **values }}
{{#af_object: **values }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:461-->
</translate>
;<nowiki>**</nowiki>values <nowiki>:</nowiki> <translate><!--T:462--> mixed</translate>
: <translate><!--T:463--> The values for the object.</translate>


<translate>
;<nowiki>**</nowiki>values <nowiki>:</nowiki> mixed
: The values for the object.
==== Return values ==== <!--T:464-->

==== Return values ====


<!--T:465-->
Returns the resulting object.
Returns the resulting object.


==== Examples ====
==== Examples ==== <!--T:466-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Create a simple one-dimensional object
| <translate><!--T:467--> Create a simple one-dimensional object</translate>
| <pre>{{#af_print: {{#af_object: a=b | b=c | c=d }} }}</pre>
| <pre>{{#af_print: {{#af_object: a=b | b=c | c=d }} }}</pre>
|
|
Line 598: Line 1,483:
* c: d
* c: d
|-
|-
| Create a multi-dimensional object
| <translate><!--T:468--> Create a multi-dimensional object</translate>
| <pre>{{#af_print: {{#af_object: head={{#af_object: title=MediaWiki | meta={{#af_list: {{#af_object: charset=UTF-8 }} }} }} }} }}</pre>
| <pre>{{#af_print: {{#af_object: head={{#af_object: title=MediaWiki | meta={{#af_list: {{#af_object: charset=UTF-8 }} }} }} }} }}</pre>
|
|
Line 606: Line 1,491:
*** 0
*** 0
**** charset: UTF-8
**** charset: UTF-8
|}

=== af_pipeline ===

{{Software version|software=ArrayFunctions|version=1.14|version-status=stable|comment=+}}

<translate><!--T:469--> This parser function creates a pipeline of operations.</translate>
<translate><!--T:470--> The parser function takes the result from expanding the previous argument, and passes that as the specified parameter (or <tvar name=1><code>prev</code></tvar> by default) to the next argument of the parser function.</translate>
<translate><!--T:471--> This is very useful to improve the readability of the template code, as it allows you to remove deeply nested parser function calls.</translate>
<translate><!--T:472--> For example, the following two snippets are semantically equivalent:</translate>

{| class="wikitable" style="width: 100%"
|-
! <translate><!--T:473--> Before</translate> !! <translate><!--T:474--> After</translate>
|-
| <pre>{{#af_print: {{#af_unique: {{#af_reverse: {{#af_list: a | a | b | c}} }} }} }}</pre>
| <pre>
{{#af_pipeline: {{#af_list: a | a | b | c }}
| {{#af_reverse: {{{prev}}} }}
| {{#af_unique: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}
</pre>
|}

<translate>
<!--T:475-->
The behaviour of this parser function might be influenced by the <tvar name=1>[[#Performance limits|<code>$wgArrayFunctionsMaxPipelineLength</code>]]</tvar> configuration parameter.

==== Description ==== <!--T:476-->
</translate>
<pre>
{{#af_pipeline: initial | *steps | parameter=parameter }}
</pre>

<translate>
==== Parameters ==== <!--T:477-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| initial || <translate><!--T:482--> mixed</translate> || || <translate><!--T:483--> The initial value of the pipeline.</translate>
|-
| <nowiki>*</nowiki>steps || <translate><!--T:484--> mixed</translate> || || <translate><!--T:485--> The subsequent steps of the pipeline.</translate>
|-
| parameter || <translate><!--T:486--> string</translate> || {{phpi|"prev"}} || <translate><!--T:487--> The name to use for the previous value.</translate>
|}

<translate>
==== Return values ==== <!--T:488-->

<!--T:489-->
Returns the value returned by the final step of the pipeline.

==== Examples ==== <!--T:490-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:491--> Create a pipeline</translate>
| <pre>
{{#af_pipeline: {{#af_list: a | a | b | c }}
| {{#af_reverse: {{{prev}}} }}
| {{#af_unique: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}
</pre>
|
* 0: c
* 1: b
* 2: a
|}
|}


=== af_print ===
=== af_print ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function prints the given value in a human-readable format.


<translate><!--T:492--> This parser function prints the given value for debug purposes.</translate>
==== Description ====
<translate><!--T:493--> Unlike <tvar name=1>[[#af_show]]</tvar>, it does '''not''' parse the value.</translate>
<translate><!--T:494--> This function should only be used for debug purposes, consider using <tvar name=1>[[#af_show]]</tvar> to display a value to the reader.</translate>

<translate>
==== Description ==== <!--T:495-->
</translate>


<pre>
<pre>
Line 618: Line 1,579:
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:496-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| <nowiki>*</nowiki>values || <translate><!--T:501--> mixed</translate> || || <translate><!--T:502--> The values to print.</translate>
|-
| end || <translate><!--T:503--> string</translate> || {{phpi|""}} || <translate><!--T:504--> The string to append to the end of each printed value.</translate>
|}


<translate>
;<nowiki>*</nowiki>values <nowiki>:</nowiki> mixed
: The values to print.
==== Return values ==== <!--T:505-->
;end <nowiki>:</nowiki> string, default=""
: The string to append to the end of each printed value. This parameter recognises the following escape sequences:
:: <code>\s</code> for spaces
:: <code>\n</code> for newlines
:: <code>\\</code> for backslashes

==== Return values ====


<!--T:506-->
Returns the value in human-readable form.
Returns the value in human-readable form.


==== Examples ====
==== Examples ==== <!--T:507-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Print a boolean
| <translate><!--T:508--> Print a boolean</translate>
| <pre>{{#af_print: {{#af_bool: yes }} }}</pre>
| <pre>{{#af_print: {{#af_bool: yes }} }}</pre>
| true
| true
|-
|-
| Print a list
| <translate><!--T:509--> Print a list</translate>
| <pre>{{#af_print: {{#af_list: a | b | c }} }}</pre>
| <pre>{{#af_print: {{#af_list: a | b | c }} }}</pre>
|
|
Line 650: Line 1,614:
=== af_push ===
=== af_push ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate>
<!--T:510-->
This parser function adds the given value to the end of the list.
This parser function adds the given value to the end of the list.


==== Description ====
==== Description ==== <!--T:511-->
</translate>

<pre>
<pre>
{{#af_push: array | value }}
{{#af_push: array | value }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:512-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:513--> array</translate>
: <translate><!--T:514--> The array to append the value to.</translate>
;value <nowiki>:</nowiki> <translate><!--T:515--> mixed</translate>
: <translate><!--T:516--> The value to add.</translate>


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:517-->
: The array to append the value to.
;value <nowiki>:</nowiki> mixed
: The value to add.

==== Return values ====


<!--T:518-->
Returns the array with the value appended.
Returns the array with the value appended.


==== Examples ====
==== Examples ==== <!--T:519-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Push a value
| <translate><!--T:520--> Push a value</translate>
| <pre>{{#af_print: {{#af_push: {{#af_list: a | b }} | c }} }}</pre>
| <pre>{{#af_print: {{#af_push: {{#af_list: a | b }} | c }} }}</pre>
|
|
Line 679: Line 1,650:
* 1: b
* 1: b
* 2: c
* 2: c
|}

=== af_put ===

{{Software version|software=ArrayFunctions|version=1.13|version-status=stable|comment=+}}

<translate><!--T:521--> This parser function sets the given value for the given index.</translate>
<translate><!--T:522--> It is identical to <tvar name=1>[[#af_set]]</tvar>, except for the parameter order.</translate>
<translate><!--T:523--> This parser function should be preferred over <tvar name=1>[[#af_set]]</tvar>.</translate>

<translate>
==== Description ==== <!--T:524-->
</translate>
<pre>
{{#af_put: array | value | *indices }}
</pre>

<translate>
==== Parameters ==== <!--T:525-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:526--> array</translate>
: <translate><!--T:527--> To array in which to set the index.</translate>
;value <nowiki>:</nowiki> <translate><!--T:528--> mixed</translate>
: <translate><!--T:529--> The value to set the index to.</translate>
;<nowiki>*</nowiki>indices <nowiki>:</nowiki> <translate><!--T:530--> string</translate>
: <translate><!--T:531--> The index to set.</translate> <translate><!--T:532--> Multiple indices can be given to index nested arrays.</translate>

<translate>
==== Return values ==== <!--T:533-->

<!--T:534-->
Returns the array with the given index set to the given value.

==== Examples ==== <!--T:535-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:536--> Replace an existing value</translate>
| <pre>{{#af_print: {{#af_put: {{#af_list: a | b | c }} | d | 2 }} }}</pre>
|
* 0: a
* 1: b
* 2: d
|-
| <translate><!--T:537--> Create a new index</translate>
| <pre>{{#af_print: {{#af_set: {{#af_object: foo=bar }} | far | boo }} }}</pre>
|
* foo: bar
* boo: far
|-
| <translate><!--T:538--> Create a new subarray</translate>
| <pre>{{#af_print: {{#af_set: {{#af_object: foo=bar }} | far | boo | far }} }}</pre>
|
* foo: bar
* boo
** far: far
|}

=== af_range ===

{{Software version|software=ArrayFunctions|version=1.12|version-status=stable|comment=+}}

<translate><!--T:539--> This parser function creates a finite range of integers.</translate>
<translate><!--T:540--> The <tvar name=1><code>i</code></tvar>th element of a range is determined by the function <tvar name=2><code>r(i) = start + step * i</code></tvar>.</translate>
<translate><!--T:541--> For a positive step, the bounds of the range are <tvar name=1><code>i >= 0</code></tvar> and <tvar name=2><code>r(i) < stop</code></tvar>.</translate>
<translate><!--T:542--> For a negative step, the bounds are <tvar name=1><code>i >= 0</code></tvar> and <tvar name=2><code>r(i) > stop</code></tvar>.</translate>

<translate>
<!--T:543-->
The behaviour of this parser function might be influenced by the <tvar name=1>[[#Performance limits|<code>$wgArrayFunctionsMaxRangeSize</code>]]</tvar> configuration parameter.

==== Description ==== <!--T:544-->
</translate>
<pre>
{{#af_range: start | stop | step }}
</pre>

<translate>
==== Parameters ==== <!--T:545-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| start || <translate><!--T:550--> integer</translate> || || <translate><!--T:551--> The start of the range.</translate>
|-
| stop || <translate><!--T:552--> integer</translate> || {{phpi|null}}
| <translate><!--T:553--> The end of the range (non-inclusive).</translate> <translate><!--T:554--> If this value is omitted, the start will become the stop, and start will default to zero.</translate>
|-
| step || <translate><!--T:555--> integer</translate> || 1 || <translate><!--T:556--> The step size between range values.</translate>
|}

<translate>
==== Return values ==== <!--T:557-->

<!--T:558-->
A finite range of integers.

==== Examples ==== <!--T:559-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:560--> Create a range from 0 to 4</translate>
| <pre>{{#af_print: {{#af_range: 0 | 5 }} }}</pre>
|
* 0: 0
* 1: 1
* 2: 2
* 3: 3
* 4: 4
|-
| <translate><!--T:561--> Alternative syntax for a range from 0 to 4</translate>
| <pre>{{#af_print: {{#af_range: 5 }} }}</pre>
|
* 0: 0
* 1: 1
* 2: 2
* 3: 3
* 4: 4
|-
| <translate><!--T:562--> Create a range from 0 to -4</translate>
| <pre>{{#af_print: {{#af_range: 0 | -5 | -1 }} }}</pre>
|
* 0: 0
* 1: -1
* 2: -2
* 3: -3
* 4: -4
|-
| <translate><!--T:563--> Create a range of all odd numbers between 0 to 10</translate>
| <pre>{{#af_print: {{#af_range: 1 | 10 | 2 }} }}</pre>
|
* 0: 0
* 1: 3
* 2: 5
* 3: 7
* 4: 9
|}

=== af_reduce ===

{{Software version|software=ArrayFunctions|version=1.2|version-status=stable|comment=+}}

<translate><!--T:564--> This parser function iteratively reduces the array to a single value using a callback.</translate>
<translate><!--T:565--> It iteratively applies <tvar name=1><code>callable</code></tvar> to the elements of the given array, so as to reduce the array to a single value.</translate>
<translate><!--T:566--> The callable is passed the value of the current iteration, as well as the result of the previous iteration.</translate>

<translate>
==== Description ==== <!--T:567-->
</translate>
<pre>
{{#af_reduce: array | carry_name | value_name | callable | initial }}
</pre>

<translate>
==== Parameters ==== <!--T:568-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:573--> array</translate> || || <translate><!--T:574--> The array to reduce.</translate>
|-
| carry_name || <translate><!--T:575--> string</translate> || || <translate><!--T:576--> The name to use for the carry.</translate>
|-
| value_name || <translate><!--T:577--> string</translate> || || <translate><!--T:578--> The name to use for the value.</translate>
|-
| callable || <translate><!--T:579--> string</translate> || || <translate><!--T:580--> The callback to use for each iteration.</translate>
|-
| initial || <translate><!--T:581--> string</translate> || {{phpi|""}} || <translate><!--T:582--> The initial carry to use.</translate>
|}

<translate>
==== Return values ==== <!--T:583-->

<!--T:584-->
Returns the resulting value.

==== Examples ==== <!--T:585-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:586--> Using reduction to concatenate values</translate>
| <pre>{{#af_reduce: {{#af_list: a | b | c }} | c | i | {{{c}}}{{{i}}} }}</pre>
| abc
|-
| <translate><!--T:587--> Using reduction to reverse and then concatenate values</translate>
| <pre>{{#af_reduce: {{#af_list: a | b | c }} | c | i | {{{i}}}{{{c}}} }}</pre>
| cba
|-
| <translate><!--T:588--> Using reduction to build an equation</translate>
| <pre>{{#af_reduce: {{#af_list: 2 | 3 | 5 | 7 | 11 }} | c | i | {{{c}}} + {{{i}}} | 0 }}</pre>
| 0 + 2 + 3 + 5 + 7 + 11
|}

=== af_reverse ===

{{Software version|software=ArrayFunctions|version=1.11|version-status=stable|comment=+}}

<translate>
<!--T:589-->
This parser function reverses the given array. It does not preserve numeric keys.

==== Description ==== <!--T:590-->
</translate>
<pre>
{{#af_reverse: array }}
</pre>

<translate>
==== Parameters ==== <!--T:591-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:592--> array</translate>
: <translate><!--T:593--> The array to reverse.</translate>

<translate>
==== Return values ==== <!--T:594-->

<!--T:595-->
Returns the original array reversed.

==== Examples ==== <!--T:596-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:597--> Reverse a list</translate>
| <pre>{{#af_print: {{#af_reverse: {{#af_list: a | b | c}} }} }}</pre>
|
* 0: c
* 1: b
* 2: a
|}

=== af_search ===

{{Software version|software=ArrayFunctions|version=1.3|version-status=stable|comment=+}}

<translate>
<!--T:598-->
This parser function searches the given array for the given value, and returns the first corresponding key if the value is found.

==== Description ==== <!--T:599-->
</translate>
<pre>
{{#af_search: array | value }}
</pre>

<translate>
==== Parameters ==== <!--T:600-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:601--> array</translate>
: <translate><!--T:602--> The array to search in.</translate>
;value <nowiki>:</nowiki> <translate><!--T:603--> mixed</translate>
: <translate><!--T:604--> The value to search for.</translate>

<translate>
==== Return values ==== <!--T:605-->

<!--T:606-->
Returns the first corresponding key if the value is found, nothing otherwise.

==== Examples ==== <!--T:607-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:608--> Search for a value in an array</translate>
| <pre>{{#af_print: {{#af_search: {{#af_list: a | b | c }} | b }} }}</pre>
| 1
|}
|}


=== af_set ===
=== af_set ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function sets the given value for the given index.
{{Deprecated-inline|1.13.0}}


<translate><!--T:609--> This parser function sets the given value for the given index.</translate>
==== Description ====
<translate><!--T:610--> It has been deprecated in <tvar name=1>ArrayFunctions</tvar> 1.13.0, and should no longer be used.</translate>
<translate><!--T:611--> It will remain available indefinitely for backwards compatibility.</translate>
<translate><!--T:612--> As an alternative, you should use <tvar name=1>[[#af_put]]</tvar> instead.</translate>


<translate>
==== Description ==== <!--T:613-->
</translate>
<pre>
<pre>
{{#af_set: value | array | *indices }}
{{#af_set: value | array | *indices }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:614-->
</translate>
;value <nowiki>:</nowiki> <translate><!--T:615--> mixed</translate>
: <translate><!--T:616--> The value to set the index to.</translate>
;array <nowiki>:</nowiki> <translate><!--T:617--> array</translate>
: <translate><!--T:618--> To array in which to set the index.</translate>
;<nowiki>*</nowiki>indices <nowiki>:</nowiki> <translate><!--T:619--> string</translate>
: <translate><!--T:620--> The index to set.</translate> <translate><!--T:621--> Multiple indices can be given to index nested arrays.</translate>


<translate>
;value <nowiki>:</nowiki> mixed
==== Return values ==== <!--T:622-->
: The value to set the index to.
;array <nowiki>:</nowiki> array
: To array in which to set the index.
;<nowiki>*</nowiki>indices <nowiki>:</nowiki> string
: The index to set. Multiple indices can be given to index nested arrays.

==== Return values ====


<!--T:623-->
Returns the array with the given index set to the given value.
Returns the array with the given index set to the given value.


==== Examples ====
==== Examples ==== <!--T:624-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Replace an existing value
| <translate><!--T:625--> Replace an existing value</translate>
| <pre>{{#af_print: {{#af_set: d | {{#af_list: a | b | c }} | 2 }} }}</pre>
| <pre>{{#af_print: {{#af_set: d | {{#af_list: a | b | c }} | 2 }} }}</pre>
|
|
Line 715: Line 1,962:
* 2: d
* 2: d
|-
|-
| Create a new index
| <translate><!--T:626--> Create a new index</translate>
| <pre>{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo }} }}</pre>
| <pre>{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo }} }}</pre>
|
|
Line 721: Line 1,968:
* boo: far
* boo: far
|-
|-
| Create a new subarray
| <translate><!--T:627--> Create a new subarray</translate>
| <pre>{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo | far }} }}</pre>
| <pre>{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo | far }} }}</pre>
|
|
Line 727: Line 1,974:
* boo
* boo
** far: far
** far: far
|}

=== af_show ===

{{Software version|software=ArrayFunctions|version=1.4|version-status=stable|comment=+}}

<translate><!--T:628--> This parser function prints the given value in a human-readable format.</translate>
<translate><!--T:629--> The formatting of the value is based on <tvar name=1><code>format</code></tvar>.</translate>

<translate>
==== Description ==== <!--T:630-->
</translate>
<pre>
{{#af_show: value | format=format }}
</pre>

<translate>
==== Parameters ==== <!--T:631-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| value || <translate><!--T:636--> mixed</translate> || || <translate><!--T:637--> The value to show.</translate>
|-
| format || <translate><!--T:638--> string</translate> || {{phpi|"simple"}}
| {{introduced-inline|1.16.0}} <translate><!--T:639--> A comma-separated list of formats.</translate> <translate><!--T:640--> The value will be shown using the first format supporting the type of the value.</translate>
|}

<translate>
===== Formats (available since version 1.16.0) ===== <!--T:641-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}}
! <translate><!--T:643--> Supported types</translate>
! {{tunit|75|Description}}
|-
| <code>simple</code>
| <code>string</code>, <code>integer</code>, <code>float</code>, <code>boolean</code>
| <translate><!--T:645--> Format the value as its string representation.</translate> <translate><!--T:646--> This is the default format.</translate>
|-
| <code>table</code>
| <code>array</code>
| <translate><!--T:647--> Format the value as a table.</translate>
|}

<translate>
==== Return values ==== <!--T:648-->

<!--T:649-->
Returns the value in human-readable form, formatted using <tvar name=1><code>format</code></tvar>.

==== Examples ==== <!--T:650-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:651--> Show a value</translate>
| <pre>{{#af_show: Hello World! }}</pre>
| Hello World!
|-
| <translate><!--T:652--> Show an integer</translate>
| <pre>{{#af_show: {{#af_int: 42 }} | format=simple }}</pre>
| 42
|}
|}


=== af_slice ===
=== af_slice ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function extracts a slice from the given array. Keys will be reset and reordered.


<translate><!--T:653--> This parser function extracts a slice from the given array.</translate>
==== Description ====
<translate><!--T:654--> Keys will be reset and reordered.</translate>


<translate>
==== Description ==== <!--T:655-->
</translate>
<pre>
<pre>
{{#af_slice: array | offset | length }}
{{#af_slice: array | offset | length }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:656-->
</translate>
{| class="wikitable"
! Name !! Type !! Required? !! Description
|-
| <code>array</code> || <translate><!--T:657--> array</translate> || {{required}} || <translate><!--T:658--> The array to take a slice from.</translate>
|-
| <code>offset</code> || <translate><!--T:659--> integer</translate> || {{required}}
| <translate><!--T:660--> The offset at which the slice starts.</translate> <translate><!--T:661--> If non-negative, the slice will start at this given offset.</translate> <translate><!--T:662--> If negative, the sequence will start that far from the end of the array.</translate>
|-
| <code>length</code> || <translate><!--T:663--> integer</translate> || {{optional}}
| <translate><!--T:664--> The length of the slice.</translate> <translate><!--T:665--> If the length is given and positive, then the slice will have that many elements in it.</translate> <translate><!--T:666--> If the length is given and negative, then the slice will stop that many elements from the end of the array.</translate> <translate><!--T:667--> If it is omitted, then the slice will have everything from offset up until the end of the array.</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:668-->
: The array to take a slice from.
;offset <nowiki>:</nowiki> integer
: The offset at which the slice starts. If non-negative, the slice will start at this given offset. If negative, the sequence will start that far from the end of the array.
;length <nowiki>:</nowiki> integer, optional
: The length of the slice. If the length is given and positive, then the slice will have that many elements in it. If the length is given and negative, then the slice will stop that many elements from the end of the array. If it is omitted, then the slice will have everything from offset up until the end of the array.

==== Return values ====


<!--T:669-->
The slice.
The slice.


==== Examples ====
==== Examples ==== <!--T:670-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Get the first two elements
| <translate><!--T:671--> Get the first two elements</translate>
| <pre>{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | 0 | 2 }} }}</pre>
| <pre>{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | 0 | 2 }} }}</pre>
|
|
Line 762: Line 2,083:
* 1: b
* 1: b
|-
|-
| Get the last element
| <translate><!--T:672--> Get the last element</translate>
| <pre>{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | -1 }} }}</pre>
| <pre>{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | -1 }} }}</pre>
|
|
Line 770: Line 2,091:
=== af_sort ===
=== af_sort ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function sorts the given list.


<translate><!--T:673--> This parser function sorts the given list.</translate>
==== Description ====
<translate><!--T:674--> It does not preserve numeric keys.</translate>


<translate>
==== Description ==== <!--T:675-->
</translate>
<pre>
<pre>
{{#af_sort: array | descending=descending }}
{{#af_sort: array | descending=descending | caseinsensitive=caseinsensitive }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:676-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| array || <translate><!--T:681--> array</translate> || || <translate><!--T:682--> The array to sort.</translate>
|-
| descending || <translate><!--T:683--> boolean</translate> || {{phpi|false}} || <translate><!--T:684--> Whether to sort the list in descending order.</translate>
|-
| caseinsensitive || <translate><!--T:685--> boolean</translate> || {{phpi|false}} || <translate><!--T:686--> Whether to ignore case when sorting.</translate>
|}


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:687-->
: The array to sort.
;descending <nowiki>:</nowiki> boolean, default=false
: Whether to sort the list in descending order.

==== Return values ====


<!--T:688-->
Returns the sorted list.
Returns the sorted list.


==== Examples ====
==== Examples ==== <!--T:689-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Sort a list in ascending order
| <translate><!--T:690--> Sort a list in ascending order</translate>
| <pre>{{#af_print: {{#af_sort: {{#af_list: b | c | a }} }} }}</pre>
| <pre>{{#af_print: {{#af_sort: {{#af_list: b | c | a }} }} }}</pre>
|
|
Line 800: Line 2,133:
* 2: c
* 2: c
|-
|-
| Sort a list in descending order
| <translate><!--T:691--> Sort a list in descending order</translate>
| <pre>{{#af_print: {{#af_sort: {{#af_list: b | c | a }} | descending=true }} }}</pre>
| <pre>{{#af_print: {{#af_sort: {{#af_list: b | c | a }} | descending=true }} }}</pre>
|
|
Line 810: Line 2,143:
=== af_split ===
=== af_split ===


{{Software version|software=ArrayFunctions|version=1.1|version-status=stable|comment=+}}

<translate>
<!--T:692-->
This parser function splits the given string based on a delimiter.
This parser function splits the given string based on a delimiter.


==== Description ====
==== Description ==== <!--T:693-->
</translate>

<pre>
<pre>
{{#af_split: string | delimiter }}
{{#af_split: string | delimiter }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:694-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| string || <translate><!--T:699--> string</translate> || || <translate><!--T:700--> The string to split.</translate>
|-
| delimiter || <translate><!--T:701--> string</translate> || {{phpi|","}} || <translate><!--T:702--> The delimiter to use.</translate>
|}


<translate>
;string <nowiki>:</nowiki> string
==== Return values ==== <!--T:703-->
: The string to split.
;delimiter <nowiki>:</nowiki> string, default=","
: The delimiter to use. This parameter recognises the following escape sequences:
:: <code>\s</code> for spaces
:: <code>\n</code> for newlines
:: <code>\\</code> for backslashes

==== Return values ====


<!--T:704-->
Returns the resulting list.
Returns the resulting list.


==== Examples ====
==== Examples ==== <!--T:705-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Split a string based on commas
| <translate><!--T:706--> Split a string based on commas</translate>
| <pre>{{#af_print: {{#af_split: a, b, c }} }}</pre>
| <pre>{{#af_print: {{#af_split: a, b, c }} }}</pre>
|
|
Line 843: Line 2,183:
* 2: c
* 2: c
|-
|-
| Split a sentence into words
| <translate><!--T:707--> Split a sentence into words</translate>
| <pre>{{#af_print: {{#af_split: Lorem ipsum dolor et | \s }} }}</pre>
| <pre>{{#af_print: {{#af_split: Lorem ipsum dolor et | \s }} }}</pre>
|
|
Line 850: Line 2,190:
* 2: dolor
* 2: dolor
* 3: et
* 3: et
|}

=== af_stringmap ===

{{Software version|software=ArrayFunctions|version=1.6|version-status=stable|comment=+}}

<translate><!--T:708--> This parser function applies a callback to each item of a delimited string, and returns the result as a delimited string, optionally with a different delimiter.</translate>
<translate><!--T:709--> This function is similar to [[<tvar name=1>Special:MyLanguage/Extension:Page Forms</tvar>|Page Forms]]' [[<tvar name=2>Special:MyLanguage/Extension:Page Forms/Page Forms and templates##arraymap</tvar>|#arraymap]] parser function.</translate>

<translate>
==== Description ==== <!--T:710-->
</translate>
<pre>
{{#af_stringmap: value | delimiter | value_name | callback | new_delimiter | conjunction }}
</pre>

<translate>
==== Parameters ==== <!--T:711-->
</translate>
{| class="wikitable"
! {{tunit|74|Name}} !! {{tunit|259|Type}} !! {{tunit|257|Default}} !! {{tunit|75|Description}}
|-
| value || <translate><!--T:716--> string</translate> || || <translate><!--T:717--> The delimited string.</translate>
|-
| delimiter || <translate><!--T:718--> string</translate> || || <translate><!--T:719--> The delimiter to split <tvar name=1><code>value</code></tvar> on.</translate> <translate><!--T:720--> If the empty string is given, <tvar name=1>","</tvar> is used.</translate>
|-
| value_name || <translate><!--T:721--> string</translate> || || <translate><!--T:722--> The name to give to the value in the callback.</translate>
|-
| callback || <translate><!--T:723--> string</translate> || || <translate><!--T:724--> The callback to apply to each element of the array.</translate>
|-
| new_delimiter || <translate><!--T:725--> string</translate> || {{phpi|", "}} || <translate><!--T:726--> The new delimiter to insert in between the mapped items.</translate>
|-
| conjunction || <translate><!--T:727--> string</translate> || {{phpi|null}}
| <translate><!--T:728--> The delimiter to place between the last two items.</translate> <translate><!--T:729--> This allows you to create a more natural summation, such as "Alice, Bob and Eve".</translate> <translate><!--T:730--> If no value is given, the value of <tvar name=1><code>new_delimiter</code></tvar> is used.</translate>
|}

<translate>
==== Return values ==== <!--T:731-->

<!--T:732-->
Returns the resulting delimited string.

==== Examples ==== <!--T:733-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:734--> Turn each item into a link</translate>
| <pre>{{#af_stringmap: William Shakespeare, Stephen King, Mark Twain | , | x | [[{{{x}}}]] }}</pre>
| [[:en:William Shakespeare|William Shakespeare]], [[:en:Stephen King|Stephen King]], [[:en:Mark Twain|Mark Twain]]
|-
| <translate><!--T:735--> Turn a comma-separated list into a human-readable list</translate>
| <pre>{{#af_stringmap: William Shakespeare, Stephen King, Mark Twain | , | x | {{{x}}} | ,\s | and }}</pre>
| William Shakespeare, Stephen King and Mark Twain
|}
|}


=== af_template ===
=== af_template ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function will invoke the given template and pass the values in the given array as (named) parameters.


<translate><!--T:736--> This parser function will invoke the given template and pass the values in the given array as arguments.</translate>
==== Description ====
<translate><!--T:737--> In case the given array is an object, the values will be passed as named arguments.</translate>
<translate><!--T:738--> Otherwise, they will be passed as numeric arguments.</translate>


<translate>
==== Description ==== <!--T:739-->
</translate>
<pre>
<pre>
{{#af_template: name | data }}
{{#af_template: name | data }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:740-->
</translate>
;name <nowiki>:</nowiki> <translate><!--T:741--> string</translate>
: <translate><!--T:742--> The name of the template to invoke.</translate> <translate><!--T:743--> If no namespace is given, it is assumed the page is in the template namespace, otherwise the given namespace is used.</translate> <translate><!--T:744--> The page must exist, must be includable and must be readable by the user, otherwise an error is given.</translate>
;data <nowiki>:</nowiki> <translate><!--T:745--> array</translate>
: <translate><!--T:746--> The data to pass to the parameters.</translate> <translate><!--T:747--> Values with numeric indices are passed as positional arguments and values with string indices are passed as named arguments.</translate>


<translate>
;name <nowiki>:</nowiki> string
==== Return values ==== <!--T:748-->
: The name of the template to invoke. If no namespace is given, it is assumed the page is in the template namespace, otherwise the given namespace is used. The page must exist, must be includable and must be readable by the user, otherwise an error is given.

==== Return values ====


<!--T:749-->
The expanded template.
The expanded template.


==== Examples ====
==== Examples ==== <!--T:750-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Invoking a template with a list
| <translate><!--T:751--> Invoking a template with a list</translate>
| <pre>{{#af_template: Echo | {{#af_list: a | b }} }}</pre>
| <pre>{{#af_template: Echo | {{#af_list: a | b }} }}</pre>
| <pre>{{Echo|a|b}}</pre>
| <pre>{{Echo|a|b}}</pre>
|-
|-
| Invoking a template with an object
| <translate><!--T:752--> Invoking a template with an object</translate>
| <pre>{{#af_template: Echo | {{#af_object: foo=bar | boo=far }} }}</pre>
| <pre>{{#af_template: Echo | {{#af_object: foo=bar | boo=far }} }}</pre>
| <pre>{{Echo|foo=bar|boo=far}}</pre>
| <pre>{{Echo|foo=bar|boo=far}}</pre>
|}

=== af_trim ===

{{Software version|software=ArrayFunctions|version=1.8|version-status=stable|comment=+}}

<translate><!--T:753--> This parser function will trim the given characters from the beginning and the end of the given string.</translate>
<translate><!--T:754--> The order of the characters does not matter.</translate>
<translate><!--T:755--> It will stop once it hits a character that is not in <tvar name=1><code>characters</code></tvar>.</translate>

<translate>
==== Description ==== <!--T:756-->
</translate>
<pre>
{{#af_trim: string | characters }}
</pre>

<translate>
==== Parameters ==== <!--T:757-->
</translate>
;string <nowiki>:</nowiki> <translate><!--T:758--> string</translate>
: <translate><!--T:759--> The string that will be trimmed.</translate>
;characters <nowiki>:</nowiki> <translate><!--T:760--> string</translate>
: <translate><!--T:761--> The characters to trim.</translate>

<translate>
==== Return values ==== <!--T:762-->

<!--T:763-->
The trimmed string.

==== Examples ==== <!--T:764-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:765--> Trimming a string</translate>
| <pre>{{#af_trim: !a! | ! }}</pre>
| <pre>a</pre>
|-
| <translate><!--T:766--> Trimming every string in a list</translate>
| <pre>{{#af_print: {{#af_map: {{#af_list: !a! | !b! | !c! }} | v | {{#af_trim: {{{v}}} | ! }} }} }}</pre>
|
* 0: a
* 1: b
* 2: c
|}
|}


=== af_unique ===
=== af_unique ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function removes duplicate values from the given array. This function does not reset keys.


<translate><!--T:767--> This parser function removes duplicate values from the given array.</translate>
==== Description ====
<translate><!--T:768--> This function does not reset keys.</translate>


<translate>
==== Description ==== <!--T:769-->
</translate>
<pre>
<pre>
{{#af_unique: array }}
{{#af_unique: array }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:770-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:771--> array</translate>
: <translate><!--T:772--> The array from which to remove duplicates.</translate>


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:773-->
: The array from which to remove duplicates.

==== Return values ====


<!--T:774-->
Returns the array with duplicates removed.
Returns the array with duplicates removed.


==== Examples ====
==== Examples ==== <!--T:775-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Remove duplicates from an array
| <translate><!--T:776--> Remove duplicates from an array</translate>
| <pre>{{#af_print: {{#af_unique: {{#af_list: a | a | b | c | b }} }} }}</pre>
| <pre>{{#af_print: {{#af_unique: {{#af_list: a | a | b | c | b }} }} }}</pre>
|
|
Line 917: Line 2,372:
=== af_unset ===
=== af_unset ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
This parser function removes the value associated with the given index from the array and returns the result. Numeric keys are '''not''' reset after calling this function.


<translate><!--T:777--> This parser function removes the value associated with the given index from the array and returns the result.</translate>
==== Description ====
<translate><!--T:778--> Numeric keys are '''not''' reset after calling this function.</translate>


<translate>
==== Description ==== <!--T:779-->
</translate>
<pre>
<pre>
{{#af_unset: array | *indices }}
{{#af_unset: array | *indices }}
</pre>
</pre>


<translate>
==== Parameters ====
==== Parameters ==== <!--T:780-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:781--> array</translate>
: <translate><!--T:782--> The array from which to remove the given key.</translate>
;<nowiki>*</nowiki>indices <nowiki>:</nowiki> <translate><!--T:783--> string</translate>
: <translate><!--T:784--> The index to remove.</translate> <translate><!--T:785--> Multiple indices can be given to index nested arrays.</translate>


<translate>
;array <nowiki>:</nowiki> array
==== Return values ==== <!--T:786-->
: The array from which to remove the given key.
;<nowiki>*</nowiki>indices <nowiki>:</nowiki> string
: The index to remove. Multiple indices can be given to index nested arrays.

==== Return values ====


<!--T:787-->
Returns the array with the given index removed.
Returns the array with the given index removed.


==== Examples ====
==== Examples ==== <!--T:788-->
</translate>

{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
|-
|-
| Remove a top-level index
| <translate><!--T:789--> Remove a top-level index</translate>
| <pre>{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 2 }} }}</pre>
| <pre>{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 2 }} }}</pre>
|
|
Line 946: Line 2,408:
* 1: b
* 1: b
|-
|-
| Remove a top-level index, keys not reset
| <translate><!--T:790--> Remove a top-level index, keys not reset</translate>
| <pre>{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 1 }} }}</pre>
| <pre>{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 1 }} }}</pre>
|
|
Line 952: Line 2,414:
* 2: c
* 2: c
|-
|-
| Remove a nested index
| <translate><!--T:791--> Remove a nested index</translate>
| <pre>{{#af_print: {{#af_unset: {{#af_object: foo={{#af_object: bar=quz | far=buz }} }} | foo | bar }} }}</pre>
| <pre>{{#af_print: {{#af_unset: {{#af_object: foo={{#af_object: bar=quz | far=buz }} }} | foo | bar }} }}</pre>
|
|
* foo
* foo
** far: buz
** far: buz
|}

=== af_zip ===

{{Software version|software=ArrayFunctions|version=1.16|version-status=stable|comment=+}}

<translate><!--T:792--> This parser functions takes one or more arrays and zips these together by key, creating an array where each key maps to an array containing all values under that key in each of the input arrays.</translate>
<translate><!--T:793--> The final array will only contain keys that were present in all of the input arrays.</translate>
<translate><!--T:794--> This means that if an input array contains a key not present in one of the other input arrays, the key will be dropped, and it will not be part of the final zipped array.</translate>

<translate>
==== Description ==== <!--T:795-->
</translate>
<pre>
{{#af_zip: array | *arrays }}
</pre>

<translate>
==== Parameters ==== <!--T:796-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:797--> array</translate>
: <translate><!--T:798--> The first array.</translate>
;<nowiki>*</nowiki>arrays <nowiki>:</nowiki> <translate><!--T:799--> array</translate>
: <translate><!--T:800--> The other arrays.</translate>

<translate>
==== Return values ==== <!--T:801-->

<!--T:802-->
Returns the arrays zipped together.

==== Examples ==== <!--T:803-->
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:804--> Zip two lists of equal length</translate>
| <pre>{{#af_print: {{#af_zip: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }}</pre>
|
* 0
** 0: a
** 1: d
* 1
** 0: b
** 1: e
* 2
** 0: c
** 1: f
|-
| <translate><!--T:805--> Zip two lists of different lengths</translate>
| <pre>{{#af_print: {{#af_zip: {{#af_list: a | b }} | {{#af_list: d | e | f }} }} }}</pre>
|
* 0
** 0: a
** 1: d
* 1
** 0: b
** 1: e
|-
| <translate><!--T:806--> Zip two objects with the same keys</translate>
| <pre>{{#af_print: {{#af_zip: {{#af_object: a=a | b=b }} | {{#af_object: a=c | b=d }} }} }}</pre>
|
* a
** 0: a
** 1: c
* b
** 0: b
** 1: d
|-
| <translate><!--T:807--> Zip two objects with different keys</translate>
| <pre>{{#af_print: {{#af_zip: {{#af_object: a=a | b=b }} | {{#af_object: a=c | different=d }} }} }}</pre>
|
* a
** 0: a
** 1: c
|}
|}


== Scribunto ==
== Scribunto ==


{{Note|type=warn|text=<translate><!--T:808--> It is recommended to use <tvar name=1>{{ll|LuaSandbox}}</tvar>, because the order of arrays is not preserved when using <tvar name=2>LuaStandalone</tvar> (see <tvar name=3>{{phab|T349590}}</tvar>).</translate>}}
This extension is particularly useful in combination with Lua, as it can be used to create the array containing data required for the presentation of the page. This array can be exported to work with ArrayFunctions:

<translate><!--T:809--> This extension is particularly useful in combination with Lua, as it can be used to create the array containing data required for the presentation of the page.</translate>
<translate><!--T:810--> This array can be exported to work with <tvar name=1>ArrayFunctions</tvar>:</translate>


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 975: Line 2,514:
</syntaxhighlight>
</syntaxhighlight>


This module may then be invoked like so:
<translate><!--T:811--> This module may then be invoked like so:</translate>


{| class="wikitable" style="width: 100%"
{| class="wikitable" style="width: 100%"
Line 984: Line 2,523:
|}
|}


== Magic words ==
=== mw.af.export ===


{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}
The extension defines a number of magic words (variables).


<translate>
=== AF_EMPTY ===
<!--T:812-->
This Lua function exports a Lua table as an <tvar name=1>ArrayFunctions</tvar> array.


==== Description ==== <!--T:813-->
This magic word returns the empty array. This is useful, because it is not possible to create an empty array with <code>#af_list</code> or <code>#af_object</code>.
</translate>
<pre>
mw.af.export( table )
</pre>


<translate>
==== Description ====
==== Parameters ==== <!--T:814-->
</translate>
;table <nowiki>:</nowiki> <translate><!--T:815--> array</translate>
: <translate><!--T:816--> The array to export.</translate>


<translate>
==== Return values ==== <!--T:817-->

<!--T:818-->
Returns the table as an ArrayFunctions array.</translate>

=== mw.af.import ===

{{Software version|software=ArrayFunctions|version=1.6|version-status=stable|comment=+}}

<translate>
<!--T:819-->
This Lua function imports an ArrayFunctions array as a Lua table.

==== Description ==== <!--T:820-->
</translate>
<pre>
<pre>
mw.af.import( array )
{{AF_EMPTY}}
</pre>
</pre>


<translate>
==== Return values ====
==== Parameters ==== <!--T:821-->
</translate>
;array <nowiki>:</nowiki> <translate><!--T:822--> array</translate>
: <translate><!--T:823--> The array to import.</translate>


<translate>
Returns the empty array.
==== Return values ==== <!--T:824-->


<!--T:825-->
== Installation ==
Returns the array as a Lua table.


== Semantic MediaWiki == <!--T:826-->
{{ExtensionInstall
</translate>
|ArrayFunctions
<translate><!--T:827--> This extension integrates with Semantic MediaWiki by adding the <tvar name=1><code>arrayfunctions</code></tvar> result format.</translate>
|registration=required
<translate><!--T:828--> The <tvar name=1><code>arrayfunctions</code></tvar> result format is used to format query results as an <tvar name=2>ArrayFunctions</tvar> array.</translate>
}}
<translate><!--T:829--> The result is almost identical to the result of [[<tvar name=1>Special:MyLanguage/Extension:Semantic Scribunto</tvar>|Semantic Scribunto]]'s <tvar name=2><code>mw.smw.ask</code></tvar> Lua function, except numeric indices start at zero.</translate>


== FAQ ==
=== arrayfunctions ===


{{Software version|software=ArrayFunctions|version=1.14|version-status=stable|comment=+}}
=== How can I define an array to be used throughout a page? ===


<translate>
It is not possible to directly define an array to be used throughout a page, because this would require sequential processing of extension tags, which is not supported by Parsoid (see [[Parsoid/Extension API#No support for sequential, in-order processing of extension tags]]). Instead, you can pass arrays around as template parameters:
<!--T:830-->
This result format formats query results as an <tvar name=1>ArrayFunctions</tvar> array.


==== Description ==== <!--T:831-->
</translate>
<pre>
<pre>
{{#ask: query | format=arrayfunctions }}
{{My template|{{#af_list:a|b|c}}}}
</pre>
</pre>


<translate>
This way, the array is available in <code>Template:My template</code> as <code>{{{1}}}</code>.
==== Return values ==== <!--T:832-->


<!--T:833-->
=== How to iterate over an array? ===
Returns the query result as an <tvar name=1>ArrayFunctions</tvar> array.


==== Examples ==== <!--T:834-->
It is possible to iteratively access elements of an array using [[#af_foreach]]:
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:835--> Simple query</translate>
| <pre>{{#af_print: {{#ask:
[[Category:City]]
[[Located in::Germany]]
|?Population
|?Area#kmยฒ=Size
|mainlabel=City
|sort=Population
|order=descending
|headers=plain
|format=arrayfunctions
}} }}</pre>
|
* 0
** City: Berlin
** Population: 3520061
** Size: 891.85 kmยฒ
* 1
** City: Munich
** Population: 1353186
** Size: 310.43 kmยฒ
* 2
** City: Cologne
** Population: 1080394
** Size: 405.02 kmยฒ
* 3
** City: Frankfurt
** Population: 679664
** Size: 248.31 kmยฒ
* 4
** City: Stuttgart
** Population: 606588
** Size: 207.35 kmยฒ
* 5
** City: Wรผrzburg
** Population: 126635
** Size: 87.63 kmยฒ
|}

== Cargo ==

<translate><!--T:836--> This extension integrates with Cargo by adding the <tvar name=1><code>arrayfunctions</code></tvar> display format.</translate>
<translate><!--T:837--> The <tvar name=1><code>arrayfunctions</code></tvar> result format is used to format query results as an <tvar name=2>ArrayFunctions</tvar> array.</translate>

=== arrayfunctions ===

{{Software version|software=ArrayFunctions|version=1.15|version-status=stable|comment=+}}

<translate>
<!--T:838-->
This display format formats query results as an <tvar name=1>ArrayFunctions</tvar> array.

==== Description ==== <!--T:839-->
</translate>
{{Note|type=warn|text=<translate><!--T:840--> The <tvar name=1><code>no html</code></tvar> parameter is required for <tvar name=2>ArrayFunctions</tvar> to interpret the result correctly.</translate>}}


<pre>
<pre>
{{#cargo_query: parameters | no html | format=arrayfunctions }}
{{#af_foreach:{{#af_list:red|green|blue}}||color|<nowiki/>
* {{{color}}} is my favourite.
}}
</pre>
</pre>


<translate>
The expected output from the snipped above is:
==== Return values ==== <!--T:841-->


<!--T:842-->
* red is my favourite.
Returns the query result as an <tvar name=1>ArrayFunctions</tvar> array.
* green is my favourite.
* blue is my favourite.


==== Examples ==== <!--T:843-->
=== How to use foreach with a delimiter? ===
</translate>
{| class="wikitable" style="width: 100%"
|-
| <translate><!--T:844--> Simple query</translate>
| <pre>{{#af_print: {{#cargo_query: tables=Books
|no html
|fields=_pageName,Authors,Genres
|format=arrayfunctions
}} }}</pre>
|
* 0
** _pageName: The Masque of the Red Death
** Authors: Edgar Allan Poe
** Genres
*** 0: Horror
*** 1: Fiction
* 1
** _pageName: Rita Hayworth and Shawshank Redemption
** Authors: Stephen King
** Genres
*** 0: Realism
*** 1: Crime
* 2
** _pageName: Animal Farm
** Authors: George Orwell
** Genres: Political satire
|}


<translate>
Using [[#af_foreach]] with a delimiter is not supported. Instead, you can use the [[#af_map]] function in conjunction with the [[#af_join]] function:
== Magic words == <!--T:845-->


<!--T:846-->
The extension defines a number of magic words (variables).
</translate>

=== AF_EMPTY ===

{{Software version|software=ArrayFunctions|version=1.0|version-status=stable|comment=+}}

<translate><!--T:847--> This magic word returns the empty array.</translate>
<translate><!--T:848--> This is useful, because it is not possible to create an empty array with <tvar name=2><code>#af_list</code></tvar> or <tvar name=1><code>#af_object</code></tvar>.</translate>

<translate>
==== Description ==== <!--T:849-->
</translate>
<pre>
<pre>
{{AF_EMPTY}}
{{#af_join:{{#af_map:{{#af_list:red|green|blue}}|color|* {{{color}}} is my favourite.}}|\n}}
</pre>
</pre>


<translate>
== See also ==
==== Return values ==== <!--T:850-->

<!--T:851-->
Returns the empty array.

== Presentations == <!--T:852-->
</translate>
<gallery>
File:SMWCon 2022 - Updates from the Wikitext Parsing World 01.pdf|<translate><!--T:853--> Presentation at SMWCon 2022 talking about <tvar name=1>ArrayFunctions</tvar></translate>
File:EMWCon Spring 2023 - MVC in MediaWiki.pdf|<translate><!--T:854--> Presentation at EMWCon 2023 talking about <tvar name=1>ArrayFunctions</tvar></translate>
File:SMWCon 2023 Parsoid workshop.pdf|<translate><!--T:855--> Workshop given at SMWCon 2023 about making your wiki Parsoid-compatible</translate>
File:SMWCon 2023 - Updates from the Wikitext Parsing World.pdf|<translate><!--T:856--> Presentation at SMWCon 2023 talking about Parsoid and <tvar name=1>ArrayFunctions</tvar></translate>
File:MUD Workshop Spring 2025 Making your wiki Parsoid-compatible with ArrayFunctions.pdf|<translate><!--T:857--> Presentation at the MUD Workshop Spring 2025 about <tvar name=1>ArrayFunctions</tvar></translate>
</gallery>

<translate>
== See also == <!--T:858-->
</translate>
* {{ll|Extension:Arrays}} โ€“ <translate><!--T:859--> similar extension that works by first defining and then manipulating arrays.</translate> <translate><!--T:860--> Incompatible with Parsoid.</translate>
* {{ll|Extension:Scribunto}} โ€“ <translate><!--T:861--> allows you to embed Lua scripts into wikipages.</translate>

<translate>
== Changelog == <!--T:862-->

<!--T:863-->
All notable changes to <tvar name=1>ArrayFunctions</tvar> will be documented here.

<!--T:864-->
The format is based on [<tvar name=url>https://keepachangelog.com/en/1.1.0/</tvar> Keep a Changelog], and this extension adheres to [<tvar name=url2>https://semver.org/spec/v2.0.0.html</tvar> Semantic Versioning].</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v2.0.0...v2.0.1 v2.0.1] - {{#dateformat:2026-06-18|ymd}} ===

==== Changed ====
* Make <code>#af_stringmap</code> able to create array with "0" as a value. Previously, "0" would be removed from the array.

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.17.0...v2.0.0 v2.0.0] - {{#dateformat:2026-01-28|ymd}} ===

==== Added ====
* Add the <code>#af_filter</code> parser function.
* Add the <code>//</code> overloaded index to remove empty values.

==== Changed ====
* BREAKING: Remove support for MediaWiki 1.39 and earlier. The oldest supported version is now MediaWiki 1.40.
* BREAKING: In <code>#af_pipeline</code>, when the first argument is empty, <code>{{{prev}}}</code> now renders literally as <code>{{{prev}}}</code>. Use <code><nowiki>{{{prev|}}}</nowiki></code> to preserve the 1.x.x behavior.
* Ignore the first argument when it is empty for <code>#af_difference</code>, <code>#af_intersect</code>, <code>#af_merge</code>, <code>#af_object</code>, <code>#af_pipeline</code>, and <code>#af_zip</code>.
* Localisation updates courtesy of [[translatewiki.net]].

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.16.2...v1.17.0 v1.17.0] - {{#dateformat:2025-08-26|ymd}} ===

{{Note|This version is not tagged properly on <code>Special:Version</code>, where it still shows as <code>1.16.2</code>.}}

<translate>
==== Added ==== <!--T:866-->
</translate>
* <translate><!--T:867--> Add the <tvar name=1><code>$wgArrayFunctionsMaxPipelineLength</code></tvar> configuration variable to limit the total number of steps in a pipeline.</translate>
* <translate><!--T:868--> Add the <tvar name=1><code>Pages using the ArrayFunctions extension</code></tvar> (<tvar name=2><code>af-tracking-category</code></tvar>) tracking category.</translate>

<translate>
==== Changed ==== <!--T:869-->
</translate>
* <translate><!--T:870--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:871-->
</translate>
* <translate><!--T:872--> Ranges that exceed <tvar name=1><code>$wgArrayFunctionsMaxRangeSize</code></tvar> are now taken into account when reporting the size of the largest constructed range in the parser limit report.</translate> <translate><!--T:873--> Previously, only ranges that did not exceed the limit would be taken into account.</translate>
* <translate><!--T:874--> Fix <tvar name=1><code>TypeError</code></tvar> that occurred when an error was propagated from a nested parser function call.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.16.1...v1.16.2 v1.16.2] - {{#dateformat:2025-06-28|ymd}} ===

<translate>
==== Fixed ==== <!--T:875-->
</translate>
* <translate><!--T:876--> Fix exception caused by storing a <tvar name=1><code>Message</code></tvar> object in the parser's extension data</translate> ([https://phabricator.wikimedia.org/T397946 T397946]).
* <translate><!--T:877--> Ensure errors are rendered in the page's target language instead of the user's language</translate> ([https://phabricator.wikimedia.org/T397951 T397951]).

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.16.0...v1.16.1 v1.16.1] - {{#dateformat:2025-06-19|ymd}} ===

<translate>
==== Changed ==== <!--T:878-->
</translate>
* <translate><!--T:879--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:880-->
</translate>
* <translate><!--T:881--> Fix <tvar name=1><code>BadMethodCallException</code></tvar> that was caused by relying on <tvar name=2><code>$wgLang</code></tvar> in the <tvar name=3><code>ParserLimitReportPrepare</code></tvar> hook.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.15.0...v1.16.0 v1.16.0] - {{#dateformat:2025-06-10|ymd}} ===

<translate>
==== Added ==== <!--T:882-->
</translate>
* <translate><!--T:884--> Add the <tvar name=1><code>$wgArrayFunctionsForeachIterationLimit</code></tvar> configuration variable to limit the total number of iterations that can be performed by <tvar name=2><code>#af_foreach</code></tvar>.</translate>
* <translate><!--T:885--> Add the <tvar name=1><code>$wgArrayFunctionsMaxRangeSize</code></tvar> configuration variable to limit the maximum number of elements that an array constructed using <tvar name=2><code>#af_range</code></tvar> can contain.</translate>
* <translate><!--T:886--> Add the <tvar name=1><code>#af_zip</code></tvar> parser function.</translate>
* <translate><!--T:887--> Add the <tvar name=1><code>!</code></tvar> overloaded index to show a value.</translate>
* <translate><!--T:888--> Add the <tvar name=1><code>table</code></tvar> and <tvar name=2><code>simple</code></tvar> formats for `<tvar name=3>#af_show</tvar>`.</translate>
* <translate><!--T:889--> Add the <tvar name=1><code>Pages with ArrayFunctions errors</code></tvar> (<tvar name=2><code>af-error-category</code></tvar>) tracking category.</translate>
* <translate><!--T:890--> Add error tracking and propagation.</translate>

<translate>
==== Changed ==== <!--T:891-->
</translate>
* <translate><!--T:892--> Allow <tvar name=1><code>#af_show</code></tvar> to take a format.</translate>
* <translate><!--T:893--> The <tvar name=1><code>#af_sort</code></tvar> and <tvar name=2><code>#af_ksort</code></tvar> parser functions now compare items normally instead of as strings.</translate>
* <translate><!--T:894--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:895-->
</translate>
* <translate><!--T:896--> Fix exception when passing the empty string to a keyword argument.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.14.2...v1.15.0 v1.15.0] - {{#dateformat:2025-05-13|ymd}} ===

<translate>
==== Added ==== <!--T:897-->
</translate>
* <translate><!--T:898--> Add the <tvar name=1><code>arrayfunctions</code></tvar> Cargo display format.</translate>

<translate>
==== Changed ==== <!--T:899-->
</translate>
* <translate><!--T:900--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.14.1...v1.14.2 v1.14.2] - {{#dateformat:2025-04-15|ymd}} ===

<translate>
==== Fixed ==== <!--T:901-->
</translate>
* <translate><!--T:902--> Remove properties without a value from the result when using the <tvar name=1><code>arrayfunctions</code></tvar> result format with Semantic MediaWiki.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.14.0...v1.14.1 v1.14.1] - {{#dateformat:2025-04-15|ymd}} ===

<translate>
==== Added ==== <!--T:903-->
</translate>
* <translate><!--T:904--> Add compatibility with Semantic MediaWiki 5.0.0.</translate>

<translate>
==== Changed ==== <!--T:905-->
</translate>
* <translate><!--T:906--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:907-->
</translate>
* <translate><!--T:908--> Restore compatibility with deprecated MediaWiki 1.35.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.13.0...v1.14.0 v1.14.0] - {{#dateformat:2025-04-01|ymd}} ===

<translate>
==== Added ==== <!--T:909-->
</translate>
* <translate><!--T:910--> Add the <tvar name=1><code>arrayfunctions</code></tvar> Semantic MediaWiki result format.</translate>
* <translate><!--T:911--> Add the <tvar name=1><code>#af_pipeline</code></tvar> parser function.</translate>

<translate>
==== Changed ==== <!--T:912-->
</translate>
* <translate><!--T:913--> Drop compatibility with MediaWiki 1.35.</translate>
* <translate><!--T:914--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:915-->
</translate>
* <translate><!--T:916--> Fix exception when using <tvar name=1><code>#af_template</code></tvar> on MediaWiki 1.44.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.12.0...v1.13.0 v1.13.0] - {{#dateformat:2025-03-04|ymd}} ===

<translate>
==== Changed ==== <!--T:917-->
</translate>
* <translate><!--T:918--> Replace the <tvar name=1><code>#af_set</code></tvar> parser function with <tvar name=2><code>#af_put</code></tvar>.</translate> <translate><!--T:982--> The <tvar name=1><code>#af_set</code></tvar> parser function remains available for backwards compatibility.</translate>
* <translate><!--T:919--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:920-->
</translate>
* <translate><!--T:921--> The magic variable IDs array is now no longer treated as associative, which would previously break CodeMirror syntax highlighting. (by <tvar name=1>alex4401</tvar>)</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.11.0...v1.12.0 v1.12.0] - {{#dateformat:2025-02-04|ymd}} ===

<translate>
==== Added ==== <!--T:922-->
</translate>
* <translate><!--T:923--> Add the <tvar name=1><code>#af_range</code></tvar> parser function.</translate>

<translate>
==== Changed ==== <!--T:924-->
</translate>
* <translate><!--T:925--> Rename the <tvar name=1><code>#af_wildcard</code></tvar> parser function to <tvar name=2><code>#af_group</code></tvar>, and add an alias for <tvar name=3><code>#af_wildcard</code></tvar>.</translate>
* <translate><!--T:926--> The <tvar name=1><code>#af_difference</code></tvar> parser function now compares items normally instead of as strings.</translate>
* <translate><!--T:927--> The <tvar name=1><code>#af_exists</code></tvar> parser function now accepts multiple keys to check if a nested key exists.</translate>
* <translate><!--T:928--> The <tvar name=1><code>#af_intersect</code></tvar> parser function now compares items normally instead of as strings.</translate>
* <translate><!--T:929--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.10.0...v1.11.0 v1.11.0] - {{#dateformat:2024-12-10|ymd}} ===

<translate>
==== Added ==== <!--T:930-->
</translate>
* <translate><!--T:931--> Add following parser functions:</translate> <code>#af_flatten</code> <code>#af_wildcard</code> <code>#af_reverse</code> .
* <translate><!--T:932--> Overload <tvar name=1><code>#af_get</code></tvar> with special indices to perform certain operations on the array instead of retrieving a key.</translate>

<translate>
==== Changed ==== <!--T:933-->
</translate>
* <translate><!--T:934--> The <tvar name=1><code>#af_unique</code></tvar> parser function now compares items normally instead of as strings.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.9.0...v1.10.0 v1.10.0] - {{#dateformat:2024-11-26|ymd}} ===

<translate>
==== Added ==== <!--T:935-->
</translate>
* <translate><!--T:936--> Add ZLIB compression using <tvar name=1><code>gzdeflate</code></tvar> and <tvar name=2><code>gzinflate</code></tvar>.</translate>

<translate>
==== Changed ==== <!--T:937-->
</translate>
* <translate><!--T:938--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.8.0...v1.9.0 v1.9.0] - {{#dateformat:2024-05-02|ymd}} ===

<translate>
==== Added ==== <!--T:939-->
</translate>
* <translate><!--T:940--> Add <tvar name=1><code>delimiter</code></tvar> option to the <tvar name=2><code>#af_foreach</code></tvar> parser function.</translate>

<translate>
==== Changed ==== <!--T:941-->
</translate>
* <translate><!--T:942--> All string parameters now support escape sequences.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.7.0...v1.8.0 v1.8.0] - {{#dateformat:2024-04-04|ymd}} ===

<translate>
==== Changed ==== <!--T:943-->
</translate>
* <translate><!--T:944--> Whitespace is now trimmed from the beginning and the end of array values to be consistent with the behaviour of other parameters in MediaWiki.</translate>
* <translate><!--T:945--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.6.0...v1.7.0 v1.7.0] - {{#dateformat:2023-10-24|ymd}} ===

<translate>
==== Added ==== <!--T:946-->
</translate>
* <translate><!--T:947--> Add the <tvar name=1><code>#af_ksort</code></tvar> parser function.</translate>
* <translate><!--T:948--> Add <tvar name=1><code>caseinsensitive</code></tvar> option to the <tvar name=2><code>#af_keysort</code></tvar> parser function.</translate>
* <translate><!--T:949--> Add <tvar name=1><code>caseinsensitive</code></tvar> option to the <tvar name=2><code>#af_sort</code></tvar> parser function.</translate>

<translate>
==== Changed ==== <!--T:950-->
</translate>
* <translate><!--T:951--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.5.0...v1.6.0 v1.6.0] - {{#dateformat:2023-09-12|ymd}} ===

<translate>
==== Added ==== <!--T:952-->
</translate>
* <translate><!--T:953--> Add the <tvar name=1><code>#af_stringmap</code></tvar> parser function.</translate>
* <translate><!--T:954--> Add the <tvar name=1><code>mw.af.import</code></tvar> Lua function to import <tvar name=2>ArrayFunctions</tvar> arrays into Lua.</translate>

<translate>
==== Changed ==== <!--T:955-->
</translate>
* <translate><!--T:956--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.4.4...v1.5.0 v1.5.0] - {{#dateformat:2023-09-07|ymd}} ===

<translate>
==== Added ==== <!--T:957-->
</translate>
* <translate><!--T:958--> Add the <tvar name=1><code>#af_difference</code></tvar> parser function.</translate>

<translate>
==== Changed ==== <!--T:959-->
</translate>
* <translate><!--T:960--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.4.3...v1.4.4 v1.4.4] - {{#dateformat:2023-06-30|ymd}} ===

<translate>
==== Changed ==== <!--T:961-->
</translate>
* <translate><!--T:962--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

<translate>
==== Fixed ==== <!--T:963-->
</translate>
* <translate><!--T:964--> Fix <tvar name=1><code>#af_template</code></tvar> parser function to no longer check a user their read permissions explicitly, as regular template transclusion also does not do this.</translate> <translate><!--T:965--> Previously, an error would be outputted.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.4.2...v1.4.3 v1.4.3] - {{#dateformat:2023-05-26|ymd}} ===

<translate>
==== Changed ==== <!--T:966-->
</translate>
* <translate><!--T:967--> Exporting a <tvar name=1><code>NULL</code></tvar> value (e.g. through the <tvar name=2><code>mw.af.export</code></tvar> Lua function) will now result in the empty string.</translate> <translate><!--T:968--> Previously, the <tvar name=1><code>NULL</code></tvar> would be returned unaltered.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.4.1...v1.4.2 v1.4.2] - {{#dateformat:2023-05-26|ymd}} ===

<translate>
==== Changed ==== <!--T:969-->
</translate>
* <translate><!--T:970--> The <tvar name=1><code>#af_template</code></tvar> parser function now shows non-existent templates as a broken link.</translate> <translate><!--T:971--> Previously, an error would be outputted.</translate>
* <translate><!--T:972--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.4.0...v1.4.1 v1.4.1] - {{#dateformat:2023-05-05|ymd}} ===

<translate>
==== Changed ==== <!--T:973-->
</translate>
* <translate><!--T:974--> The <tvar name=1><code>#af_split</code></tvar> parser function now allows the empty string as its first parameter.</translate> <translate><!--T:975--> Previously, an error would be outputted.</translate>
* <translate><!--T:976--> Localisation updates courtesy of <tvar name=1>[[translatewiki.net]]</tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.3.0...v1.4.0 v1.4.0] - {{#dateformat:2023-04-26|ymd}} ===

<translate>
==== Added ==== <!--T:977-->
</translate>
* <translate><!--T:978--> Add the <tvar name=1><code>#af_show</code></tvar> parser function.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.2.0...v1.3.0 v1.3.0] - {{#dateformat:2023-03-27|ymd}} ===

<translate>
==== Added ==== <!--T:979-->
</translate>
* <translate><!--T:980--> Add the <tvar name=1><code>#af_search</code></tvar> parser function.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.1.0...v1.2.0 v1.2.0] - {{#dateformat:2023-03-03|ymd}} ===

<translate>
==== Added ==== <!--T:981-->
</translate>
* Add the following parser functions: <code>#af_intersect</code> <code>#af_merge</code> <code>#af_reduce</code>.

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.0.1...v1.1.0 v1.1.0] - {{#dateformat:2023-02-03|ymd}} ===

<translate>
==== Added ==== <!--T:139-->
</translate>
* <translate><!--T:140--> Add the <tvar name=1><code>#af_split</code></tvar> parser function.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/compare/v1.0.0...v1.0.1 v1.0.1] - {{#dateformat:2023-01-09|ymd}} ===

<translate>
==== Changed ==== <!--T:141-->

<!--T:142-->
* The <tvar name=1><code>mw.af.export</code></tvar> Lua function now supports parameters of all types.</translate> <translate><!--T:143--> Previously, it only supported arrays.</translate>

<translate>
==== Fixed ==== <!--T:144-->
</translate>
* <translate><!--T:145--> Fix issue where an exception was thrown when a parameter with an incorrect type was passed to <tvar name=1><code>mw.af.export</code></tvar>.</translate>

=== [https://github.com/wikimedia/mediawiki-extensions-ArrayFunctions/releases/tag/v1.0.0 v1.0.0] - {{#dateformat:2023-01-07|ymd}} ===

<translate>
==== Added ==== <!--T:146-->
</translate>
* <translate><!--T:147--> Add the parser functions:</translate> <code>#af_bool</code> <code>#af_count</code> <code>#af_exists</code> <code>#af_float</code> <code>#af_foreach</code> <code>#af_get</code> <code>#af_if</code> <code>#af_int</code> <code>#af_isarray</code> <code>#af_join</code> <code>#af_keysort</code> <code>#af_list</code> <code>#af_map</code> <code>#af_object</code> <code>#af_print</code> <code>#af_push</code> <code>#af_set</code> <code>#af_slice</code> <code>#af_sort</code> <code>#af_template</code> <code>#af_unique</code> <code>#af_unset</code>


{{Used by}}
* {{ll|Extension:Arrays}} - similar extension that works by first defining and then manipulating arrays. Incompatible with Parsoid.
* {{ll|Extension:PhpTags Functions/Functions/Array}} - includes over fifty functions for working with arrays using the PHP syntax.
* {{ll|Extension:Scribunto}} - allows you to embed Lua scripts into wikipages.


[[Category:Array extensions{{#translation:}}]]
[[Category:Array extensions{{#translation:}}]]

Latest revision as of 07:25, 12 August 2026

MediaWiki extensions manual
ArrayFunctions
Release status: stable
Implementation Parser function
Description Provides a set of pure parser functions that operate on arrays
Author(s) Marijn van Wezel (Wikibase Solutions)
Latest version 2.0.1 (2026-06-18)
Compatibility policy Master maintains backward compatibility.
MediaWiki >=1.40.0
PHP >=7.4
Database changes No
Composer wikibase-solutions/array-functions
  • $wgArrayFunctionsMaxPipelineLength
  • $wgArrayFunctionsEnableErrorTracking
  • $wgArrayFunctionsMaxRangeSize
  • $wgArrayFunctionsForeachIterationLimit
Licence GNU General Public License 2.0 or later
Download
Translate the ArrayFunctions extension if it is available at translatewiki.net
Issues Open tasks ยท Report a bug

Documentation for other releases: 1.0 ยท 1.1 ยท 1.2 ยท 1.3 ยท 1.4 ยท 1.5 ยท 1.6 ยท 1.7 ยท 1.8 ยท 1.9 ยท 1.10 ยท 1.11 ยท 1.12 ยท 1.13 ยท 1.14 ยท 1.15 ยท 1.16 ยท 1.17 ยท 2.0.

The ArrayFunctions extension creates a set of pure, Parsoid -compatible (see here) parser functions that perform operations on arrays.

Installation

[edit | edit source]
  • Download and move the extracted ArrayFunctions folder to your extensions/ directory.
    Developers and code contributors should install the extension from Git instead, using:
    cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/ArrayFunctions
    
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'ArrayFunctions' );
    // Increase $wgMaxArticleSize to allow for larger arrays (default: 2048)
    $wgMaxArticleSize = 8192;
    
  • Configure performance limits as necessary.
  • Yes Done โ€“ Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

TL;DR

[edit | edit source]

ArrayFunctions defines a collection of parser functions that allow you to work with immutable lists and objects, collectively "arrays". Instead of storing these arrays in memory, they are outputted by the parser function directly, and can be passed around to templates or to other parser functions. While it is possible to create and work with arrays entirely through wikitext, the recommended approach is to use Scribunto (Lua), Semantic MediaWiki or Cargo to create an array that contains all the necessary data, and use ArrayFunctions only for formatting the array. The #af_list and #af_object parser functions should primarily be used for simple, one-off arrays.

ArrayFunctions defines many (over 30) parser functions, but the most useful ones are:

  • #af_get โ€“ to retrieve a value, or perform some operation succincly;
  • #af_pipeline โ€“ to create a pipeline of operations to prevent deep nesting;
  • #af_foreach โ€“ to iterate over an array;
  • #af_show โ€“ to output a value stored in an array;
  • #af_print โ€“ to print an array for debug purposes;
  • #af_map โ€“ to modify all elements of an array;
  • #af_template โ€“ to invoke a template with data stored in an array;
  • #af_list โ€“ to create a list;
  • #af_object — to create an object;
  • #af_zip โ€“ to zip two or more arrays together.

Compatibility

[edit | edit source]

ArrayFunctions has a commitment to not breaking backwards-compatibility. This means that user-facing code (e.g. parser functions, Lua fuctions and magic words) will keep working indefinitely, and changes will always be implemented in a backwards-compatible manner.

ArrayFunctions also tries to keep compatibility with older versions of MediaWiki for as long as possible, but due to the nature of extensions, this may not be possible indefinitely. In case an update of MediaWiki breaks a feature that ArrayFunctions relies on, it will be patched in a backwards compatible manner if this is possible. Otherwise, a new MAJOR version of ArrayFunctions will be released.

Compatibility Matrix
ArrayFunctions MediaWiki PHP Scribunto1 Semantic MediaWiki1 Cargo1
2.0.0+
MediaWiki version:
1.40
PHP version
7.4.3
Scribunto version
1.40
Semantic MediaWiki version
5.0
Cargo version
3.8
1.15.0+
MediaWiki version:
1.35
PHP version
7.4.0
Scribunto version
1.35
Semantic MediaWiki version
4.0
Cargo version
3.0
1.14+
MediaWiki version:
1.35
PHP version
7.4.0
Scribunto version
1.35
Semantic MediaWiki version
4.0
N/A
1.0-1.13
MediaWiki versions:
1.35 – 1.43
PHP version
7.4.0
Scribunto version
1.35
N/A N/A
1 Soft dependency; version constraint only applicable when the software is installed.

Performance limits

[edit | edit source]
ArrayFunctions version
1.16
MediaWiki already limits the total size of arrays through $wgMaxArticleSize .

ArrayFunctions provides a number of configuration parameters to limit the computation time of certain functions and prevent users from performing a denial-of-service attack on your wiki. For backwards-compatibility reasons, these limits are off by default. The available configuration parameters are:

Name Default Recommended Description
$wgArrayFunctionsForeachIterationLimit -1 ~1024 This configuration parameter limits the maximum number of #af_foreach iterations a page is allowed to perform. Setting it to anything below zero will impose no restrictions on the number of iterations.
$wgArrayFunctionsMaxRangeSize -1 ~4096 This configuration parameter limits the maximum size of an array constructed with the #af_range parser function. Setting it to anything below zero will impose no restrictions on the size of arrays constructed with #af_range.
$wgArrayFunctionsMaxPipelineLength -1 ~$wgMaxTemplateDepth / 4 This configuration parameter limits the maximum length of a pipeline (#af_pipeline). Setting it to anything below zero will impose no restrictions on the number of steps in a pipeline.

These limits are reported in the parser limit report, which can be used to tweak the settings or debug a page.

How can I define an array to be used throughout a page?

[edit | edit source]

It is not possible to directly define an array to be used throughout a page, because this would require sequential processing of extension tags, which is not supported by Parsoid (see Extension API#No support for sequential, in-order processing of extension tags). Instead, you can pass arrays around as template parameters:

{{My template|{{#af_list:a|b|c}}}}

This way, the array is available in Template:My template as {{{1}}}.

How to iterate over an array?

[edit | edit source]

It is possible to iteratively access elements of an array using #af_foreach:

{{#af_foreach:{{#af_list:red|green|blue}}||color|<nowiki/>
* {{{color}}} is my favourite.
}}

The expected output from the snipped above is:

  • red is my favourite.
  • green is my favourite.
  • blue is my favourite.

Why are values not recognized as arrays?

[edit | edit source]

This may happen because your arrays are too large. MediaWiki internally keeps a counter on the total size of template arguments, which can be increased by increasing $wgMaxArticleSize.

How is this different from extensions such as Arrays or Variables?

[edit | edit source]

The main difference between ArrayFunctions and those extensions is that ArrayFunctions parser functions are pure. This means that instead of modifying or declaring a variable, the parser function directly outputs its result.

For example with #af_map, the given array is not modified; instead, a copy is created, modified and then outputted. No parser function in ArrayFunctions modifies global state: all computation happens solely with the invocation of the parser function. This makes working with ArrayFunctions very different from working with other extensions such as Arrays or Variables. Instead of imperatively modifying an array stored in a variable, function composition must be used to perform more complex operations. For example:

Arrays
{{#arraydefine: fruits | orange, banana, strawberry, apple }}
{{#arraysort: fruits | asc }}
{{#arrayprint: fruits }}
ArrayFunctions
{{#af_pipeline: {{#af_list: orange | banana | strawberry | apple }}
| {{#af_sort: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}

Why is whitespace trimmed from array values?

[edit | edit source]

Whitespace is inherently implicit in wikitext. In order to be consistent with this behaviour, whitespace is recursively trimmed from array values.

Functions

[edit | edit source]
  • Parameters prefixed with a single asterisk (*) denote that a variable number of positional arguments can be passed.
  • Parameters prefixed with a double asterisk (**) denote that a variable number of named arguments can be passed.
  • All keyword argument names are case-sensitive.
  • All string arguments support the following escape sequences:
\s for spaces
\n for newlines
\\ for backslashes

The extension defines the following parser functions, Lua functions, Semantic MediaWiki result formats, Cargo display formats and magic words:

Construct an array or value
Name Description
#af_bool Cast a string to a boolean.
#af_float Cast a string to a float.
#af_int Cast a string to an integer.
#af_list Create a new list from values.
#af_object Create a new object from values.
#af_range Create a finite range of integers.
#af_split Split a string based on a delimiter.
AF_EMPTY The empty array.
mw.af.export Create a new array from Lua.
arrayfunctions Format a Semantic MediaWiki query result as an ArrayFunctions array.
arrayfunctions Display a Cargo query result as an ArrayFunctions array.
Extract information from an array
Name Description
#af_count Count the number of values in an array.
#af_exists Check whether a key or index exists in an array.
#af_get Retrieve an element from an array by index.
#af_isarray Check if a value is an array.
#af_print Print an array for debug purposes.
#af_search Searches an array for a value.
#af_show Show a value in a human-readable format.
mw.af.import Create a new table from an ArrayFunctions array.
Create an array from an existing array
Name Description
#af_difference Compute the difference between arrays.
#af_filter Filter element from an array.
#af_flatten Flatten an array.
#af_group Compute an array that when indexed yields all elements of a key from all subarrays.
#af_intersect Compute the intersection of arrays.
#af_keysort Sort a list of objects based on the values of a key.
#af_ksort Sort an array by key.
#af_merge Compute the union of arrays.
#af_push Add a value to the end of a list.
#af_put Set a value at an index.
#af_reverse Reverse an array.
#af_set Set a value at an index (deprecated in 1.13.0).
#af_slice Extract a slice from an array.
#af_sort Sort a list.
#af_unique Remove duplicates from an array.
#af_unset Remove a value from an array by index.
#af_zip Zip two or more arrays together.
Iterate over an array
Name Description
#af_foreach Iterate over an array.
#af_join Recursively join the items of an array together with a separator.
#af_map Apply a callback to each element of a list.
#af_reduce Iteratively reduce the array to a single value using a callback.
Miscellaneous functions
Name Description
#af_if Select one of two alternatives based on a predicate.
#af_pipeline Create a pipeline of functions.
#af_stringmap Apply a callback to each value in a delimited string.
#af_template Invoke a template with the values in an array.
#af_trim Trim characters at the start and end of a string.

af_bool

[edit | edit source]
ArrayFunctions version
1.0

This parser function casts a string to a boolean. This is useful for creating an array containing a boolean. The values that are considered as true are "1", "true", "on", and "yes". The values that are considered as false are "0", "false", "off", and "no". If any other value is given, an error will be returned.

Description

[edit | edit source]
{{#af_bool: value }}

Parameters

[edit | edit source]
Name Type Description
value string or boolean The value to cast to a boolean.

Return values

[edit | edit source]

Returns the casted boolean.

Examples

[edit | edit source]
Create an opaque representation of a boolean
{{#af_bool: yes }}, {{#af_bool: no }}, {{#af_bool: true }}
boolean__^__1, boolean__^__0, boolean__^__1
Create an array containing a boolean
{{#af_print: {{#af_list: {{#af_bool: yes}} }} }}
  • 0: true

af_count

[edit | edit source]
ArrayFunctions version
1.0

This parser functions counts the number of values in an array. By default, this parser function only counts the number of top-level elements. However, if the keyword argument recursive is given a true value, it will also count the number of items in any sub-arrays. Note that in case of recursive=true, both the element containing an array, as well as all values inside the array are counted.

Description

[edit | edit source]
{{#af_count: array | recursive=recursive }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to count.
recursive boolean false Whether to count items recursively.

Return values

[edit | edit source]

The number of items in the array.

Examples

[edit | edit source]
Count the number of items in a one-dimensional list
{{#af_print: {{#af_count: {{#af_list: a | b | c }} }} }}
3
Count the number of items in a multi-dimensional list
{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }} }}
2
Recursively count the number of items in a multi-dimensional list
{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} | recursive=true }} }}
6

af_difference

[edit | edit source]
ArrayFunctions version
1.5
The difference between three arrays

This parser function computes the difference between arrays. This function preserves keys.

Description

[edit | edit source]
{{#af_difference: array | *arrays }}

Parameters

[edit | edit source]
Name Type Description
array array The first array.
*arrays array The other arrays.

Return values

[edit | edit source]

Returns an array containing all values from array that are not present in any of the arrays in arrays. The keys in array are preserved.

Examples

[edit | edit source]
Compute the difference of three arrays
{{#af_print: {{#af_difference: {{#af_list: a | b | c }} | {{#af_list: a }} | {{#af_list: b}} }} }}
  • 2: c

af_exists

[edit | edit source]
ArrayFunctions version
1.0

This parser function checks whether the given key or index exists in the given array. Multiple keys can be given to check nested arrays.

Description

[edit | edit source]
{{#af_exists: array | *keys }}

Parameters

[edit | edit source]
Name Type Description
array array The array to check.
keys string or int The key to check. Multiple keys can be given to check if a nested key exists.

Return values

[edit | edit source]

Returns true if array contains the key, false otherwise.

Examples

[edit | edit source]
Check if a key exists
{{#af_print: {{#af_exists: {{#af_object: hello=world }} | hello }} }}
true
Check if an index exists
{{#af_print: {{#af_exists: {{#af_list: a | b | c }} | 2 }} }}
true
Check if a nested key exists
{{#af_print: {{#af_exists: {{#af_list: a | {{#af_list: b | c }} }} | 0 | 3 }} }}
false

af_filter

[edit | edit source]
ArrayFunctions version
2.0

This parser function filters elements from an array based on a condition. The condition is evaluated for each element in the array, and if the condition returns a "falsy" value, the element will be removed. Otherwise, the element will be kept. The values that are considered "falsy" are:

  • The empty string;
  • The boolean false;
  • The strings "no", "off", "false" and "0".

If no condition is given, all empty elements are removed. Indices are not reset after calling this function.

Description

[edit | edit source]
{{#af_filter: array | value_name | condition }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to filter.
value_name string null The name to give to the value in the condition.
condition string null The condition to filter on.

Return values

[edit | edit source]

Returns the array with all values for which the condition returned a falsy value removed.

Examples

[edit | edit source]
Remove all empty values
{{#af_print: {{#af_filter: {{#af_list: a | | c }} }} }}
  • 0: a
  • 2: c
Remove the element "b"
{{#af_print: {{#af_filter: {{#af_list: a | b | c }} | v | {{#ifeq: {{{v}}} | b | yes }} }} }}
  • 0: a
  • 2: c

af_flatten

[edit | edit source]
ArrayFunctions version
1.11

This parser function creates a new array with all sub-array elements concatenated into it recursively up to the specified depth, or until it is completely flattened if no depth is specified.

Description

[edit | edit source]
{{#af_flatten: array | depth }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to flatten.
depth integer null The depth to flatten to, or nothing to flatten until the array is completely flat.

Return values

[edit | edit source]

Returns an array with all sub-array elements concatenated into it recursively up to the specified depth, or a completely flattened array if no depth is specified.

Examples

[edit | edit source]
Flatten an array one level deep
{{#af_print: {{#af_flatten: {{#af_list: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }} }}
  • 0: a
  • 1: b
  • 2: c
  • 3: d
  • 4: e
  • 5: f

af_float

[edit | edit source]
ArrayFunctions version
1.0

This parser function casts a string to a float. This is useful for creating an array containing a float.

Description

[edit | edit source]
{{#af_float: value }}

Parameters

[edit | edit source]
Name Type Description
value string or float The value to cast to a float.

Return values

[edit | edit source]

Returns the casted float.

Examples

[edit | edit source]
Create an opaque representation of a float
{{#af_float: 1.298 }}, {{#af_float: 0 }}
float__^__1.298, float__^__0
Create an array containing a float
{{#af_print: {{#af_list: {{#af_float: 1.298 }} }} }}
  • 0: 1.298

af_foreach

[edit | edit source]
ArrayFunctions version
1.0

This parser function provides a way to iterate over arrays. For each of the elements in the given input array, it will evaluate its body, replacing any occurences of the key_name and value_name variables. These variables can be accessed in the same manner as template parameters, by surrounding them with triple braces (e.g. {{{value_name}}}).

The behaviour of this parser function might be influenced by the $wgArrayFunctionsForeachIterationLimit configuration parameter.

Description

[edit | edit source]
{{#af_foreach: array | key_name | value_name | body | delimiter=delimiter }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array over which to iterate.
key_name string null The name to use for the key.
value_name string null The name to use for the value.
body string The body to return for each iteration.
delimiter string "" The delimiter to put between results (available since version 1.9.0).

Return values

[edit | edit source]

Returns the resulting wikitext.

Examples

[edit | edit source]
Iterate over a list
{{#af_foreach: {{#af_list: John | Steve | Harry }} | | name | Hello, {{{name}}}!<br/> }}
Hello, John!
Hello, Steve!
Hello, Harry!
Iterate over an object
{{#af_foreach: {{#af_object: Hello=John | Hi=Steve | Welcome=Harry }} | greeting | name | {{{greeting}}}, {{{name}}}!<br/> }}
Hello, John!
Hi, Steve!
Welcome, Harry!
Iterate over a list and add a delimiter
{{#af_foreach: {{#af_list: John | Steve | Harry }} | | name | Hello, {{{name}}}! | delimiter=<br/> }}
Hello, John!
Hello, Steve!
Hello, Harry!

af_get

[edit | edit source]
ArrayFunctions version
1.0

This parser function retrieves the element with the given index from the given array, or performs some operation if the index is overloaded. If the index does not exist and it is not an overloaded index, the empty string is returned.

An overloaded index allows the user to succinctly perform certain simple operations on arrays during indexing. Using an overloaded index is identical to first performing an #af_get with all indices before the overloaded index, then performing the function specified by the overloaded index, and then performing another #af_get with the remaining indices. For example, {{#af_get: {{{1}}} | a | * | b }} is equivalent to {{#af_get: {{#af_group: {{#af_get: {{{1}}} | a }} }} | b }}.

If the array contains a key that clashes with the symbol for an overloaded index, the value under the key will be retrieved instead. This was done for backwards compatibility reasons.

Description

[edit | edit source]
{{#af_get: array | *indices }}

Parameters

[edit | edit source]
Name Type Description
array array The array in which to index.
*indices string The index. Multiple indices can be given to index nested arrays.
Overloaded indices (available since version 1.11.0)
[edit | edit source]
Syntax Equivalent function Description
* #af_group Group subarrays together by key.
<- #af_reverse Reverse the array.
>< #af_flatten Flatten the array once.
>><< #af_flatten Flatten the array until it is completely flat.
# #af_unique Remove duplicates from the array.
n..m #af_slice Retrieve a slice of the array (either n or m can be omitted to index from the start up to the end of the array respectively).
! (available since version 1.16.0) #af_show Show the value in a human-readable way using format table, simple.
// (available since version 2.0.0) #af_filter Remove all empty elements from the array.

Return values

[edit | edit source]

Returns the indexed value, or the empty string if the index does not exist.

Examples

[edit | edit source]
Get a top-level element
{{#af_get: {{#af_list: a | b | c }} | 1 }}
b
Get a subarray
{{#af_print: {{#af_get: {{#af_list: a | {{#af_list: b | c }} }} | 1 }} }}
  • 0: b
  • 1: c
Get a nested element
{{#af_get: {{#af_list: a | {{#af_object: hello=world }} }} | 1 | hello }}
world
Reverse the array
{{#af_print: {{#af_get: {{#af_list: a | b | c }} | <- }} }}
  • 0: c
  • 1: b
  • 2: a
Retrieve the last element of the array
{{#af_print: {{#af_get: {{#af_list: a | b | c }} | <- | 0 }} }}
c

af_group

[edit | edit source]
ArrayFunctions version
1.11

This parser function computes an array where subarrays are recursively grouped together by key.

Description

[edit | edit source]
{{#af_group: array }}

Parameters

[edit | edit source]
Name Type Description
array array The array for which to group subarrays together.

Return values

[edit | edit source]

Returns an array where subarrays are recursively grouped together by key.

Examples

[edit | edit source]
Group subarrays by key
{{#af_print: {{#af_group: {{#af_list: {{#af_object: a=1 | b=2 }} | {{#af_object: a=3 | b=4 }} | {{#af_object: a=5 | b=5 }} }} }} }}
  • a
    • 0: 1
    • 1: 3
    • 2: 5
  • b
    • 0: 2
    • 1: 4
    • 2: 5

af_if

[edit | edit source]
ArrayFunctions version
1.0

This parser function selects one of two alternatives based on the given predicate.

Description

[edit | edit source]
{{#af_if: predicate | consequent | alternative }}

Parameters

[edit | edit source]
Name Type Default Description
predicate boolean The predicate.
consequent string The value to return if the predicate holds (i.e. is true).
alternative string "" The value to return if the predicate does not hold (i.e. is false).

Return values

[edit | edit source]

Returns the consequent if the predicate holds, or the alternative if it is given and the predicate does not hold.

Examples

[edit | edit source]
Check if a value is an array
{{#af_if: {{#af_isarray: not an array }} | A beautiful array! | Not an array! }}
Not an array!

af_int

[edit | edit source]
ArrayFunctions version
1.0

This parser function casts a string to an integer. This is useful for creating an array containing an integer.

Description

[edit | edit source]
{{#af_int: value }}

Parameters

[edit | edit source]
Name Type Description
value string or int The value to cast to an integer.

Return values

[edit | edit source]

Returns the casted integer.

Examples

[edit | edit source]
Create an opaque representation of an integer
{{#af_int: 42 }}, {{#af_int: -12 }}
integer__^__42, integer__^__-12
Create an array containing an integer
{{#af_print: {{#af_list: {{#af_int: -129}} }} }}
  • 0: -129

af_intersect

[edit | edit source]
ArrayFunctions version
1.2
The intersection of three arrays

This parser function computes the intersection of arrays. This function preserves keys.

Description

[edit | edit source]
{{#af_intersect: array | *arrays }}

Parameters

[edit | edit source]
Name Type Description
array array The first array.
*arrays array The other arrays.

Return values

[edit | edit source]

Returns the intersection of the given arrays.

Examples

[edit | edit source]
Compute the intersection of two identical arrays
{{#af_print: {{#af_intersect: {{#af_list: a | b | c }} | {{#af_list: a | b | c }} }} }}
  • 0: a
  • 1: b
  • 2: c
Compute the intersection of two partially overlapping arrays
{{#af_print: {{#af_intersect: {{#af_list: a | b | c }} | {{#af_list: c | d | e }} }} }}
  • 2: c

af_isarray

[edit | edit source]
ArrayFunctions version
1.0

This parser function checks if the given value is an array.

Description

[edit | edit source]
{{#af_isarray: value }}

Parameters

[edit | edit source]
Name Type Description
value mixed The value to check.

Return values

[edit | edit source]

Returns true if value is an array, false otherwise.

Examples

[edit | edit source]
Check if an array is an array
{{#af_print: {{#af_isarray: {{#af_list: a | b | c }} }} }}
true
Check if a string is an array
{{#af_print: {{#af_isarray: Hello, World! }} }}
false

af_join

[edit | edit source]
ArrayFunctions version
1.0

This parser function recursively joins the items of an array together with a given separator.

Description

[edit | edit source]
{{#af_join: array | glue }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to join.
glue string "" The string used to join each item.

Return values

[edit | edit source]

Returns the joined array.

Examples

[edit | edit source]
Join a one-dimensional array
{{#af_join: {{#af_list: a | b | c }} }}
abc
Join a one-dimensional array using a separator
{{#af_join: {{#af_list: a | b | c }} | \s-\s }}
a - b - c
Join a multi-dimensional array using a separator
{{#af_join: {{#af_list: a | b | {{#af_list: c | d }} }} | \s-\s }}
a - b - c - d

af_keysort

[edit | edit source]
ArrayFunctions version
1.0

This parser function sorts a list of objects based on the values of the specified key. To sort an array by key, use #af_ksort.

Description

[edit | edit source]
{{#af_keysort: array | key | descending=descending | caseinsensitive=caseinsensitive }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to sort.
key string The key of the values on which the sort should be based.
descending boolean false Whether to sort in a descending order.
caseinsensitive boolean false Whether to ignore case when sorting (available since version 1.7.0).

Return values

[edit | edit source]

Returns the sorted array.

Examples

[edit | edit source]
Sort based on age
{{#af_print: {{#af_keysort: {{#af_list:
    {{#af_object: name=John | age=56 }} |
    {{#af_object: name=Harry | age=12 }} |
    {{#af_object: name=Bob | age=24 }}
}} | age }} }}
  • 0
    • name: Harry
    • age: 12
  • 1
    • name: Bob
    • age: 24
  • 2
    • name: John
    • age: 56
Sort based on age, in descending order
{{#af_print: {{#af_keysort: {{#af_list:
    {{#af_object: name=John | age=56 }} |
    {{#af_object: name=Harry | age=12 }} |
    {{#af_object: name=Bob | age=24 }}
}} | age | descending=true }} }}
  • 0
    • name: John
    • age: 56
  • 1
    • name: Bob
    • age: 24
  • 2
    • name: Harry
    • age: 12

af_ksort

[edit | edit source]
ArrayFunctions version
1.7

This parser function sorts an array by key.

Description

[edit | edit source]
{{#af_ksort: array | descending=descending | caseinsensitive=caseinsensitive }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to sort.
descending boolean false Whether to sort in a descending order.
caseinsensitive boolean false Whether to ignore case when sorting (available since version 1.7.0).

Return values

[edit | edit source]

Returns the sorted array.

Examples

[edit | edit source]
Sort by key
{{#af_print: {{#af_ksort: {{#af_object: c=banana | a=orange | b=apple }} }} }}
  • a: orange
  • b: apple
  • c: banana

af_list

[edit | edit source]
ArrayFunctions version
1.0

This parser function creates a new list from the given parameters.

Description

[edit | edit source]
{{#af_list: *values }}

Parameters

[edit | edit source]
*values : mixed
The values for the list.

Return values

[edit | edit source]

Returns the resulting list.

Examples

[edit | edit source]
Create a simple one-dimensional list
{{#af_print: {{#af_list: a | b | c }} }}
  • 0: a
  • 1: b
  • 2: c
Create a multi-dimensional list
{{#af_print: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }}
  • 0
    • 0: a
    • 1: b
  • 1
    • 0: c
    • 1: d
Create a list of objects
{{#af_print: {{#af_list:
    {{#af_object: name=Harry | age=22 }} |
    {{#af_object: name=Bobby | age=29 }}
}} }}
  • 0
    • name: Harry
    • age: 22
  • 1
    • name: Bobby
    • age: 29

af_map

[edit | edit source]
ArrayFunctions version
1.0

This parser function applies a callback to each element of a list.

Description

[edit | edit source]
{{#af_map: array | value_name | callback }}

Parameters

[edit | edit source]
array : array
The array to run through the callback.
value_name : string
The name to give to the value in the callback.
callback : string
The callback to apply to each element of the array.

Return values

[edit | edit source]

Returns the resulting mapped array.

Examples

[edit | edit source]
Appending a string to each element
{{#af_print: {{#af_map: {{#af_list: a | b | c }} | v | {{{v}}}-appended }} }}
  • 0: a-appended
  • 1: b-appended
  • 2: c-appended
Altering list elements
{{#af_print: {{#af_map: {{#af_list: {{#af_list: a }} | {{#af_list: b }} }} | v | {{#af_push: {{{v}}} | c }} }} }}
  • 0
    • 0: a
    • 1: c
  • 1
    • 0: b
    • 1: c

af_merge

[edit | edit source]
ArrayFunctions version
1.2

This parser function computes the merges two or more arrays. It merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It does not remove duplicates.

If the input arrays have the same string key, then the later value for that key will overwrite the previous one. If the arrays contain numeric keys, later values will be appended instead and the later keys will be renumbered.

Description

[edit | edit source]
{{#af_merge: array | *arrays }}

Parameters

[edit | edit source]
array : array
The first array.
*arrays : array
The other arrays.

Return values

[edit | edit source]

Returns the union of the given arrays.

Examples

[edit | edit source]
Appending a string to each element
{{#af_print: {{#af_merge: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }}
  • 0: a
  • 1: b
  • 2: c
  • 3: d
  • 4: e
  • 5: f

af_object

[edit | edit source]
ArrayFunctions version
1.0

This parser function creates a new object from the given parameters.

Description

[edit | edit source]
{{#af_object: **values }}

Parameters

[edit | edit source]
**values : mixed
The values for the object.

Return values

[edit | edit source]

Returns the resulting object.

Examples

[edit | edit source]
Create a simple one-dimensional object
{{#af_print: {{#af_object: a=b | b=c | c=d }} }}
  • a: b
  • b: c
  • c: d
Create a multi-dimensional object
{{#af_print: {{#af_object: head={{#af_object: title=MediaWiki | meta={{#af_list: {{#af_object: charset=UTF-8 }} }} }} }} }}
  • head
    • title: MediaWiki
    • meta
      • 0
        • charset: UTF-8

af_pipeline

[edit | edit source]
ArrayFunctions version
1.14

This parser function creates a pipeline of operations. The parser function takes the result from expanding the previous argument, and passes that as the specified parameter (or prev by default) to the next argument of the parser function. This is very useful to improve the readability of the template code, as it allows you to remove deeply nested parser function calls. For example, the following two snippets are semantically equivalent:

Before After
{{#af_print: {{#af_unique: {{#af_reverse: {{#af_list: a | a | b | c}} }} }} }}
{{#af_pipeline: {{#af_list: a | a | b | c }}
| {{#af_reverse: {{{prev}}} }}
| {{#af_unique: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}

The behaviour of this parser function might be influenced by the $wgArrayFunctionsMaxPipelineLength configuration parameter.

Description

[edit | edit source]
{{#af_pipeline: initial | *steps | parameter=parameter }}

Parameters

[edit | edit source]
Name Type Default Description
initial mixed The initial value of the pipeline.
*steps mixed The subsequent steps of the pipeline.
parameter string "prev" The name to use for the previous value.

Return values

[edit | edit source]

Returns the value returned by the final step of the pipeline.

Examples

[edit | edit source]
Create a pipeline
{{#af_pipeline: {{#af_list: a | a | b | c }}
| {{#af_reverse: {{{prev}}} }}
| {{#af_unique: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}
  • 0: c
  • 1: b
  • 2: a

af_print

[edit | edit source]
ArrayFunctions version
1.0

This parser function prints the given value for debug purposes. Unlike #af_show, it does not parse the value. This function should only be used for debug purposes, consider using #af_show to display a value to the reader.

Description

[edit | edit source]
{{#af_print: *values | end=end }}

Parameters

[edit | edit source]
Name Type Default Description
*values mixed The values to print.
end string "" The string to append to the end of each printed value.

Return values

[edit | edit source]

Returns the value in human-readable form.

Examples

[edit | edit source]
Print a boolean
{{#af_print: {{#af_bool: yes }} }}
true
Print a list
{{#af_print: {{#af_list: a | b | c }} }}
  • 0: a
  • 1: b
  • 2: c

af_push

[edit | edit source]
ArrayFunctions version
1.0

This parser function adds the given value to the end of the list.

Description

[edit | edit source]
{{#af_push: array | value }}

Parameters

[edit | edit source]
array : array
The array to append the value to.
value : mixed
The value to add.

Return values

[edit | edit source]

Returns the array with the value appended.

Examples

[edit | edit source]
Push a value
{{#af_print: {{#af_push: {{#af_list: a | b }} | c }} }}
  • 0: a
  • 1: b
  • 2: c

af_put

[edit | edit source]
ArrayFunctions version
1.13

This parser function sets the given value for the given index. It is identical to #af_set, except for the parameter order. This parser function should be preferred over #af_set.

Description

[edit | edit source]
{{#af_put: array | value | *indices }}

Parameters

[edit | edit source]
array : array
To array in which to set the index.
value : mixed
The value to set the index to.
*indices : string
The index to set. Multiple indices can be given to index nested arrays.

Return values

[edit | edit source]

Returns the array with the given index set to the given value.

Examples

[edit | edit source]
Replace an existing value
{{#af_print: {{#af_put: {{#af_list: a | b | c }} | d | 2 }} }}
  • 0: a
  • 1: b
  • 2: d
Create a new index
{{#af_print: {{#af_set: {{#af_object: foo=bar }} | far | boo }} }}
  • foo: bar
  • boo: far
Create a new subarray
{{#af_print: {{#af_set: {{#af_object: foo=bar }} | far | boo | far }} }}
  • foo: bar
  • boo
    • far: far

af_range

[edit | edit source]
ArrayFunctions version
1.12

This parser function creates a finite range of integers. The ith element of a range is determined by the function r(i) = start + step * i. For a positive step, the bounds of the range are i >= 0 and r(i) < stop. For a negative step, the bounds are i >= 0 and r(i) > stop.

The behaviour of this parser function might be influenced by the $wgArrayFunctionsMaxRangeSize configuration parameter.

Description

[edit | edit source]
{{#af_range: start | stop | step }}

Parameters

[edit | edit source]
Name Type Default Description
start integer The start of the range.
stop integer null The end of the range (non-inclusive). If this value is omitted, the start will become the stop, and start will default to zero.
step integer 1 The step size between range values.

Return values

[edit | edit source]

A finite range of integers.

Examples

[edit | edit source]
Create a range from 0 to 4
{{#af_print: {{#af_range: 0 | 5 }} }}
  • 0: 0
  • 1: 1
  • 2: 2
  • 3: 3
  • 4: 4
Alternative syntax for a range from 0 to 4
{{#af_print: {{#af_range: 5 }} }}
  • 0: 0
  • 1: 1
  • 2: 2
  • 3: 3
  • 4: 4
Create a range from 0 to -4
{{#af_print: {{#af_range: 0 | -5 | -1 }} }}
  • 0: 0
  • 1: -1
  • 2: -2
  • 3: -3
  • 4: -4
Create a range of all odd numbers between 0 to 10
{{#af_print: {{#af_range: 1 | 10 | 2 }} }}
  • 0: 0
  • 1: 3
  • 2: 5
  • 3: 7
  • 4: 9

af_reduce

[edit | edit source]
ArrayFunctions version
1.2

This parser function iteratively reduces the array to a single value using a callback. It iteratively applies callable to the elements of the given array, so as to reduce the array to a single value. The callable is passed the value of the current iteration, as well as the result of the previous iteration.

Description

[edit | edit source]
{{#af_reduce: array | carry_name | value_name | callable | initial }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to reduce.
carry_name string The name to use for the carry.
value_name string The name to use for the value.
callable string The callback to use for each iteration.
initial string "" The initial carry to use.

Return values

[edit | edit source]

Returns the resulting value.

Examples

[edit | edit source]
Using reduction to concatenate values
{{#af_reduce: {{#af_list: a | b | c }} | c | i | {{{c}}}{{{i}}} }}
abc
Using reduction to reverse and then concatenate values
{{#af_reduce: {{#af_list: a | b | c }} | c | i | {{{i}}}{{{c}}} }}
cba
Using reduction to build an equation
{{#af_reduce: {{#af_list: 2 | 3 | 5 | 7 | 11 }} | c | i | {{{c}}} + {{{i}}} | 0 }}
0 + 2 + 3 + 5 + 7 + 11

af_reverse

[edit | edit source]
ArrayFunctions version
1.11

This parser function reverses the given array. It does not preserve numeric keys.

Description

[edit | edit source]
{{#af_reverse: array }}

Parameters

[edit | edit source]
array : array
The array to reverse.

Return values

[edit | edit source]

Returns the original array reversed.

Examples

[edit | edit source]
Reverse a list
{{#af_print: {{#af_reverse: {{#af_list: a | b | c}} }} }}
  • 0: c
  • 1: b
  • 2: a
[edit | edit source]
ArrayFunctions version
1.3

This parser function searches the given array for the given value, and returns the first corresponding key if the value is found.

Description

[edit | edit source]
{{#af_search: array | value }}

Parameters

[edit | edit source]
array : array
The array to search in.
value : mixed
The value to search for.

Return values

[edit | edit source]

Returns the first corresponding key if the value is found, nothing otherwise.

Examples

[edit | edit source]
Search for a value in an array
{{#af_print: {{#af_search: {{#af_list: a | b | c }} | b }} }}
1

af_set

[edit | edit source]
ArrayFunctions version
1.0

(deprecated in 1.13.0)

This parser function sets the given value for the given index. It has been deprecated in ArrayFunctions 1.13.0, and should no longer be used. It will remain available indefinitely for backwards compatibility. As an alternative, you should use #af_put instead.

Description

[edit | edit source]
{{#af_set: value | array | *indices }}

Parameters

[edit | edit source]
value : mixed
The value to set the index to.
array : array
To array in which to set the index.
*indices : string
The index to set. Multiple indices can be given to index nested arrays.

Return values

[edit | edit source]

Returns the array with the given index set to the given value.

Examples

[edit | edit source]
Replace an existing value
{{#af_print: {{#af_set: d | {{#af_list: a | b | c }} | 2 }} }}
  • 0: a
  • 1: b
  • 2: d
Create a new index
{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo }} }}
  • foo: bar
  • boo: far
Create a new subarray
{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo | far }} }}
  • foo: bar
  • boo
    • far: far

af_show

[edit | edit source]
ArrayFunctions version
1.4

This parser function prints the given value in a human-readable format. The formatting of the value is based on format.

Description

[edit | edit source]
{{#af_show: value | format=format }}

Parameters

[edit | edit source]
Name Type Default Description
value mixed The value to show.
format string "simple" (introduced in 1.16.0) A comma-separated list of formats. The value will be shown using the first format supporting the type of the value.
Formats (available since version 1.16.0)
[edit | edit source]
Name Supported types Description
simple string, integer, float, boolean Format the value as its string representation. This is the default format.
table array Format the value as a table.

Return values

[edit | edit source]

Returns the value in human-readable form, formatted using format.

Examples

[edit | edit source]
Show a value
{{#af_show: Hello World! }}
Hello World!
Show an integer
{{#af_show: {{#af_int: 42 }} | format=simple }}
42

af_slice

[edit | edit source]
ArrayFunctions version
1.0

This parser function extracts a slice from the given array. Keys will be reset and reordered.

Description

[edit | edit source]
{{#af_slice: array | offset | length }}

Parameters

[edit | edit source]
Name Type Required? Description
array array Required The array to take a slice from.
offset integer Required The offset at which the slice starts. If non-negative, the slice will start at this given offset. If negative, the sequence will start that far from the end of the array.
length integer Optional The length of the slice. If the length is given and positive, then the slice will have that many elements in it. If the length is given and negative, then the slice will stop that many elements from the end of the array. If it is omitted, then the slice will have everything from offset up until the end of the array.

Return values

[edit | edit source]

The slice.

Examples

[edit | edit source]
Get the first two elements
{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | 0 | 2 }} }}
  • 0: a
  • 1: b
Get the last element
{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | -1 }} }}
  • 0: c

af_sort

[edit | edit source]
ArrayFunctions version
1.0

This parser function sorts the given list. It does not preserve numeric keys.

Description

[edit | edit source]
{{#af_sort: array | descending=descending | caseinsensitive=caseinsensitive }}

Parameters

[edit | edit source]
Name Type Default Description
array array The array to sort.
descending boolean false Whether to sort the list in descending order.
caseinsensitive boolean false Whether to ignore case when sorting.

Return values

[edit | edit source]

Returns the sorted list.

Examples

[edit | edit source]
Sort a list in ascending order
{{#af_print: {{#af_sort: {{#af_list: b | c | a }} }} }}
  • 0: a
  • 1: b
  • 2: c
Sort a list in descending order
{{#af_print: {{#af_sort: {{#af_list: b | c | a }} | descending=true }} }}
  • 0: c
  • 1: b
  • 2: a

af_split

[edit | edit source]
ArrayFunctions version
1.1

This parser function splits the given string based on a delimiter.

Description

[edit | edit source]
{{#af_split: string | delimiter }}

Parameters

[edit | edit source]
Name Type Default Description
string string The string to split.
delimiter string "," The delimiter to use.

Return values

[edit | edit source]

Returns the resulting list.

Examples

[edit | edit source]
Split a string based on commas
{{#af_print: {{#af_split: a, b, c }} }}
  • 0: a
  • 1: b
  • 2: c
Split a sentence into words
{{#af_print: {{#af_split: Lorem ipsum dolor et | \s }} }}
  • 0: Lorem
  • 1: ipsum
  • 2: dolor
  • 3: et

af_stringmap

[edit | edit source]
ArrayFunctions version
1.6

This parser function applies a callback to each item of a delimited string, and returns the result as a delimited string, optionally with a different delimiter. This function is similar to Page Forms' #arraymap parser function.

Description

[edit | edit source]
{{#af_stringmap: value | delimiter | value_name | callback | new_delimiter | conjunction }}

Parameters

[edit | edit source]
Name Type Default Description
value string The delimited string.
delimiter string The delimiter to split value on. If the empty string is given, "," is used.
value_name string The name to give to the value in the callback.
callback string The callback to apply to each element of the array.
new_delimiter string ", " The new delimiter to insert in between the mapped items.
conjunction string null The delimiter to place between the last two items. This allows you to create a more natural summation, such as "Alice, Bob and Eve". If no value is given, the value of new_delimiter is used.

Return values

[edit | edit source]

Returns the resulting delimited string.

Examples

[edit | edit source]
Turn each item into a link
{{#af_stringmap: William Shakespeare, Stephen King, Mark Twain | , | x | [[{{{x}}}]] }}
William Shakespeare, Stephen King, Mark Twain
Turn a comma-separated list into a human-readable list
{{#af_stringmap: William Shakespeare, Stephen King, Mark Twain | , | x | {{{x}}} | ,\s | and }}
William Shakespeare, Stephen King and Mark Twain

af_template

[edit | edit source]
ArrayFunctions version
1.0

This parser function will invoke the given template and pass the values in the given array as arguments. In case the given array is an object, the values will be passed as named arguments. Otherwise, they will be passed as numeric arguments.

Description

[edit | edit source]
{{#af_template: name | data }}

Parameters

[edit | edit source]
name : string
The name of the template to invoke. If no namespace is given, it is assumed the page is in the template namespace, otherwise the given namespace is used. The page must exist, must be includable and must be readable by the user, otherwise an error is given.
data : array
The data to pass to the parameters. Values with numeric indices are passed as positional arguments and values with string indices are passed as named arguments.

Return values

[edit | edit source]

The expanded template.

Examples

[edit | edit source]
Invoking a template with a list
{{#af_template: Echo | {{#af_list: a | b }} }}
{{Echo|a|b}}
Invoking a template with an object
{{#af_template: Echo | {{#af_object: foo=bar | boo=far }} }}
{{Echo|foo=bar|boo=far}}

af_trim

[edit | edit source]
ArrayFunctions version
1.8

This parser function will trim the given characters from the beginning and the end of the given string. The order of the characters does not matter. It will stop once it hits a character that is not in characters.

Description

[edit | edit source]
{{#af_trim: string | characters }}

Parameters

[edit | edit source]
string : string
The string that will be trimmed.
characters : string
The characters to trim.

Return values

[edit | edit source]

The trimmed string.

Examples

[edit | edit source]
Trimming a string
{{#af_trim: !a! | ! }}
a
Trimming every string in a list
{{#af_print: {{#af_map: {{#af_list: !a! | !b! | !c! }} | v | {{#af_trim: {{{v}}} | ! }} }} }}
  • 0: a
  • 1: b
  • 2: c

af_unique

[edit | edit source]
ArrayFunctions version
1.0

This parser function removes duplicate values from the given array. This function does not reset keys.

Description

[edit | edit source]
{{#af_unique: array }}

Parameters

[edit | edit source]
array : array
The array from which to remove duplicates.

Return values

[edit | edit source]

Returns the array with duplicates removed.

Examples

[edit | edit source]
Remove duplicates from an array
{{#af_print: {{#af_unique: {{#af_list: a | a | b | c | b }} }} }}
  • 0: a
  • 2: b
  • 3: c

af_unset

[edit | edit source]
ArrayFunctions version
1.0

This parser function removes the value associated with the given index from the array and returns the result. Numeric keys are not reset after calling this function.

Description

[edit | edit source]
{{#af_unset: array | *indices }}

Parameters

[edit | edit source]
array : array
The array from which to remove the given key.
*indices : string
The index to remove. Multiple indices can be given to index nested arrays.

Return values

[edit | edit source]

Returns the array with the given index removed.

Examples

[edit | edit source]
Remove a top-level index
{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 2 }} }}
  • 0: a
  • 1: b
Remove a top-level index, keys not reset
{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 1 }} }}
  • 0: a
  • 2: c
Remove a nested index
{{#af_print: {{#af_unset: {{#af_object: foo={{#af_object: bar=quz | far=buz }} }} | foo | bar }} }}
  • foo
    • far: buz

af_zip

[edit | edit source]
ArrayFunctions version
1.16

This parser functions takes one or more arrays and zips these together by key, creating an array where each key maps to an array containing all values under that key in each of the input arrays. The final array will only contain keys that were present in all of the input arrays. This means that if an input array contains a key not present in one of the other input arrays, the key will be dropped, and it will not be part of the final zipped array.

Description

[edit | edit source]
{{#af_zip: array | *arrays }}

Parameters

[edit | edit source]
array : array
The first array.
*arrays : array
The other arrays.

Return values

[edit | edit source]

Returns the arrays zipped together.

Examples

[edit | edit source]
Zip two lists of equal length
{{#af_print: {{#af_zip: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }}
  • 0
    • 0: a
    • 1: d
  • 1
    • 0: b
    • 1: e
  • 2
    • 0: c
    • 1: f
Zip two lists of different lengths
{{#af_print: {{#af_zip: {{#af_list: a | b }} | {{#af_list: d | e | f }} }} }}
  • 0
    • 0: a
    • 1: d
  • 1
    • 0: b
    • 1: e
Zip two objects with the same keys
{{#af_print: {{#af_zip: {{#af_object: a=a | b=b }} | {{#af_object: a=c | b=d }} }} }}
  • a
    • 0: a
    • 1: c
  • b
    • 0: b
    • 1: d
Zip two objects with different keys
{{#af_print: {{#af_zip: {{#af_object: a=a | b=b }} | {{#af_object: a=c | different=d }} }} }}
  • a
    • 0: a
    • 1: c

Scribunto

[edit | edit source]
It is recommended to use LuaSandbox , because the order of arrays is not preserved when using LuaStandalone (see T349590).

This extension is particularly useful in combination with Lua, as it can be used to create the array containing data required for the presentation of the page. This array can be exported to work with ArrayFunctions:

local p = {};

function p.world()
	return mw.af.export({
		["Hello"] = "World"
	});
end

return p;

This module may then be invoked like so:

{{#af_print: {{#invoke: Hello | world }} }}
  • Hello: World

mw.af.export

[edit | edit source]
ArrayFunctions version
1.0

This Lua function exports a Lua table as an ArrayFunctions array.

Description

[edit | edit source]
mw.af.export( table )

Parameters

[edit | edit source]
table : array
The array to export.

Return values

[edit | edit source]

Returns the table as an ArrayFunctions array.

mw.af.import

[edit | edit source]
ArrayFunctions version
1.6

This Lua function imports an ArrayFunctions array as a Lua table.

Description

[edit | edit source]
mw.af.import( array )

Parameters

[edit | edit source]
array : array
The array to import.

Return values

[edit | edit source]

Returns the array as a Lua table.

Semantic MediaWiki

[edit | edit source]

This extension integrates with Semantic MediaWiki by adding the arrayfunctions result format. The arrayfunctions result format is used to format query results as an ArrayFunctions array. The result is almost identical to the result of Semantic Scribunto's mw.smw.ask Lua function, except numeric indices start at zero.

arrayfunctions

[edit | edit source]
ArrayFunctions version
1.14

This result format formats query results as an ArrayFunctions array.

Description

[edit | edit source]
{{#ask: query | format=arrayfunctions }}

Return values

[edit | edit source]

Returns the query result as an ArrayFunctions array.

Examples

[edit | edit source]
Simple query
{{#af_print: {{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#kmยฒ=Size
 |mainlabel=City
 |sort=Population
 |order=descending
 |headers=plain
 |format=arrayfunctions
}} }}
  • 0
    • City: Berlin
    • Population: 3520061
    • Size: 891.85 kmยฒ
  • 1
    • City: Munich
    • Population: 1353186
    • Size: 310.43 kmยฒ
  • 2
    • City: Cologne
    • Population: 1080394
    • Size: 405.02 kmยฒ
  • 3
    • City: Frankfurt
    • Population: 679664
    • Size: 248.31 kmยฒ
  • 4
    • City: Stuttgart
    • Population: 606588
    • Size: 207.35 kmยฒ
  • 5
    • City: Wรผrzburg
    • Population: 126635
    • Size: 87.63 kmยฒ

Cargo

[edit | edit source]

This extension integrates with Cargo by adding the arrayfunctions display format. The arrayfunctions result format is used to format query results as an ArrayFunctions array.

arrayfunctions

[edit | edit source]
ArrayFunctions version
1.15

This display format formats query results as an ArrayFunctions array.

Description

[edit | edit source]
The no html parameter is required for ArrayFunctions to interpret the result correctly.
{{#cargo_query: parameters | no html | format=arrayfunctions }}

Return values

[edit | edit source]

Returns the query result as an ArrayFunctions array.

Examples

[edit | edit source]
Simple query
{{#af_print: {{#cargo_query: tables=Books
 |no html
 |fields=_pageName,Authors,Genres
 |format=arrayfunctions
}} }}
  • 0
    • _pageName: The Masque of the Red Death
    • Authors: Edgar Allan Poe
    • Genres
      • 0: Horror
      • 1: Fiction
  • 1
    • _pageName: Rita Hayworth and Shawshank Redemption
    • Authors: Stephen King
    • Genres
      • 0: Realism
      • 1: Crime
  • 2
    • _pageName: Animal Farm
    • Authors: George Orwell
    • Genres: Political satire

Magic words

[edit | edit source]

The extension defines a number of magic words (variables).

AF_EMPTY

[edit | edit source]
ArrayFunctions version
1.0

This magic word returns the empty array. This is useful, because it is not possible to create an empty array with #af_list or #af_object.

Description

[edit | edit source]
{{AF_EMPTY}}

Return values

[edit | edit source]

Returns the empty array.

Presentations

[edit | edit source]

See also

[edit | edit source]
  • Extension:Arrays โ€“ similar extension that works by first defining and then manipulating arrays. Incompatible with Parsoid.
  • Extension:Scribunto โ€“ allows you to embed Lua scripts into wikipages.

Changelog

[edit | edit source]

All notable changes to ArrayFunctions will be documented here.

The format is based on Keep a Changelog, and this extension adheres to Semantic Versioning.

v2.0.1 - 2026 June 18

[edit | edit source]

Changed

[edit | edit source]
  • Make #af_stringmap able to create array with "0" as a value. Previously, "0" would be removed from the array.

v2.0.0 - 2026 January 28

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_filter parser function.
  • Add the // overloaded index to remove empty values.

Changed

[edit | edit source]
  • BREAKING: Remove support for MediaWiki 1.39 and earlier. The oldest supported version is now MediaWiki 1.40.
  • BREAKING: In #af_pipeline, when the first argument is empty, {{{prev}}} now renders literally as {{{prev}}}. Use {{{prev|}}} to preserve the 1.x.x behavior.
  • Ignore the first argument when it is empty for #af_difference, #af_intersect, #af_merge, #af_object, #af_pipeline, and #af_zip.
  • Localisation updates courtesy of translatewiki.net.

v1.17.0 - 2025 August 26

[edit | edit source]
This version is not tagged properly on Special:Version, where it still shows as 1.16.2.

Added

[edit | edit source]
  • Add the $wgArrayFunctionsMaxPipelineLength configuration variable to limit the total number of steps in a pipeline.
  • Add the Pages using the ArrayFunctions extension (af-tracking-category) tracking category.

Changed

[edit | edit source]

Fixed

[edit | edit source]
  • Ranges that exceed $wgArrayFunctionsMaxRangeSize are now taken into account when reporting the size of the largest constructed range in the parser limit report. Previously, only ranges that did not exceed the limit would be taken into account.
  • Fix TypeError that occurred when an error was propagated from a nested parser function call.

v1.16.2 - 2025 June 28

[edit | edit source]

Fixed

[edit | edit source]
  • Fix exception caused by storing a Message object in the parser's extension data (T397946).
  • Ensure errors are rendered in the page's target language instead of the user's language (T397951).

v1.16.1 - 2025 June 19

[edit | edit source]

Changed

[edit | edit source]

Fixed

[edit | edit source]
  • Fix BadMethodCallException that was caused by relying on $wgLang in the ParserLimitReportPrepare hook.

v1.16.0 - 2025 June 10

[edit | edit source]

Added

[edit | edit source]
  • Add the $wgArrayFunctionsForeachIterationLimit configuration variable to limit the total number of iterations that can be performed by #af_foreach.
  • Add the $wgArrayFunctionsMaxRangeSize configuration variable to limit the maximum number of elements that an array constructed using #af_range can contain.
  • Add the #af_zip parser function.
  • Add the ! overloaded index to show a value.
  • Add the table and simple formats for `#af_show`.
  • Add the Pages with ArrayFunctions errors (af-error-category) tracking category.
  • Add error tracking and propagation.

Changed

[edit | edit source]
  • Allow #af_show to take a format.
  • The #af_sort and #af_ksort parser functions now compare items normally instead of as strings.
  • Localisation updates courtesy of translatewiki.net.

Fixed

[edit | edit source]
  • Fix exception when passing the empty string to a keyword argument.

v1.15.0 - 2025 May 13

[edit | edit source]

Added

[edit | edit source]
  • Add the arrayfunctions Cargo display format.

Changed

[edit | edit source]

v1.14.2 - 2025 April 15

[edit | edit source]

Fixed

[edit | edit source]
  • Remove properties without a value from the result when using the arrayfunctions result format with Semantic MediaWiki.

v1.14.1 - 2025 April 15

[edit | edit source]

Added

[edit | edit source]
  • Add compatibility with Semantic MediaWiki 5.0.0.

Changed

[edit | edit source]

Fixed

[edit | edit source]
  • Restore compatibility with deprecated MediaWiki 1.35.

v1.14.0 - 2025 April 1

[edit | edit source]

Added

[edit | edit source]
  • Add the arrayfunctions Semantic MediaWiki result format.
  • Add the #af_pipeline parser function.

Changed

[edit | edit source]
  • Drop compatibility with MediaWiki 1.35.
  • Localisation updates courtesy of translatewiki.net.

Fixed

[edit | edit source]
  • Fix exception when using #af_template on MediaWiki 1.44.

v1.13.0 - 2025 March 4

[edit | edit source]

Changed

[edit | edit source]
  • Replace the #af_set parser function with #af_put. The #af_set parser function remains available for backwards compatibility.
  • Localisation updates courtesy of translatewiki.net.

Fixed

[edit | edit source]
  • The magic variable IDs array is now no longer treated as associative, which would previously break CodeMirror syntax highlighting. (by alex4401)

v1.12.0 - 2025 February 4

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_range parser function.

Changed

[edit | edit source]
  • Rename the #af_wildcard parser function to #af_group, and add an alias for #af_wildcard.
  • The #af_difference parser function now compares items normally instead of as strings.
  • The #af_exists parser function now accepts multiple keys to check if a nested key exists.
  • The #af_intersect parser function now compares items normally instead of as strings.
  • Localisation updates courtesy of translatewiki.net.

v1.11.0 - 2024 December 10

[edit | edit source]

Added

[edit | edit source]
  • Add following parser functions: #af_flatten #af_wildcard #af_reverse .
  • Overload #af_get with special indices to perform certain operations on the array instead of retrieving a key.

Changed

[edit | edit source]
  • The #af_unique parser function now compares items normally instead of as strings.

v1.10.0 - 2024 November 26

[edit | edit source]

Added

[edit | edit source]
  • Add ZLIB compression using gzdeflate and gzinflate.

Changed

[edit | edit source]

v1.9.0 - 2024 May 2

[edit | edit source]

Added

[edit | edit source]
  • Add delimiter option to the #af_foreach parser function.

Changed

[edit | edit source]
  • All string parameters now support escape sequences.

v1.8.0 - 2024 April 4

[edit | edit source]

Changed

[edit | edit source]
  • Whitespace is now trimmed from the beginning and the end of array values to be consistent with the behaviour of other parameters in MediaWiki.
  • Localisation updates courtesy of translatewiki.net.

v1.7.0 - 2023 October 24

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_ksort parser function.
  • Add caseinsensitive option to the #af_keysort parser function.
  • Add caseinsensitive option to the #af_sort parser function.

Changed

[edit | edit source]

v1.6.0 - 2023 September 12

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_stringmap parser function.
  • Add the mw.af.import Lua function to import ArrayFunctions arrays into Lua.

Changed

[edit | edit source]

v1.5.0 - 2023 September 7

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_difference parser function.

Changed

[edit | edit source]

v1.4.4 - 2023 June 30

[edit | edit source]

Changed

[edit | edit source]

Fixed

[edit | edit source]
  • Fix #af_template parser function to no longer check a user their read permissions explicitly, as regular template transclusion also does not do this. Previously, an error would be outputted.

v1.4.3 - 2023 May 26

[edit | edit source]

Changed

[edit | edit source]
  • Exporting a NULL value (e.g. through the mw.af.export Lua function) will now result in the empty string. Previously, the NULL would be returned unaltered.

v1.4.2 - 2023 May 26

[edit | edit source]

Changed

[edit | edit source]
  • The #af_template parser function now shows non-existent templates as a broken link. Previously, an error would be outputted.
  • Localisation updates courtesy of translatewiki.net.

v1.4.1 - 2023 May 5

[edit | edit source]

Changed

[edit | edit source]
  • The #af_split parser function now allows the empty string as its first parameter. Previously, an error would be outputted.
  • Localisation updates courtesy of translatewiki.net.

v1.4.0 - 2023 April 26

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_show parser function.

v1.3.0 - 2023 March 27

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_search parser function.

v1.2.0 - 2023 March 3

[edit | edit source]

Added

[edit | edit source]
  • Add the following parser functions: #af_intersect #af_merge #af_reduce.

v1.1.0 - 2023 February 3

[edit | edit source]

Added

[edit | edit source]
  • Add the #af_split parser function.

v1.0.1 - 2023 January 9

[edit | edit source]

Changed

[edit | edit source]
  • The mw.af.export Lua function now supports parameters of all types. Previously, it only supported arrays.

Fixed

[edit | edit source]
  • Fix issue where an exception was thrown when a parameter with an incorrect type was passed to mw.af.export.

v1.0.0 - 2023 January 7

[edit | edit source]

Added

[edit | edit source]
  • Add the parser functions: #af_bool #af_count #af_exists #af_float #af_foreach #af_get #af_if #af_int #af_isarray #af_join #af_keysort #af_list #af_map #af_object #af_print #af_push #af_set #af_slice #af_sort #af_template #af_unique #af_unset