How to access Confluence page content using script runner?

Roberto Saltini October 17, 2016

I would like to setup a script job that scans a page for web inks and marks in red those that are not valid any more.

The reason why I do not want to use a macro for this is that I want watchers to be notified of this change.

I have installed Confluence script runner, I had a look at the confluence API especially regarding ContentEntityObject but I could not figure out a way to read the page content and update it.

2 answers

1 accepted

3 votes
Answer accepted
Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 19, 2016

Hi Roberto,

My name is Rafael Franco and I'm the Tech Lead of the Product Team here at Adaptavist. Thanks for using ScriptRunner for Confluence.

May I ask how do you define that a link is not valid any more? 

Regards,

Rafael

Roberto Saltini October 19, 2016

Hi Rafael,

I would just use HttpUrlConnection to send a "HEAD" command and then verify whether its return code is 200

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 20, 2016

Hi Roberto,

Ok, clear now. What you need essentially is understand that each page is composed of a sort of meta language called storage format https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html

In essence what you need to do is look for all pages, iterate over them, get the page content and update it, so something like:

ContentEntityObject contentEntityObject = page.getContentEntityObject()
 
String body = contentEntityObject.getBodyAsString()
 
//Do a regex to replace the "a" links and add a style="background-color:red" to it
 
after the replace do 
 
contentEntityObject.setBodyAsString() with the updated body
 
and then finally:
 
this.pageManager.saveContentEntity(contentEntityObject, SILENT_SAVE_CONTEXT);
MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 20, 2016

Roberto, link checking could add quite a load to the Confluence server, so I'd recommend you have some way to restrict how often connections are made

Rafael Franco
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 20, 2016

Matt, I guess this is a one time operation, so should be fine

0 votes
Akhil Agarwal September 3, 2019

@Rafael Franco 

Hi,

I was trying to get a list of all the users in Confluence through a groovy script. Could you help me with it?

 

Thanks in advance.

Akhil

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events