How do I find the backlinks to a given page?

Last modified by Thomas Mortagne on 2017/03/28

Question How do I find the backlinks to a given page?

Note: the feature is now available, see:

You can use the Velocity code below to get the backlinks of the current page. Note: the code here requires access to the privileged API, but you can also change it so that is uses only the simple XWiki API (use xwiki.searchDocumentNames, instead of calling directly the Hibernate store).

\#set (\$sql = "select xlink.fullName from XWikiLink as xlink where'\$cdoc.fullName' order by xlink.fullName")
\#set (\$results = \$xwiki.xWiki.getHibernateStore().search(\$sql,50,0,\$context.context))
\#if (\$results.size() > 0)
\#foreach (\$result in \$results)
\#set (\$backlink = \$xwiki.getDocument(\$result))
* [\$>\$result]

Notes entered before the feature has been made available:

There is no easy solution yet.. So for the moment the response would be "it is not possible". The rest of the answer is more for developers on how to handle this:

This is a little complex.. Doing it without saving the links of a page in a separate structure in the database, this can be an important query because you need to query the whole database searching for links using regular expression.

Now the regular expression is complex because there are multiple cases which can transform the link to a link to a specific page. For example if you have the page Ludovic Dubost, then the following texts will create a link to this page:

So the regular expression should be something allow a white space between any of the characters of the text.

There are some additional specific cases with accents which are converted to links without accents.. So for example Ludôvic Dubost also links to my page 

More importantly there is the issue of the space. If you are not in the space then you need to add XWiki. in front of your text, but if the document is in the XWiki space, then you don't need it and the following links would work:

I think the right way to handle the backlinks would be to create a separate tables with the links. This table would be updated everytime the document is updated.


Get Connected