cogGenerates a MindMap of the wiki
TypeSnippet
Category
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Generates a MindMap of the wiki using the Mindmap Macro (you'll need to make sure you enable it first!)

This snippet offers a "update" button for admin users which generates a MindMap of the current site and attaches the generated MindMap to the page it's executed in. 

You'll need to have programming rights to execute this snippet as it's in Groovy

A more advanced MindMap application also allows to import MindMap to wiki pages.

This snippet doesn't work for versions greater than 3.0M3 since the Velocity Mindmap macro has been removed in 3.0M3. If you still wish to use it you'll need to add the following macro before the Groovy snippet:

{{velocity}}
#macro(mindmap $mindmap $width $height)
#if(!$width)
#set($width="100%")
#end
#if(!$height)
#set($height="600")
#end
#if(!$mindmap)
#set($mindmap = "")
#set($mmurl = "")
#else
#if($mindmap.startsWith("http"))
#set($mmurl = $mindmap)
#else
#set($mmurl1 = $doc.getAttachmentURL($mindmap,"download"))
#set($mmurl = "${escapetool.url(${request.scheme})}://${escapetool.url(${request.serverName})}:${escapetool.url(${request.serverPort})}${mmurl1}")
#end
#end
<applet code="freemind.main.FreeMindApplet.class" archive="${escapetool.xml(${request.contextPath})}/freemind/freemind.jar" width="${escapetool.xml(${width})}" height="${escapetool.xml(${height})}">
<param name="type" value="application/x-java-applet;version=1.4" />
<param name="scriptable" value="false" />
<param name="modes" value="freemind.modes.browsemode.BrowseMode" />
<param name="browsemode_initial_map" value="$mmurl" />
<param name="initial_mode" value="Browse" />
<param name="selection_method" value="selection_method_direct" />
</applet>
#end
{{/velocity}}

You'll also need to download the freemind JAR and copy it in XWiki's WEB-INF/lib directory (or attach it to the page where you've put this snippet and use the jars parameter of the groovy macro).

Code

Add the following in a page, using the XWiki Syntax 2.1:

{{groovy}}
import java.io.StringReader
import java.util.HashMap
import com.xpn.xwiki.doc.XWikiAttachment
import groovy.xml.MarkupBuilder

def generateXmlForChildNode(nodeName, xmlBuilder)
{
    xmlBuilder.node(link:xwiki.getURL(nodeName, "view"), text:nodeName) {
        services.query.xwql("select distinct doc.web, doc.name, doc.parent from XWikiDocument as doc where doc.parent = :parent").bindValue("parent", nodeName).execute().each() {
            def docRef = services.model.createDocumentReference("", it[0], it[1])
            def serializedDocRef = services.model.serialize(docRef, "local")
            generateXmlForChildNode(serializedDocRef, xmlBuilder)
        }
    }
}

def generateMap(firstNodeName, xmlBuilder)
{
    xmlBuilder.map() {
        generateXmlForChildNode(firstNodeName, xmlBuilder)
    }
}

if (xwiki.hasAdminRights())
{
    println "[[Update the Map>>||queryString='update=1']]"

    if (request.update) {
        print "Start mindmap generation... "

        def writer = new StringWriter()
        def xmlBuilder = new MarkupBuilder(writer)
        generateMap("Main.WebHome", xmlBuilder)

        def attachmentName = "sitemap.mm"

        def attachment = doc.getDocument().getAttachment(attachmentName)
        if (!attachment)
        {
            attachment = doc.getDocument().addAttachment(attachmentName, writer.toString().getBytes(), xcontext.context)
        } else {
            attachment.setContent(writer.toString().getBytes())
        }
        doc.getDocument().saveAttachmentContent(attachment, xcontext.context)
        doc.save()

        println "done"
    }
}
{{/groovy}}

{{velocity}}
#if ($doc.attachmentList.size() > 0)
  {{html}}
  #mindmap("sitemap.mm" "100%" "500")
  {{/html}}
#end
{{/velocity}}

Result

sitemap.png

Tags: groovy mindmap
     

Get Connected