or $services.rendering.escape("attach:$attachmentRef", ...). We have this in platform (AttachmentSelector, OfficeImporterResults, ExtensionSheet, ExtensionVersionSheet, ExtensionAuthorsDisplayer), it’s becoming increasingly present in contrib extensions too (e.g. application-blog, application-releasenotes), and LLM-generated code reproduces it.
It works, but:
the script has to know the resource type prefixes (doc:, attach:, page:, space:, pageAttach:), which are a syntax detail;
it has to remember to put the prefix in (without it, a reference containing :, such as one with a wiki prefix, can be read as another resource type);
it has to remember to escape, and pick the right syntax to escape for;
the serializer that does this already exists in Java (ResourceReferenceSerializer with hint xwiki/2.1/link), but scripts can’t use it.
Proposal
Add to RenderingScriptService ($services.rendering, module xwiki-platform-rendering-xwiki):
/**
* @return the resource reference serialized for the given syntax (e.g. "doc:Space.Page" for xwiki/2.1),
* escaped so that it can be used as is as the reference of a link written in that syntax
* @since 18.10.0RC1
*/
@Unstable
String serializeResourceReference(ResourceReference reference, Syntax syntax)
The syntax is required, as for escape(String, Syntax), which is the practice in this script service (no method defaults to the current syntax).
Make EntityReference convertible to ResourceReference, so that the method can be called with an entity reference without a second signature. Velocity already converts arguments through the ConverterManager, and strings like 'doc:Space.Page' already convert through the existing ResourceReferenceConverter from xwiki-rendering. Since converters are looked up by target type only, this means overriding that converter in platform (higher priority, in xwiki-platform-rendering-xwiki, next to the existing ResourceReference → EntityReference resolvers) to handle EntityReferences and delegate the rest:
DOCUMENT → doc, SPACE → space, ATTACHMENT → attach, PAGE → page, PAGE_ATTACHMENT → pageAttach; other entity types aren’t supported;
the reference is serialized in full (with the wiki), so the link works wherever the content is rendered.
And we then migrate the existing hand-written places.
Open questions:
Should it escape (my preference: yes, since forgetting to escape is the usual mistake), or return the raw reference and leave escaping to the caller as today?
Should we go one step further and add a helper that generates the whole link ([[label>>reference]], with the label escaped too)?
The Security Developer Guide documents the current manual way: escape the link label twice (escape(escape($label))) and the reference once. It doesn’t mention the resource type prefix, and its translation example even inserts >>$target unescaped. If we agree on a new API, I’ll update that section to recommend it.
Loading... (open since 2022) asks for helpers to perform escaping with wiki syntax, including “escape labels of wiki links (double escape)”. This proposal would address part of it.
In Syntax support in translations/translation parameters we agreed to pass blocks (e.g. links) as translation parameters (already supported by the localization module) and to “provide a script API that allow creating common blocks (like links, emphasize, bold, italics) easily without writing syntax”. That script API was never implemented.
Regarding my open question 2 (a helper generating the whole link), I see these options. To compare them, here’s the same link written with each. Done properly by hand today, it reads:
escapeLinkLabel(label, syntax), used together with serializeResourceReference(). The template keeps showing the link syntax and Velocity templating stays natural, but it’s verbose and you still have to escape the parameter values yourself:
serializeLink(label, reference, parameters, syntax), returning the link in the given syntax, with the label, the reference and the parameter values all escaped by the renderer. Safer and shorter to write, but you lose sight of the syntax in the template:
A #wikiLink($label $reference $parameters) Velocity macro (in macros.vm), built on 2. It’s consistent with the existing #wikiTranslation helper, and a bit closer to the template syntax than a script service call:
A createLink(label, reference, parameters) returning a LinkBlock. Not that useful on its own (it still needs rendering), but it would also serve the translation use case above, where the link is passed as a translation parameter:
#set ($link = $services.rendering.createLink($label, $reference, {'queryString': $queryString}))
* $services.rendering.render($link, 'xwiki/2.1')
## or, inside a translation such as "myapp.archive=Articles from {0}":
* $services.localization.render('myapp.archive', 'xwiki/2.1', [$link])