Is there a find links to another space?
Asked by Sharon Russell:

*This question is in reference to Atlassian Documentation: [Links](https://confluence.atlassian.com/pages/viewpage.action?pageId=776656293)*

In some cases, editors are selecting the wrong space when entering a link. When we publish documents, links to other spaces are removed. We need to be able to run a report in Conlfuence that will find all links that go to this other space before we publish the document.


Answers:
author: Jens Iwanenko
body: Found this question on accident. Even though it is already over 2 years old, I'd still like to post an answer. I wrote a user macro to identify outgoing links in our documentation spaces. It works a treat for me, but having just re-scanned the code (also 2 years old) I noticed that it is not really optimized for large spaces. It is also partially commented in German... ;-)

This was also written for Confluence 5 or so. It could definitely be improved upon, but it works as is in 6.14.3. At least for me.

```
## Macro title: Outgoing Links
## Macro has a body: N
## Body processing: rendered
## Output: HTML
##
## Developed by: Jens Iwanenko
## Date created: 2016-08-15
## Installed by: Jens Iwanenko

## This macro lists all outgoing links of a Space or a Pagetree.
## @param Method:title=Methode|type=enum|enumValues=Teilbaum,Space|default=Teilbaum|desc=W?hlen, ob die Ausgehenden Links eines Space oder eines Teilbaums gesammelt werden sollen (Default: Teilbaum).
## @param Space:title=Space|type=spacekey|desc=Den Space-Key eingeben, zu dem die ausgehenden Links gesammelt werden sollen. Wird ignoriert, wenn die Methode "Teilbaum" ist (Default: aktueller Space).
## @param Parent:title=Seite|type=confluence-content|desc=Den Seitennamen des Teilbaum-Parents eingeben, zu dem die ausgehenden Links gesammelt werden sollen. Wird ignoriert, wenn die Methode "Space" ist (Default: aktuelle Seite).

#set($containerManagerClass=$action.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($spaceManager=$containerContext.getComponent('spaceManager'))
#set($pageManager=$containerContext.getComponent('pageManager'))

#set($spaceKey = $content.getSpaceKey())
#set($parentPageTitle = $content.getPageTitle())
#set($outgoingLinks = [])
#set($containingPages = [])

## Teilbaum Start
#if($paramMethod == "Teilbaum")

#if($paramParent)
#if ($paramParent.contains(':'))
#set ( $strArray = [] )
#foreach($str in $paramParent.split(':'))
#set($dummy = $strArray.add($str)) ## Assign returnval to avoid printing 'true'.
#end
#set($spaceKey = $strArray.get(0).trim())
#set($parentPageTitle = $strArray.get(1).trim())
#else
#set($parentPageTitle = $paramPage.trim())
#end
#end

#set($parentPage = $pageManager.getPage($spaceKey, $parentPageTitle))

#end
## Teilbaum Ende
## Space Start
#if($paramMethod == "Space")

#if($paramSpace)
#set($spaceKey = $paramSpace)
#end

#set($space = $spaceManager.getSpace($spaceKey))
#set($parentPage = $space.getHomePage())

#end
## Space Ende
## Allgemein Start
#foreach($ogLink in $parentPage.getOutgoingLinks())
#if($ogLink.getDestinationSpaceKey() != $spaceKey)
#set($dummy = $outgoingLinks.add($ogLink))
#set($dummy = $containingPages.add($parentPage))
#end
#end

#set($subPages = $parentPage.getDescendants())
#foreach($subPage in $subPages)
#foreach($ogLink in $subPage.getOutgoingLinks())
#if($ogLink.getDestinationSpaceKey() != $spaceKey)
#if(!$ogLink.isUrlLink())
#set($dummy = $outgoingLinks.add($ogLink))
#set($dummy = $containingPages.add($subPage))
#end
#end
#end
#end
## Allgemein Ende
## HTML Start
<div class="enc-outgoing-links">
<div>
#if($outgoingLinks.size() == 1)
$outgoingLinks.size() ausgehender Link 
#else
$outgoingLinks.size() ausgehende Links 
#end
#if($paramMethod == "Space")
im Space <a href="$parentPage.getUrlPath()">$spaceKey</a>.
#end
#if($paramMethod == "Teilbaum")
im Teilbaum von <a href="$parentPage.getUrlPath()">$parentPage.getTitle()</a> ($spaceKey).
#end
</div>
<table class="aui aui-table-sortable">
<thead>
<tr>
<th>Seite</th>
<th>Zielseite</th>
<th>Zielspace</th>
</tr>
</thead>
<tbody>
#set($iterator = 0)
#foreach($ogLink in $outgoingLinks)
<tr>
<td><a href="$containingPages.get($iterator).getUrlPath()">$containingPages.get($iterator).getTitle()</a></td>
<td><a href="$pageManager.getPage($ogLink.getDestinationSpaceKey(),$ogLink.getDestinationPageTitle()).getUrlPath()">$ogLink.getDestinationPageTitle()</a></td>
<td>$ogLink.getDestinationSpaceKey()</td>
</tr>
#set($iterator = $iterator +1)
#end
</tbody>
</table>
</div>
## HTML Ende
```


author: Sharon Russell
body: We export the space to Word. The links should not go to this other space: they are incorrect links, so I want to find all of the links that go to that space and change them or remove them. Once the pages are exported to Word, all the links to that other space are removed, so we can't easily find them in the Word document. It just looks like text so it doesn't stand out in any way.  
In Confluence, the links aren't broken because the other space still exists. The only way to know, would be to click the link and see if it goes to the other space, which is going to be a long and tedious process with over 2500 pages and multiple links per page. A report that finds all the links going outside of the space would be ideal. Is there such a thing?  
I know how to enter links to other spaces or within the space. But we have multiple authors on the pages and getting everyone to do it correctly all the time is difficult. An easy tool to find the problems after the fact would really help.


author: Rodney Hughes
body: Not sure what you mean by "publish the document"- paper? PDF? web page? Confluence page?

Why do you want to remove the links the author has put in - is it because they "might" be wrong?

But why/how are they are entering wrong links?

If linking from on page to another (even in different Space) in Confluence,

1. first go to the target page,
2. go back to the page where you want the link.
3. edit mode
4. Insert Link\> Recently Viewed

Or any other linking:

* copy the URL itself in the browser (but can be very long)  
  or
* in normal page viewing mode, press the letter "k" on your keyboard to get a short URL which is a permanent link to that page .. even if you change the page title


Tags: documentation

link