cogConverts JSPWiki pages to XWiki pages
TypeOther
Developed by

xwiki:XWiki.fborry

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

This would need to be adapted to use the new XWiki Rendering module since this module knows how to convert from JSPWiki syntax to XWiki syntax.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.io.output.ByteArrayOutputStream;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.WikiEngine;
import com.ecyrd.jspwiki.WikiPage;
import com.ecyrd.jspwiki.attachment.Attachment;
import com.ecyrd.jspwiki.providers.WikiPageProvider;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiConfig;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;

public class JspWikiImporter
{
    
      public final static String OLD_WIKI_SPACE_NAME = "OldWiki";
      public final static String INSERT_BLOG_LINKS_PATTER ="#insertBlogLinksHere";
      public final static DateFormat DATE_FORMAT = newSimpleDateFormat("dd/MM/yyyy");
      public final static DateFormat JSPWIKI_DATE_FORMAT = newSimpleDateFormat("ddMMyy");
    
      private WikiEngine engine;
    
      private XWikiContext context;

      private XWiki xwiki;
    
      private Map<String,XWikiDocument> xwikiPages = newTreeMap<String,XWikiDocument>();
    
      private Map<String,XWikiDocument> rootPages = newTreeMap<String,XWikiDocument>();

      private Set<XWikiDocument> blogPages = new HashSet<XWikiDocument>();
    
      boolean inCode = false;
    
      boolean inTable = false;

    
      public static void main(String[] args)
      {
            new JspWikiImporter().export("C:/home/dev/tools/cots/xwiki/jspwiki-exporter/src/content/jspwiki.properties");
            System.exit(0);
      }

      public void export(String configFileName)
      {
            try
            { // Start xwiki
                  this.context = new XWikiContext();
                  XWikiConfig config = new XWikiConfig();
                  this.xwiki = new com.xpn.xwiki.XWiki(config, this.context);

                  // Export from jspwiki
                  Properties props = new Properties();
                  props.load(new FileInputStream(new File(configFileName)));

                  props.put(WikiEngine.PROP_ENCODING, "UTF-8");

                  engine = new WikiEngine(props);
                  export(props);

                  // Save to archive
                  com.xpn.xwiki.plugin.packaging.Package export = newcom.xpn.xwiki.plugin.packaging.Package();
                  export.setWithVersions(false);
                  export.setAuthorName("Fred");
                  export.setDescription("ExportJspWiki");
                  export.setLicence("");
                  export.setVersion("");
                  export.setBackupPack(false);
                  export.setName("ExportJspWiki");
                  export.addAllWikiDocuments(context);
                  export.export(newFileOutputStream("c:\\Temp\\jspwikiexport.zip"), context);
            }
            catch (Exception e)
            {
                  e.printStackTrace();

                  throw new RuntimeException("Export failed", e);
            }
      }

      private void export(Properties props) throws Exception
      {
            // export
            export("c:/temp/jspwiki-export/", props);
      }
    
