Search code examples
mediawikisemantic-mediawiki

Semantic Mediawiki: Aggregation similar to GROUP BY, adding the Count


Little by little I am trying to learn Semantic Mediawiki almost like in a tutorial. I got so save info (including uri, titles and tags) for each element of a list using subobjects and then to get the list of the tag.

This is the wiki page with the list of the tags: link

Now I'd like to further explore the articles related to each tag. For example, is it possible to list the articles having the tag x? I wonder if it would be a nice idea to create a Module to parse the output of the semantic query.


Solution

  • Best solution here is to make use of array extension. Create an array containing all tags, and make it unique to have a "distinct list". Then print your array, and run an ask query for each tag in the print loop, with the count format .

    {{#arraydefine:tags| {{#ask:[[-Has subobject::{{FULLPAGENAME}}]] |?Tags#-=| mainlabel=-|limit = 1000}} |,|unique}}
    {{#arrayprint:tags|, |@@@@|<nowiki/>
    [[@@@@]] ({{#ask:[[Tags::@@@@]]|format=count}})
    }}
    

    This code will print a link to each page named as tag value, and print the number of subobjects that hold this tag. Even if the solution is not optimal, as you are running a lot of independent queries, you should not have performance issues unless you have very high traffic on your wiki.

    Nota bene : The best practice is to create a specific template for tag pages, one that list all articles having the tag. With the Page Forms extension, you can create each page automatically with this template, simply by running the job queue.