Update macros on a page with a script

Michael Wolfe
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 18, 2024

I'm working on migrating a DC instance to cloud and want to bulk change macro's on a page without having to stop/restart Confluence using Atlassian's method via the database.  I'm by no means an expert at doing any of this via scripting and could use some assistance from anyone that may have done this before.  Does anyone have a script they can share, or examples of scripts that are capable of doing this?  Due to security reasons, I do not have Confluence Admin, or access to the database, so I'm limited to anything a Space Admin can do.

1 answer

0 votes
Andrii Maliuta
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.
March 19, 2024

Hello @Michael Wolfe ,
You can use Cloud migration assistant (https://support.atlassian.com/migration/docs/use-the-confluence-cloud-migration-assistant-to-migrate/) as a ready to use tool to migrate the instance.

As for content changes with APIs: 

You can change the pages Storage format (body of pages in xHTML) and operate on it as on a String. The macro is stored on the page as part of page body, so you can find and replace part of page body text as you need. Each macro has a corresponding storage format (https://confluence.atlassian.com/doc/macros-139387.html).

You can either use REST API or internal Confluence Java API to operate on pages.

For example to replace the Status macro on page to ToC macro replace:

<ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="c06bc145-f124-46ac-b8da-69b3a7ee803a"><ac:parameter ac:name="title">ddd</ac:parameter></ac:structured-macro> 


 
<ac:structured-macro ac:name="excerpt" ac:schema-version="1" ac:macro-id="2c3c02cc-d4fb-45e8-8de2-e691a3955c1f"><ac:parameter ac:name="atlassian-macro-output-type">INLINE</ac:parameter><ac:rich-text-body>
<p>asdasd</p></ac:rich-text-body></ac:structured-macro>
Michael Wolfe
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2024

CCMA is the tool of choice that we will be using and I've used in the past for other efforts.  It's really the amount of content formatting that's been done on these pages that we need to revert essentially that we're trying to do programmatically.  What would really help is if someone has a script they have used to swap out some of the macros that doesn't require restarting Confluence.  I've been working on one, but it keeps running into an API Rate Limit, and failing.

Andrii Maliuta
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.
March 22, 2024

@Michael Wolfe ,

Changing page bodies does not require Confluence stop. REST API or Java API via ScriptRunner can be used when the instance is always up.

Example script to change the macros on pages:

import com.atlassian.sal.api.transaction.TransactionCallback
import java.util.regex.Pattern;
import java.util.regex.Matcher;

transactionTemplate.execute(new TransactionCallback() {
  
  @Override
  public Object doInTransaction() {
      
      SaveContext saveContext = new DefaultSaveContext(true, true, false);
          
      Page page = pageManager.getPage(000000000); // ----- Root page id here
          
      pageManager.saveNewVersion(page, new TextModification(), saveContext);                       
                         
      return "Success";
  }
});

    class TextModification implements Modification<Page> {

        @Override
        public void modify(Page page) {
          
          String pattern = "(<ac:structured-macro ac:name=\"scroll-ignore\".+?</ac:structured-macro>)";
          Pattern regex = Pattern.compile(pattern);
          Matcher matcher = regex.matcher(page.getBodyAsString());
          while (matcher.find()) {
     String expMacro= matcher.group(0); 
            String pageBody = page.getBodyAsString();
           pageBody = pageBody.replace(expMacro, "")
           page.setBodyAsString(pageBody);
          }
            
        }
}
Like Andrii Maliuta likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events