Jump to content

Extension:Arrays

From mediawiki.org
MediaWiki extensions manual
ArrayExtension
Release status: beta
Implementation Parser function
Description Enhances parser with array functions.
Author(s) Li Ding and Jie Bao
Latest version 1.0.3 (1 February 2009)
MediaWiki 1.13+
  • $wgArraysCompatibilityMode
  • $wgArraysExpansionEscapeTemplates
Licence MIT License
Download Subversion [Help]

Browse source code

ArrayExtension-1.0.3.zip
Help Help:Extension:Arrays
Example examples

ArrayExtension defines an additional set of parser functions that operate on arrays.

Functions

This module defines these functions:

group functions
construct an array, (no print) #arraydefine
print array #arrayprint,#arraysize,#arraymember, and #arrayindex
alter an array (no print) #arraysort, #arrayunique,#arraypush,and #arraypop
create a new array (no print) #arraymerge
create a new array without duplicates (no print) #arrayintersect,#arraydefine,and #arraydiff

arraydefine

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}}

Note(s):

  • 'values' is a list of strings separated by 'delimiter'
  • the resulting array is an array of strings.
  • the default delimiter is ',' if not specified
  • the white-spaces surrounding delimiter will be trimmed
  • this function shows nothing
  • users can define an empty array, or reset an array by leaving the 'values' empty (see example)


