Extension:WSArrays

The WSArrays (or ComplexArrays) extension creates an additional set of parser functions that operate on multidimensional and associative arrays.

Functions
This extension defines several parser functions:

complexarraydefine
This function creates a new array, identified by 'key'. The syntax used to define a new array is called WSON, a MediaWiki friendly version of JSON. The syntax of WSON is exactly the same as that of JSON, however, in WSON all { and } are replaced with respectively (( and )).

An example:

(( "foo": "bar", "baz": ["qux", "quux"] ))

Explanation

 * key (required): The name of the array;
 * array (required): The array in WSON (see above) or using a delimiter.
 * delimiter (optional, default: ,): The delimiter used when defining a simple array.
 * noparse (optional, default: 0): Set this to the correct level to tell the parser what to parse in the 'array' (see ).

Examples
It could be possible your array comes out malformed when you provide #complexarraydefine with invalid WSON. This is because when the input supplied is not valid WSON, the parser assumes you are trying to define a simple array.

complexarraydefinedarrays
This debug function creates a new array containing the keys of all currently defined arrays.

Explanation

 * key (required): The name of the array that will contain the keys of the currently defined arrays

complexarrayprint
This function prints the values of the array in a custom format.

Explanation

 * key (required): The name of the (sub)array that should be printed;
 * options (optional):
 * ordered: Print the result as an ordered list;
 * map: Map the result in a string;
 * markup: Print the result as WSON.
 * map (optional, required when the option is map): The string the result should be mapped on;
 * subject (optional, default is @@): The string that should be replaced on the map.
 * noparse (optional, default: 0): Set this to the correct level to tell the parser what to parse in the 'subject' (see ).

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * foo: quz
 * baz: bar
 * 2
 * baz: bar
 * foo: bar
 * 3
 * baz:.

complexarraysize
This parser function returns the size of a (sub)array.

Explanation

 * key (required): The name of the (sub)array;
 * options (optional):
 * top: Only count the number of items in the top array (not recursively).

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * foo: quz
 * baz: bar
 * 2
 * baz: bar
 * foo: bar

complexarrayslice
This parser function can extract a slice from an array.

Explanation

 * new_key (required): The name of the array that is going to be created;
 * key (required): The name of the array that needs to be sliced;
 * offset (required): For a positive offset, the new array will start at that offset in the first array. For a negative offset, the new array will start that far from the end of the first array;
 * length (optional): The length of the new array.

Examples
We have an array called foobar:


 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar
 * 2
 * boo: bar
 * far: quz
 * 3
 * foo: quux
 * bar: bar

complexarraysearch
This parser function searches through an array for a keyword and returns the corresponding key of the first match. If there are no matches, it will return 0.

Explanation

 * key (required): The name of the array that is going to be searched;
 * keyword (required): The keyword that is going to get searched for.

Examples
We have an array called foobar:


 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar
 * 2
 * boo: bar
 * far: quz
 * 3
 * foo: quux
 * bar: bar

complexarraysearcharray
This parser function searches through an array for a keyword and creates a new array with the corresponding keys of the matches. If there are no matches, it will return 0.

Explanation

 * new_key (required): The name of the array that is going to be created containing all the keys;
 * key (required): The name of the array that is going to be searched;
 * keyword (required): The keyword that is going to get searched for.

Examples
We have an array called foobar:


 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar
 * 2
 * boo: bar
 * far: quz
 * 3
 * foo: quux
 * bar: bar

complexarrayaddvalue
This parser function adds a value or subarray to an existing array or replaces an existing subarray with that value.

Explanation

 * key (required): The name of the subarray that will be created;
 * value (required): A value in plaintext or a subarray in WSON.

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * foo: quz
 * baz: bar
 * 2
 * baz: bar
 * foo: bar

complexarrayreset
This parser function resets all arrays or one array.

Explanation

 * key (optional): The name of the array that must be unset, when empty, all arrays will be unset.

complexarraysort
This parser function sorts an array.