      private void export(String exportPath, Properties properties) throwsException
      {
            Set<String> visited = new HashSet<String>();

            // pages
            Collection pages = engine.getPageManager().getAllPages();
            Iterator ipages = pages.iterator();
            while (ipages.hasNext())
            {
                  WikiPage page = (WikiPage) ipages.next();

                  if (!visited.contains(page.getName()))
                  {
                        exportPage(engine, properties, page);
                        visited.add(page.getName());
                  }
            }
            // Create WebHome page
            XWikiDocument webHomePage = xwiki.getDocument(OLD_WIKI_SPACE_NAME,"WebHome" , context);
            webHomePage.setTitle("Old Wiki");
            webHomePage.setDate(new Date());
            webHomePage.setComment("Page generated by JSPWiki importer ");
          
            StringBuilder webHomeContent = new StringBuilder();
            webHomeContent.append("1 Old Wiki\n");
          
            // Try and find parent pages
            for(Map.Entry<String,XWikiDocument> entry : xwikiPages.entrySet())
            {
                  String name = entry.getKey();
                  int underscoreIndex = name.lastIndexOf('_');
                  if(underscoreIndex > 0)
                  {
                        String parentName = name.substring(0,underscoreIndex);
                        XWikiDocument parent = xwikiPages.get(parentName);
                        boolean stop = false;
                        while(!stop)
                        {
                             XWikiDocument doc = entry.getValue();
                             if(parent != null)
                             {     // Update parent ref
                                   doc.setParent(OLD_WIKI_SPACE_NAME + "."+parentName);
                                   xwiki.saveDocument(doc, context);
                                   System.out.println("Setting parent / child relation : "+parentName+"->"+name);
                                   stop = true;
                             }
                             else
                             {
                                   underscoreIndex = parentName.lastIndexOf('_');
                                   if(underscoreIndex > 0)
                                   {
                                         parentName = parentName.substring(0,underscoreIndex);
                                         parent = xwikiPages.get(parentName);
                                   }
                                   else
                                   {     // Orphan or root page
                                         stop = true;
                                         if(shouldIndex(name))
                                         {
                                               rootPages.put(name, doc);
                                         }
                                   }
                             }
                        }
                  }
                  else
                  {
                        if(shouldIndex(name))
                        {
                             rootPages.put(name, entry.getValue());
                        }
                  }
            }
          
            // Build index page
            for(XWikiDocument rootPage : rootPages.values())
            {
                  buildIndex(webHomeContent, rootPage, 1);
            }
          
            // Save webHome
            webHomePage.setContent(webHomeContent.toString());
            xwiki.saveDocument(webHomePage, context);
          
            // Process blog pages
            for(XWikiDocument blogPage : blogPages)
            {
                  StringBuilder entriesLinks = new StringBuilder();
                  String blogPageName = blogPage.getName();
                  SortedMap<Date,XWikiDocument> entries = new TreeMap<Date, XWikiDocument>();
                  for(Map.Entry<String,XWikiDocument> entry :xwikiPages.entrySet())
                  {
                        String pageName = entry.getKey();
                       if((!pageName.equals(blogPage))&&pageName.startsWith(blogPageName))
                        {     // This is a blog entry
                             XWikiDocument entryDoc = entry.getValue();
                             Date date;
                             // Compute date
                             int dateIndex = pageName.indexOf("blogentry_");
                             if(dateIndex < 0)
                             {
                                   dateIndex = pageName.indexOf("comments_");
                                   if(dateIndex > 0)
                                   {
                                         dateIndex += 9;
                                   }
                             }
                             else
                             {
                                   dateIndex += 10;
                             }
                             if(dateIndex > 0)
                             {
                                   String dateString = pageName.substring(dateIndex);
                                   int tailIndex = dateString.indexOf("_");
                                   if(tailIndex > 0)
                                   {
                                         dateString = dateString.substring(0,tailIndex);
                                    }
                                   try
                                   {
                                         date =JSPWIKI_DATE_FORMAT.parse(dateString);
                                   }
                                   catch(Exception e)
                                   {
                                         date = new Date();
                                   }
                             }
                             else
                             {
                                   date = new Date();
                             }
                             while(entries.containsKey(date))
                             {     // Already an entry at this date
                                   date = new Date((date.getTime()+1));
                             }
                             entries.put(date, entryDoc);
                        }
                  }
                  if(!entries.isEmpty())
                  {
                        List<Map.Entry<Date,XWikiDocument>> sortedEntries = newArrayList<Map.Entry<Date,XWikiDocument>>(entries.entrySet());
                        Collections.reverse(sortedEntries);
                        for(Map.Entry<Date,XWikiDocument> entry : sortedEntries)
                        {
                             Date date = entry.getKey();
                             XWikiDocument entryDoc = entry.getValue();
                             String pageName = entryDoc.getName();
                             entriesLinks.append("* [");
                             String firstLine =extractFirstLine(entryDoc.getContent(),pageName);
                             if(firstLine == null)
                             {
                                   firstLine = pageName;
                             }
                             else
                             {
                                   if(firstLine.startsWith("1.1.1"))
                                   {
                                         firstLine = firstLine.substring(5);
                                   }
                                   else if(firstLine.startsWith("1.1"))
                                   {
                                         firstLine = firstLine.substring(3);
                                   }
                                   else if(firstLine.startsWith("1"))
                                   {
                                         firstLine = firstLine.substring(1);
                                   }
                                   else if(firstLine.startsWith("**"))
                                   {
                                         firstLine = firstLine.substring(2);
                                   }
                                   else if(firstLine.startsWith("*"))
                                   {
                                         firstLine = firstLine.substring(1);
                                   }
                                   else if(firstLine.startsWith("11."))
                                   {
                                         firstLine = firstLine.substring(3);
                                   }
                                   else if(firstLine.startsWith("1."))
                                   {
                                         firstLine = firstLine.substring(2);
                                   }
                                   int lfIndex = firstLine.indexOf("\\");
                                   if(lfIndex > 0)
                                   {
                                         firstLine = firstLine.substring(0,lfIndex);
                                   }
                             }
                             entriesLinks.append(firstLine).append(" (");
                             entriesLinks.append(DATE_FORMAT.format(date));
                             entriesLinks.append(")|").append(pageName).append("]\n");
                             System.out.println("Adding blog entry : "+blogPage.getName()+"->"+pageName);
                        }
                  }
                  String newContent = blogPage.getContent().replace(INSERT_BLOG_LINKS_PATTER, entriesLinks.toString());
                  blogPage.setContent(newContent);
                  xwiki.saveDocument(blogPage, context);
            }

            //attachments
            Collection files =engine.getAttachmentManager().getAllAttachments();
            Iterator ifiles = files.iterator();
            while (ifiles.hasNext())
            {
                  Attachment atta = (Attachment) ifiles.next();
                  exportAttachment(exportPath, properties, atta);
            }
      }

