How to automate adding wiki macro to pages

Description

If you are migrating from Confluence 3.x and want to retain wiki markup on some specific pages, then here is a quick way to do so. The key is knowing what contain you want to retain as wiki markup. Often, pages can be identified by what macros are on the page. The following will use this case as an example, but really any Confluence search terms can be used. 

This is for Confluence 3.x only

This needs to be done before migrating !!!

Be careful, test first

Whenever using automation that could affect a large number of pages, be very careful and test on a test space or test instance. It is hard to undo most source changes.

Figure out quote escaping for your OS

See Tips.

References

Dependencies

Steps

  1. Determine your search criteria to find pages. Verify the UI finds the right pages. 

    Example
    Example: "{macroname" NOT "{wiki}".
  2. Construct an appropriate runFromContentList command similar to: How to globally search and modify content 

    Example
    --action getContentList     --search " \"{macroname\" NOT \"{wiki}\" "
     
    --action runFromContentList --search " \"{macroname\" NOT \"{wiki}\" " --common " ... "
  3. Test the modifyPage action on a test page 

    confluence --action modifyPage --space xxx --title test --content "{wiki} \n " --content2 " \n {wiki}" 
  4. Construct the combined command similar to: How to globally search and modify content 

    Example
    confluence --action runFromContentList --search " \"{macroname\" NOT \"{wiki}\" " --space @all --common "--action  modifyPage --space @space@ --id @pageId@ --content \"{wiki} \n \" --content2 \" \n {wiki}\" "

Alternatives

For people comfortable with SQL and having access to the Confluence database, runFromSql can be used as an alternative to find all the pages that need to be modified. There is a runFromSql example on How to automate adding text to Confluence pages. Here is some example SQL:

Id of latest version of pages containing search string
select contentid from bodycontent, content 
    where contenttype = 'PAGE' 
        and version = (select max(version) from content as c where c.title = content.title) 
        and bodycontent.body like '%name="table-plus">%'