From b9f405d1e8b960529eb1ce6e84a86ab5aa8851bc Mon Sep 17 00:00:00 2001 From: low012 Date: Fri, 8 Oct 2010 00:32:50 +0000 Subject: [PATCH] *) added comments *) more beautyful and easier to understand code (IMO) *) added display= parameter to a lot of links in Wiki.html git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7226 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/Wiki.html | 43 +-- htroot/Wiki.java | 64 +++-- source/de/anomic/data/wiki/wikiBoard.java | 320 +++++++++++++++------- 3 files changed, 285 insertions(+), 142 deletions(-) diff --git a/htroot/Wiki.html b/htroot/Wiki.html index 643381e69..cee22f331 100644 --- a/htroot/Wiki.html +++ b/htroot/Wiki.html @@ -5,32 +5,31 @@ #%env/templates/metas.template%# - #(display)# - #%env/templates/simpleheader.template%# - #%env/templates/submenuPublication.template%# - :: - #%env/templates/header.template%# - #%env/templates/submenuPublication.template%# - :: - #%env/templates/embeddedheader.template%# + #(display)# + #%env/templates/simpleheader.template%# + #%env/templates/submenuPublication.template%# + :: + #%env/templates/header.template%# + #%env/templates/submenuPublication.template%# + :: + #%env/templates/embeddedheader.template%# #(/display)# #(mode)# -

YaCyWiki page: #[pagename]#

#[page]#
@@ -38,6 +37,7 @@
:: +

Edit

@@ -54,6 +54,7 @@ You can use Wiki Code here.

+ @@ -96,6 +97,7 @@ You can use Wiki Code here.

+ @@ -104,6 +106,7 @@ :: +

Index

@@ -112,7 +115,7 @@ #{pages}# - + @@ -120,14 +123,16 @@
Subject
#[subject]##[subject]# #[date]# #[author]#
#(error)#::

IO Error reading wiki database: #[message]#

#(/error)# :: +

Versions

#(error)#
-
Select versions of page '#[page]# +
Select versions of page '#[page]#' +
:
@@ -149,6 +154,7 @@
:: +

Error

IO Error reading wiki database: #[message]#

:: #(/error)# @@ -189,6 +195,7 @@ Wiki Code here.

