Is it possible to search the Confluence Databases for instances of a specific macros?
Asked by Ian Mason:

We are looking to remove/disable a macros on our instance of confluence, but we need to run a check to see if any pages are using the plugin to preclude their media from breaking? Can one query the Confluence DB for occurrences of a macros on any pages within Confluence?


Answers:
author: Evelin
body: You can use the search to find out if a macro was inserted on one or more pages. An example: If you like to know if the multiexcerpt or multiexcerpt-include macro has been used in your Confluence, type macroName:multi\* in the Search field, and hit Enter.

comments: author: Ian Mason
body: So if I'm looking for Widget Connector, can I search "macroName:'Widget Connector'"?


author: Evelin
body: Yes, I tried macroName:widget\* and it worked.


author: discountrobot
body: Hey, Ian.

You can do this via the XMLRPC

Here is a example written in python finding all pages with a instance of the thumbnail macro (ac:thumbnail)

Once complete all links containing said macro are written to a errors.json file.

```
#!/usr/bin/env pythonimport xmlrpclibimport jsonclass bcolors:    HEADER = '\033[95m'    OKBLUE = '\033[94m'    OKGREEN = '\033[92m'    WARNING = '\033[93m'    FAIL = '\033[91m'    ENDC = '\033[0m'    def disable(self):        self.HEADER = ''        self.OKBLUE = ''        self.OKGREEN = ''        self.WARNING = ''        self.FAIL = ''        self.ENDC = ''#Confluence XML-RPC infourl 		= 'http://localhost:1990/rpc/xmlrpc'client 		= xmlrpclib.Server(url, verbose=0);authToken	= client.confluence2.login("admin", "admin");spaces 		= client.confluence2.getSpaces(authToken);# Error stackerrors 		= []# Macro definition we're looking formacro = "ac:thumbnail"for space in spaces:  print ("Parsing space: " + space['name']);  spacePages = client.confluence2.getPages(authToken,space['key'])  for spacePage in spacePages:      print ("- Parsing page: " + spacePage['title']);      # get the page in question with content      page = client.confluence2.getPage(authToken, spacePage["id"])      # did we find the macro definition?      if(page["content"].find(macro) &gt; 0):        print ("{0}Found {1} macro!{2}".format(bcolors.WARNING, macro, bcolors.ENDC))        errors.append(spacePage["url"])# print the errorsfor error in errors:	print error["url"]#store the errorswith open('errors.json', 'w') as outfile:  json.dump([errors], outfile)client.confluence2.logout(authToken)
```

comments: author: Ian Mason
body: For this line of code -  
`# Macro definition we're looking for`  
`macro ``=` `"ac:thumbnail"`  
where do we find macro = "sometext" in Confluence?


author: discountrobot
body: To view the macro handle, go to a page containing the macro and click **Tools-\>View Storage format**

You should now see something like this:

```
&lt;ac:structured-macro ac:name="tip"&gt;
```

where *tip* is the macro handle. Try this with the answer by Evelin  
Or simply update the lines

```
# Macro definition we're looking formacro = 'ac:structured-macro ac:name="tip"'
```


author: discountrobot
body: The macro handle for Widget Connector is simply :)

```
ac:structured-macro ac:name="widget"
```


Tags: confluence-administration