      private static boolean shouldIndex(String pageName)
      {
            boolean result = false;
            if(pageName.startsWith("Java"))
            {
                  result = true;
            }
            else if(pageName.startsWith("JMX"))
            {
                  result = true;
            }
            else if(pageName.startsWith("LeSiteDe"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Logosonnerie"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Logosonnerie"))
            {
                  result = true;
            }
            else if(pageName.startsWith("LS"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Main"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Maven"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Plateforme"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Puma"))
            {
                  result = true;
            }
            else if(pageName.startsWith("PUMA"))
            {
                  result = true;
            }
            else if(pageName.startsWith("Qualite"))
            {
                  result = true;
            }
            else if(pageName.startsWith("SDAC"))
            {
                  result = true;
            }
            return result;
      }

    
      private void buildIndex(StringBuilder content, XWikiDocument page, intindent) throws Exception
      {
            String pageName = page.getName();
            String firstLine = extractFirstLine(page.getContent(),pageName);
            for(int index = 0 ; index < indent ; index++)
            {
                  content.append('*');
            }
            content.append(" [");
            content.append(firstLine);
            content.append(" (").append(pageName);
            content.append(")|").append(pageName).append("]\n");
            System.out.println("Adding index item : "+pageName);
            // Search for children
            String parentPageName = OLD_WIKI_SPACE_NAME + "." + pageName;
            for(XWikiDocument childPage : xwikiPages.values())
            {
                  if(childPage.getParent().equals(parentPageName))
                  {     // This is a child page
                        buildIndex(content, childPage, indent + 1);
                  }
            }
      }
    
      private static String extractFirstLine(String pageContent, String pageName) throws Exception
      {
            BufferedReader reader = new BufferedReader(newStringReader(pageContent));
            String firstLine = reader.readLine();
            while(firstLine != null && (firstLine.startsWith("#toc") ||"".equals(firstLine.trim())))
            {
                  firstLine = reader.readLine();
            }
            if(firstLine == null || firstLine.startsWith("<"))
            {
                  firstLine = pageName;
            }
            else
            {
                  if(firstLine.startsWith("1.1.1"))
                  {
                        firstLine = firstLine.substring(5);
                  }
                  else if(firstLine.startsWith("1.1"))
                  {
                        firstLine = firstLine.substring(3);
                  }
                  else if(firstLine.startsWith("1"))
                  {
                        firstLine = firstLine.substring(1);
                  }
                  else if(firstLine.startsWith("**"))
                  {
                        firstLine = firstLine.substring(2);
                  }
                  else if(firstLine.startsWith("*"))
                  {
                        firstLine = firstLine.substring(1);
                  }
                  else if(firstLine.startsWith("11."))
                  {
                        firstLine = firstLine.substring(3);
                  }
                  else if(firstLine.startsWith("1."))
                  {
                        firstLine = firstLine.substring(2);
                  }
            }
            returnfirstLine.trim().replace("[","").replace("]","").replace("|","").replace("*","");
      }