+ @@ -197,8 +204,8 @@ #(/versioning)# #(/mode)# diff --git a/htroot/Wiki.java b/htroot/Wiki.java index b58119cbf..451007aea 100644 --- a/htroot/Wiki.java +++ b/htroot/Wiki.java @@ -47,8 +47,10 @@ import de.anomic.search.Switchboard; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; import de.anomic.yacy.yacyNewsPool; +import java.util.Map; public class Wiki { + private static final String ANONYMOUS = "anonymous"; //private static String ListLevel = ""; //private static String numListLevel = ""; @@ -74,12 +76,11 @@ public class Wiki { String access = sb.getConfig("WikiAccess", "admin"); final String pagename = get(post, "page", "start"); final String ip = get(post, HeaderFramework.CONNECTION_PROP_CLIENTIP, "127.0.0.1"); - String author = get(post, "author", "anonymous"); - if (author.equals("anonymous")) { + String author = get(post, "author", ANONYMOUS); + if (author.equals(ANONYMOUS)) { author = wikiBoard.guessAuthor(ip); if (author == null) { - if (sb.peers.mySeed() == null) author = "anonymous"; - else author = sb.peers.mySeed().get("Name", "anonymous"); + author = (sb.peers.mySeed() == null) ? ANONYMOUS : sb.peers.mySeed().get("Name", ANONYMOUS); } } @@ -94,10 +95,13 @@ public class Wiki { access = post.get("access", "admin"); sb.setConfig("WikiAccess", access); } - if (access.equals("admin")) prop.put("mode_access", "0"); - if (access.equals("all")) prop.put("mode_access", "1"); + if (access.equals("admin")) { + prop.put("mode_access", "0"); + } else if (access.equals("all")) { + prop.put("mode_access", "1"); + } - wikiBoard.entry page = sb.wikiDB.read(pagename); + wikiBoard.Entry page = sb.wikiDB.read(pagename); if (post != null && post.containsKey("submit")) { @@ -114,18 +118,22 @@ public class Wiki { } catch (final UnsupportedEncodingException e) { content = post.get("content", "").getBytes(); } - final wikiBoard.entry newEntry = sb.wikiDB.newEntry(pagename, author, ip, post.get("reason", "edit"), content); + final wikiBoard.Entry newEntry = sb.wikiDB.newEntry(pagename, author, ip, post.get("reason", "edit"), content); sb.wikiDB.write(newEntry); // create a news message - final HashMap map = new HashMap(); + final Map map = new HashMap(); map.put("page", pagename); map.put("author", author.replace(',', ' ')); - if (post.get("content", "").trim().length() > 0 && !page.page().equals(content)) + if (post.get("content", "").trim().length() > 0 && !new String(page.page()).equals(new String(content))) { sb.peers.newsPool.publishMyNews(sb.peers.mySeed(), yacyNewsPool.CATEGORY_WIKI_UPDATE, map); + } page = newEntry; prop.putHTML("LOCATION", "/Wiki.html?page=" + pagename); + prop.put("LOCATION", prop.get("LOCATION") + "&display=" + display); } + prop.put("mode_display", display); + if (post != null && post.containsKey("edit")) { if ((access.equals("admin") && (!sb.verifyAuthentication(header, true)))) { // check access right for admin @@ -139,6 +147,7 @@ public class Wiki { prop.putHTML("mode_author", author); prop.putHTML("mode_page-code", new String(page.page(), "UTF-8")); prop.putHTML("mode_pagename", pagename); + prop.put("mode_display", display); } catch (final UnsupportedEncodingException e) {} } @@ -147,6 +156,7 @@ public class Wiki { // preview the page prop.put("mode", "2");//preview prop.putHTML("mode_pagename", pagename); + prop.put("mode_display", display); prop.putHTML("mode_author", author); prop.put("mode_date", dateString(new Date())); prop.putWiki("mode_page", post.get("content", "")); @@ -157,18 +167,16 @@ public class Wiki { else if (post != null && post.containsKey("index")) { // view an index prop.put("mode", "3"); //Index - String subject; try { final Iterator i = sb.wikiDB.keys(true); - wikiBoard.entry entry; int count=0; while (i.hasNext()) { - subject = new String(i.next()); - entry = sb.wikiDB.read(subject); - prop.putHTML("mode_pages_"+count+"_name",wikiBoard.webalize(subject)); - prop.putHTML("mode_pages_"+count+"_subject", subject); - prop.put("mode_pages_"+count+"_date", dateString(entry.date())); - prop.putHTML("mode_pages_"+count+"_author", entry.author()); + final String subject = new String(i.next()); + final wikiBoard.Entry entry = sb.wikiDB.read(subject); + prop.putHTML("mode_pages_" + count + "_name",wikiBoard.webalize(subject)); + prop.putHTML("mode_pages_" + count + "_subject", subject); + prop.put("mode_pages_" + count + "_date", dateString(entry.date())); + prop.putHTML("mode_pages_" + count + "_author", entry.author()); count++; } prop.put("mode_pages", count); @@ -177,6 +185,7 @@ public class Wiki { prop.putHTML("mode_error_message", e.getMessage()); } prop.putHTML("mode_pagename", pagename); + prop.put("mode_display", display); } else if (post != null && post.containsKey("diff")) { @@ -184,12 +193,13 @@ public class Wiki { prop.put("mode", "4"); prop.putHTML("mode_page", pagename); prop.putHTML("mode_error_page", pagename); + prop.put("mode_error_display", display); try { final Iterator it = sb.wikiDB.keysBkp(true); - wikiBoard.entry entry; - wikiBoard.entry oentry = null; - wikiBoard.entry nentry = null; + wikiBoard.Entry entry; + wikiBoard.Entry oentry = null; + wikiBoard.Entry nentry = null; int count = 0; boolean oldselected = false, newselected = false; while (it.hasNext()) { @@ -209,10 +219,12 @@ public class Wiki { } count--; // don't show current version - if (!oldselected) // select latest old entry + if (!oldselected) { // select latest old entry prop.put("mode_error_versions_" + (count - 1) + "_oldselected", "1"); - if (!newselected) // select latest new entry (== current) + } + if (!newselected) { // select latest new entry (== current) prop.put("mode_error_curselected", "1"); + } if (count == 0) { prop.put("mode_error", "2"); // no entries found @@ -226,7 +238,9 @@ public class Wiki { prop.put("mode_error_curfdate", dateString(entry.date())); } - if (nentry == null) nentry = entry; + if (nentry == null) { + nentry = entry; + } if (post.containsKey("compare") && oentry != null && nentry != null) { // TODO: split into paragraphs and compare them with the same diff-algo final diff diff = new diff( @@ -237,6 +251,7 @@ public class Wiki { } else if (post.containsKey("viewold") && oentry != null) { prop.put("mode_versioning", "2"); prop.putHTML("mode_versioning_pagename", pagename); + prop.put("mode_versioning_display", display); prop.putHTML("mode_versioning_author", oentry.author()); prop.put("mode_versioning_date", dateString(oentry.date())); prop.putWiki("mode_versioning_page", oentry.page()); @@ -252,6 +267,7 @@ public class Wiki { // show page prop.put("mode", "0"); //viewing prop.putHTML("mode_pagename", pagename); + prop.put("mode_display", display); prop.putHTML("mode_author", page.author()); prop.put("mode_date", dateString(page.date())); prop.putWiki("mode_page", page.page()); diff --git a/source/de/anomic/data/wiki/wikiBoard.java b/source/de/anomic/data/wiki/wikiBoard.java index e95575698..7964f07a9 100644 --- a/source/de/anomic/data/wiki/wikiBoard.java +++ b/source/de/anomic/data/wiki/wikiBoard.java @@ -41,7 +41,9 @@ import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.order.Base64Order; import net.yacy.kelondro.order.NaturalOrder; - +/** + * + */ public class wikiBoard { public static final int keyLength = 64; @@ -54,11 +56,17 @@ public class wikiBoard { SimpleFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); } - MapHeap datbase = null; - MapHeap bkpbase = null; - static HashMap authors = new HashMap(); + private MapHeap datbase = null; + private MapHeap bkpbase = null; + private static final Map AUTHORS = new HashMap(); - public wikiBoard( final File actpath, final File bkppath) throws IOException { + /** + * Constructor. + * @param actpath path of database which contains current wiki data. + * @param bkppath path of backup database. + * @throws IOException if error occurs during HDD access. + */ + public wikiBoard(final File actpath, final File bkppath) throws IOException { new File(actpath.getParent()).mkdirs(); if (datbase == null) { //datbase = new MapView(BLOBTree.toHeap(actpath, true, true, keyLength, recordSize, '_', NaturalOrder.naturalOrder, actpathNew), 500, '_'); @@ -71,224 +79,329 @@ public class wikiBoard { } } + /** + * Gets total number of entries of wiki DB and DB which contains backup entries. + * @return number of entries in wiki plus number of old entries. + */ public int sizeOfTwo() { return datbase.size() + bkpbase.size(); } + /** + * Gets number of entries of wiki DB. + * @return number of entries in wiki. + */ public int size() { return datbase.size(); } + /** + * Closes database files. + */ public void close() { datbase.close(); bkpbase.close(); } + /** + * Gets current date. + * @return current date. + */ static String dateString() { return dateString(new Date()); } + /** + * Gets String representation of a Date. + * @param date the Date. + * @return String representation of Date. + */ public static String dateString(final Date date) { synchronized (SimpleFormatter) { return SimpleFormatter.format(date); } } + /** + * Gets normalized version of a key. + * @param key the key. + * @return normalized version of key. + */ private static String normalize(final String key) { return (key != null) ? key.trim().toLowerCase() : "null"; } + /** + * Normalizes key and replaces spaces by escape code. + * @param key the key. + * @return normalized and webalized version. + */ public static String webalize(final String key) { return (key != null) ? normalize(key).replaceAll(" ", "%20") : "null"; } + /** + * Tries to guess the name of the author by a given IP address. + * @param ip the IP address. + * @return + */ public static String guessAuthor(final String ip) { - final String author = authors.get(ip); + final String author = AUTHORS.get(ip); //yacyCore.log.logDebug("DEBUG: guessing author for ip = " + ip + " is '" + author + "', authors = " + authors.toString()); return author; } + /** + * Adds an author name and a corresponding IP to internal Map. + * @param ip IP address of the author. + * @param author name of author. + */ public static void setAuthor(final String ip, final String author) { - authors.put(ip,author); + AUTHORS.put(ip,author); } - public entry newEntry(final String subject, final String author, final String ip, final String reason, final byte[] page) throws IOException { - return new entry(normalize(subject), author, ip, reason, page); + /** + * Creates new Entry. + * @param subject subject of entry. + * @param author author of entry. + * @param ip IP address of author. + * @param reason reason for new Entry (for example "edit"). + * @param page content of Entry. + * @return new Entry. + * @throws IOException + */ + public Entry newEntry(final String subject, final String author, final String ip, final String reason, final byte[] page) throws IOException { + return new Entry(normalize(subject), author, ip, reason, page); } - public class entry { + /** + * Contains information of wiki page. + */ + public class Entry { private static final String ANONYMOUS = "anonymous"; - String key; - Map record; - - public entry(final String subject, String author, String ip, String reason, final byte[] page) throws IOException { - 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", Base64Order.enhancedCoder.encode(author.getBytes("UTF-8"))); - if ((ip == null) || (ip.length() == 0)) { - ip = ""; - } - record.put("ip", ip); - if ((reason == null) || (reason.length() == 0)) { - reason = ""; - } - record.put("reason", Base64Order.enhancedCoder.encode(reason.getBytes("UTF-8"))); - if (page == null) { - record.put("page", ""); - } else { - record.put("page", Base64Order.enhancedCoder.encode(page)); - } - authors.put(ip, author); - //System.out.println("DEBUG: setting author " + author + " for ip = " + ip + ", authors = " + authors.toString()); + private String key; + private final Map record; + + /** + * Constructor which creates new Entry using given information. + * @param subject subject of Entry. + * @param author author of Entry. + * @param ip IP address of author. + * @param reason reason for new Entry (for example "edit"). + * @param page content of Entry. + * @throws IOException + */ + public Entry(final String subject, final String author, final String ip, final String reason, final byte[] page) throws IOException { + this.record = new HashMap(); + this.key = subject.substring(0, Math.min((key != null) ? key.length() : 0, keyLength)); + this.record.put("date", dateString()); + this.record.put("author", Base64Order.enhancedCoder.encodeString((author != null && author.length() > 0) ? author : ANONYMOUS)); + this.record.put("ip", (ip != null && ip.length() > 0) ? ip : ""); + this.record.put("reason", Base64Order.enhancedCoder.encodeString((reason != null && reason.length() > 0) ? reason : "")); + this.record.put("page", (page != null) ? Base64Order.enhancedCoder.encode(page) : ""); + AUTHORS.put(ip, author); } - entry(final String key, final Map record) { + /** + * Constructor which creates Entry using key and record. + * @param key key of Entry. + * @param record record which contains data. + */ + Entry(final String key, final Map record) { this.key = key; this.record = record; } + /** + * Gets subject of Entry. + * @return subject of entry. + */ public String subject() { return key; } + /** + * Gets date of Entry. + * @return date of Entry. + */ public Date date() { + Date ret; try { final String c = record.get("date"); if (c == null) { System.out.println("DEBUG - ERROR: date field missing in wikiBoard"); - return new Date(); - } - synchronized (SimpleFormatter) { - return SimpleFormatter.parse(c); + ret = new Date(); + } else { + synchronized (SimpleFormatter) { + ret = SimpleFormatter.parse(c); + } } } catch (final ParseException e) { - return new Date(); + ret = new Date(); } + return ret; } + /** + * Gets author of Entry. + * @return author of Entry. + */ public String author() { final String a = record.get("author"); final byte[] b; return (a != null && (b = Base64Order.enhancedCoder.decode(a)) != null) ? new String(b) : ANONYMOUS; } + /** + * Gets reason for Entry. + * @return reason for Entry. + */ public String reason() { + final String ret; final String r = record.get("reason"); - if (r == null) { - return ""; - } - final byte[] b = Base64Order.enhancedCoder.decode(r); - if (b == null) { - return "unknown"; + if (r != null) { + final byte[] b; + ret = ((b = Base64Order.enhancedCoder.decode(r)) != null) ? new String(b) : "unknown"; + } else { + ret = ""; } - return new String(b); + return ret; } + /** + * Gets actual content of Entry. + * @return content of Entry. + */ public byte[] page() { final String m = record.get("page"); final byte[] b; return (m != null && (b = Base64Order.enhancedCoder.decode(m)) != null) ? b : new byte[0]; } + /** + * Sets date of previous version of Entry. + * @param date date of previous version of Entry. + */ void setAncestorDate(final Date date) { record.put("bkp", dateString(date)); } + /** + * Gets date of previous version of Entry. + * @return date of previous version of Entry. + */ private Date getAncestorDate() { + Date ret = null; try { final String c = record.get("date"); - if (c == null) return null; - synchronized (SimpleFormatter) { - return SimpleFormatter.parse(c); + if (c != null) { + synchronized (SimpleFormatter) { + ret = SimpleFormatter.parse(c); + } } } catch (final ParseException e) { - return null; + ret = null; } + return ret; } - /* - public boolean hasAncestor() { - Date ancDate = getAncestorDate(); - if (ancDate == null) return false; - try { - return bkpbase.has(key + dateString(ancDate)); - } catch (IOException e) { - return false; - } - } + /** + * Gets previous version of Entry. + * @return previous version of Entry. */ - - public entry getAncestor() { + public Entry getAncestor() { final Date ancDate = getAncestorDate(); return (ancDate == null) ? null : read(key + dateString(ancDate), bkpbase); } + /** + * Adds child of current Entry. + * @param subject subject of child of current Entry. + */ void setChild(final String subject) { record.put("child", Base64Order.enhancedCoder.encode(subject.getBytes())); } + /** + * Gets name (= subject) of child of this Entry. + * @return name of child of this Entry. + */ private String getChildName() { final String c = record.get("child"); final byte[] subject; return (c != null && (subject = Base64Order.enhancedCoder.decode(c)) != null) ? new String(subject) : null; } + /** + * Tells if Entry has child. + * @return true if has child, else false. + */ public boolean hasChild() { final String c = record.get("child"); return (c != null && Base64Order.enhancedCoder.decode(c) != null) ? true : false; } - public entry getChild() { + /** + * Gets child of this Entry. + * @return child of this Entry. + */ + public Entry getChild() { final String childName = getChildName(); return (childName == null) ? null : read(childName, datbase); } } - public String write(final entry page) { + /** + * Writes content of Entry to database and returns key. + * @param entry Entry to be written. + * @return key of Entry. + */ + public String write(final Entry entry) { // writes a new page and returns key - String ret = null; + String key = null; try { // first load the old page - final entry oldEntry = read(page.key); + final Entry oldEntry = read(entry.key); // set the bkp date of the new page to the date of the old page final Date oldDate = oldEntry.date(); - page.setAncestorDate(oldDate); - oldEntry.setChild(page.subject()); + entry.setAncestorDate(oldDate); + oldEntry.setChild(entry.subject()); // write the backup - //System.out.println("key = " + page.key); - //System.out.println("oldDate = " + oldDate); - //System.out.println("record = " + oldEntry.record.toString()); - bkpbase.insert((page.key + dateString(oldDate)).getBytes(), oldEntry.record); + bkpbase.insert((entry.key + dateString(oldDate)).getBytes(), oldEntry.record); // write the new page - datbase.insert(page.key.getBytes(), page.record); - ret = page.key; + datbase.insert(entry.key.getBytes(), entry.record); + key = entry.key; } catch (final Exception e) { Log.logException(e); } - return ret; + return key; } - public entry read(final String key) { + /** + * Reads content of Entry from database. + * @param key key of Entry. + * @return Entry which contains data. + */ + public Entry read(final String key) { return read(key, datbase); } - entry read(String key, final MapHeap base) { - entry ret = null; + /** + * Reads content of Entry from database. + * @param key key of Entry. + * @param base database containing data. + * @return Entry which contains data. + */ + Entry read(final String key, final MapHeap base) { + Entry ret = null; try { - key = normalize(key); - if (key.length() > keyLength) { - key = key.substring(0, keyLength); + String copyOfKey = normalize(key); + if (copyOfKey.length() > keyLength) { + copyOfKey = copyOfKey.substring(0, keyLength); } - final Map record = base.get(key.getBytes()); - ret = (record == null) ? newEntry(key, ANONYMOUS, "127.0.0.1", "New Page", "".getBytes()) : new entry(key, record); + final Map record = base.get(copyOfKey.getBytes()); + ret = (record == null) ? newEntry(copyOfKey, ANONYMOUS, "127.0.0.1", "New Page", "".getBytes()) : new Entry(copyOfKey, record); } catch (final IOException e) { Log.logException(e); } catch (RowSpaceExceededException e) { @@ -296,25 +409,32 @@ public class wikiBoard { } return ret; } - - public entry readBkp(final String key) { + + /** + * Reads old Entry from backup database. + * @param key key of Entry. + * @return the Entry. + */ + public Entry readBkp(final String key) { return read(key, bkpbase); } - /* - public boolean has(String key) { - try { - return datbase.has(normalize(key)); - } catch (IOException e) { - return false; - } - } + /** + * Gets Iterator of keys in database. + * @param up + * @return keys of Entries in database. + * @throws IOException */ - public Iterator keys(final boolean up) throws IOException { return datbase.keys(up, false); } + /** + * Gets Iterator of keys in backup database. + * @param up + * @return keys of Entries in backup database. + * @throws IOException + */ public Iterator keysBkp(final boolean up) throws IOException { return bkpbase.keys(up, false); }