Explanation

 * key (required): The array that needs to be sorted;
 * options (optional):
 * multisort: Sort the array using multisort;
 * asort: Sort the array using asort;
 * arsort: Sort the array using arsort;
 * krsort: Sort the array using krsort;
 * natcasesort: Sort the array using natcasesort;
 * natsort: Sort the array using natsort;
 * rsort: Sort the array using rsort;
 * shuffle: Shuffle the array;
 * keysort: Sort a two-dimensional array according to the values of a key in the second array. You can change the order to descending by appending ",desc" in options (so "keysort,desc").
 * sortingkey (optional, required when using keysort): The key with which the array must be sorted when using keysort.

See the PHP documentation for the sorting algorithms.

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * foo: quz
 * baz: bar
 * 2
 * baz: bar
 * foo: bar

complexarrayunique
This parser function removes duplicate keys and values from a array.

Explanation

 * key (required): The name of the array.

complexarraypush
This parser function pushes a new value or subarray to the end of an existing (sub)array.

Explanation

 * key (required): The name of the (sub)array;
 * value (required): A value in plaintext or a subarray in WSON.
 * noparse (optional, default: 0): Set this to the correct level to tell the parser what to parse in 'value' (see ).

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * foo: quz
 * baz: bar
 * 2
 * baz: bar
 * foo: bar

complexarrayextract
This parser function creates a new array from a subarray.

Explanation

 * new_key (required): The name of the array that is going to be created;
 * subarray (required): The subarray that needs to be extracted.

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar

complexarraymerge
This parser function creates a new array by merging two or more arrays.

Explanation

 * new_key (required): The name of the array that is going to be created;
 * key1 (required): The name of the first array;
 * key2 (required): The name of the second array;
 * keyn (optional): The name of the n'th array;
 * options
 * recursive: Merge recursively
 * recursive: Merge recursively

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar

and an array called 'boofar':


 * 0
 * boo: bar
 * far: quz
 * 1
 * foo: quux
 * bar: bar

complexarraypusharray
This parser function creates a new array by pushing one or more arrays to the end of another array.

Explanation

 * new_key (required): The name of the array that is going to be created;
 * key1 (required): The name of the first array;
 * key2 (required): The name of the second array;
 * keyn (optional): The name of the n'th array;
 * keyn (optional): The name of the n'th array;

Examples
We have an array called 'foobar':


 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar

and an array called 'boofar':


 * 0
 * boo: bar
 * far: quz
 * 1
 * foo: quux
 * bar: bar

complexarraymap
This parser function iterates over an array and maps the values of the array onto a mapping key.

Explanation

 * key (required): The name of the array that is going to be mapped;
 * mapping_key (required): The keyword that will be replaced in the subject;
 * subject (required): The string on which the mapping will be applied;
 * delimiter (optional): The delimiter appended to every line, except the last;
 * hide (optional): Set this to 'true' in order to hide the mapping key when no string value for it exists.

Examples
We have an array called foobar:


 * 0
 * bar
 * quz
 * 1
 * bar
 * bar
 * 2
 * bar
 * quz
 * 3
 * quux

complexarraymaptemplate
This parser function maps the contents of an array in a template.

Explanation

 * key (required): The name of the array that needs to be mapped;
 * template (required): The name of the template the array must be mapped to.

Examples
We have an array called 'foobar', an array called 'boofar' and an array called 'bazbar', resp.:

foobar:
 * 0
 * foo: bar
 * baz: quz
 * 1
 * baz: bar
 * foo: bar
 * 2
 * foo: quz
 * baz: bar
 * bar
 * bex
 * box

boofar:
 * foobar
 * bazbar
 * boofar

bazbar:
 * foo: bar
 * boo: far

Anonymous
These functions use anonymous arrays. The function handles both the definition and output of the array. You do not have to define an array beforehand.

complexarrayarraymap
This parser function is very similar to, but the input and output is handled in the same function (which makes it anonymous).

Explanation

 * value (required): The value to be split;
 * delimiter (required): The delimiter to split value on;
 * variable (required): String to be replaced in 'formula';
 * formula (required): String where 'variable' is replaced;
 * new_delimiter (required): The delimiter between results.

