www.wikidata.org contains many SPARQL queries. Usually they are either embedded within a page via one of the query templates (e.g. [Template:SPARQL](https://www.wikidata.org/wiki/Template:SPARQL)) or they are placed on a dedicated page via [Template:Query page](https://www.wikidata.org/wiki/Template:Query_page), which allows their WDQS links to be embedded on other pages. The reason why we use these templates, as opposed to just using `<syntaxhighight lang=sparql>` from the [SyntaxHighlight](https://www.mediawiki.org/wiki/Extension:SyntaxHighlight) extension, is to automatically link the Wikidata Query Service for them. There is however a big problem with using MediaWiki templates for SPARQL, which is that both attribute special meaning to the `|` and `=` characters, meaning they should be escaped as `{{!}}` or `{{=}}` respectively when they occur within the SPARQL query. (Template:SPARQL tries to be smart about it [but that doesn't always work](https://www.wikidata.org/wiki/User:Push-f/Example_of_ConcatArgs_going_wrong) and leads to a confusing user experience when it does inevitably fail). Having to escape these characters is obviously quite cumbersome.
I just implemented [a small MediaWiki extension](https://www.mediawiki.org/wiki/Extension:Code) to remedy that problem by allowing the setup of a `<query>` tag for queries embedded in pages and allowing the setup that pages ending in [.rq](https://www.w3.org/TR/rdf-sparql-query/#mediaType) are displayed as SPARQL via a new page content model. This way no special characters have to be escaped and queries can easily be copy'n'pasted between the wiki and the WDQS.
I set up a demo wiki, so you can try it out: https://demo-wiki.push-f.com/wiki/index.php?title=Code
My extension also implements a special page so the WDQS can be easily linked for queries on other pages e.g. `[[Special:CodeAction/run/Wikidata:Topics/MediaWiki/Extensions.rq]]` could link the WDQS for the query defined on the page `Wikidata:Topics/MediaWiki/Extensions.rq`.
Lastly my extension can integrate with Scribunto allowing a Scribunto Lua module to be configured to be able to customize the highlighted code e.g. to linkify identifiers or to display labels next to identifiers to make the code more readable (the only reason that my demo wiki currently doesn't do that is that I don't have Wikibase installed on my demo wiki). T67626 has been brought to my attention, so I also implemented an `additionalOutput` Lua hook which could be used to display query results once a WDQS lua function has been implemented. As a demonstration my demo wiki currently showcases how that could look like via [Extension:UnlinkedWikibase](https://www.mediawiki.org/wiki/Extension:UnlinkedWikibase) but that is really just meant as a proof of concept to show that my extension is flexible enough to support the embedding of additional output.
Sidenote: While a `Query` MediaWiki namespace has already been created (but is currently unused), I think we should instead keep queries in the `Wikidata:` namespace because that allows them to be better integrated with the rest of our pages e.g. you can have one wikitext page and then several queries as subpages to the wikitext page, which can easily link the subpages via relative links (which wouldn't work cross-namespaces).
Cheers,
Martin