How to access Confluence page content using script runner?

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.

1 answer

1 accepted

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

Hi Rafael,

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

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);

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

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

365 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you