Semantic mediawiki #ask query: Displaying nested properties on the same query - mediawiki

I would like to display in the same query properties of a page which is related to the pages im querying for.
Let's say I would like to query all the pages in the City category, which are located in Germany, and I want to display the title of the page, but also I want to display the surface data of Germany, for example.
Something like this: {{#ask: [[Category:City]] [[location::Germany]] |?mainlabel |?Location.surface }}
I know this wont work, but you can see what I want to achieve.

I'm not sure if there's a way to nest queries directly inside other queries. The normal method of doing it is using a template. So you might define a template (or subpage of the template if this going into a template) called {{tablerow}} that consists of:
<includeonly>
|- valign="top"
| [[{{{1|}}}]]
| {{#show: {{{1|}}} | ?surface }}</includeonly>
The <includeonly> tags are important for reasons I don't really understand, it produces errors sometimes if you leave them out. Then you just run an #ask query with format = template. (You can build the header into the query, but I find it simpler to just put it outside.)
{| class="wikitable smwtable sortable"
|- valign="bottom"
! [[City]]
! [[Surface]]
{{#ask: [[Category:City]] [[location::Germany]]
| format = template
| template = tablerow
| link = none
}}
|}
That will punch each result returned by the query through the template as {{{1}}} and generate a row based on it. If you have other data to return from the main query, additional properties that you ask for will come out as consecutive unnamed parameters (so if you include | ?population, that will go into the template as {{{2}}} and will need to be added to the row structure or it will be dropped).

Related

Is there a way to set a "new" parameter, internally to a template or other page?

Let's say I have some page which can take two parameters, x and y. I often find myself doing things in that page like in this silly little example:
{{#if: {{{x|{{{y|}}}}}} | '''{{{x|{{{y|}}}}}}'''. I repeat, {{{x|{{{y|}}}}}}.}}
In many cases, I would greatly prefer to simplify the reuse of {{{x|{{{y|}}}}}}, for clarity, for ease of typing, and for making it more difficult to miss or to screw up. So, I'd like to do something like the following hypothetical syntax:
{{#set:z|{{{x|{{{y|}}}}}}}}
{{#if: {{{z|}}} | '''{{{z}}}'''. I repeat, {{{z}}}.}}
That is, make a new parameter, z, which was not passed to the page, but nonetheless can be used in the page. Is there a way to do something like this?
There is the extension Variables, but it assumes a certain order of parsing, which will not be the case when pages are parsed with Parsoid.
To guarantee the right order of parsing you can use Lua.
But the simplest way will be to create a new template, taking only one parameter and to call it from the original one.
Template1:
{{template2| z = {{{x|{{{y|}}}}}} }}
Template2:
{{#if: {{{z}}} | '''{{{z}}}'''. I repeat, {{{z}}}.}}

Conditional categories

I'm using a template {{MyTemplate|foo}} which consists of the following code:
{{#switch: {{{1}}}
| foo = [[Category:Foo]]
| bar = [[Category:Bar]]
| #default = [[Category:Unknown]]
}}
When I'm using it on a page, the categories show up nicely as wished at the bottom of that page. But unfortunately inside of that category-page (Category:Foo for example) the page doesn't show up.
If I add [[Category:Foo]] manually to that page, the page shows up in the Foo-category-page.
Any suggestions why this doesn't work via the template?
Does the template page that contain the previous code appear under Unknown category?. If yes, then any page contain that template must appear in it's category. You may just have to clear cache, and wait few seconds if you have a large wiki.
Make sure that foo & bar categories are not hidden in your wiki. See this: https://www.mediawiki.org/wiki/Help:Categories#Hidden_categories
EDIT
Also, try one of these solutions, which can do the same goal:
{{#switch: {{{1}}}
| foo = [[Category:Foo]]
| bar = [[Category:Bar]]
| #default = [[Category:Unknown]]
}}
OR
{{#ifeq: {{{1}}} | foo | [[Category:Foo]] | {{#ifeq: {{{1}}} | bar | [[Category:Bar]] | [[Category:Unknown]]}} }}
Do you run the jobqueue on your wiki? Linkstables are updated asynchronously and not at page save time.
https://www.mediawiki.org/wiki/Manual:Job_queue
https://www.mediawiki.org/wiki/Manual:RunJobs.php
Cant understand the description to your problem, but anyway, it seems like you could achieve the same goal by:
{{#vardefine: my_category |wow}}
[[Category: {{#ifeq: {{#var:my_category}}| default| unknown| {{#var:my_category}} }} ]]
Which adds this category to the page,
In my opinion, you should use switch when you are adding category in any case but also decide not to add category in another case.
If you are adding category in all cases, you can write as i wrote above, or export the final category to another variable and use it inside the category.
As for your problem, you can use "?action=purge" for this page, if that wont work, write here some more info.

best way to pull pictures for an item, from a separate table, in django

Im having a hard time deciding what is the best way to pull pictures from a separate table, for a certain item.
In django, I have two tables:
class item(models.Model):
title..
descr..
class item_pic(models.Model):
item=models.ForeignKey(item)
pic=models.ImageField...
Im displaying the item.title and item.descr in a div, and in the same div I want to pull the pictures.
So far I've tried creating a template filter like item|getPics which returns a list of the pics for that item.
{% for pic in item|getPics %} <img src="/uploads/{{pic.pic}}"> {%endfor%}
In the end I got rid of this since it was firing too many sql queries
Right now I finished pulling the pictures through ajax, the only downside here is that the images take some time to appear. And I assume is the same number of sql queries (only that django debug toolbar doesnt see them since they are fired up in the background).
How would you approach this? Thanks for any tips!
You should use select_related() (docs) to get the image objects together with the items.
It would look like this:
item = item.objects.select_related().get(...)
This means you won't get additional database queries when you're retrieving the item_pics.
As a final note, it's better to capitalize the names of classes as this is convention in Python/Django.

How to represent invisible name-value pair data associated with HTML table row?

Background
I am presenting data using a HTML frameset. The left-side frame is a navigation tree-table constructed as an HTML table. Only minimal data is shown in this frame because I want to use the right-side "details" frame to give the user more details when he selects one of the navigation table rows.
+----------------------------+
| | |
| tree | "details" |
| table | pertaining to |
| nav. | selected |
| | row |
|=selected=| |
| | |
| | |
| | |
+----------------------------+
Think of this like a directory browser where you can see filesize, type, modification date, etc. on the right when you select an item in the left-hand tree.
Obtaining item details server-side is a sequential task, i.e. to get details on the nth item, the server has to work through all n-1 preceding items. For this reason, I think the most straightforward way to present the detailed data to the user is to have the server embed all detailed information within the navigation table rows and have a script generate the details page in a right-hand frame.
Question
How should I represent the detailed data within the navigation table HTML? Should I make up my own element tagnames? Should I use extra columns that are not displayed? Or something else? The data is typically name-value pairs - both name and value can be text. Each element may have a different set of data pairs. Is there a standard way to represent user data within an (X)HTML document?
NEVER, EVER EVER EVER EVER EVER EVER mix data and display. I also think you can easily get around the iterating over n elements to get the data you require. Here is how you do it.
Create a model (your data storage) in the javascript.
var data = [
{
title: "item 1",
foo: "bar",
baz: 10
},
{
title: "item 2",
foo: "bazbar",
baz: 20
}
];
Then, using Javascript, you could use the above data to create the following table on the left
<table>
<tr><td>item 1</td></tr>
<tr><td>item 2</td></tr>
</table>
So then you would have your show details function
function showDetails(index){
var currentData = data[index];
/* Do something with data */
}
I have created a working example here. There is an error in that code that says showDetails is not defined, but that is due to a jsfiddle issue, the code will work if put into a HTML page. ALSO, be sure to use the strict doctype at the top (to avoid cross browser quirsk).
Might I also suggest, that you look at YUI 2's layout manager instead of using a frameset. Framesets require multiple pages with javascript snaked throughout and can be a maintenance nightmare down the road.

Pass a template parameter to a semantic mediawiki query

In page "Massachusetts", I have defined an attribute:
[[country::USA]]
In page "Boston", I define another attribute, and reference a template:
[[state:Massachusetts]]
{{City
| state=Massachusetts
}}
In template: Template:City, I want to get the country of the city which uses the template by way of the state's property. I can reference the state like so:
{{{state}}}
And I can get the country by hand-coding the state:
{{#ask: [[Massachusetts]] | ?country=}}
But I want to do it using the parameter, like so:
{{#ask: [[{{{state}}}]] | ?country=}}
I'm told this should work, but however I format it, the command seems to return a list of all the attributes referenced in the wiki, whether I've defined them or not.
I see a lot of similar questions asked online, all described in various ways — nested queries, subqueries, linked properties, property chains, and so on — and they all have different suggested solutions, none of which seem to work. How can I do this, and is there a way to refer to this situation?
The syntax shown above works:
{{#ask: [[{{{state}}}]] | ?country=}}
So does:
{{#show: {{{state}}} | ?country=}}
Check your parameter declarations, make sure you aren't declaring any twice.