      private void exportPage(WikiEngine engine, Properties properties, WikiPage page) throws Exception
      {
            inCode = false;
            inTable = false;
            // params
            String jspWikiPageName = page.getName();
            String name = convertPageName(jspWikiPageName);
            String title = engine.beautifyTitle(jspWikiPageName);
            String author = page.getAuthor();
            Date lastmod = page.getLastModified();

            String meta = engine.getPageManager().getPageText(jspWikiPageName, WikiPageProvider.LATEST_VERSION);

            XWikiDocument doc = xwiki.getDocument(OLD_WIKI_SPACE_NAME, name,context);
            doc.setTitle(title);
            String content;
            if(meta.startsWith("[{Form"))
            {
                  content = convertPluginContent(page, meta);
            }
            else
            {
                  content = convertWikiPage(doc,meta);
            }
            doc.setContent(content);
            doc.setDate(lastmod);
            doc.setAuthor(author);
            doc.setComment("Version : " + page.getVersion());
            doc.setCreationDate(lastmod);
            doc.setCreator(author);
            doc.setContentAuthor(author);
            xwiki.saveDocument(doc, context);
            xwikiPages.put(name, doc);

            // progress
            System.out.println("Page:\t" + name);
      }

      @SuppressWarnings("unchecked")
      private void exportAttachment(String exportPath, Properties properties, Attachment atta) throws Exception
      {

            String parent = convertPageName(atta.getParentName());
            String fileName = atta.getFileName();
            String author = atta.getAuthor();
            int version = atta.getVersion();
            Date date = atta.getLastModified();

            // Get data
            InputStream is =engine.getAttachmentManager().getAttachmentStream(atta);

            // copy
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            while (true)
            {
                  byte[] buf = new byte[64 * 1024];
                  int sz = is.read(buf);
                  if (sz == -1)
                  {
                        break;
                  }
                  outSteam.write(buf, 0, sz);
            }
            outSteam.close();
            is.close();

            XWikiDocument doc = xwiki.getDocument(OLD_WIKI_SPACE_NAME, parent,context);

            // Read XWikiAttachment
            XWikiAttachment attachment = doc.getAttachment(fileName);

            if (attachment == null)
            {
                  attachment = new XWikiAttachment();
                  doc.getAttachmentList().add(attachment);
            }
            attachment.setContent(outSteam.toByteArray());
            attachment.setFilename(fileName);
            attachment.setAuthor(author);
            attachment.setDate(date);

            // Add the attachment to the document
            attachment.setDoc(doc);

            // Adding a comment with a link to the download URL
            doc.setComment("Version :"+version);

            // Save the content and the archive
            doc.saveAttachmentContent(attachment, context);

            // progress
            System.out.println("Attachment: " + fileName);
      }
    
      private static String convertPageName(String jspWikiName)
      {
            return jspWikiName.replace(".", "_");
      }
    
      private String convertWikiPage(XWikiDocument page, String jspWikiPage)throws Exception
      {
            StringBuilder result = new StringBuilder();
            BufferedReader reader = new BufferedReader(newStringReader(jspWikiPage));
            String currentLine = reader.readLine();
            while(currentLine != null)
            {
                  result.append(convertWikiLine(page,currentLine)).append('\n');
                  currentLine = reader.readLine();
            }
            if(inTable)
            {     // Close any pending table
                  result.append("\n{table}");
            }
            return result.toString();
      }
    
