Template talk:SPARQL

From Wikidata
Jump to navigation Jump to search

logical-or[edit]

The template has a problem with the logical-or-operator (||). The query is cut. E.g.

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/> 
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?item ?taxonName WHERE {
  ?item wdt:P225 ?taxonName .
  ?item wdt:P105 wd:Q35409 . # family
  FILTER(!(STRENDS(?taxonName, "idae") || STRENDS(?taxonName, "aceae")))
}

becomes

SELECT ?item ?taxonName WHERE {
  ?item wdt:P225 ?taxonName .
  ?item wdt:P105 wd:Q35409 . # family
  FILTER(!(STRENDS(?taxonName, "idae")|| STRENDS(?taxonName, "aceae")))
}
Try it!
@Succu: It's a wiki template, so you have to escape vertical bars. Try
SELECT ?item ?taxonName WHERE {
  ?item wdt:P225 ?taxonName .
  ?item wdt:P105 wd:Q35409 . # family
  FILTER(!(STRENDS(?taxonName, "idae") || STRENDS(?taxonName, "aceae")))
}
Try it!

Jheald (talk) 16:38, 25 November 2015 (UTC)[reply]

Thanks, Jheald. I'm not very familiar with templates. --Succu (talk) 16:43, 25 November 2015 (UTC)[reply]
I stumbled upon the same. For those to whom Jheald's code looks just the same as above, he typed

{{!}}{{!}}

Documentation: https://www.mediawiki.org/wiki/Help:Magic_words#Other --Vladimir Alexiev (talk) 17:11, 23 February 2017 (UTC)[reply]

New version[edit]

The version seems to have been partially corrected with the use of the Module:ConcatArgs introduced in 2016 by @TomT0m:. However, now I come across a case where the SPARQL template does not render it correctly:

PREFIX target: <http://www.wikidata.org/entity/Q53172006>

SELECT
  ?count
  ?work ?workLabel
  (CONCAT("#work/", SUBSTR(STR(?work), 32)) AS ?workUrl)
