How do I find the backlinks to a given page?

Last modified by Vincent Massol on 2007/08/17

Question How do I find the backlinks to a given page?
Answer Note: the feature is now available, see: http://jira.xwiki.org/jira/browse/XWIKI-20

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 xlink.link='\$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))
* [\$backlink.name>\$result]
\#end
\#end

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.

Tags:
Created by SebPaquet on 2005/11/02
    

Get Connected