Thursday, June 30, 2005

Wiki migration, XML-RPC and the Jakarta IO Taglib

Some time ago I chose to install a Wiki for the development team in which I work. The engine I chose at that time was UseModWiki. UseModWiki is a monolithic Perl script and as such was very easy to install and configure. One of the other reasons I chose it was because it was being used by the Apache Software Foundation. Since that time Apachehave migrated from UseModWiki to MoinMoin. Perl isn't really my first language anymore and I'd like to add a couple of bells and whistles so I'd be happier to use something Java technology based and there are now some quite mature Java based wiki engines about.

My problem, I expect, is a relatively common one. I have quite a bit of content in the UseModWiki instance that I'd like to migrate to my brand spanking new wiki engine (JSPWiki). Now UseModWiki uses db files for page storage, I'm not sure I remember enough Perl to write a database extraction utility for this purpose. So what do I do?

It turns out that somebody has written a companion Perl script for UseModWiki to enable it to export its content using XML-RPC.

0xDECAFBAD's XmlRpcToWiki

Cool but doesn't using XML-RPC in Java sound a little too much like hard work? Well that is what I thought at first too until I remembered the Jakarta IO Taglib.

This Jakarta IO taglib supports XML-RPC so with a little help from JSTL, and a Base64 decoding taglib I found on the net, I am able to query and write the UseModWiki content to text files. Granted this is probably a couple of the ugliest JSPs I have ever written (and I've written some doozies in my time). This kind of thing really belongs in a command line Perl script butsince the taglibs are making things so easy and it is a one-off, then it won't hurt to do this just this once!


<%@ taglib uri="http://jakarta.apache.org/taglibs/io-1.0" prefix="io" %>
<%@ taglib uri="http://www.servletsuite.com/servlets/base64tag" prefix="base64" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>

<c:set var="textResponse"><%--
--%>
<io:xmlrpc url="http://someserver/cgi-bin/usemod_xmlrpc.pl"><%--
--%>
<io:body><%--
--%>
<methodCall><%--
--%>
<methodName>wiki.getAllPages</methodName><%--
--%>
</methodCall><%--
--%>
</io:body><%--
--%>
</io:xmlrpc><%--
--%>
</c:set>

<c:out value="${textResponse}"/>

<x:parse xml="${textResponse}" var="responseXml"/>

<br />

<ul>
<x:forEach select="$responseXml/methodResponse/params/param/value/array/data/value/string" var="pageName">
<li><x:out select="."/></li>

<c:import url="script2.jsp" var="dummy">
<c:param name="pageName"><x:out select="."/></c:param>
</c:import>
</x:forEach>
</ul>

The JSP page above queries the UseModWiki server using XML-RPC. It receives a list of all the pages on the Wiki (in an XML message format), it then processes this list calling the page below once for every page in the wiki.


<%@ taglib uri="http://jakarta.apache.org/taglibs/io-1.0" prefix="io" %>
<%@ taglib uri="http://www.servletsuite.com/servlets/base64tag" prefix="base64" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %>
<%@ page import="java.io.*" %>

<c:set var="pageName"><c:out value="${param.pageName}" default="Rubbish" /></c:set>

<c:set var="textResponse"><%--
--%>
<io:xmlrpc url="http://someserver/cgi-bin/usemod_xmlrpc.pl"><%--
--%>
<io:body><%--
--%>
<methodCall><%--
--%>
<methodName>wiki.getPage</methodName><%--
--%>
<params><%--
--%>
<param><%--
--%>
<value><c:out value="${pageName}"/></value><%--
--%>
</param><%--
--%>
</params><%--
--%>
</methodCall><%--
--%>
</io:body><%--
--%>
</io:xmlrpc><%--
--%>
</c:set>

<h1><%= pageContext.getAttribute("pageName") %></h1>

<%
File file = new File( "c://jspwiki//" + pageContext.getAttribute("pageName") + ".txt" );
FileWriter fileWriter = new FileWriter( file );

%>

<x:parse xml="${textResponse}" var="responseXml"/>

<c:set var="output"><%--
--%>
<base64:decode><%--
--%>
<x:out select="$responseXml/methodResponse/params/param/value/base64"/><%--
--%>
</base64:decode><%--
--%>
</c:set>


<h2>Writing....</h2>
<pre>
<c:out value="${output}" escapeXml="false"/>

</pre>
<h2>To</h2>
<h1><%= file.getCanonicalPath() %></h1>

<io:pipe writer="<%= fileWriter %>"><%--
--%>
<c:out value="${output}" escapeXml="false"/><%--
--%>
</io:pipe>

<%
fileWriter.close();
%>

The JSP page above is passed a pagename parameter and will query the UseModWiki for the page contents. It receives the contents in an XML format message and converts the string value it receives from Base64 encoding to plain text. It then writes this to a text file.

It worked, I now have all my UseModWiki files in the original text format. All I needed to do was to convert from the UseMod TextFormatting to the JSPWiki TextFormatting. I found WinGrep came in quite handy for that, although I suppose I could have written it using the Jakarta RegExp taglib, now that would have been a really ugly JSP!

Tags :

2 comments:

Mark McLaren said...

I'm trying to do a similar thing; however, I've got a bit of a snag. When I run the below JSP, I get the error, "cannot write to a URLConnection if doOutput=false - call setDoOutput(true)." How would I modify the below to "call setDoOutput(true)"?















SecurityLogin



myLogin

myPassword

prod








Note: Comment imported. Original by Alan Richards at 2005-07-10 20:11

Mark McLaren said...

Hi Alan. Good to hear from you. I think the setDoOutput(true) is achieved by using:






Note: Comment imported. Original by markmc website: http://content.mark-mclaren.info/ at 2005-07-16 16:22