Wairudokado operator
The Wairudokado operator (Japanese: ワイルドカード, pronounced Wairudokādo, meaning wildcard; a tribute to the Scope Resolution Operator in PHP) is a powerful operator that gives users the ability to use wildcards when manipulating or printing subarrays.

Example
This operator can be used on every parser function that supports subarrays (such as ). Suppose we have the array foobar:


 * 0
 * name: Foo
 * 1
 * name: Bar
 * 2
 * name: Baz
 * 3
 * name: Bex

And we want to print every value of "name". We could use a to iterate over the array and print each value seperately, but we can also use a Wairudokado operator in the #complexarrayprint function.

Semantic MediaWiki
This extension introduces a new format for displaying Semantic MediaWiki results. In order to use the new format, use. Because the result printer defines a new complexarray, you need to provide a name for that array using ; if   is left empty, the result printer will print the array as WSON.

Semantic variables
A list of semantic variables is included in the complexarray when you query a list of pages. The following variables are included:


 * catitle: The title of the page
 * cafullurl: The full URL of the page
 * canamespace: The namespace the page lives in
 * caexists: Whether or not the page exists (0/1)
 * cadisplaytitle: The displaytitle of the page

Additional parameters
WSArrays defines three new parameters:


 * name: The name of the array that is going to be created
 * simple: Option to hide Semantic variables from page results as well as hiding mailto: prefixes on email results (yes/no, default: yes)

Writing extensions
While WSArrays provides many functions for manipulating and showing arrays, sometimes these are not enough. For this reason, we provide an interface to easily extend the functionality of WSArrays with new parser functions.

Extensions are placed in the extensions/ directory. Each extension is implemented as a derived class from. The name of the file in which this class will be located, must be the same as the class name (case-sensitive). An example for the class  would be. The class must implement at least the following methods:

Returning the result
is executed by the parser hook. You can read how to write parser functions here. The documentation below is specific to WSArrays.

Manipulating arrays
All arrays in WSArrays must be stored as a SafeComplexArray object. If you want to manipulate an existing array, you must convert the SafeComplexArray object to an array, manipulate the array and then convert it back to a SafeComplexArray object.

or when using :

Registering the extension
In order to enable the extension, you must add the extension name and aliases to.

Add the following to :

Manipulation of the parser
WSArrays manipulates the Parser in several ways. In order to use WSArrays to its fullest extend, it is important to know about this. This only applies to the following parser functions:


 * 
 * 
 * 

How WSArrays manipulates the parser
WSArrays manipulates the parser by using SFH_OBJECT_ARGS. This way, the parser arguments are passed as PPNode objects instead of plain text. This allows for conditional expansion of the parse tree (see doc.wikimedia.org for more information). Using SFH_OBJECT_ARGS we can tell the parser to not parse the elements and return the raw text inputted by the user, which we can then parse in WSArrays.

How to control the manipulation
Different people have different needs, which is why we allow you to control the manipulation of the parser. Again, this documentation only applies to the parser functions listed above.

By default, all input is automatically parsed by the parser before it is passed over to WSArrays (except for the 'map' in and .) This allows you to use template parameters as well as magic words and templates when manipulating or defining arrays. This is however not always what you want. For instance, when you want to use a very complex template inside of an array, and you don't want to completely break when you use on it, you can tell WSArrays to not parse the input. If you then print the array, the template is printed as if it were enclosed with nowiki tags.

What if I want to parse the template later on?
Not parsing the template entirely is probably not what you want. WSArrays tells the parser to parse the element when necessary, such as when using it in a map or when printing it as a value.

Parser levels
WSArrays has five parser levels.

Configuration
WSArrays has 3 configuration parameters.


 * Option to skip version control and force the initialization of WSArrays, accepts a boolean (default: false).
 * Option to skip version control and force the initialization of WSArrays, accepts a boolean (default: false).


 * Option to enable the Semantic MediaWiki result printer, accepts a boolean (default: false).
 * Option to enable the Semantic MediaWiki result printer, accepts a boolean (default: false).


 * Option to escape HTML characters in arrays, accepts a boolean (default: false).
 * Option to escape HTML characters in arrays, accepts a boolean (default: false).