Uit wikibase-solutions.com
Ga naar:navigatie, zoeken

<ul><li>"No" is niet herkend als een booleaanse waarde (waar/onwaar).</li> <!--br--><li>"Yes" is niet herkend als een booleaanse waarde (waar/onwaar).</li></ul>

Developer Log #3 :: WSArrays

If you are somewhat in the MediaWiki scene, you have probably heard of an extension called Arrays. It is a very useful extension that allows you to create simple arrays from comma separated strings. It also allows you to manipulate and parse those arrays. For instance, you can map them onto a template or sort them.

However, the Arrays extension has some major limitations. It doesn't allow you to create associative or multidimensional arrays. If you don't know what those words mean, I'll explain them for you. An associative array is a data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection (Wikipedia, 2019). A multidimensional array is like a list of lists. You can create an array (list) of items, and those items can themselves be an array (aka list).

As you might be able to see, those are some really useful functionalities that the Arrays function lacks. That is why Wikibase Solutions developed a new arrays extension, namely WSArrays, which adds those functionalities and much more!

WSArrays adds over 15 parser functions that allow you to create, extract, manipulate, sort and display complex, associative and multidimensional arrays. WSArrays uses a custom version of the widely available JSON format for defining arrays. This version of JSON is called WSON (Wikibase Solutions Object Notation) and is almost identical to JSON, with the accolades replaced with double brackets ( } -> )), { -> (( ). The reason we chose for this weird data serialisation language is because of the way MediaWiki parses nested accolades (it doesn't).

Let's define an array:

{{#complexarraydefine: ArrayName |
[
    ((
        "wikibase": "solutions"
        "subarray": [
            "item1",
            "item2"
        ]
    )),
    ((
        "ws": "arrays"
    )),
    ((
        "solutions": "wikibase",
        "wikibase": "solutions"
    ))
]
}}

With WSArrays, you can easily view the array you have just defined using the {{#complexarrayprint: ArrayName}} function. Let's do that now:

  • 0
    • wikibase: solutions
    • subarray
      • item1
      • item2
  • 1
    • ws: arrays
  • 2
    • solutions: wikibase
    • wikibase: solutions


As you can see, WSArrays allows you to easily view and debug otherwise extremely complex looking arrays. Another very useful feature of WSArrays is its sort function. You can sort on a multitude of ways, all of which are documented on the MediaWiki page of this extension here.

One of WSArrays coolest feature is its close integration with Semantic MediaWiki. If you have Semantic MediaWiki installed, you can choose to enable the 'complexarray' result printer (format=complexarray). This result printer will define a new array with a name of your choosing containing all the information Semantic MediaWiki has for that query. Let's query some (fictional) vehicles as an example. We want to know the vehicle's model, license number and build year, so we will execute the following query:

{{#ask: [[Class::Car]]
|?Model
|?License number
|?Build year
|limit=2
|format=complexarray
|name=ArrayName
}}

We will get the following beautifully formatted result from {{#caprint: ArrayName}}:

  • 0
    • Model
      • fulltext: DeLorean DMC-12
      • fullurl: //wikibase.nl/wiki/DeLorean_DMC-12
      • namespace: 0
      • exists: 1
      • displaytitle: DeLorean Timetraveller
    • License number: OUTATIME
    • Build year: 1985
    • catitle: DeLorean DMC-12 (OUTATIME)
    • cafullurl: //wikibase.nl/DeLorean_DMC-12_(OUTATIME)
    • canamespace: 0
    • caexists: 1
    • cadisplaytitle: Car of Dr. Emmett Brown
  • 1
    • Model
      • fulltext: The Batmobile
      • fullurl: //wikibase.nl/wiki/The_Batmobile
      • namespace: 0
      • exists: 1
      • displaytitle:
    • License number: BAT-MAN
    • Build year: 1960
    • catitle: Batmobile (BAT-MAN)
    • cafullurl: //wikibase.nl/wiki/Batmobile_(BAT-MAN)
    • canamespace: 0
    • caexists: 1
    • cadisplaytitle: Car of Bruce Wayne


As shown above, a lot of information about page names, display titles et cetera is provided by Semantic MediaWiki. WSArrays allows you to easily map this information onto a template or simply display this information in the format above.

WSArrays still has many other features, such as mapping the array on a template or string, array slicing, array push, merging arrays and much more; the documentation for all those functions can be found here. The source code of the extension can be viewed in its entirety here.

Cookies helpen ons onze services aan te bieden. Door onze services te gebruiken stemt u in met het gebruik van onze cookies.