Example(s):

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 array named 'c' empty
{{#arraydefine:c}}
define a one-element array named 'd', use no delimiter
{{#arraydefine:d|apple, pear||}}
define a one-element array named 'e', using ';' as delimiter
{{#arraydefine:e|apple, pear|;|}}

arrayprint

This function prints the values of an array in comtomizable format.

Syntax:

{{#arrayprint:key|delimiter|prefix|suffix}}

The customizable output format is:

prefix <value_1> suffix  delimiter prefix <value_2> suffix ... prefix <value_n> suffix 

Note(s):

  • the following characters are escaped
 [  => \[
 {  => \{
 }  => \}
 \  => \\
  • there should be at least a white space between escaped '\}' and the end of template '}}'

Example(s):

print - use default delimiter ','
{{#arrayprint:b}}
print - use '<br>' as delimiter
{{#arrayprint:b|<br/>}}
print - use '' as delimiter and use prefix and suffix
{{#arrayprint:b||-begin-|-end-}}
print - add prefix and suffix to create wiki links
{{#arrayprint:b|<br/>|\[\[|]]}}
print - add prefix and suffix to create wiki template
{{#arrayprint:b|<br/>|\{\{#set:a=|\}\} }}
print - add prefix and suffix to create SMW links
{{#arrayprint:b|<br/>|\[\[name::|]]}}

arraysize

This function returns the size (number of elements) of an array. See: http://www.php.net/manual/en/function.count.php

Syntax:

{{#arraysize:key}}

Example(s):

size:
{{#arraysize:b}}

arraymember

This function checks if 'value' is member of the array (identified by 'key'), and returns '1' (yes), '0' (no). See: http://www.php.net/manual/en/function.in-array.php

Syntax:

{{#arraymember:key|value}}

Example(s):

membership test

http://www.php.net/manual/en/function.in-array.php

{{#arraymember:a|white}}
{{#arraymember:a|red}}

arrayindex

This function print the value of an array (identified by 'key') at position 'index'. See: http://www.php.net/manual/en/function.in-array.php

Syntax:

{{#arraymember:key|value}}

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.

Example(s):

array index test
{{#arrayindex:b|2}}

arraysort

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
  • none (default) - no sort
  • desc - in descending order
  • asce - in ascending order


Syntax:

{{#arraysort:key|order}}

Note(s):

  • each array element is a string

Example(s):

sort an array
{{#arraysort:x|desc}}

arrayunique

This function converts an array (identified by 'key') into a set (no duplicated members). see: http://www.php.net/manual/en/function.array-unique.php

Syntax:

{{#arrayunique:key}}

Example(s):

convert array to set
{{#arrayunique:b}}

arraypush

This function pushes a set of values at the end of an array (identified by 'key'). See: http://www.php.net/manual/en/function.array-push.php

Syntax:

{{#arraypush:key|values|delimiter}}

Note(s):

  • this function helps add elements to an array (identified by 'key')

Example(s):

push
{{#arraypush:a|grey,black,black,black|,}}

arraypop

This function popes a positive 'number' (default 1) of values from the end of an array (identified by 'key'). See: http://www.php.net/manual/en/function.array-pop.php

Syntax:

{{#arraypop:key|number}}

Note(s):

  • if the 'number' is larger than the size of the array, the array will be emptied
  • setting 'number' to a non-number or a non-positive number will not cause any operation.

Example(s):

pop the last element
{{#arraypop:a}}
pop the last 5 elements
{{#arraypop:a|5}}

arraymerge

This function merges values of two arrayes (identified by 'key1' and 'key2') into a new array (identified by 'key'). See: http://www.php.net/manual/en/function.in-array.php

Syntax:

{{#arraymerge:key|key1|key2}}

Note(s):

  • this merge is different from array_merge offerred by PHP because it merges values instead of keys

Example(s):

merge two arrays
{{#arraymerge:x|a|b}}
duplicate an array (keep the third argument of arraymerge empty
{{#arraymerge:x|b}}

arrayintersect

This function computes the set theoretic interesection of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. See: http://www.php.net/manual/en/function.array-intersect.php

Syntax:

{{#arrayintersect:key|key1|key2}}

Note(s):

  • this is a set operator, i.e., the returned array is a set without duplicated values.

Example(s):

intersect
{{#arrayintersect:x|a|b}}

arrayunion

This function computes the set theoretic union of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. See: http://www.php.net/manual/en/function.array-union.php

Syntax:

{{#arrayunion:key|key1|key2}}

Note(s):

  • this is a set operator, i.e., the returned array is a set without duplicated values.
  • similar to arraymerge, this union operation works on values.

Example(s):

union
{{#arrayunion:x|a|b}}

arraydiff

This function computes the (set theoretic) difference of two given arrays (identified by 'key1' and 'key2'), and the result array is identified by 'key'. 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

Syntax:

{{#arraydiff:key|key1|key2}}

Note(s):

  • 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

Example(s):

diff (b-a)
{{#arraydiff:x|b|a}}
diff (a-b)
{{#arraydiff:x|a|b}}

Installation

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. Please use the following instructions to install this extension.

1. Install source code

  • you can copy the source code from SVN, and put them under "WIKI-PATH/extentions/ArrayExtension"
  • or if you have shell access, you can install using svn
 svn co http://smwbp.googlecode.com/svn/trunk/mediawiki/extensions/ArrayExtension/

2. Append the following to LocalSettings.php (near the bottom) of your MediaWiki installation:

require_once ("$IP/extensions/ArrayExtension/ArrayExtension.php");

FAQ

ArrayExtension allows users to populate an array using a SMW query result.

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. n-ary property)

given a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00"

1. create an array 'colors'
{{#arraydefine:colors|red;#da2021, yellow;#fcff00, green;#00ff00}}

2. split the first element of 'colors' into another array 'colors0'
{{#arraydefine:color0|{{#arrayindex:colors|0}}|;}}

Note(s)

  • 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

work with Extension:Loops, iteratively access array elements

We can iteratively access elements of an array. The following code requires mediawiki extensions:

<!--define an array-->
{{#arraydefine: colors|Red,Blue,Yellow}}

<!--iteratively visit elements of an array -->
<!--
initialize variable i
-->{{#vardefine: i | 0 }}<!--

run loop
-->{{#while:
  | {{ #ifexpr: {{ #var: i }} < {{#arraysize:colors}} | true }}
  |<!--

one loop iteration
--><nowiki/>
* {{ #var: i }}: {{#arrayindex:colors|{{ #var: i }} }} <!--

increment i
-->{{ #vardefine: i | {{ #expr: {{ #var: i }} + 1 }} }}
}}

below is the expected output:

  • 0: Red
  • 1: Blue
  • 2: Yellow

for live examples, follow this URL

ArrayExtension allows users to populate an array using a SemanticQueryFormTool query result.

to create a list of instances of the class 'Color'

{{#arraydefine: colors|{{#sask: ?Color | format=list | lastsep=}} }}

source: thanks for zehetner@molgen.mpg.de

Change Log

ArrayExtension 1.0 has been tested on MediaWiki versions 1.13.3.

History:

  • 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).