Extension:Arrays

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

Functions
This extension defines the following parser functions:

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:

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, 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 preg_split)
 * users can define an empty array (see example)
 * users can specify options including unique, sort, and print (see example). new

Example(s):

arrayprint
This function prints the values of an array in customizable format.

Syntax:

Note(s):
 * 'subject' now accept wiki link, templates and parser functions. new

Example(s):

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

In case the given array doesn't exist the output of the function will be a void string instead of a number.

Syntax:

Example(s):

arraysearch
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 '-1' when failed. when 'yes' and 'no' specified, print value of yes if found, and value of no otherwise. See: http://www.php.net/manual/en/function.array-search.php

Syntax:

Example(s):

arrayindex
This function print the value of an array (identified by 'key') at position 'index'.

Syntax:

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.
 * users can specify options on default value

Example(s):

arraysort
This function sorts an array in the following order. Syntax:
 * none (default) - no sort
 * desc - in descending order (see: http://www.php.net/manual/en/function.sort.php)
 * asce/asc - in ascending order (see: http://www.php.net/manual/en/function.rsort.php)
 * random - in random order (see: http://www.php.net/manual/en/function.array-rand.php)
 * reverse - in reverse order (see: http://www.php.net/manual/en/function.array-reverse.php)

Note(s):
 * each array element is a string

Example(s):

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

Syntax:

Example(s):

arrayreset
This function free-up some or all defined arrays.

Syntax:

arraymerge
This function merges values of two arrayes (identified by 'key1' and 'key2') into a new array (identified by 'new_key'). See: http://www.php.net/manual/en/function.array-merge.php Note: a wrong parameter will terminate this operation, and the array identified by 'new_key' will not be affected.

Syntax:

Note(s):
 * this merge is different from array_merge offered by PHP because it merges values instead of keys

Example(s):

arrayunion
This function merges values of two arrayes (identified by 'key1' and 'key2') into a new array (identified by 'new_key') without duplicated values.

Syntax:

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):

arraysearcharray
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:

Note:
 * If value is a string the new_key array will only contain entries of exact this string.
 * Negative index values like  can be used to search the last n entries only.

Example(s):

arrayslice
This function extract a sub-array from an array (identified by 'key') into a new array (identified by 'new_key'). See: http://www.php.net/manual/en/function.array-slice.php

Syntax:

Notes:
 * 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). 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

Example(s):

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

Syntax:

Note(s):
 * this is a set operator, i.e., the returned array is a set without duplicated values.

Example(s):

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

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):

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 svn co http://smwbp.googlecode.com/svn/trunk/mediawiki/extensions/ArrayExtension/
 * Copy the source code from SVN
 * In your extensions folder, create a new folder called ArrayExtension
 * Add the new files to the new  ArrayExtension folder
 * Alternatively, if you have shell access, you can install using svn

2. Add following to LocalSettings.php (near the bottom):

Working with Extension:SemanticMediaWiki
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.

Using Extension:Semantic Result Formats
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 ArryExtension array. This is the preferred solution dealing with semantic data in arrays. Details can be found on the semantic-mediawiki.org.

Example:

Using a standard query
If you can't use the SRF solution above, ArrayExtension 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'

Example B: To create a unique list of values of property 'has color'

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 'colors'

2. split the first element of 'colors' into another array 'colors0'

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
 * 'mainlabel=-' option cut of the page column

work with Extension:DynamicPageList
In a similar way as described above for SMW the ArrayExtension can be used to store results of a DPL query.

In the Example we show how 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.

iteratively access array elements
We can iteratively access elements of an array using with the following code:

below is the expected output:

for live examples, follow this URL

work with Extension:SemanticQueryFormTool
ArrayExtension allows users to populate an array using a SemanticQueryFormTool query result.

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

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 'colornames'

2. create an array 'colorcodes'

this can be combined with filtering 3. create an array 'none_red_colornames' but leave out any color containing 'red'

source: thanks for zehetner@molgen.mpg.de

Change Log
The latest ArrayExtension has been tested on MediaWiki version 1.17


 * A complete change log can be found within the RELEASE-NOTES