WITH {
  SELECT
    (COUNT(?work) AS ?count)
    ?work
  WHERE {
    target: (^wdt:P2860| wdt:P2860) / (^wdt:P2860
Try it!

It is unclear to me why works in other cases, but not this one. — Finn Årup Nielsen (fnielsen) (talk) 00:57, 23 March 2023 (UTC)[reply]

I see it is related to the equality sign in the line after the pipe character. Moving the FILTER line before the line with the pipe results in the SPARQL displayed fully. — Finn Årup Nielsen (fnielsen) (talk) 01:02, 23 March 2023 (UTC)[reply]

Bug with Flow[edit]

@TomT0m, Jheald: it seems this template has a bug on Flow talk pages. See my page for example : User talk:VIGNERON. nowiki are weirdly automatically added and there is no link to query.wikidata.org...

Cdlt, VIGNERON (talk) 10:42, 17 May 2016 (UTC)[reply]

@Trizek (WMF): I confirm this bug. It seems Flow or parsoid automatically thinks the "{" must be escaped, but a single one has no meaning in Wikitext. Is it known ? Can you upstream this with Flow or parsoid devs ? author  TomT0m / talk page 16:25, 17 May 2016 (UTC)[reply]
@TomT0m, Jheald, VIGNERON:
Hi
I can't edit the post to check it because I'm not autopatrolled on WikiData.
Can you describe the expected behavior?
Thanks, Trizek (WMF) (talk) 15:19, 18 May 2016 (UTC)[reply]
Hi Trizek (WMF),
This is how this template usualy appears :
SELECT ?item ?itemLabel ?coord WHERE {
?item wdt:P131+ wd:Q3357 ; wdt:P625 ?coord .
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" }
}
Try it!
With syntax coloration (a detail but an useful one), no nowiki (obviously) and the most important a link at the end pointing to the actual query on query.wikidata.org.
Cdlt, VIGNERON (talk) 15:33, 18 May 2016 (UTC)[reply]
I've tried it with the visual editor and, if the result looks fine, the wikicode has <nowiki> tags too. There is still a missing curly bracket at the end of that query. That's probably a Parsoid issue.
What is the best way to describe that problem? Have you tried to change parameters on the TemplateData, like selecting "type": "unbalanced-wikitext" instead of "string"?
I've created a specific bug report concerning #tag:syntaxhighlight.
Trizek (WMF) (talk) 14:00, 19 May 2016 (UTC)[reply]

Syntaxhighlight[edit]

Regarding that tag, that feedback may interest you: "You used #tag:syntaxhighlight rather than <syntaxhighlight>? I think it's the same either way. It is interpreted, but needs to be styled." Trizek (WMF) (talk) 16:20, 20 May 2016 (UTC)[reply]

@Trizek (WMF): I think I switched one with the over for a reason when writing this template to solve a bug (could be this template, I'd have to check my history) but I can't remember why. author  TomT0m / talk page 16:31, 20 May 2016 (UTC)[reply]

Vertical space between query and Try it! link[edit]

@Jheald, TomT0m: I think there’s too much vertical space between the query and the Try it! link. I suggest the <br/> just before [http://query.wikidata.org/#... should be removed. WDYT? --WikidataFacts (talk) 18:06, 28 May 2016 (UTC)[reply]

@Jheald, TomT0m: if no one objects, I’ll make this change soon… let’s hope nothing breaks --TweetsFactsAndQueries (talk) 14:14, 30 June 2016 (UTC)[reply]
✓ Done, and everything seems to still be working :) --TweetsFactsAndQueries (talk) 13:28, 3 July 2016 (UTC)[reply]

Add a query title[edit]

Any objections putting the query into a table with title like this?

Cats Try it!
SELECT ?item ?itemLabel
WHERE
{
	?item wdt:P31 wd:Q146 . 
	SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

To compare, the current layout is:

# Cats
SELECT ?item ?itemLabel
WHERE
{
	?item wdt:P31 wd:Q146 . 
	SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
Try it!

An alternative would be to show the query title as header of some given depth so it is also shown in the table of contents. -- JakobVoss (talk) 08:10, 12 July 2016 (UTC)[reply]

We shouldn't use "try it" on the right because WDQS uses "execute" button on the left d1g (talk) 23:45, 28 August 2017 (UTC)[reply]

Exclude URL during print?[edit]

Not sure where to report it. Try to print preview at examples-heavy page e.g. User:TweetsFactsAndQueries/A_Guide_To_WDQS

It will result in

  • meaningless "try it" labels on paper
  • duplicate queries, urlencoded (omit them)

d1g (talk) 04:12, 1 February 2017 (UTC)[reply]

Two links[edit]

I'd like to replace the current, single, "try it" link with two links.

The first would say "Run it!", and link to the embed.html version of the query.

The second, using the current link target, would say something like "View/ edit".

Any thoughts? Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 21:15, 18 June 2017 (UTC)[reply]

name embed.html as "embed"?
I also need third link to "debug" [this query] as parameter for complex queries d1g (talk) 22:58, 19 July 2017 (UTC)[reply]
I don't understand the difference between "debug" and "view/edit" but at least two links make sense -- JakobVoss (talk) 16:02, 29 August 2017 (UTC)[reply]

Hide query[edit]

I'd like an option to not show the SPARQL code but only links ("try it"/run it!"/"show query"...). The SPARQL code could be collapsed (see Template:Collapse). -- JakobVoss (talk) 16:02, 29 August 2017 (UTC)[reply]

@JakobVoss: I had created User:Marsupium/Template:SPARQL for that. I've introduced the functionality here now with the parameter |state=. I've used the terms of en:Template:Navbox#Setup parameters. Because it isn't obvious anymore what can be tried with "Try it!" I've also introduced the parameter |label= that can be used to customize the link label. The "Collapse"/"Expand" toggle button is displayed at the right of the page with the exception of list obviously which is handy. Example:
  • SELECT ?item WHERE {
      ?item wdt:P31 wd:Q146.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    query finding house cats
I hope the changes are acceptable. Please remark any ideas for improvements! Thanks in advance, --Marsupium (talk) 14:33, 25 May 2018 (UTC)[reply]
@Marsupium: great, thanks! How do we get these parameters added to the template? -- JakobVoss (talk) 06:50, 27 May 2018 (UTC)[reply]
@JakobVoss: ?? They are already added. You can look at the documentation on the other side. :) --Marsupium (talk) 08:58, 27 May 2018 (UTC)[reply]
Sorry for confusion and many thanks! -- JakobVoss (talk) 20:39, 27 May 2018 (UTC)[reply]

Correct non-breaking spaces and other white spaces[edit]

Hello, I've come across problem like copy-pasting with non breaking white spaces. For instance :

#Population des pays voisins de l'Allemagne
SELECT ?year ?population ?countryLabel WHERE {
  {
    SELECT ?country ?year (AVG(?population) AS ?population) WHERE {
      {        SELECT ?country (YEAR(?date) AS ?year) ?population WHERE {
          ?country wdt:P47 wd:Q183.
          ?country p:P1082 ?populationStatement.
          ?populationStatement ps:P1082 ?population.
          ?populationStatement pq:P585 ?date.
          FILTER(year(?date)>=1900) .
          #FILTER( ?country not in (wd:Q1201)).
        }      }
    }
    GROUP BY ?country ?year
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr". }
}
Try it!

gives an error due to non-break white spaces. How to handle them ? I tried a , seems not working... Thanks

Some keywords are not highlighted properly[edit]

For example, VALUES and UNDEF:

SELECT * {
  VALUES (?x) {(UNDEF)}
}
Try it!

-- Luitzen (talk) 16:37, 4 February 2019 (UTC)[reply]

Adding Short URL display[edit]

Can anyone see why this didn't work? Andy Mabbett (Pigsonthewing); Talk to Andy; Andy's edits 15:08, 16 April 2019 (UTC)[reply]

The curly brackets were at the wrong place. (The <nowiki> tags are there just to make sure the parenthesis is not in the link without putting spaces around it.) —Tacsipacsi (talk) 10:50, 17 April 2019 (UTC)[reply]

endpoint -parameter[edit]

It would be nice to have the ability to use other endpoints like the mw:Template:SPARQL and w:fi:SPARQL have. Also it would be nice to have project alias for Sophox (OSM) endpoint. --Zache (talk) 14:54, 18 August 2020 (UTC)[reply]

embed[edit]

Using "embed" changes the expected behaviour of the template, which is at least annoying. The template should be reverted to its previous unless both behaviours can be included as options - using a parameter or two links.

I notify @Bouzinac:.--Pere prlpz (talk) 16:44, 13 June 2021 (UTC)[reply]