summaryrefslogtreecommitdiff
path: root/source/de/anomic/data/wikiBoard.java
blob: ce8b65a06ff3091e63ad82e367c1e579642ee793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// wikiBoard.java 
// -------------------------------------
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
// last major change: 20.07.2004
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.

package de.anomic.data;

import java.io.*;
import java.util.*;
import java.text.*;
import de.anomic.kelondro.*;
import de.anomic.server.*;

public class wikiBoard {
    
    private static final int keyLength = 64;
    private static final String dateFormat = "yyyyMMddHHmmss";
    private static final int recordSize = 512;

    private static TimeZone GMTTimeZone = TimeZone.getTimeZone("PST");
    private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat(dateFormat);

    private kelondroMap datbase = null;
    private kelondroMap bkpbase = null;
    private Hashtable authors = new Hashtable();
    private int sn = 0;

    public wikiBoard(File actpath, File bkppath, int bufferkb) throws IOException {
	new File(actpath.getParent()).mkdir();
        if (datbase == null) {
	    if (actpath.exists())
		datbase = new kelondroMap(new kelondroDyn(actpath, bufferkb * 0x40));
	    else
		datbase = new kelondroMap(new kelondroDyn(actpath, bufferkb * 0x400, keyLength, recordSize));
	}
	new File(bkppath.getParent()).mkdir();
	if (bkpbase == null) {
	    if (bkppath.exists())
		bkpbase = new kelondroMap(new kelondroDyn(bkppath, bufferkb * 0x400));
	    else
		bkpbase = new kelondroMap(new kelondroDyn(bkppath, bufferkb * 0x400, keyLength + dateFormat.length(), recordSize));
	}
	sn = 0;
    }

    public void close() throws IOException {
        datbase.close();
        bkpbase.close();
    }
    
    private static String dateString() {
	return dateString(new GregorianCalendar(GMTTimeZone).getTime());
    }

    private static String dateString(Date date) {
	return SimpleFormatter.format(date);
    }

    private static String normalize(String key) {
        if (key == null) return "null";
        return key.trim().toLowerCase();
    }

    public static String webalize(String key) {
        if (key == null) return "null";
        key = key.trim().toLowerCase();
        int p;
        while ((p = key.indexOf(" ")) >= 0)
            key = key.substring(0, p) + "%20" + key.substring(p +1);
        return key;
    }
    
    public String guessAuthor(String ip) {
        String author = (String) authors.get(ip);
        //System.out.println("DEBUG: guessing author for ip = " + ip + " is '" + author + "', authors = " + authors.toString());
        return author;
    }

    public entry newEntry(String subject, String author, String ip, String reason, byte[] page) {
	return new entry(normalize(subject), author, ip, reason, page);
    }

    public class entry {
	
	String key;
        Map record;

	public entry(String subject, String author, String ip, String reason, byte[] page) {
	    record = new HashMap();
	    key = subject;
	    if (key.length() > keyLength) key = key.substring(0, keyLength);
	    record.put("date", dateString());
	    if ((author == null) || (author.length() == 0)) author = "anonymous";
	    record.put("author", serverCodings.enhancedCoder.encodeBase64(author.getBytes()));
	    if ((ip == null) || (ip.length() == 0)) ip = "";
	    record.put("ip", ip);
	    if ((reason == null) || (reason.length() == 0)) reason = "";
	    record.put("reason", serverCodings.enhancedCoder.encodeBase64(reason.getBytes()));
	    if (page == null)
		record.put("page", "");
	    else
		record.put("page", serverCodings.enhancedCoder.encodeBase64(page));
            authors.put(ip, author);
            //System.out.println("DEBUG: setting author " + author + " for ip = " + ip + ", authors = " + authors.toString());
	}

	private entry(String key, Map record) {
	    this.key = key;
	    this.record = record;
	}

	public String subject() {
	    return key;
	}

	public Date date() {
	    try {
		String c = (String) record.get("date");
		return SimpleFormatter.parse(c);
	    } catch (ParseException e) {
		return new Date();
	    }
	}

	public String author() {
	    String a = (String) record.get("author");
	    if (a == null) return "anonymous";
	    byte[] b = serverCodings.enhancedCoder.decodeBase64(a);
	    if (b == null) return "anonymous";
	    return new String(b);
	}

	public String reason() {
	    String r = (String) record.get("reason");
	    if (r == null) return "";
	    byte[] b = serverCodings.enhancedCoder.decodeBase64(r);
	    if (b == null) return "unknown";
	    return new String(b);
	}

	public byte[] page() {
	    String m = (String) record.get("page");
	    if (m == null) return new byte[0];
	    byte[] b = serverCodings.enhancedCoder.decodeBase64(m);
	    if (b == null) return "".getBytes();
	    return b;
	}
        
	private void setAncestorDate(Date date) {
	    record.put("bkp", dateString(date));
	}

	private Date getAncestorDate() {
	    try {
		String c = (String) record.get("date");
		if (c == null) return null;
		return SimpleFormatter.parse(c);
	    } catch (ParseException e) {
		return null;
	    }
	}

        /*
	public boolean hasAncestor() {
	    Date ancDate = getAncestorDate();
	    if (ancDate == null) return false;
	    try {
		return bkpbase.has(key + dateString(ancDate));
	    } catch (IOException e) {
		return false;
	    }
	}
        */
        
	public entry getAncestor() {
	    Date ancDate = getAncestorDate();
	    if (ancDate == null) return null;
	    return read(key + dateString(ancDate), bkpbase);
	}

 	private void setChild(String subject) {
	    record.put("child", serverCodings.enhancedCoder.encodeBase64(subject.getBytes()));
	}
        
        private String getChildName() {
            String c = (String) record.get("child");
	    if (c == null) return null;
	    byte[] subject = serverCodings.enhancedCoder.decodeBase64(c);
            if (subject == null) return null;
            return new String(subject);
	}
        
        public boolean hasChild() {
            String c = (String) record.get("child");
	    if (c == null) return false;
	    byte[] subject = serverCodings.enhancedCoder.decodeBase64(c);
            return (subject != null);
	}

        public entry getChild() {
	    String childName = getChildName();
	    if (childName == null) return null;
	    return read(childName, datbase);
	}
    }

    public String write(entry page) {
	// writes a new page and returns key
	try {
	    // first load the old page
	    entry oldEntry = read(page.key);
	    // set the bkp date of the new page to the date of the old page
	    Date oldDate = oldEntry.date();
	    page.setAncestorDate(oldDate);
            oldEntry.setChild(page.subject());
	    // write the backup
            //System.out.println("key = " + page.key);
            //System.out.println("oldDate = " + oldDate);
            //System.out.println("record = " + oldEntry.record.toString());
	    bkpbase.set(page.key + dateString(oldDate), oldEntry.record);
	    // write the new page
	    datbase.set(page.key, page.record);
	    return page.key;
	} catch (IOException e) {
	    return null;
	}
    }

    public entry read(String key) {
	return read(key, datbase);
    }

    private entry read(String key, kelondroMap base) {
	try {
            key = normalize(key);
	    Map record = base.get(key);
	    if (record == null)
		return newEntry(key, "anonymous", "127.0.0.1", "New Page", "".getBytes());
	    else
		return new entry(key, record);
	} catch (IOException e) {
	    return null;
	}
    }

    /*
    public boolean has(String key) {
	try {
	    return datbase.has(normalize(key));
	} catch (IOException e) {
	    return false;
	}
    }
    */
    
    public Iterator keys(boolean up) throws IOException {
	return datbase.keys(up, false);
    }

}