001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.util;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.*;
025:
026: import org.apache.commons.lang.SystemUtils;
027:
028: import com.ecyrd.jspwiki.*;
029: import com.ecyrd.jspwiki.providers.AbstractFileProvider;
030: import com.ecyrd.jspwiki.providers.VersioningFileProvider;
031: import com.ecyrd.jspwiki.providers.WikiPageProvider;
032:
033: /**
034: * A command-line application that allows you to convert from
035: * one provider to another. Currently this only supports
036: * conversion from RCSFileProvider to VersioningFileProvider.
037: * <p>
038: * This class is mostly a hack, so do not trust it very much.
039: * It leaves the converted directory in /tmp/converter-tmp/
040: * and does not touch the original in any way.
041: *
042: * @author jalkanen
043: *
044: * @since
045: */
046: public class ProviderConverter {
047: private String m_rcsSourceDir;
048:
049: protected void setRCSSourceDir(String dir) {
050: m_rcsSourceDir = dir;
051: }
052:
053: private static final String[] WINDOWS_DEVICE_NAMES = { "con",
054: "prn", "nul", "aux", "lpt1", "lpt2", "lpt3", "lpt4",
055: "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "com1", "com2",
056: "com3", "com4", "com5", "com6", "com7", "com8", "com9" };
057:
058: protected String mangleName(String pagename) {
059: pagename = TextUtil.urlEncode(pagename, "UTF-8");
060:
061: pagename = TextUtil.replaceString(pagename, "/", "%2F");
062:
063: if (SystemUtils.IS_OS_WINDOWS) {
064: String pn = pagename.toLowerCase();
065: for (int i = 0; i < WINDOWS_DEVICE_NAMES.length; i++) {
066: if (WINDOWS_DEVICE_NAMES[i].equals(pn)) {
067: pagename = "$$$" + pagename;
068: }
069: }
070: }
071:
072: return pagename;
073: }
074:
075: protected void convert() throws WikiException, IOException {
076: Properties props = new Properties();
077:
078: props.setProperty(WikiEngine.PROP_APPNAME, "JSPWikiConvert");
079: props.setProperty(AbstractFileProvider.PROP_PAGEDIR,
080: m_rcsSourceDir);
081: props.setProperty(PageManager.PROP_PAGEPROVIDER,
082: "RCSFileProvider");
083: props.setProperty(PageManager.PROP_USECACHE, "false");
084: props.setProperty("log4j.appender.outlog",
085: "org.apache.log4j.ConsoleAppender");
086: props.setProperty("log4j.appender.outlog.layout",
087: "org.apache.log4j.PatternLayout");
088: props.setProperty("jspwiki.useLucene", "false");
089: props.setProperty("log4j.rootCategory", "INFO,outlog");
090: WikiEngine engine = new WikiEngine(props);
091:
092: WikiPageProvider sourceProvider = engine.getPageManager()
093: .getProvider();
094:
095: File tmpDir = new File(SystemUtils.JAVA_IO_TMPDIR,
096: "converter-tmp");
097:
098: props.setProperty(AbstractFileProvider.PROP_PAGEDIR, tmpDir
099: .getAbsolutePath());
100: WikiPageProvider destProvider = new VersioningFileProvider();
101:
102: destProvider.initialize(engine, props);
103:
104: Collection allPages = sourceProvider.getAllPages();
105:
106: int idx = 1;
107:
108: for (Iterator i = allPages.iterator(); i.hasNext();) {
109: WikiPage p = (WikiPage) i.next();
110:
111: System.out.println("Converting page: " + p.getName() + " ("
112: + idx + "/" + allPages.size() + ")");
113: List pageHistory = engine.getVersionHistory(p.getName());
114:
115: for (ListIterator v = pageHistory.listIterator(pageHistory
116: .size()); v.hasPrevious();) {
117: WikiPage pv = (WikiPage) v.previous();
118:
119: String text = engine.getPureText(pv.getName(), pv
120: .getVersion());
121:
122: destProvider.putPageText(pv, text);
123: }
124:
125: //
126: // Do manual setting now
127: //
128:
129: for (Iterator v = pageHistory.iterator(); v.hasNext();) {
130: WikiPage pv = (WikiPage) v.next();
131:
132: File f = new File(tmpDir, "OLD");
133: f = new File(f, mangleName(pv.getName()));
134: f = new File(f, pv.getVersion() + ".txt");
135:
136: System.out.println(" Setting old version "
137: + pv.getVersion() + " to date "
138: + pv.getLastModified());
139: f.setLastModified(pv.getLastModified().getTime());
140: }
141:
142: idx++;
143: }
144:
145: tmpDir.delete();
146: }
147:
148: /**
149: * @param args
150: */
151: public static void main(String[] args) throws Exception {
152: ProviderConverter c = new ProviderConverter();
153:
154: c.setRCSSourceDir(args[0]);
155:
156: c.convert();
157: }
158:
159: }
|