Extension:Arrays: Difference between revisions
m Dark mode fixes. |
|||
| (227 intermediate revisions by 59 users not shown) | |||
| Line 1: | Line 1: | ||
<languages/> |
|||
{{Parsoid compatibility warning}} |
|||
{{Extension |
{{Extension |
||
| |
|type1 = parser function |
||
| |
|type2 = |
||
|author = [[User:lidingpku|Li Ding]], [[User:baojie|Jie Bao]], [[User:Danwe|Daniel Werner]] |
|||
|status = beta |
|||
|hook1 = ParserClearState |
|||
|author = [[User:lidingpku|Li Ding]] and [[User:baojie|Jie Bao]] |
|||
|hook2 = ParserFirstCallInit |
|||
|image = |
|||
|description = Enhances parser with array functions. |
|description = <translate><!--T:1--> Enhances parser with array functions.</translate> |
||
| |
|status = stable |
||
| |
|image = |
||
| |
|version = 2.2.1 |
||
|update = 2020-12-08 |
|||
|license = MIT License |
|||
| |
|mediawiki = 1.31+ |
||
|php = |
|||
[http://smwbp.googlecode.com/svn/trunk/mediawiki/extensions/ArrayExtension/ Subversion] <sup>[<span></span>[[Subversion|Help]]]</sup><br> |
|||
|download = {{WikimediaDownload|Arrays|phab=EARR}} |
|||
[http://code.google.com/p/smwbp/source/browse/#svn/trunk/mediawiki/extensions/ArrayExtension Browse source code] |
|||
|readme = {{git file|project=mediawiki/extensions/Arrays|branch=HEAD|file=README|text=README}} |
|||
|readme = |
|||
|changelog = {{git file|project=mediawiki/extensions/Arrays|branch=HEAD|file=RELEASE-NOTES|text=CHANGELOG}} |
|||
|changelog = |
|||
|needs-updatephp = No |
|||
|example = [http://tw.rpi.edu/portal/Help:ArrayExtension examples] |
|||
|tags = |
|||
}} |
|||
|rights = |
|||
[[Category:Modifiable_variables_extensions]] |
|||
|example = |
|||
|compatibility = |
|||
|phabricator = mediawiki-extensions-arrays |
|||
}} |
|||
<translate> |
|||
<!--T:2--> |
|||
The <tvar name=1>'''Arrays'''</tvar> extension (formerly known as <tvar name=2>'''ArrayExtension'''</tvar>) creates an additional set of [[<tvar name=3>Special:MyLanguage/Help:Parser function</tvar>|parser functions]] that operate on [[w:Array|arrays]]. |
|||
== Functions == <!--T:3--> |
|||
'''ArrayExtension''' defines an additional set of [[m:Help:Parser function|parser function]]s that operate on arrays. |
|||
<!--T:4--> |
|||
==Functions== |
|||
This |
This extension defines the following parser functions: |
||
</translate> |
|||
{| style="background:#EEE; cellspacing:2px; margin:4px; vertical-align:top; border:1px solid #CCC;" |
|||
! style="background:#BBB;" | group |
|||
{| class="wikitable" |
|||
! style="background:#BBB;" | functions |
|||
! <translate><!--T:5--> Group</translate> |
|||
! <translate><!--T:6--> Functions</translate> |
|||
|- |
|- |
||
| <translate><!--T:7--> Construct an array, (with unique, sort, print options)</translate> |
|||
| style="background:#DFD; border:1px solid #AFA;" |construct an array, (no print) |
|||
| |
| <code>[[#arraydefine]]</code> |
||
|- |
|- |
||
| <translate><!--T:8--> Extract information from an array</translate> |
|||
| style="background:#DFD; border:1px solid #AFA;" |print array |
|||
| |
| <code>[[#arrayprint]]</code>, <code>[[#arrayindex]]</code>, <code>[[#arraysize]]</code>, <code>[[#arraysearch]]</code>, <code>[[#arraysearcharray]]</code>, <code>[[#arrayslice]]</code> |
||
|- |
|- |
||
| <translate><!--T:9--> Alter an array</translate> |
|||
| style="background:#DFD; border:1px solid #AFA;" |alter an array (no print) |
|||
| <code>[[#arrayreset]]</code>, <code>[[#arrayunique]]</code>, <code>[[#arraysort]]</code> |
|||
|- |
|- |
||
| <translate><!--T:10--> Interaction between several arrays</translate> |
|||
| style="background:#DFD; border:1px solid #AFA;" |create a new array (no print) |
|||
| <code>[[#arraymerge]]</code>, <code>[[#arrayunion]]</code>, <code>[[#arrayintersect]]</code>, <code>[[#arraydiff]]</code> |
|||
| style="background:#DFD; border:1px solid #AFA;" |<tt>[[#arraymerge]]</tt>, and <tt>[[#arrayslice]]</tt> |
|||
|- |
|- |
||
| <translate><!--T:11--> In case <tvar name=1>{{ll|Extension:HashTables}}</tvar> is installed, for array/hash-table interaction</translate> |
|||
| style="background:#DFD; border:1px solid #AFA;" |create a new array without duplicates (no print) |
|||
| <code>{{ll|Extension:HashTables#hashtoarray|<nowiki>#</nowiki>hashtoarray}}</code>, <code>{{ll|Extension:HashTables#arraytohash|<nowiki>#</nowiki>arraytohash}}</code> |
|||
| style="background:#DFD; border:1px solid #AFA;" | <tt>[[#arrayintersect]]</tt>,<tt>[[#arraydefine]]</tt>,and <tt>[[#arraydiff]]</tt> |
|||
|} |
|} |
||
<translate> |
|||
===arraydefine=== |
|||
== Constructing arrays == <!--T:12--> |
|||
This function constructs an array (identified by 'key') using a list of 'values' separated by the 'delimiter'. |
|||
</translate> |
|||
The variable can be accessed by other functions later. |
|||
=== arraydefine === |
|||
<translate><!--T:13--> This function constructs an array (identified by "key") using a list of "values" separated by the "delimiter". </translate> |
|||
Syntax: |
|||
<translate><!--T:14--> The variable can be accessed by other functions later.</translate> |
|||
<pre> |
|||
{{#arraydefine:key|values|delimiter}} |
|||
</pre> |
|||
'''<translate><!--T:15--> Syntax:</translate>''' |
|||
Note(s): |
|||
* 'values' is a list of strings separated by 'delimiter' |
|||
{{tmpl|0=<code>{{#arraydefine:$1 | $2 | $3 | $4}}</code> |
|||
* the resulting array is an array of strings. |
|||
|1=<translate><!--T:259--> key</translate> |
|||
* the default delimiter is ',' if not specified, a delimiter can be (i) a string (the white-spaces surrounding delimiter will be trimmed) or (ii) a perl regular expression (for advanced user only), e.g. '/\s*,\s*/' (see [http://us3.php.net/manual/en/function.preg-split.php preg_split]) |
|||
|2=<translate><!--T:260--> values</translate> |
|||
* this function shows nothing |
|||
|3=<translate><!--T:261--> delimiter</translate> |
|||
* users can define an empty array, or reset an array by leaving the 'values' empty (see example) |
|||
|4=<translate><!--T:262--> options</translate> |
|||
}} |
|||
'''<translate><!--T:16--> Notes:</translate>''' |
|||
* <translate><!--T:63--> "values" is a list of strings separated by "delimiter".</translate> |
|||
* <translate><!--T:64--> The resulting array is an array of strings.</translate> |
|||
* <translate><!--T:65--> The default delimiter is <tvar name=1><code>,</code></tvar> if not specified, a delimiter can be a string (the white-spaces surrounding delimiter will be trimmed) or a Perl regular expression, e.g. <tvar name=2><code>/\s*,\s*/</code></tvar> (see <tvar name=3>[https://php.net/function.preg-split preg_split]</tvar>).</translate> |
|||
* <translate><!--T:66--> Users can define an empty array (see example).</translate> |
|||
* <translate><!--T:67--> Users can specify options including unique, sort, and print (see example).</translate> |
|||
** <translate><!--T:68--> Options are ignored unless a delimiter is also specified.</translate> |
|||
'''<translate><!--T:17--> Examples:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| |
| <translate><!--T:69--> Define a one-element array named <tvar name=1><code>a</code></tvar></translate> |
||
| <pre>{{#arraydefine:a|red}}</pre> |
| <pre>{{#arraydefine:a|red}}</pre> |
||
|- |
|- |
||
| |
| <translate><!--T:70--> Define a four-element array named <tvar name=1><code>b</code></tvar>, use default delimiter (<tvar name=2><code>,</code></tvar>)</translate> |
||
| <pre>{{#arraydefine:b|orange, |
| <pre>{{#arraydefine:b|orange, red, yellow, yellow}}</pre> |
||
|- |
|- |
||
| |
| <translate><!--T:71--> Define/set an empty array named <tvar name=1><code>c</code></tvar></translate> |
||
| <pre>{{#arraydefine:c}}</pre> |
| <pre>{{#arraydefine:c}}</pre> |
||
|- |
|- |
||
| |
| <translate><!--T:72--> Define a two-element array named <tvar name=1><code>d</code></tvar>, using <tvar name=2><code>;</code></tvar> as delimiter</translate> |
||
| <pre>{{#arraydefine:d|apple |
| <pre>{{#arraydefine:d|apple; pear|;}}</pre> |
||
|- |
|- |
||
| |
| <translate><!--T:73--> Define a three-element array named <tvar name=1><code>e</code></tvar>, using the regular expression <tvar name=2><code>/\s*[;,]\s*/</code></tvar> as delimiter</translate> |
||
| <pre>{{#arraydefine:e|apple, pear|;}}</pre> |
| <pre>{{#arraydefine:e|apple, pear; orange|/\s*[;,]\s*/}}</pre> |
||
|- |
|- |
||
| <translate><!--T:74--> Define a three-element array named <tvar name=1><code>f</code></tvar>, use delimiter (<tvar name=2><code>,</code></tvar>), "<tvar name=3>unique, sort=desc, print=list</tvar>" options (the array elements are unique, sorted in descending order and being printed).</translate> <translate><!--T:75--> For more option-values of sort, see <tvar name=1>[[#arraysort]]</tvar>.</translate> |
|||
| (advanced user only) define a three-element array named 'f', using '/\s*[;,]\s*/' as delimiter |
|||
| <pre>{{#arraydefine:f| |
| <pre>{{#arraydefine:f|orange, red, yellow, yellow |, |unique, sort=desc, print=list}}</pre> |
||
|} |
|} |
||
<translate> |
|||
== Working with arrays == <!--T:76--> |
|||
=== Extraction === <!--T:77--> |
|||
===arrayprint=== |
|||
</translate> |
|||
This function prints the values of an array in customizable format. |
|||
==== arrayprint ==== |
|||
<translate><!--T:78--> This function prints the values of an array in customizable format.</translate> |
|||
'''<translate><!--T:18--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{#arrayprint:key|delimiter|pattern| |
{{#arrayprint:key|delimiter|pattern|subject|options}} |
||
</pre> |
</pre> |
||
'''<translate><!--T:19--> Notes:</translate>''' |
|||
The customizable output format is: |
|||
* <translate><!--T:79--> <tvar name=1>''subject''</tvar> accepts wiki links, templates and parser functions.</translate> |
|||
<pre> |
|||
* <translate><!--T:80--> Within the <tvar name=1>''subject''</tvar>, you '''don't''' have to escape pipe characters <tvar name=2><code>|</code></tvar>.</translate> <translate><!--T:81--> Within the whole construct, the <tvar name=1>''pattern''</tvar> will be searched and replaced with the current (escaped) array value of each loop.</translate> <translate><!--T:82--> Finally, the whole string will be parsed and put into an array of results which will be imploded with <tvar name=1>''delimiter''</tvar> as separator.</translate> |
|||
template<value_1> delimiter template<value_2> delimiter ... delimiter template<value_n> |
|||
* <translate><!--T:83--> In case the array which should be printed doesn't exist, an empty string will be returned (Introduced in 1.4 alpha, part of compatibility mode).</translate> |
|||
</pre> |
|||
* <translate><!--T:84--> The default delimiter is language dependent, for English it is <tvar name=1><code>, </code></tvar> (Introduced in 2.0, part of compatibility mode).</translate> |
|||
'''<translate><!--T:20--> Examples:</translate>''' |
|||
Note(s): |
|||
{| class="wikitable" |
|||
* the 'template' should not embed wiki template <nowiki>''{{...}}''</nowiki>. |
|||
! <translate><!--T:263--> Task</translate> |
|||
* warning: templates or parser functions defined in the output will only be printed instead of being executed. |
|||
! <translate><!--T:264--> Example code</translate> |
|||
! <translate><!--T:265--> Output (the array b is defined above)</translate> |
|||
Example(s): |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:85--> Print - using language dependent default list delimiter</translate> |
|||
| print - use default delimiter ',' |
|||
|<pre>{{#arrayprint:b}}</pre> |
|<pre>{{#arrayprint:b}}</pre> |
||
| |
|||
* orange, red, yellow, yellow |
|||
|- |
|- |
||
| <translate><!--T:86--> Print - without delimiter</translate> |
|||
| print - use <nowiki>''</nowiki> as delimiter |
|||
|<pre>{{#arrayprint:b|}}</pre> |
|<pre>{{#arrayprint:b | }}</pre> |
||
| |
|||
* orangeredyellowyellow |
|||
|- |
|- |
||
| |
| <translate><!--T:87--> Print - using <tvar name=1>{{tag|br|single}}</tvar> (line-break) as delimiter</translate> |
||
|<pre>{{#arrayprint:b|<br/>}}</pre> |
|<pre>{{#arrayprint:b |<br/> }}</pre> |
||
|orange |
|||
red |
|||
yellow |
|||
yellow |
|||
|- |
|- |
||
| <translate><!--T:88--> Pretty list output where the last two elements are chained with an <tvar name=1><code> and </code></tvar> (or the local languages equivalent).</translate> <translate><!--T:89--> Even though the <tvar name=1>''delimiter''</tvar> parameter is empty, <tvar name=2><code>, </code></tvar> (or the languages equivalent) will be used since it wouldn't be pretty otherwise.</translate> |
|||
| print - use pattern to create wiki links |
|||
|<pre>{{#arrayprint:b| |
|<pre>{{#arrayprint:b ||@ |@ |print=pretty }}</pre> |
||
| |
|||
|- |
|- |
||
| <translate><!--T:90--> Embed wiki link to categories</translate> |
|||
| print - add pattern to create SMW links |
|||
|<pre>{{#arrayprint:b|<br/>|@@@@|[[ |
|<pre>{{#arrayprint:b |<br/> |@@@@ |[[:Category:@@@@|@@@@]] }}</pre> |
||
|[[:Category:Orange|orange]] |
|||
[[:Category:Red|red]] |
|||
[[:Category:Yellow|yellow]] |
|||
[[:Category:Yellow|yellow]] |
|||
|- |
|||
| <translate><!--T:91--> Define a Semantic MediaWiki property value</translate> |
|||
|<pre>{{#arrayprint:b |<br/> |@@@@ |[[prop1::@@@@]] }}</pre> |
|||
| |
|||
|- |
|||
| <translate><!--T:92--> Embed parser function</translate> |
|||
|<pre>{{#arrayprint:b |<br/> |@@@@ |length of @@@@:{{#len:@@@@}} }}</pre> |
|||
| |
|||
|- |
|||
| <translate><!--T:93--> Embed template (with parameters)</translate> |
|||
|<pre>{{#arrayprint:b|<br/>|@@@@|{{template|prop2|@@@@}} }}</pre> |
|||
| |
|||
|} |
|} |
||
=== |
==== arrayindex ==== |
||
<translate><!--T:94--> This function prints the value of an array (identified by <tvar name=1>''key''</tvar>) at position <tvar name=2>''index''</tvar>.</translate> |
|||
This function returns the size (number of elements) of an array. |
|||
See: http://www.php.net/manual/en/function.count.php |
|||
'''<translate><!--T:21--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arrayindex:key|index|default}} |
||
</pre> |
</pre> |
||
'''<translate><!--T:22--> Notes:</translate>''' |
|||
Example(s): |
|||
* <translate><!--T:95--> Invalid index (non-number, out of bound) will result in printing an empty string.</translate> |
|||
{| border=1 |
|||
* <translate><!--T:96--> The index is 0-based, i.e. the first element's index is 0.</translate> |
|||
* <translate><!--T:97--> Negative indexes will return an element that far from the end (e.g. <tvar name=1><code>-1</code></tvar> would be the arrays last element).</translate> |
|||
* <translate><!--T:98--> <tvar name=1>''default''</tvar> will be returned in case the array doesn't exist, the key doesn't exist within the array or if the value is an empty string.</translate> |
|||
'''<translate><!--T:23--> Examples:</translate>''' |
|||
{| class="wikitable" |
|||
|- |
|||
| <translate><!--T:99--> Third element within array <tvar name=1>''a''</tvar></translate> |
|||
|<pre>{{#arrayindex:a |2 }}</pre> |
|||
|- |
|||
| <translate><!--T:100--> Last element within array <tvar name=1>''b''</tvar></translate> |
|||
|<pre>{{#arrayindex:b |-1 }}</pre> |
|||
|- |
|- |
||
| <translate><!--T:101--> Print default value for invalid index</translate> |
|||
| size: |
|||
|<pre>{{# |
|<pre>{{#arrayindex:c |foo |bad value }}</pre> |
||
|} |
|} |
||
=== |
==== arraysize ==== |
||
<translate> |
|||
This function returns the index of the first occurrence of the 'value' in the array (identified by 'key'), and returns '-1' when failed. |
|||
<!--T:102--> |
|||
See: http://www.php.net/manual/en/function.array-search.php |
|||
This function returns the size (number of elements) of an array. |
|||
<!--T:103--> |
|||
Syntax: |
|||
See <tvar name=1>https://php.net/function.count</tvar> |
|||
</translate> |
|||
<translate><!--T:104--> In case the given array doesn't exist the output of the function will be a void string instead of a number.</translate> <translate><!--T:105--> This allows to check whether the array exists.</translate> |
|||
'''<translate><!--T:24--> Syntax:</translate>''' |
|||
<pre> |
<pre> |
||
{{# |
{{#arraysize:key}} |
||
</pre> |
</pre> |
||
'''<translate><!--T:25--> Examples:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:106--> Size of array <tvar name=1>''a''</tvar>:</translate> |
|||
| search first occurrence of a value |
|||
|<pre>{{# |
|<pre>{{#arraysize:a}}</pre> |
||
|- |
|||
{{#arraysearch:b|red}}</pre> |
|||
| <translate><!--T:107--> Check whether array <tvar name=1>''a''</tvar> exists or not:</translate> |
|||
|<pre>{{#if: {{#arraysize:a}} | ''<translate nowrap><!--T:108--> array exists</translate>'' | ''<translate nowrap><!--T:109--> array not defined</translate>'' }}</pre> |
|||
|} |
|} |
||
=== |
==== arraysearch ==== |
||
<translate><!--T:110--> This function returns the index of the first occurrence of the <tvar name=1><code>value</code></tvar> in the array (identified by <tvar name=2><code>key</code></tvar>) starting from the position identified by <tvar name=3><code>index</code></tvar> parameter, and returns an empty string when failed.</translate> |
|||
This function print the value of an array (identified by 'key') at position 'index'. |
|||
<translate><!--T:111--> When <tvar name=1>''yes''</tvar> and/or <tvar name=2>''no''</tvar> specified, this will expand the value set to <tvar name=1>''yes''</tvar> if found, value of <tvar name=2>''no''</tvar> otherwise.</translate> |
|||
<translate><!--T:112--> See <tvar name=1>https://php.net/function.array-search</tvar></translate> |
|||
'''<translate><!--T:26--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arraysearch:key|value|index|yes|no}} |
||
</pre> |
</pre> |
||
Note(s): |
|||
* invalid index (non-number, out of bound) will result in printing an empty string. |
|||
* the index is 0-based, i.e. the first element's index is 0. |
|||
'''<translate><!--T:27--> Examples:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:114--> Return index of first occurrence of a value</translate> |
|||
| array index test |
|||
|<pre>{{# |
|<pre>{{#arraysearch:b|white}} |
||
{{#arraysearch:b|red}} |
|||
<translate nowrap><!--T:115--> use offset</translate> |
|||
{{#arraysearch:b|red|0}} |
|||
{{#arraysearch:b|red|2}} |
|||
<translate nowrap><!--T:116--> use preg regular expression match</translate> |
|||
{{#arraysearch:b|/low/}} |
|||
{{#arraysearch:b|/LOW/i}} - <translate nowrap><!--T:117--> case insensitive</translate> |
|||
{{#arraysearch:b|low}} |
|||
<translate nowrap><!--T:118--> use yes no print option</translate> |
|||
{{#arraysearch:b|white|0|yes|no}} |
|||
{{#arraysearch:b|yellow|0|yes|no}}</pre> |
|||
|} |
|} |
||
=== |
==== arraysearcharray ==== |
||
<translate><!--T:119--> This function searches an array (identified by <tvar name=1>''key''</tvar>) and creates a new array (identified by <tvar name=2>''new_key''</tvar>) from the search with all the results.</translate> <translate><!--T:120--> The search criteria <tvar name=1>''value''</tvar> can be a string or a regular expression.</translate> <translate><!--T:121--> If <tvar name=1>''index''</tvar> is given the search will start there, <tvar name=2>''limit''</tvar> can define the maximum search results.</translate> <translate><!--T:122--> The parameter identified by <tvar name=1>''transform''</tvar> can be used if <tvar name=2>''value''</tvar> is a regular expression.</translate> <translate><!--T:123--> It can transform the result of the matched entries into the <tvar name=1>''new_key''</tvar> array like <tvar name=2>[https://php.net/function.preg-replace PHP preg_replace]</tvar> would do it.</translate> |
|||
This function sorts an array in the following order. |
|||
See: http://www.php.net/manual/en/function.sort.php; http://www.php.net/manual/en/function.rsort.php; http://www.php.net/manual/en/function.array-rand.php |
|||
* none (default) - no sort |
|||
* desc - in descending order |
|||
* asce - in ascending order |
|||
* random - in random order |
|||
'''<translate><!--T:28--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{#arraysearcharray:new_key|key|value|index|limit|transform}} |
|||
{{#arraysort:key|order}} |
|||
</pre> |
</pre> |
||
'''<translate><!--T:29--> Notes:</translate>''' |
|||
Note(s): |
|||
* <translate><!--T:124--> If <tvar name=1>''value''</tvar> is a string, the <tvar name=2>''new_key''</tvar> array will only contain entries of exact this string.</translate> |
|||
* each array element is a string |
|||
* <translate><!--T:125--> Negative <tvar name=1>''index''</tvar> values like <tvar name=2><code>-n</code></tvar> can be used to search the last <tvar name=3>''n''</tvar> entries only.</translate> |
|||
* <translate><!--T:126--> If <tvar name=1>{{ll|Extension:Regex Fun}}</tvar> is available within the wiki, Regex Fun's <tvar name=2>''e''</tvar> modifier can be used within the regex.</translate> <translate><!--T:127--> This has nothing to do with PHPs <tvar name=1>''e''</tvar> modifier (which would be a security breach).</translate> <translate><!--T:128--> With active <tvar name=1>''e''</tvar> modifier the <tvar name=2>''transform''</tvar> string will be parsed after back-refs are inserted, after that it will replace the actual match.</translate> |
|||
'''<translate><!--T:30--> Examples:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:129--> Find all entries in array <tvar name=1><code>a</code></tvar> that start with <tvar name=3><code>A</code></tvar> followed by a space and put them into a new array <tvar name=2><code>x</code></tvar>.</translate> |
|||
| sort an array |
|||
|<pre> |
|||
|<pre>{{#arraysort:x|desc}}</pre> |
|||
{{#arraysearcharray:x |a |/^A\s.+/ }} |
|||
</pre> |
|||
|- |
|- |
||
| <translate><!--T:130--> Searching all entries of array <tvar name=1><code>a</code></tvar> which end with numbers and put the numbers only into a new array <tvar name=2><code>y</code></tvar>.</translate> |
|||
| randomize an array |
|||
|<pre> |
|||
|<pre>{{#arraysort:x|random}}</pre> |
|||
{{#arraysearcharray:y |a |/^.*?(\d+)$/ |0 |-1 | $1 }} |
|||
</pre> |
|||
|- |
|||
| <translate><!--T:131--> Searching all entries of array <tvar name=1><code>a</code></tvar> which end with numbers and put the length of these items into the new array (this requires <tvar name=2>''Regex Fun''</tvar> extension).</translate> |
|||
|<pre> |
|||
{{#arraysearcharray:y |y |/^.*?\d+$/e |0 |-1 | {{#len:$0}} }} |
|||
</pre> |
|||
|- |
|||
| <translate><!--T:132--> Remove empty values from array <tvar name=1><code>a</code></tvar>.</translate> |
|||
|<pre> |
|||
{{#arraysearcharray:a|a|/\S+/}} |
|||
</pre> |
|||
|} |
|} |
||
=== |
==== arrayslice ==== |
||
<translate> |
|||
This function converts an array (identified by 'key') into a set (no duplicated members). |
|||
<!--T:133--> |
|||
see: http://www.php.net/manual/en/function.array-unique.php |
|||
This function extracts a sub-array from an array (identified by <tvar name=1><code>key</code></tvar>) into a new array (identified by <tvar name=2><code>new_key</code></tvar>). |
|||
<!--T:134--> |
|||
Syntax: |
|||
See <tvar name=1>https://php.net/function.array-slice</tvar> |
|||
</translate> |
|||
'''<translate><!--T:31--> Syntax:</translate>''' |
|||
<pre> |
<pre> |
||
{{# |
{{#arrayslice:new_key|key|offset|length}} |
||
</pre> |
</pre> |
||
'''<translate><!--T:32--> Notes:</translate>''' |
|||
Example(s): |
|||
* <translate><!--T:135--> Offset indicates starting point of slice, it can be non-negative number or negative number for backwards index (e.g. the last element of the array's offset is -1).</translate> <translate><!--T:136--> Offset is different from index (which must be non-negative number)</translate> |
|||
{| border=1 |
|||
* <translate><!--T:137--> Length indicates how many element to extract.</translate> <translate><!--T:138--> If it is omitted, then the sequence will have everything from offset up until the end of the array.</translate> |
|||
* <translate><!--T:139--> If offset is no less than array size, empty array will be returned, if offset if no greater than negative array size, a new array with all elements will be returned.</translate> |
|||
'''<translate><!--T:33--> Examples:</translate>''' |
|||
{| class="wikitable" |
|||
|- |
|||
| <translate><!--T:140--> Extract a two-element slice starting from the element at offset 1.</translate> |
|||
| <pre>{{#arrayslice:x|b|1|2}}</pre> |
|||
|- |
|- |
||
| <translate><!--T:141--> Extract a two-element slice starting from the element at offset -2.</translate> |
|||
| convert array to set |
|||
|<pre>{{# |
| <pre>{{#arrayslice:x|b|-2|2}}</pre> |
||
|} |
|} |
||
<translate> |
|||
=== Alteration === <!--T:142--> |
|||
<!--T:143--> |
|||
Functions which alter an array directly instead of creating a new array. |
|||
</translate> |
|||
==== arrayunique ==== |
|||
<translate> |
|||
<!--T:144--> |
|||
This function converts an array (identified by 'key') into a set (no duplicated members, no empty element). |
|||
<!--T:145--> |
|||
===arrayreset=== |
|||
See <tvar name=1>https://php.net/function.array-unique</tvar> |
|||
This function free-up all defined arrays. |
|||
</translate> |
|||
'''<translate><!--T:34--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arrayunique:key}} |
||
</pre> |
</pre> |
||
'''<translate><!--T:35--> Example:</translate>''' |
|||
{| class="wikitable" |
|||
|- |
|||
| <translate><!--T:146--> Convert array to set.</translate> |
|||
| <pre>{{#arrayunique:b}}</pre> |
|||
|} |
|||
=== |
==== arrayreset ==== |
||
<translate> |
|||
This function merges values of two arrayes (identified by 'key1' and 'key2') into a new array (identified by 'key'). |
|||
<!--T:147--> |
|||
See: http://www.php.net/manual/en/function.array-merge.php |
|||
This function will unset some or all defined arrays. |
|||
</translate> |
|||
'''<translate><!--T:36--> Syntax:</translate>''' |
|||
Syntax: |
|||
<syntaxhighlight lang="html"> |
|||
{{#arrayreset:}} <!-- <translate nowrap><!--T:148--> will unset ALL arrays</translate> --> |
|||
{{#arrayreset:key1 |key2 |... |key-n }} |
|||
</syntaxhighlight> |
|||
'''<translate><!--T:37--> Notes:</translate>''' |
|||
* <translate><!--T:149--> Using <tvar name=1>[[#arraysize|arraysize]]</tvar> on them will return an empty string instead of <tvar name=2><code>0</code></tvar>, so they are really unset, not empty.</translate> <translate><!--T:150--> To simply empty an array one can use <tvar name=1><code><nowiki>{{#arraydefine:key}}</nowiki></code></tvar>.</translate> |
|||
* <translate><!--T:151--> Prior to version 1.4 alpha <tvar name=1><code>,</code></tvar> is used to separate several arrays which should be unset.</translate> |
|||
==== arraysort ==== |
|||
<translate> |
|||
<!--T:152--> |
|||
This function sorts an array in the following order. |
|||
</translate> |
|||
* <code>none</code> - <translate><!--T:153--> no sort (default)</translate> |
|||
* <code>desc</code> - <translate><!--T:154--> in descending order (see <tvar name=1>https://php.net/function.sort</tvar>)</translate> |
|||
* <code>asce</code>/<code>asc</code> - <translate><!--T:155--> in ascending order (see <tvar name=1>https://php.net/function.rsort</tvar>)</translate> |
|||
* <code>random</code> - <translate><!--T:156--> in random order (see <tvar name=1>https://php.net/function.array-rand</tvar>)</translate> |
|||
* <code>reverse</code> - <translate><!--T:157--> in reverse order (see <tvar name=1>https://php.net/function.array-reverse</tvar>)</translate> |
|||
'''<translate><!--T:258--> Syntax:</translate>''' |
|||
<pre> |
<pre> |
||
{{# |
{{#arraysort:key|order}} |
||
</pre> |
</pre> |
||
'''<translate><!--T:38--> Note:</translate>''' |
|||
Note(s): |
|||
* <translate><!--T:158--> Each array element is being treated as a string, this means numbers might not be ordered as expected.</translate> |
|||
* this merge is different from array_merge offered by PHP because it merges values instead of keys |
|||
'''<translate><!--T:39--> Examples:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:159--> Sort an array.</translate> |
|||
| merge two arrays |
|||
|<pre>{{# |
|<pre>{{#arraysort:x|desc}}</pre> |
||
|- |
|||
| <translate><!--T:160--> Randomize an array.</translate> |
|||
|<pre>{{#arraysort:x|random}}</pre> |
|||
|- |
|- |
||
| <translate><!--T:161--> Reverse an array.</translate> |
|||
| duplicate an array (keep the third argument of arraymerge empty |
|||
|<pre>{{# |
|<pre>{{#arraysort:x|reverse}}</pre> |
||
|} |
|} |
||
<translate> |
|||
=== Interaction === <!--T:162--> |
|||
</translate> |
|||
<translate><!--T:163--> Functions which work with more than one array, creating one new array or overwriting an existing one as result.</translate> <translate><!--T:164--> Since version 2.0, these functions can interact with more than just two arrays at a time.</translate> <translate><!--T:165--> In case they deal with only one array, they simply create a copy of that array.</translate> <translate><!--T:166--> Any non-existent arrays will simply be ignored by these functions.</translate> |
|||
==== arraymerge ==== |
|||
<translate> |
|||
<!--T:167--> |
|||
This function merges values of two or more arrays into a new array (identified by <tvar name=1>''new_key''</tvar>). |
|||
<!--T:168--> |
|||
===arrayslice=== |
|||
See <tvar name=1>https://php.net/function.array-merge</tvar> |
|||
This function extract a sub-array from an array (identified by 'key1') into a new array (identified by 'key'). |
|||
</translate> |
|||
See: http://www.php.net/manual/en/function.array-slice.php |
|||
'''<translate><!--T:40--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arraymerge:new_key |key1 |key2 |... |key-n }} |
||
</pre> |
</pre> |
||
'''<translate><!--T:41--> Examples:</translate>''' |
|||
Note(s): |
|||
{| class="wikitable" |
|||
* offset indicates starting point of slice, it can be (i) non-negative number (ii) negative number for backwards index (e.g. the last element of the array's offset is -1) |
|||
* length indicates how many element to extract. If it is omitted, then the sequence will have everything from offset up until the end of the array. |
|||
Example(s): |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:169--> Merge two arrays.</translate> |
|||
| extract a two-element slice starting from the element at offset 1 |
|||
|<pre>{{# |
|<pre>{{#arraymerge:x |a |b }}</pre> |
||
|- |
|- |
||
| <translate><!--T:170--> Duplicate an array (keep the third argument of <tvar name=1>''arraymerge''</tvar> empty).</translate> |
|||
| extract a two-element slice starting from the element at offset -2 |
|||
|<pre>{{#arraymerge:x|b |
|<pre>{{#arraymerge:x |b }}</pre> |
||
|} |
|} |
||
=== |
==== arrayunion ==== |
||
<translate> |
|||
This function computes the set theoretic intersection of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. |
|||
<!--T:171--> |
|||
See: http://www.php.net/manual/en/function.array-intersect.php |
|||
This function merges values of two or more arrays into a new array (identified by <tvar name=1>''new_key''</tvar>) without duplicated values. |
|||
</translate> |
|||
'''<translate><!--T:42--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arrayunion:new_key |key1 |key2 |... |key-n }} |
||
</pre> |
</pre> |
||
'''<translate><!--T:43--> Notes:</translate>''' |
|||
Note(s): |
|||
* |
* <translate><!--T:172--> This is a set operator, i.e., the returned array is a set without duplicated values.</translate> |
||
* <translate><!--T:173--> This is equal to <tvar name=1>''arraymerge''</tvar> with <tvar name=2>''arrayunique''</tvar> afterwards.</translate> |
|||
'''<translate><!--T:44--> Example:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:174--> Union of three arrays.</translate> |
|||
| intersect |
|||
|<pre>{{# |
|<pre>{{#arrayunion:x |a |b |c }}</pre> |
||
|} |
|} |
||
=== |
==== arraydiff ==== |
||
This function computes the set theoretic |
<translate><!--T:175--> This function computes the (set theoretic) difference of two or more arrays.</translate> <translate><!--T:176--> The result array is identified by <tvar name=1>''new_key''</tvar>.</translate> |
||
<translate><!--T:177--> The returned array is a set that contains elements of the first given array (identified by <tvar name=1>''key1''</tvar>) which are not defined within any of the other arrays.</translate> |
|||
See: http://www.php.net/manual/en/function.array-union.php |
|||
<translate><!--T:178--> See <tvar name=1>https://php.net/function.array-diff</tvar></translate> |
|||
'''<translate><!--T:179--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arraydiff:new_key |key1 |key2 |... |key-n }} |
||
</pre> |
</pre> |
||
'''<translate><!--T:45--> Note:</translate>''' |
|||
Note(s): |
|||
* |
* <translate><!--T:180--> This is a set operator, i.e. the returned array is a set without duplicated values.</translate> |
||
* <translate><!--T:181--> This function can be used to test sub-class relation.</translate> |
|||
* similar to arraymerge, this union operation works on values. |
|||
'''<translate><!--T:46--> Examples:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:182--> Diff</translate> (b-a) |
|||
| union |
|||
|<pre>{{# |
|<pre>{{#arraydiff:x |b |a }}</pre> |
||
|- |
|||
| <translate><!--T:183--> Diff</translate> (a-b) |
|||
|<pre>{{#arraydiff:x |a |b }}</pre> |
|||
|- |
|||
| <translate><!--T:184--> Diff</translate> (a-(b+c)) |
|||
|<pre>{{#arraydiff:x |a |b |c }}</pre> |
|||
|} |
|} |
||
=== |
==== arrayintersect ==== |
||
This function computes the |
<translate><!--T:185--> This function computes the set theoretic intersection of two or more given arrays.</translate> <translate><!--T:186--> The result array is identified by <tvar name=1>''new_key''</tvar>.</translate> |
||
<translate><!--T:187--> See <tvar name=1>https://php.net/function.array-intersect</tvar></translate> |
|||
The returned array (identified by 'key') is a set that contains elements in an array (identified by 'key1') but not in the other array (identified by 'key2'). |
|||
See: http://www.php.net/manual/en/function.array-diff.php |
|||
'''<translate><!--T:47--> Syntax:</translate>''' |
|||
Syntax: |
|||
<pre> |
<pre> |
||
{{# |
{{#arrayintersect:new_key |key1 |key2 |... |key-n }} |
||
</pre> |
</pre> |
||
'''<translate><!--T:48--> Note:</translate>''' |
|||
Note(s): |
|||
* |
* <translate><!--T:188--> This is a set operator, i.e., the returned array is a set without duplicated values.</translate> |
||
* this function can be used to test sub-class relation |
|||
'''<translate><!--T:49--> Example:</translate>''' |
|||
Example(s): |
|||
{| class="wikitable" |
|||
{| border=1 |
|||
|- |
|- |
||
| <translate><!--T:189--> Intersect of three arrays put into new array <tvar name=1>''x''</tvar></translate> |
|||
| diff (b-a) |
|||
|<pre>{{# |
|<pre>{{#arrayintersect:x |a |b |c }}</pre> |
||
|- |
|||
| diff (a-b) |
|||
|<pre>{{#arraydiff:x|a|b}}</pre> |
|||
|} |
|} |
||
<translate> |
|||
==Installation== |
|||
== Installation == <!--T:54--> |
|||
This extension has been tested on MediaWiki 1.13+ and PHP 5. But most of its functions are compatible with PHP 4, so it is reasonable to try this extension on earlier versions of MediaWiki. |
|||
</translate> |
|||
Please use the following instructions to install this extension. |
|||
{{ExtensionInstall |
|||
|registration=required |
|||
|custom-steps=* [[#Configuration|<translate><!--T:55--> Configure as required.</translate>]] |
|||
}} |
|||
<translate> |
|||
1. Install source code |
|||
== Configuration == <!--T:56--> |
|||
* you can copy the source code from [http://code.google.com/p/smwbp/source/browse/#svn/trunk/mediawiki/extensions/ArrayExtension SVN], and put them under "WIKI-PATH/extentions/ArrayExtension" |
|||
</translate> |
|||
* or if you have ''shell access'', you can install using svn |
|||
<translate><!--T:190--> ''Arrays'' 2.0 introduces two configuration variables:</translate> |
|||
svn co http://smwbp.googlecode.com/svn/trunk/mediawiki/extensions/ArrayExtension/ |
|||
; $egArraysCompatibilityMode |
|||
2. Append the following to <tt>[[LocalSettings.php]]</tt> (near the bottom) of your MediaWiki installation: |
|||
: <translate><!--T:191--> (<tvar name=1>''$egArrayExtensionCompatbilityMode''</tvar> in 1.4 alpha) Set to <tvar name=2>''true''</tvar>, this will activate the compatibility mode which will bring back the behavior of the old <tvar name=3>''ArrayExtension''</tvar> 1.3.2 as far as possible.</translate> <translate><!--T:192--> This is because in Version 2.0 several breaking changes have been introduced.</translate> <translate><!--T:193--> So using this compatibility mode allows a smooth switch from 1.x to 2.x <tvar name=1>''Arrays''</tvar> extension.</translate> <translate><!--T:194--> By default, compatibility mode is inactive.</translate> <translate><!--T:196--> If you have been using the old <tvar name=1>''ArrayExtension''</tvar> within your wiki before, you might want to take a look at that list and adjust your templates before switching to <tvar name=2>''Arrays''</tvar> without compatibility mode.</translate> |
|||
<source lang="php">require_once ("$IP/extensions/ArrayExtension/ArrayExtension.php");</source> |
|||
; $egArraysExpansionEscapeTemplates |
|||
==FAQ== |
|||
: <translate><!--T:197--> Contains a key-value pair list of characters that should be replaced by a template or parser function call within array values included into an <tvar name=1>[[#arrayprint]]</tvar>.</translate> <translate><!--T:198--> By replacing these special characters before including the values into the string which is being expanded afterwards, array values can't distract the surrounding MW code.</translate> <translate><!--T:199--> Otherwise the array values themselves would be parsed as well. By default this will escape the following characters with the following template or parser function calls:</translate> |
|||
===work with [[Extension:SemanticMediaWiki]]=== |
|||
:* <code>=</code> = <code><nowiki>{{=}}</nowiki></code> (<translate><!--T:200--> "<tvar name=1>Template:=</tvar>" should print <tvar name=2><code>=</code></tvar></translate>) |
|||
ArrayExtension allows users to populate an array using a SMW query result. |
|||
:* <code>|</code> = <code><nowiki>{{!}}</nowiki></code> (<translate><!--T:201--> "<tvar name=1>Template:!</tvar>" should print <tvar name=2><code>|</code></tvar></translate>) |
|||
:: {{Note}} <translate><!--T:202--> Starting with MW 1.24.0 it is no longer necessary to create "<tvar name=1>Template:!</tvar>" since its purpose is served by the new <tvar name=2><code>{{ll|Help:Magic_words#Other|<nowiki>{{!}}</nowiki>}}</code></tvar> magic word.</translate> |
|||
:* <code><nowiki>{{</nowiki></code> = <code><nowiki>{{((}}</nowiki></code> (<translate><!--T:203--> "<tvar name=1>Template:((</tvar>" should print <tvar name=2><code><nowiki>{{</nowiki></code></tvar></translate>) |
|||
:* <code><nowiki>}}</nowiki></code> = <code><nowiki>{{))}}</nowiki></code> (<translate><!--T:204--> "<tvar name=1>Template:))</tvar>" should print <tvar name=2><code><nowiki>}}</nowiki></code></tvar></translate>) |
|||
: <translate><!--T:205--> Make sure these templates or parser functions exist within your wiki or change this variable accordingly.</translate> <translate><!--T:206--> If this is not set up properly, <tvar name=1><code>#arrayprint</code></tvar> might print unexpected values in case one of these character sequences is being used within array values.</translate> |
|||
: <translate><!--T:207--> <tvar name=1>''$egArraysExpansionEscapeTemplates''</tvar> also can simply be set to <tvar name=2>''null''</tvar>, in this case it switches back to pre 2.0 behavior where array values with these character sequences did break the given <tvar name=3>''subject''</tvar> code within <tvar name=4><code>#arrayprint</code></tvar>.</translate> <translate><!--T:208--> If the compatibility mode is active, this will always be treated as set to <tvar name=1>''null''</tvar>.</translate> |
|||
<translate> |
|||
Example A. to create a list of instances of the class 'Color' |
|||
==FAQ== <!--T:57--> |
|||
<pre> |
|||
{{#arraydefine:colors|{{#ask:[[Category:Color]][[:+]] |sep =, |limit=1000}} }} |
|||
</pre> |
|||
=== Iteratively accessing array elements === <!--T:58--> |
|||
Example B. to create a unique list of values of property 'has color' |
|||
</translate> |
|||
<pre> |
|||
<translate><!--T:209--> It is possible to iteratively access elements of an array using <tvar name=1>[[#arrayprint]]</tvar> or <tvar name=2>{{ll|Extension:Loops}}</tvar>.</translate> |
|||
{{#arraydefine:colors|{{#ask:[[has color::+]][[:+]] |?color= |mainlabel=- |sep =, |limit=1000}} |,|unique}} |
|||
<translate> |
|||
</pre> |
|||
==== Using <tvar name=1>arrayprint</tvar> ==== <!--T:210--> |
|||
</translate> |
|||
<syntaxhighlight lang="html"> |
|||
<!-- <translate nowrap><!--T:211--> define an array</translate> --> |
|||
{{#arraydefine:colors|Red,Blue,Yellow}} |
|||
{{#arrayprint:colors||@@@@|<nowiki/> |
|||
Example C. to deal with 2D array generated by SWM query (e.g. n-ary property) |
|||
* <translate nowrap><!--T:212--> length of <tvar name=1>@@@@</tvar>:</translate> {{#len:@@@@}} |
|||
<pre> |
|||
}} |
|||
given a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00" |
|||
</syntaxhighlight> |
|||
<translate> |
|||
<!--T:213--> |
|||
Below is the expected output: |
|||
</translate> |
|||
{|style="color:var(--color-base, #202122); background:var(--background-color-interactive--hover, #dadde3)" |
|||
| |
|||
* <translate><!--T:214--> length of <tvar name=1>Red</tvar>:</translate> 3 |
|||
* <translate><!--T:215--> length of <tvar name=1>Blue</tvar>:</translate> 4 |
|||
* <translate><!--T:216--> length of <tvar name=1>Yellow</tvar>:</translate> 6 |
|||
|} |
|||
<translate> |
|||
<!--T:217--> |
|||
More examples can be found at [<tvar name=url>https://web.archive.org/web/20211008081714/https://tw.rpi.edu/portal/Help:ArrayExtension</tvar> the former Tetherless World Wiki]. |
|||
=== Reusing keys === <!--T:59--> |
|||
1. create an array 'colors' |
|||
{{#arraydefine:colors|red;#da2021, yellow;#fcff00, green;#00ff00}} |
|||
<!--T:218--> |
|||
2. split the first element of 'colors' into another array 'colors0' |
|||
Once an array previously defined is printed, the same key can be reused for another array further down the page. As long as this sequence is observed, there is no need to define a unique key for every array. |
|||
{{#arraydefine:color0|{{#arrayindex:colors|0}}|;}} |
|||
</pre> |
|||
==== Using Loops extension ==== <!--T:60--> |
|||
Note(s) |
|||
* [http://semantic-mediawiki.org/wiki/Help:Inline_queries#Standard_parameters_for_inline_queries semantic query parameters] |
|||
** 'limit=1000' option is used to exhaust all returned results of the semantic query |
|||
** 'sep=,' option is used to set the separator for entries of the results |
|||
** 'mainlable=-' option cut of the page column |
|||
<!--T:219--> |
|||
===work with [[Extension:Loops]], iteratively access array elements=== |
|||
For more complex tasks it is possible to loop through an array using the <tvar name=1>{{ll|Extension:Loops|Loops}}</tvar> extension. |
|||
We can iteratively access elements of an array. The following code requires mediawiki extensions: |
|||
</translate> |
|||
* [[Extension:Loops]] |
|||
* [[Extension:Variables]] |
|||
<pre> |
|||
<!--define an array--> |
|||
{{#arraydefine: colors|Red,Blue,Yellow}} |
|||
<syntaxhighlight lang="html"> |
|||
<!--iteratively visit elements of an array --> |
|||
{{ #arraydefine: colors | red;#FF0000, green;#00FF00, blue;#0000FF }} |
|||
<!-- |
|||
{{ |
|||
initialize variable i |
|||
#loop: i |
|||
-->{{#vardefine: i | 0 }}<!-- |
|||
| 0 <!-- <translate nowrap><!--T:220--> loops start value for <tvar name=1>{{ #var:i }}</tvar></translate> --> |
|||
| {{ #arraysize:colors }} <!-- <translate nowrap><!--T:221--> number of loops</translate> --> |
|||
| <nowiki/> |
|||
* {{ |
|||
#arraydefine: val | {{ #arrayindex:colors | {{ #var:i }} }} | ; |
|||
}} |
|||
<span style="color:{{ #arrayindex: val | 1 }}"> |
|||
{{ #arrayindex: val | 0 }} |
|||
</span> |
|||
}} |
|||
</syntaxhighlight> |
|||
<translate><!--T:222--> This would output something like:</translate> |
|||
run loop |
|||
-->{{#while: |
|||
| {{ #ifexpr: {{ #var: i }} < {{#arraysize:colors}} | true }} |
|||
|<!-- |
|||
* {{red|red}} |
|||
one loop iteration |
|||
* {{green|green}} |
|||
--><nowiki/> |
|||
* {{blue|blue}} |
|||
* {{ #var: i }}: {{#arrayindex:colors|{{ #var: i }} }} <!-- |
|||
<translate nowrap> |
|||
=== Working with <tvar name=1>{{ll|Extension:Semantic MediaWiki}}</tvar> === <!--T:223--> |
|||
</translate> |
|||
<translate><!--T:224--> There are two ways populating an array with semantic data.</translate> <translate><!--T:225--> The first solution, using Semantic Result Formats is faster and more reliable, also works with complex data sets including record data and multiple values for one property.</translate> |
|||
<translate nowrap> |
|||
increment i |
|||
==== Using <tvar name=1>{{ll|Extension:Semantic Result Formats}}</tvar> ==== <!--T:226--> |
|||
-->{{ #vardefine: i | {{ #expr: {{ #var: i }} + 1 }} }} |
|||
</translate> |
|||
}} |
|||
<translate><!--T:227--> Semantic Result Formats (SRF) introduces the ''Array'' format in version 1.6.1.</translate> <translate><!--T:228--> It can be used to query data which will automatically be stored within an ''Extension:Arrays'' array.</translate> <translate><!--T:229--> This is the preferred solution dealing with semantic data in arrays.</translate> <translate><!--T:230--> Details can be found on the <tvar name=1>[https://semantic-mediawiki.org/wiki/Special:MyLanguage/Help:Array_format semantic-mediawiki.org]</tvar>.</translate> |
|||
'''<translate><!--T:50--> Example:</translate>''' |
|||
<pre> |
|||
{{#ask: [[Category:Color]][[:+]] |format=array |name=colors}} |
|||
{{#arrayprint: colors}} |
|||
</pre> |
</pre> |
||
<translate> |
|||
below is the expected output: |
|||
==== Using a standard query ==== <!--T:61--> |
|||
{|border=1 |
|||
</translate> |
|||
| |
|||
<translate><!--T:231--> If you can't use the SRF solution above, ''Arrays'' also allows to populate an array using a SMW query result of the [<tvar name=url>https://semantic-mediawiki.org/wiki/Special:MyLanguage/Help:List_format</tvar> list] format:</translate> |
|||
* 0: Red |
|||
* 1: Blue |
|||
* 2: Yellow |
|||
|} |
|||
'''<translate><!--T:51--> Example A:</translate>''' <translate><!--T:232--> To create a list of instances of the class <tvar name=1><code>Color</code></tvar></translate> |
|||
for live examples, follow [http://tw.rpi.edu/portal/Help:ArrayExtension#iteratively_access_array_members this URL] |
|||
<pre> |
|||
{{#arraydefine:colors|{{#ask:[[Category:Color]][[:+]] |sep =, |limit=1000}} }} |
|||
</pre> |
|||
'''<translate><!--T:52--> Example B:</translate>''' <translate><!--T:233--> To create a unique list of values of property <tvar name=1><code>has color</code></tvar></translate> |
|||
===work with [[Extension:SemanticQueryFormTool]]=== |
|||
<pre> |
|||
ArrayExtension allows users to populate an array using a SemanticQueryFormTool query result. |
|||
{{#arraydefine:colors|{{#ask:[[has color::+]][[:+]] |?color= |mainlabel=- |sep =, |limit=1000}} |,|unique}} |
|||
</pre> |
|||
'''<translate><!--T:53--> Example C:</translate>''' <translate><!--T:234--> To deal with 2D array generated by SWM query (e.g. record-type property)</translate> |
|||
to create a list of instances of the class 'Color' |
|||
<pre> |
<pre> |
||
given a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00" |
|||
{{#arraydefine: colors|{{#sask: ?Color | format=list | lastsep=}} }} |
|||
<translate nowrap><!--T:235--> 1. create an array <tvar name=1><code>colors</code></tvar></translate> |
|||
{{#arraydefine:colors|red;#da2021, yellow;#fcff00, green;#00ff00}} |
|||
<translate nowrap><!--T:236--> 2. split the first element of <tvar name=1><code>colors</code></tvar> into another array <tvar name=2><code>colors0</code></tvar></translate> |
|||
{{#arraydefine:color0|{{#arrayindex:colors|0}}|;}} |
|||
</pre> |
</pre> |
||
'''<translate><!--T:237--> Notes:</translate>''' |
|||
<span style="color:green; font-size=80%">source: thanks for zehetner@molgen.mpg.de</span> |
|||
* [[semantic-mw:Special:MyLanguage/Help:Inline queries#Standard parameters for inline queries|<translate><!--T:238--> semantic query parameters</translate>]] |
|||
** <translate><!--T:239--> <tvar name=1><code>limit=1000</code></tvar> option is used to exhaust all returned results of the semantic query</translate> |
|||
** <translate><!--T:240--> <tvar name=1><code>sep=,</code></tvar> option is used to set the separator for entries of the results</translate> |
|||
** <translate><!--T:241--> <tvar name=1><code>mainlabel=-</code></tvar> option to cut off the page column</translate> |
|||
<translate nowrap> |
|||
=== Working with <tvar name=1>{{ll|Extension:DynamicPageList3}}</tvar> === <!--T:242--> |
|||
</translate> |
|||
<translate><!--T:243--> In a similar way as described above for SMW the ''Arrays'' extension can be used to store results of a '''DPL query'''.</translate> <translate><!--T:244--> A result list can be inverted.</translate> <translate><!--T:245--> We collect all parameter values which are used by certain pages when they include a given template.</translate> <translate><!--T:246--> We store pairs of ''template parameter value'' and ''pagename''.</translate> <translate><!--T:247--> Then we sort the array and print the pairs.</translate> <translate><!--T:248--> If consecutive array elements have the same first part (i.e. the parameter values are identical), the first part is only printed once.</translate> <translate><!--T:249--> Thus we can construct a simple inverted index.</translate> <translate><!--T:250--> The same mechanism could be applied to other problems as well.</translate> |
|||
<translate> |
|||
== See also == <!--T:62--> |
|||
===related MediaWiki extensions=== |
|||
</translate> |
|||
*[[Extension:ParserFunctions | ParserFunctions]] |
|||
* {{ll|Extension:HashTables}} - <translate><!--T:251--> very similar extension for the use of hash tables in MediaWiki.</translate> |
|||
*[[Extension:VariablesExtension | VariablesExtension]] |
|||
* {{ll|Extension:Variables}} |
|||
*[[Extension:VariablesExtension | StringFunctions]] |
|||
* {{ll|Extension:Page Forms}} - <translate><!--T:253--> offers the parser function <tvar name=1>{{ll|Extension:Page Forms/Page Forms and templates#arraymap|#arraymap}}</tvar>.</translate> <translate><!--T:254--> Since input and output are handled by the same function, it does away with the need to define a key.</translate> |
|||
*[[Extension:Loops| Loops]] |
|||
* {{ll|Extension:WSArrays}} - <translate><!--T:255--> provides a set of parser functions to operate on multidimensional and associative arrays.</translate> |
|||
* {{ll|Extension:Scribunto}} - <translate><!--T:256--> Scribunto which allows you to embed Lua scripts into wikipages, which provides arrays and standard imperative programming language control flow.</translate> |
|||
* {{ll|Extension:ArrayFunctions}} - <translate><!--T:257--> a Parsoid-compatible alternative, that also works with multidimensional and associative arrays.</translate> |
|||
{{Used by}} |
|||
==Change Log== |
|||
ArrayExtension 1.1 has been tested on MediaWiki versions 1.13.3. |
|||
[[Category:Array extensions{{#translation:}}]] |
|||
History: |
|||
[[Category:Number extensions{{#translation:}}]] |
|||
* Feb 05, 2009 -- v1.1 |
|||
[[Category:Modifiable_variables_extensions{{#translation:}}]] |
|||
- update #arraydefine: replacing 'explode' by 'preg_split', |
|||
and we now allow delimitors to be (i) a string; or (ii) a perl regular expressnion pattern, sourrounded by '/', e.g. '/..blah.../' |
|||
- update #arrayprint, change parameters from "prefix","suffix" to a "template", |
|||
and users can replace a substring in the template with array value, similar to arraymap in semantic forms |
|||
- update #arrayunique, empty elements will be removed |
|||
- update #arraysort: adding "random" option to make the array of values in random order |
|||
- add #arrayreset to free all defined arrays for memory saving |
|||
- add #arrayslice to return an array bounded by start_index and length. |
|||
- add #arraysearch. now we can return the index of the first occurence of an element, return -1 if not found |
|||
- remove #arraymember, obsoleted by #arraysearch |
|||
- remove #arraypush, obsoleted by #arraydefine and #arraymerge |
|||
- remove #arraypop, obsoleted by #arrayslice |
|||
- add safty check code to avoid unset parameters |
|||
* Feb 01, 2009 -- v1.0.3 -- fixed bug on arrayunique, array_unique (PHP function) only makes values unique but does not update array index. (arraydefine is also affected) |
|||
* Jan 28, 2009 -- v1.0.2 -- fix #arraypop to support pop multiple elements; add #arrayindex |
|||
* Jan 27, 2009 -- v1.0.1 -- fix #arraydefine to support defining an empty string |
|||
* Jan 27, 2009 -- v1.0 -- First release(alpha). |
|||
Latest revision as of 18:58, 29 September 2025
Release status: stable |
|
|---|---|
| Implementation | Parser function |
| Description | Enhances parser with array functions. |
| Author(s) | Li Ding, Jie Bao, Daniel Werner |
| Latest version | 2.2.1 (2020-12-08) |
| MediaWiki | 1.31+ |
| Database changes | No |
|
|
| Licence | MIT License |
| Download | README CHANGELOG |
| Help | Help:Extension:Arrays |
| Translate the Arrays extension if it is available at translatewiki.net | |
| Issues | Open tasks · Report a bug |
The Arrays extension (formerly known as ArrayExtension) creates an additional set of parser functions that operate on arrays.
Functions
[edit | edit source]This extension defines the following parser functions:
| Group | Functions |
|---|---|
| Construct an array, (with unique, sort, print options) | #arraydefine
|
| Extract information from an array | #arrayprint, #arrayindex, #arraysize, #arraysearch, #arraysearcharray, #arrayslice
|
| Alter an array | #arrayreset, #arrayunique, #arraysort
|
| Interaction between several arrays | #arraymerge, #arrayunion, #arrayintersect, #arraydiff
|
| In case Extension:HashTables is installed, for array/hash-table interaction | #hashtoarray, #arraytohash
|
Constructing arrays
[edit | edit source]arraydefine
[edit | edit source]This function constructs an array (identified by "key") using a list of "values" separated by the "delimiter". The variable can be accessed by other functions later.
Syntax:
{{#arraydefine:key | values | delimiter | options}}
Notes:
- "values" is a list of strings separated by "delimiter".
- The resulting array is an array of strings.
- The default delimiter is
,if not specified, a delimiter can be a string (the white-spaces surrounding delimiter will be trimmed) or a Perl regular expression, e.g./\s*,\s*/(see preg_split). - Users can define an empty array (see example).
- Users can specify options including unique, sort, and print (see example).
- Options are ignored unless a delimiter is also specified.
Examples:
Define a one-element array named a
|
{{#arraydefine:a|red}}
|
Define a four-element array named b, use default delimiter (,)
|
{{#arraydefine:b|orange, red, yellow, yellow}}
|
Define/set an empty array named c
|
{{#arraydefine:c}}
|
Define a two-element array named d, using ; as delimiter
|
{{#arraydefine:d|apple; pear|;}}
|
Define a three-element array named e, using the regular expression /\s*[;,]\s*/ as delimiter
|
{{#arraydefine:e|apple, pear; orange|/\s*[;,]\s*/}}
|
Define a three-element array named f, use delimiter (,), "unique, sort=desc, print=list" options (the array elements are unique, sorted in descending order and being printed). For more option-values of sort, see #arraysort.
|
{{#arraydefine:f|orange, red, yellow, yellow |, |unique, sort=desc, print=list}}
|
Working with arrays
[edit | edit source]Extraction
[edit | edit source]arrayprint
[edit | edit source]This function prints the values of an array in customizable format.
Syntax:
{{#arrayprint:key|delimiter|pattern|subject|options}}
Notes:
- subject accepts wiki links, templates and parser functions.
- Within the subject, you don't have to escape pipe characters
|. Within the whole construct, the pattern will be searched and replaced with the current (escaped) array value of each loop. Finally, the whole string will be parsed and put into an array of results which will be imploded with delimiter as separator. - In case the array which should be printed doesn't exist, an empty string will be returned (Introduced in 1.4 alpha, part of compatibility mode).
- The default delimiter is language dependent, for English it is
,(Introduced in 2.0, part of compatibility mode).
Examples:
| Task | Example code | Output (the array b is defined above) |
|---|---|---|
| Print - using language dependent default list delimiter | {{#arrayprint:b}}
|
|
| Print - without delimiter | {{#arrayprint:b | }}
|
|
Print - using <br /> (line-break) as delimiter
|
{{#arrayprint:b |<br/> }}
|
orange
red yellow yellow |
Pretty list output where the last two elements are chained with an and (or the local languages equivalent). Even though the delimiter parameter is empty, , (or the languages equivalent) will be used since it wouldn't be pretty otherwise.
|
{{#arrayprint:b ||@ |@ |print=pretty }}
|
|
| Embed wiki link to categories | {{#arrayprint:b |<br/> |@@@@ |[[:Category:@@@@|@@@@]] }}
|
orange |
| Define a Semantic MediaWiki property value | {{#arrayprint:b |<br/> |@@@@ |[[prop1::@@@@]] }}
|
|
| Embed parser function | {{#arrayprint:b |<br/> |@@@@ |length of @@@@:{{#len:@@@@}} }}
|
|
| Embed template (with parameters) | {{#arrayprint:b|<br/>|@@@@|{{template|prop2|@@@@}} }}
|
arrayindex
[edit | edit source]This function prints the value of an array (identified by key) at position index.
Syntax:
{{#arrayindex:key|index|default}}
Notes:
- Invalid index (non-number, out of bound) will result in printing an empty string.
- The index is 0-based, i.e. the first element's index is 0.
- Negative indexes will return an element that far from the end (e.g.
-1would be the arrays last element). - default will be returned in case the array doesn't exist, the key doesn't exist within the array or if the value is an empty string.
Examples:
| Third element within array a | {{#arrayindex:a |2 }}
|
| Last element within array b | {{#arrayindex:b |-1 }}
|
| Print default value for invalid index | {{#arrayindex:c |foo |bad value }}
|
arraysize
[edit | edit source]This function returns the size (number of elements) of an array.
See https://php.net/function.count In case the given array doesn't exist the output of the function will be a void string instead of a number. This allows to check whether the array exists.
Syntax:
{{#arraysize:key}}
Examples:
| Size of array a: | {{#arraysize:a}}
|
| Check whether array a exists or not: | {{#if: {{#arraysize:a}} | ''array exists'' | ''array not defined'' }}
|
arraysearch
[edit | edit source]This function returns the index of the first occurrence of the value in the array (identified by key) starting from the position identified by index parameter, and returns an empty string when failed.
When yes and/or no specified, this will expand the value set to yes if found, value of no otherwise.
See https://php.net/function.array-search
Syntax:
{{#arraysearch:key|value|index|yes|no}}
Examples:
| Return index of first occurrence of a value | {{#arraysearch:b|white}}
{{#arraysearch:b|red}}
use offset
{{#arraysearch:b|red|0}}
{{#arraysearch:b|red|2}}
use preg regular expression match
{{#arraysearch:b|/low/}}
{{#arraysearch:b|/LOW/i}} - case insensitive
{{#arraysearch:b|low}}
use yes no print option
{{#arraysearch:b|white|0|yes|no}}
{{#arraysearch:b|yellow|0|yes|no}}
|
arraysearcharray
[edit | edit source]This function searches an array (identified by key) and creates a new array (identified by new_key) from the search with all the results. The search criteria value can be a string or a regular expression. If index is given the search will start there, limit can define the maximum search results. The parameter identified by transform can be used if value is a regular expression. It can transform the result of the matched entries into the new_key array like PHP preg_replace would do it.
Syntax:
{{#arraysearcharray:new_key|key|value|index|limit|transform}}
Notes:
- If value is a string, the new_key array will only contain entries of exact this string.
- Negative index values like
-ncan be used to search the last n entries only. - If Extension:Regex Fun is available within the wiki, Regex Fun's e modifier can be used within the regex. This has nothing to do with PHPs e modifier (which would be a security breach). With active e modifier the transform string will be parsed after back-refs are inserted, after that it will replace the actual match.
Examples:
Find all entries in array a that start with A followed by a space and put them into a new array x.
|
{{#arraysearcharray:x |a |/^A\s.+/ }}
|
Searching all entries of array a which end with numbers and put the numbers only into a new array y.
|
{{#arraysearcharray:y |a |/^.*?(\d+)$/ |0 |-1 | $1 }}
|
Searching all entries of array a which end with numbers and put the length of these items into the new array (this requires Regex Fun extension).
|
{{#arraysearcharray:y |y |/^.*?\d+$/e |0 |-1 | {{#len:$0}} }}
|
Remove empty values from array a.
|
{{#arraysearcharray:a|a|/\S+/}}
|
arrayslice
[edit | edit source]This function extracts a sub-array from an array (identified by key) into a new array (identified by new_key).
See https://php.net/function.array-slice
Syntax:
{{#arrayslice:new_key|key|offset|length}}
Notes:
- Offset indicates starting point of slice, it can be non-negative number or negative number for backwards index (e.g. the last element of the array's offset is -1). Offset is different from index (which must be non-negative number)
- Length indicates how many element to extract. If it is omitted, then the sequence will have everything from offset up until the end of the array.
- If offset is no less than array size, empty array will be returned, if offset if no greater than negative array size, a new array with all elements will be returned.
Examples:
| Extract a two-element slice starting from the element at offset 1. | {{#arrayslice:x|b|1|2}}
|
| Extract a two-element slice starting from the element at offset -2. | {{#arrayslice:x|b|-2|2}}
|
Alteration
[edit | edit source]Functions which alter an array directly instead of creating a new array.
arrayunique
[edit | edit source]This function converts an array (identified by 'key') into a set (no duplicated members, no empty element).
See https://php.net/function.array-unique
Syntax:
{{#arrayunique:key}}
Example:
| Convert array to set. | {{#arrayunique:b}}
|
arrayreset
[edit | edit source]This function will unset some or all defined arrays.
Syntax:
{{#arrayreset:}} <!-- will unset ALL arrays -->
{{#arrayreset:key1 |key2 |... |key-n }}
Notes:
- Using arraysize on them will return an empty string instead of
0, so they are really unset, not empty. To simply empty an array one can use{{#arraydefine:key}}. - Prior to version 1.4 alpha
,is used to separate several arrays which should be unset.
arraysort
[edit | edit source]This function sorts an array in the following order.
none- no sort (default)desc- in descending order (see https://php.net/function.sort)asce/asc- in ascending order (see https://php.net/function.rsort)random- in random order (see https://php.net/function.array-rand)reverse- in reverse order (see https://php.net/function.array-reverse)
Syntax:
{{#arraysort:key|order}}
Note:
- Each array element is being treated as a string, this means numbers might not be ordered as expected.
Examples:
| Sort an array. | {{#arraysort:x|desc}}
|
| Randomize an array. | {{#arraysort:x|random}}
|
| Reverse an array. | {{#arraysort:x|reverse}}
|
Interaction
[edit | edit source]Functions which work with more than one array, creating one new array or overwriting an existing one as result. Since version 2.0, these functions can interact with more than just two arrays at a time. In case they deal with only one array, they simply create a copy of that array. Any non-existent arrays will simply be ignored by these functions.
arraymerge
[edit | edit source]This function merges values of two or more arrays into a new array (identified by new_key).
See https://php.net/function.array-merge
Syntax:
{{#arraymerge:new_key |key1 |key2 |... |key-n }}
Examples:
| Merge two arrays. | {{#arraymerge:x |a |b }}
|
| Duplicate an array (keep the third argument of arraymerge empty). | {{#arraymerge:x |b }}
|
arrayunion
[edit | edit source]This function merges values of two or more arrays into a new array (identified by new_key) without duplicated values.
Syntax:
{{#arrayunion:new_key |key1 |key2 |... |key-n }}
Notes:
- This is a set operator, i.e., the returned array is a set without duplicated values.
- This is equal to arraymerge with arrayunique afterwards.
Example:
| Union of three arrays. | {{#arrayunion:x |a |b |c }}
|
arraydiff
[edit | edit source]This function computes the (set theoretic) difference of two or more arrays. The result array is identified by new_key. The returned array is a set that contains elements of the first given array (identified by key1) which are not defined within any of the other arrays. See https://php.net/function.array-diff
Syntax:
{{#arraydiff:new_key |key1 |key2 |... |key-n }}
Note:
- This is a set operator, i.e. the returned array is a set without duplicated values.
- This function can be used to test sub-class relation.
Examples:
| Diff (b-a) | {{#arraydiff:x |b |a }}
|
| Diff (a-b) | {{#arraydiff:x |a |b }}
|
| Diff (a-(b+c)) | {{#arraydiff:x |a |b |c }}
|
arrayintersect
[edit | edit source]This function computes the set theoretic intersection of two or more given arrays. The result array is identified by new_key. See https://php.net/function.array-intersect
Syntax:
{{#arrayintersect:new_key |key1 |key2 |... |key-n }}
Note:
- This is a set operator, i.e., the returned array is a set without duplicated values.
Example:
| Intersect of three arrays put into new array x | {{#arrayintersect:x |a |b |c }}
|
Installation
[edit | edit source]- Download and move the extracted
Arraysfolder to yourextensions/directory.
Developers and code contributors should install the extension from Git instead, using:cd extensions/ git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Arrays
- Add the following code at the bottom of your LocalSettings.php file:
wfLoadExtension( 'Arrays' );
- Configure as required.
Done – Navigate to Special:Versionon your wiki to verify that the extension is successfully installed.
Configuration
[edit | edit source]Arrays 2.0 introduces two configuration variables:
- $egArraysCompatibilityMode
- ($egArrayExtensionCompatbilityMode in 1.4 alpha) Set to true, this will activate the compatibility mode which will bring back the behavior of the old ArrayExtension 1.3.2 as far as possible. This is because in Version 2.0 several breaking changes have been introduced. So using this compatibility mode allows a smooth switch from 1.x to 2.x Arrays extension. By default, compatibility mode is inactive. If you have been using the old ArrayExtension within your wiki before, you might want to take a look at that list and adjust your templates before switching to Arrays without compatibility mode.
- $egArraysExpansionEscapeTemplates
- Contains a key-value pair list of characters that should be replaced by a template or parser function call within array values included into an #arrayprint. By replacing these special characters before including the values into the string which is being expanded afterwards, array values can't distract the surrounding MW code. Otherwise the array values themselves would be parsed as well. By default this will escape the following characters with the following template or parser function calls:
=={{=}}("Template:=" should print=)|={{!}}("Template:!" should print|)
Note: Starting with MW 1.24.0 it is no longer necessary to create "Template:!" since its purpose is served by the new {{!}}magic word.
{{={{((}}("Template:((" should print{{)}}={{))}}("Template:))" should print}})
- Make sure these templates or parser functions exist within your wiki or change this variable accordingly. If this is not set up properly,
#arrayprintmight print unexpected values in case one of these character sequences is being used within array values. - $egArraysExpansionEscapeTemplates also can simply be set to null, in this case it switches back to pre 2.0 behavior where array values with these character sequences did break the given subject code within
#arrayprint. If the compatibility mode is active, this will always be treated as set to null.
FAQ
[edit | edit source]Iteratively accessing array elements
[edit | edit source]It is possible to iteratively access elements of an array using #arrayprint or Extension:Loops.
Using arrayprint
[edit | edit source]<!-- define an array -->
{{#arraydefine:colors|Red,Blue,Yellow}}
{{#arrayprint:colors||@@@@|<nowiki/>
* length of @@@@: {{#len:@@@@}}
}}
Below is the expected output:
|
More examples can be found at the former Tetherless World Wiki.
Reusing keys
[edit | edit source]Once an array previously defined is printed, the same key can be reused for another array further down the page. As long as this sequence is observed, there is no need to define a unique key for every array.
Using Loops extension
[edit | edit source]For more complex tasks it is possible to loop through an array using the Loops extension.
{{ #arraydefine: colors | red;#FF0000, green;#00FF00, blue;#0000FF }}
{{
#loop: i
| 0 <!-- loops start value for {{ #var:i }} -->
| {{ #arraysize:colors }} <!-- number of loops -->
| <nowiki/>
* {{
#arraydefine: val | {{ #arrayindex:colors | {{ #var:i }} }} | ;
}}
<span style="color:{{ #arrayindex: val | 1 }}">
{{ #arrayindex: val | 0 }}
</span>
}}
This would output something like:
- red
- green
- blue
Working with Extension:Semantic MediaWiki
[edit | edit source]There are two ways populating an array with semantic data. The first solution, using Semantic Result Formats is faster and more reliable, also works with complex data sets including record data and multiple values for one property.
Semantic Result Formats (SRF) introduces the Array format in version 1.6.1. It can be used to query data which will automatically be stored within an Extension:Arrays array. This is the preferred solution dealing with semantic data in arrays. Details can be found on the semantic-mediawiki.org.
Example:
{{#ask: [[Category:Color]][[:+]] |format=array |name=colors}}
{{#arrayprint: colors}}
Using a standard query
[edit | edit source]If you can't use the SRF solution above, Arrays also allows to populate an array using a SMW query result of the list format:
Example A: To create a list of instances of the class Color
{{#arraydefine:colors|{{#ask:[[Category:Color]][[:+]] |sep =, |limit=1000}} }}
Example B: To create a unique list of values of property has color
{{#arraydefine:colors|{{#ask:[[has color::+]][[:+]] |?color= |mainlabel=- |sep =, |limit=1000}} |,|unique}}
Example C: To deal with 2D array generated by SWM query (e.g. record-type property)
given a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00"
1. create an array <code>colors</code>
{{#arraydefine:colors|red;#da2021, yellow;#fcff00, green;#00ff00}}
2. split the first element of <code>colors</code> into another array <code>colors0</code>
{{#arraydefine:color0|{{#arrayindex:colors|0}}|;}}
Notes:
- semantic query parameters
limit=1000option is used to exhaust all returned results of the semantic querysep=,option is used to set the separator for entries of the resultsmainlabel=-option to cut off the page column
Working with Extension:DynamicPageList3
[edit | edit source]In a similar way as described above for SMW the Arrays extension can be used to store results of a DPL query. A result list can be inverted. We collect all parameter values which are used by certain pages when they include a given template. We store pairs of template parameter value and pagename. Then we sort the array and print the pairs. If consecutive array elements have the same first part (i.e. the parameter values are identical), the first part is only printed once. Thus we can construct a simple inverted index. The same mechanism could be applied to other problems as well.
See also
[edit | edit source]- Extension:HashTables - very similar extension for the use of hash tables in MediaWiki.
- Extension:Variables
- Extension:Page Forms - offers the parser function #arraymap. Since input and output are handled by the same function, it does away with the need to define a key.
- Extension:WSArrays - provides a set of parser functions to operate on multidimensional and associative arrays.
- Extension:Scribunto - Scribunto which allows you to embed Lua scripts into wikipages, which provides arrays and standard imperative programming language control flow.
- Extension:ArrayFunctions - a Parsoid-compatible alternative, that also works with multidimensional and associative arrays.
| This extension is included in the following wiki farms/hosts and/or packages: |
- Stable extensions
- Parser function extensions
- ParserClearState extensions
- ParserFirstCallInit extensions
- MIT licensed extensions
- Extensions in Wikimedia version control
- All extensions
- Extensions included in BlueSpice
- Extensions included in Canasta
- Extensions included in Fandom
- Extensions included in Miraheze
- Extensions included in ProWiki
- Extensions included in semantic::core
- Extensions included in Telepedia
- Extensions included in wiki.gg
- Array extensions
- Number extensions
- Modifiable variables extensions
