Creating backlink for a page with scripting

Hello, I am very new to Xwiki and have a page structure for Shows.Actors.Name of the actor and then Shows.Personal Reviews.Name of show. Now I want to add a cast to the show, and then on each actor page, I have a velocity script that checks for backlinks and then creates a TOC of shows that link to that actor. From my understanding, backlinks are only created if a link to the page exists in the source code, but not within script tags or macros. I tried to make a macro with a PagePicker parameter that allowed me to pick an actor; however, the link renders correctly, but since it is within a Velocity script, it does not show as a backlink. Any suggestions would be great. Thank you

Yes, I’ve asked chatgpt and the answer was:

By default:

Backlinks are detected from rendered content, meaning links inside a page’s XWiki syntax / HTML content are parsed when the page is indexed.

For XObjects:

If the XObject has a property of type TextArea and that property is rendered as wiki syntax or HTML (e.g., it contains [[SomePage]]), then those links are indexed and will show up as backlinks, because the rendering system parses them into links.

If the TextArea is treated as plain text (no wiki rendering), then the content is not parsed for links, so backlinks won’t be created.

I’ve asked ChatGPT:

How to programmatically (inside a velocity script) force indexing a link as a backlink?

And the first 2 answers are:

I don’t think (but I’m not sure) that there’s an existing API to add a backlink programmatically. When asked about an API, ChatGPT mentions:

you’d need to extend the Solr indexing module by writing a custom SolrIndexerEventListener (in Java, not in Velocity). […]

Thx

For link extraction, we don’t execute macros, but we parse the content of macros whose content is declared as XWiki syntax. So when you have a link inside an info macro, it should be correctly indexed. However, when the link is created by Velocity code, it cannot be indexed currently.

To get more content indexed for documents, you can create a component of type SolrEntityMetadataExtractor<XWikiDocument> that is then called during indexing. You can check the code of the existing link extractor how links are stored in the Solr document, you need to add your links to the links and extended links fields like in that code.

For your specific case of a macro, there is actually another, much simpler option: we use components of type MacroRefactoring to extract links from macros. You can take a look at the default implementation and the include macro’s implementation. It might seem like this primarily for refactoring, to update the reference when the referenced page is renamed, but the method extractReferences is what is called by the indexer to get the links of the macro and its content. I think we should add support for indexing (and refactoring) parameters of the respective standard types in XWiki itself, feel free to open an issue. In the meantime, the easiest option would be to implement your own macro refactoring.

There is no such thing like a SolrIndexerEventListener. There is SolrIndexEventListener, but it’s the event listener that is responsible for triggering the indexing when a document is changed in XWiki and it has no ability to influence the indexing of links.

I also don’t think any of the other things ChatGPT suggested work. As I’ve said, links produced by Velocity code aren’t indexed because we don’t execute the Velocity code during indexing.