How to migrate a macro 3x to 4x in existing content

Hi,

A customer just migrated from a 3.5 to a 5.8 version of Confluence.

Unfortunately the macro migration failed and the wiki pages now contain the old version of the macro:

  <ac:structured-macro ac:macro-id="08531f28-34b3-41b9-8297-0a85e7b1be97" ac:name="mockup" ac:schema-version="1">
    <ac:parameter ac:name="1">28</ac:parameter>
    <ac:parameter ac:name="">test</ac:parameter>
  </ac:structured-macro>

And this is how it should look like in order for the plugin content to be displayed properly:

<ac:structured-macro ac:macro-id="08531f28-34b3-41b9-8297-0a85e7b1be97" ac:name="mockup" ac:schema-version="1"> 
<ac:parameter ac:name="Version">28</ac:parameter>
<ac:parameter ac:name="Name">test</ac:parameter>
</ac:structured-macro>

 

Unfortunately the rolling back to 3.5 is not an option.

Is there a way to convert the macro in existing content outside the migration process (e.g. some sort of script)?

2 answers

1 accepted

3 votes
Answer accepted

Since the macro values are dynamic, it could be that a standard search/replace might not work.  However, Confluence's storage format is (more or less) valid XML, so you can use the browser engine to parse it via Javascript.  This is what I was able to throw together, but I can't guarantee it will work on all pages:

jQuery.ajax({
    url: contextPath + "/rest/api/content/" + AJS.params.pageId + "?expand=body.storage,version,ancestors",
    success: function(response) {
        console.log(response);
        bodyText = "&lt;xml&gt;" + response.body.storage.value + "&lt;/xml&gt;";
        xmlBodyText = bodyText
            .split("ac:").join("ac--")
            .split("ri:").join("ri--")
            .split("&amp;nbsp;").join("nnbbsspp")
            .split("&amp;quot;").join("qquuoott");
        xmlDoc = jQuery.parseXML(xmlBodyText);
        xml = jQuery(xmlDoc);
        macro = xml.find('ac--structured-macro[ac--name="mockup"]');
        jQuery(macro).each(function() {
            jQuery(this).find("ac--parameter").eq(0).attr("ac--name", "Version");
            jQuery(this).find("ac--parameter").eq(1).attr("ac--name", "Name");
        });
        ser = new XMLSerializer();
        newBodyText = ser.serializeToString(xml[0]);
        newBodyText = newBodyText
            .split("ac--").join("ac:")
            .split("ri--").join("ri:")
            .split("nnbbsspp").join("&amp;nbsp;")
            .split("&lt;xml&gt;").join("")
            .split("&lt;/xml&gt;").join("")
            .split("/&gt;").join(" /&gt;")
            .split("qquuoott").join("&amp;quot;");
        response.body.storage.value = newBodyText;
        response.version.number += 1;
        jQuery.ajax({
            contentType: 'application/json',
            type: 'PUT',
            url: contextPath + "/rest/api/content/" + AJS.params.pageId,
            data: JSON.stringify(response),
            success: function(response) {
                console.log(response);
            },
            error: function(response) {
                console.log(response);
            }
        });
    }
});

You can paste this JS in the browser console, and it should be able to replace the macro parameters in the page with the proper values.  It only works on a page-by-page basis, but it should be easily adaptable to search for all pages containing the macro.

EDIT: Forgot to include "ancestors" in the URL string (needed in order to keep the same place in the page hierarchy)

Thanks Stephen! I will pass the info to the customer. Great support!

You could try using the Search and Replace plugin or Bob Swift's How to globally search and modify content to replace the parameters with the correctly named ones.

If you decide to use the plugin, feel free to ask for more help here.

Best, Lukas

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

271 views 2 10
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