001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001 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.plugin;
021:
022: import org.apache.log4j.Logger;
023: import com.ecyrd.jspwiki.*;
024: import com.ecyrd.jspwiki.attachment.Attachment;
025: import java.util.*;
026: import java.io.StringWriter;
027: import java.text.SimpleDateFormat;
028:
029: /**
030: * Returns the Recent Changes.
031: *
032: * Parameters: since=number of days,
033: * format=(compact|full)
034: *
035: * @author Janne Jalkanen
036: */
037: public class RecentChangesPlugin implements WikiPlugin {
038: /** How many days we show by default. */
039: private static final int DEFAULT_DAYS = 100 * 365;
040:
041: private static Logger log = Logger
042: .getLogger(RecentChangesPlugin.class);
043:
044: private boolean isSameDay(Date a, Date b) {
045: Calendar aa = Calendar.getInstance();
046: aa.setTime(a);
047: Calendar bb = Calendar.getInstance();
048: bb.setTime(b);
049:
050: return (aa.get(Calendar.YEAR) == bb.get(Calendar.YEAR) && aa
051: .get(Calendar.DAY_OF_YEAR) == bb
052: .get(Calendar.DAY_OF_YEAR));
053: }
054:
055: public String execute(WikiContext context, Map params)
056: throws PluginException {
057: int since = TextUtil.parseIntParameter((String) params
058: .get("since"), DEFAULT_DAYS);
059: int spacing = 4;
060: boolean showAuthor = true;
061: WikiEngine engine = context.getEngine();
062:
063: //
064: // Which format we want to see?
065: //
066: String format = (String) params.get("format");
067: if ("compact".equals(params.get("format"))) {
068: spacing = 0;
069: showAuthor = false;
070: }
071:
072: Calendar sincedate = new GregorianCalendar();
073: sincedate.add(Calendar.DAY_OF_MONTH, -since);
074:
075: log.debug("Calculating recent changes from "
076: + sincedate.getTime());
077:
078: // FIXME: Should really have a since date on the getRecentChanges
079: // method.
080: Collection changes = engine.getRecentChanges();
081: StringWriter out = new StringWriter();
082:
083: //
084: // This linkProcessor is used to transform links.
085: //
086: TranslatorReader linkProcessor = new TranslatorReader(context,
087: new java.io.StringReader(""));
088:
089: if (changes != null) {
090: Date olddate = new Date(0);
091:
092: SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy");
093: SimpleDateFormat tfmt = new SimpleDateFormat("HH:mm:ss");
094:
095: out.write("<table border=\"0\" cellpadding=\"" + spacing
096: + "\">\n");
097:
098: for (Iterator i = changes.iterator(); i.hasNext();) {
099: WikiPage pageref = (WikiPage) i.next();
100:
101: Date lastmod = pageref.getLastModified();
102:
103: if (lastmod.before(sincedate.getTime())) {
104: break;
105: }
106:
107: if (!isSameDay(lastmod, olddate)) {
108: out.write("<tr>\n");
109: out.write(" <td colspan=\"2\"><b>"
110: + fmt.format(lastmod) + "</b></td>\n");
111: out.write("</tr>\n");
112: olddate = lastmod;
113: }
114:
115: String link = linkProcessor
116: .makeLink(
117: (pageref instanceof Attachment) ? TranslatorReader.ATTACHMENT
118: : TranslatorReader.READ,
119: pageref.getName(), engine
120: .beautifyTitle(pageref
121: .getName()));
122:
123: out.write("<tr>\n");
124:
125: out.write("<td width=\"30%\">" + link + "</td>\n");
126:
127: if (pageref instanceof Attachment) {
128: out.write("<td>" + tfmt.format(lastmod) + "</td>");
129: } else {
130: out.write("<td><a href=\""
131: + context.getURL(WikiContext.DIFF, pageref
132: .getName(), "r1=-1") + "\">"
133: + tfmt.format(lastmod) + "</a></td>\n");
134: }
135:
136: //
137: // Display author information.
138: //
139:
140: if (showAuthor) {
141: String author = pageref.getAuthor();
142: String displayAuthor = author;
143:
144: if (author != null) {
145: displayAuthor = context
146: .getUserCommonName(author);
147: if (engine.pageExists(author)) {
148: displayAuthor = linkProcessor.makeLink(
149: TranslatorReader.READ, author,
150: author);
151: }
152: } else {
153: displayAuthor = "unknown";
154: }
155:
156: out.write("<td>" + displayAuthor + "</td>");
157: }
158:
159: out.write("</tr>\n");
160: }
161:
162: out.write("</table>\n");
163: }
164:
165: return out.toString();
166: }
167:
168: }
|