      private String convertWikiLine(XWikiDocument page, String jspWikiLine)
      {
            String result = jspWikiLine;
            if(!inCode)
            {
                  if(result.startsWith("!!! "))
                  {
                        result = "1" + result.substring(3);
                  }
                  else if(result.startsWith("!!!"))
                  {
                        result = "1 " + result.substring(3);
                  }
                  else if(result.startsWith("!! "))
                  {
                        result = "1.1" + result.substring(2);
                  }
                  else if(result.startsWith("!!"))
                  {
                        result = "1.1 " + result.substring(2);
                  }
                  else if(result.startsWith("! "))
                  {
                        result = "1.1.1" + result.substring(1);
                  }
                  else if(result.startsWith("!"))
                  {
                        result = "1.1.1 " + result.substring(1);
                  }
                  else if(result.startsWith("### "))
                  {
                        result = "111." + result.substring(3);
                  }
                  else if(result.startsWith("###"))
                  {
                        result = "111. " + result.substring(3);
                  }
                  else if(result.startsWith("## "))
                  {
                        result = "11." + result.substring(2);
                  }
                  else if(result.startsWith("##"))
                  {
                        result = "11. " + result.substring(2);
                  }
                  else if(result.startsWith("# "))
                  {
                        result = "1." + result.substring(1);
                  }
                  else if(result.startsWith("#"))
                  {
                        result = "1. " + result.substring(1);
                  }
                  else if(result.startsWith("***** "))
                  {     // Nothing to do
                  }
                  else if(result.startsWith("*****"))
                  {
                        result = "***** " + result.substring(5);
                  }
                  else if(result.startsWith("**** "))
                  {     // Nothing to do
                  }
                  else if(result.startsWith("****"))
                  {
                        result = "**** " + result.substring(4);
                  }
                  else if(result.startsWith("*** "))
                  {     // Nothing to do
                  }
                  else if(result.startsWith("***"))
                  {
                        result = "*** " + result.substring(3);
                  }
                  else if(result.startsWith("** "))
                  {     // Nothing to do
                  }
                  else if(result.startsWith("**"))
                  {
                        result = "** " + result.substring(2);
                  }
                  else if(result.startsWith("* "))
                  {     // Nothing to do
                  }
                  else if(result.startsWith("*"))
                  {
                        result = "* " + result.substring(1);
                  }
            }
            // Table handling
            if(result.startsWith("|"))
            {
                  if(inTable)
                  {     // Remove leading pipe
                        result = result.replace("||","|").substring(1);
                  }
                  else
                  {
                        inTable = true;
                        result = "{table}\n" + result.replace("||","|").substring(1);
                  }
            }
            else if(inTable)
            {     // Close table
                  result = "{table}\n" + result;
                  inTable = false;
            }
            if(result.contains("{WeblogPlugin"))
            {     // Weblog plugin page
                  result = INSERT_BLOG_LINKS_PATTER;
                  blogPages.add(page);
            }
            else if(result.contains("{WeblogEntryPlugin"))
            {
                  result = "";
            }
            else if(result.contains("{WeblogArchivePlugin"))
            {
                  result = "";
            }
            else
            {
                  if(result.indexOf("{{{")>=0)
                  {
                        result = result.replace("{{{", "{" + "code" + "}");
                        inCode = true;
                  }
                  if(!inCode)
                  {
                        result = result.replace("<","&lt;").replace(">","&gt;");
                  }
                  if(result.indexOf("}}}")>=0)
                  {
                        result = result.replace("}}}", "{" + "code" + "}");
                        inCode = false;
                  }
                  if(!inCode)
                  {
                        result = result.replace("[{TableOfContents}]","#toc(\"\" \"\" \"\")");
                        result = result.replace("__", "*");
                        result = result.replace("''", "~~");
                        result = result.replace("{{", "<tt>");
                        result = result.replace("}}", "</tt>");
                        result = result.replace("http://mobilite.wiki.tls.123multimedia.com:8080/wiki/Wiki.jsp?page=","http://xwiki.tls.123multimedia.com/xwiki/bin/view/"+OLD_WIKI_SPACE_NAME+"/");

                        result = result.replace("http://mobilite.wiki.tls.123multimedia.com:8080/wiki/attach","http://xwiki.tls.123multimedia.com/xwiki/bin/download/"+OLD_WIKI_SPACE_NAME);
                        int imageIndex = result.indexOf(".png]");
                        if(imageIndex > 0)
                        {
                             String endOfLine = result.substring(imageIndex + 5);
                             String startOfLine = result.substring(0,imageIndex);
                             int linkStart = startOfLine.lastIndexOf("[");
                             if(linkStart >= 0)
                             {
                                    result = startOfLine.substring(0,linkStart) + "{image:" + startOfLine.substring(linkStart + 1) + "}" + endOfLine;
                             }
                        }
                  }
            }
            return result;
      }
    
      private String convertPluginContent(WikiPage page, String pageContent)throws Exception
      {
        WikiContext context = new WikiContext(engine, page);          
            String html = engine.textToHTML(context, pageContent);
            return html.trim();
      }
}

Prerequisites & Installation Instructions

  • You will need to add JSPWiki and XWiki libraries and dependencies to your ClassPath.
  • You will also need a WEB-INF dir in your ClassPath with the JspWiki configuration files in it. Can't exactly remember why, but JspWiki looks there!...
  • The generated jspwikiexport.zip is an XWiki archive (xar) that can be imported from the XWiki admin page.
Tags: migration
Created by Vincent Massol on 2008/12/26 12:52
    

Get Connected