Identify offensive or inappropriate content in confluence page
Asked by Madhura Siddappagowda:

As part of the Inclusivity Initiative from our company, we are looking into identify the non-inclusive(restricted) words like master, slave etc., in the confluence page and restrict user from using it.  
We are evaluating the script runner for Confluence for the same. We are using event listener functionality to achieve it.  
We were able to add the comment after we publish if the restricted words are found in the page but It does not block the user from publishing it.  
We want to block the user from publishing the page if restricted words are found in the page.  
Is there a way to identify this in draft page and block user from publishing the page until restricted words are replaced?


Answers:
author: Tiffany Wortham
body: Hey [@Madhura Siddappagowda](/t5/user/viewprofilepage/user-id/2575911) !  

I have an approach for this that involves censoring the content you want to restricted. You'd create a ScriptRunner custom event listener, and have it listen for the 'PageCreateEvent' and 'PageUpdateEvent'. This would be your script:  

```
import com.atlassian.confluence.core.Modification
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator

def page = event.page as Page
def body = page.bodyContent.body
def restrictedWords = ['fizz', 'buzz', 'foo', 'bar']
def containsRestrictedWord = restrictedWords.findAll() { word -> body.contains(word) }

if (containsRestrictedWord) {

    containsRestrictedWord.each { word ->
        body = body.replace(word, '<span style="color: rgb(255,0,0);">[REDACTED]</span>')
    }

    def pageManager = ComponentLocator.getComponent(PageManager)

    pageManager.saveNewVersion(page, { Page pageObject ->
        pageObject.setBodyAsString(body)
    } as Modification<Page>)
}
```

Any time a user creates or edits a page and uses a restricted word, it'll be replaced with styled text that makes it easier for the user to spot, and know they have to replace. I hope this is helpful. (:

comments: author: Henry Brown
body: Hi, I have a similar use case to this. I am trying to add content to a page after the edit using the PageUpdateEvent listener. However, the issue I am having is that, because I am using the PageUpdateEvent listener, having code that edits the page in the script will cause the script to fire off multiple times until it times out.

The script listens for page edit, then the script edits the page, which then fires off the script again. Do you have this issue with this code?


author: Tiffany Wortham
body: Hi Henry!

Thank you for pointing this out! This can be fixed by adding a SaveContext which suppresses events (the last parameter you pass in when creating a new save context is the one that determines if events are suppressed):

```
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.core.Modification
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator

def page = event.page as Page
def body = page.bodyContent.body
def restrictedWords = ['fizz', 'buzz', 'foo', 'bar']
def containsRestrictedWord = restrictedWords.findAll() { word -> body.contains(word) }

if (containsRestrictedWord) {

    containsRestrictedWord.each { word ->
        body = body.replace(word, '<span style="color: rgb(255,0,0);">[REDACTED]</span>')
    }

    def pageManager = ComponentLocator.getComponent(PageManager)

    pageManager.saveNewVersion(page, { Page pageObject ->
        pageObject.setBodyAsString(body)
    } as Modification<Page>, new DefaultSaveContext(true, true, true))
}
```

Let me know if this works for you!


author: Henry Brown
body: Yes, this worked! Thank you very much


author: James Conway
body: Hello [@Fabian Lim](/t5/user/viewprofilepage/user-id/3131564) , thank you for recommending Comala Document Management (link)!

Hello [@Madhura Siddappagowda](/t5/user/viewprofilepage/user-id/2575911),

Comala Document Management enables customers to build a customised review process that could form part of your solution here, for example Comala Document Management can add and remove restrictions as you move through your process. Currently the app does not have the ability to check for restricted words, but you can integrate ScriptRunner with Comala Document Management, see here (link = <https://docs.adaptavist.com/sr4c/latest/features/event-listeners/custom-event-listener/comala-document-management>) for an example (different use case).

Keep in mind that Comala Document Management does not block pages from being saved, but applies controls to pages that have been saved.

Note that ScriptRunner also provides the ability to add / remove page restrictions.

I hope that helps

James


author: Fabian Lim
body: Hi [@Madhura Siddappagowda](/t5/user/viewprofilepage/user-id/2575911)

Based on your requirements, I would consider an app such as comala workflows that allows to have a workflow on pages before they get published. Approvers get notified to do the review.

The are other apps as well, but the one above is the one we use.

Regards


Tags: adaptavist

confluence-server

scriptrunner

server