Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Salvatore "Sax" Cammarata October 7, 2015

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
Stephen Deutsch
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 8, 2015

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)

Salvatore "Sax" Cammarata October 8, 2015

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

0 votes
Lukas Knoch -Rumpelcoders-
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 8, 2015

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
TAGS
AUG Leaders

Atlassian Community Events