Page MenuHomePhabricator

Passing a template parameter into a query throws an error
Closed, InvalidPublic

Description

Author: mitchell_neill

Description:
Hi.

I have a number of queries that I use on a lot of pages. Rather than put the query into every page, it would be much easier to create a template with the query in it and then pass in the required parameters.

An example using the cities theme.

I have a cities query that is used on a number of pages. So in each page that uses it I simply have:

{{City Query|Berlin}} (or whatever city I want - this is a trivial example).

The City Query template contains:

{{#ask:
[[Category:Cities]]
[[City::{{{1|}}}]]

?Population
?Mayor
?Size
?Twinned with
?Has a river

}}

However, {{City Query|Berlin}} throws the error:
"The part "]]" of the query was not understood. Results might not be as
expected."

and the ask returns all cities in the cities category, not just Berlin.

Surely this is just a simple passing issue that can be easily fixed?
It would be very powerful to be able to pass in parameters to a query like this and would also result in cleaner pages without duplicating almost identical queries everywhere.

I am using SMW 1.5.1.


Version: unspecified
Severity: major

Details

Reference
bz24337

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 11:05 PM
bzimport set Reference to bz24337.

mitchell_neill wrote:

Sorry, was using a SMW 1.4.3 instance. Seems to work fine on SMW 1.5.1, which is great!

mitchell_neill wrote:

Spoke too soon. The bug is present.

If you have more than one template variable then it throws the error. For example:

{{City Query|+|Yes}}

{{#ask:
[[Category:Cities]]
[[City::{{{1|}}}]]
[[Has a river::{{{2|}}}]]

?Population
?Mayor
?Size
?Twinned with
?Has a river

}}

Throws the error:
"]][[Has a river::" cannot be used as a page name in this wiki. Some use of "<nowiki>

and it returns all cities in the category regardless of Has a river value.

So, as I first suspected, there seems to be a parsing issue here.

It turns out that there is a minor issue here but that the main problem is actually not a bug: what the above template does is to construct a malformed query when some of the parameters are not given. Namely, the parameters are simply replaced by nothing (empty strings). So you get a query like, say,

{{#ask:
[[Category:Cities]]
[[City::]]
[[Has a river::]]

?Population
?Mayor
?Size
?Twinned with
?Has a river

}}

This query, whether created by a template or not, will cause an error message from SMW since the conditions cannot be parsed. The minor issue is that SMW assumes that property condition values are of length at least one. So the first "]" is taken to belong to a value, which thus extends until the next closing pair of "]]" after the following condition. This makes the error somewhat hard to read. But the query would not work without errors either way.

If you want to have optional query conditions in a template, then you should use the ParserFunctions extension with its #if parser function to test for emptiness. The whole query part (from [[ to ]]( should then be omitted if no parameter is given for it. Alternatively, the template could use some relevant default (syntax: {{{1|default}}}) instead of an empty string. Using "_" there will at least make the error more readable.

In particular, queries do work properly with template parameters, and this bug can be closed.

mitchell_neill wrote:

Hi Markus.

Thanks for your response.

There's been a lot of chat about using #if on the mailing list :)

I guess, as you say, that queries should not return anything if there is an error in the parameters rather than returning the entire query set.

Thanks for the comment: it made me notice an omission in our documentation that I have now fixed. SMW already has the feature that you ask for (not running queries if there are errors), but it is disabled by default. The configuration option for this is called $smwgIgnoreQueryErrors, see http://semantic-mediawiki.org/wiki/Help:Configuration#smwgIgnoreQueryErrors .

mitchell_neill wrote:

Hi Markus.

Thanks for amending the documentation. Being able to stop SMW running queries with dud parameters is really useful.

:)