package com.ecyrd.jspwiki.plugin; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import com.ecyrd.jspwiki.PageManager; import com.ecyrd.jspwiki.TextUtil; import com.ecyrd.jspwiki.WikiContext; import com.ecyrd.jspwiki.WikiEngine; import com.ecyrd.jspwiki.WikiPage; import com.ecyrd.jspwiki.WikiProvider; import com.ecyrd.jspwiki.providers.ProviderException; /** *

GategoryWeblogPlugin.java

* *

* View Source *

* * @author $Author$ * @version $Reversion$ $Date$ */ public class WeblogCategoryPlugin implements WikiPlugin { private static Logger log = Logger.getLogger(WeblogPlugin.class); public static final int DEFAULT_DAYS = 7; //public static final String DEFAULT_PAGEFORMAT = "%p_blogentry_"; public static final String DEFAULT_DATEFORMAT = "ddMMyy"; //emptry for all category public static final String DEFAULT_CATEGORY = ""; public static final String PARAM_STARTDATE = "startDate"; public static final String PARAM_DAYS = "days"; public static final String PARAM_ALLOWCOMMENTS = "allowComments"; public static final String PARAM_MAXENTRIES = "maxEntries"; //public static final String PARAM_PAGE = "page"; //category param key public static final String PARAM_CATEGORY = "category"; //public static final String PARAM_MAXENTRYLENGTH = "maxEntryLength"; // public static String makeEntryPage( // String pageName, // String date, // String entryNum) { // return TextUtil.replaceString(DEFAULT_PAGEFORMAT, "%p", pageName) // + date // + "_" // + entryNum; // } // // public static String makeEntryPage(String pageName) { // return TextUtil.replaceString(DEFAULT_PAGEFORMAT, "%p", pageName); // // } // // public static String makeEntryPage(String pageName, String date) { // return TextUtil.replaceString(DEFAULT_PAGEFORMAT, "%p", pageName) // + date; // } public String execute(WikiContext context, Map params) throws PluginException { Calendar startTime; Calendar stopTime; int numDays; WikiEngine engine = context.getEngine(); // // Parse parameters. // String days; String startDay = null; boolean hasComments = false; int maxEntries; String weblogName; int maxEntryLength; // if ((weblogName = (String) params.get(PARAM_PAGE)) == null) { // weblogName = context.getPage().getName(); // } if ((days = context.getHttpParameter("weblog." + PARAM_DAYS)) == null) { days = (String) params.get(PARAM_DAYS); } if (days != null && days.equalsIgnoreCase("all")) { numDays = Integer.MAX_VALUE; } else { numDays = TextUtil.parseIntParameter(days, DEFAULT_DAYS); } if ((startDay = (String) params.get(PARAM_STARTDATE)) == null) { startDay = context.getHttpParameter("weblog." + PARAM_STARTDATE); } if (TextUtil.isPositive((String) params.get(PARAM_ALLOWCOMMENTS))) { hasComments = true; } maxEntries = TextUtil.parseIntParameter( (String) params.get(PARAM_MAXENTRIES), Integer.MAX_VALUE); //Category String category = (String) params.get(PARAM_CATEGORY); if (null == category || 0 >= category.length()) { //all category category = ""; } else { // // ;General;Java;Art; // for compare the page is in category list or not... // such as:category.indexOf(";Java;");... // category = ";" + category + ";"; } // maxEntryLength = // TextUtil.parseIntParameter( // (String) params.get(PARAM_MAXENTRYLENGTH), // Integer.MAX_VALUE); // // Determine the date range which to include. // startTime = Calendar.getInstance(); stopTime = Calendar.getInstance(); if (startDay != null) { SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT); try { Date d = fmt.parse(startDay); startTime.setTime(d); stopTime.setTime(d); } catch (ParseException e) { return "Illegal time format: " + startDay; } } // // We make a wild guess here that nobody can do millisecond // accuracy here. // startTime.add(Calendar.DAY_OF_MONTH, -numDays); startTime.set(Calendar.HOUR, 0); startTime.set(Calendar.MINUTE, 0); startTime.set(Calendar.SECOND, 0); stopTime.set(Calendar.HOUR, 23); stopTime.set(Calendar.MINUTE, 59); stopTime.set(Calendar.SECOND, 59); String result = ""; try { List blogEntries = findBlogEntries( engine.getPageManager(), //weblogName, startTime.getTime(), stopTime.getTime(), category); //generate blog entry list... result = generateBlogEntryList( engine, hasComments, maxEntries, blogEntries, category); } catch (ProviderException e) { log.error("Could not locate blog entries", e); throw new PluginException( "Could not locate blog entries: " + e.getMessage()); } //Result of blog entry list... return result; } /** * Generate blog entry list * * @param engine * @param hasComments * @param maxEntries * @param blogEntries * @return * @throws ProviderException */ private String generateBlogEntryList( WikiEngine engine, boolean hasComments, int maxEntries, List blogEntries, String categoryCfg) throws ProviderException { StringBuffer sb = new StringBuffer(); Collections.sort(blogEntries, new PageDateComparator()); SimpleDateFormat entryDateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // category set,if the category is not seted... Set categorySet = new HashSet(); //blog entry list content sb.append("
\n"); for (Iterator i = blogEntries.iterator(); i.hasNext() && maxEntries-- > 0; ) { WikiPage p = (WikiPage) i.next(); sb.append("
\n"); // // Heading // sb.append("
\n"); Date entryDate = p.getLastModified(); sb.append(entryDateFmt.format(entryDate)); sb.append("
\n"); // // Category for each blog entry // String pageName = p.getName(); String category = pageName.substring(0, pageName.indexOf("_")); categorySet.add(category); sb .append("
\n") .append( "  Category:") .append(category + "
\n"); // // Append the text of the latest version. Reset the // context to that page. // sb.append("
\n"); WikiContext entryCtx = new WikiContext(engine, p); sb.append(engine.getHTML(entryCtx, engine.getPage(p.getName()))); sb.append("
\n"); // // Append footer // sb.append("
\n"); String author = p.getAuthor(); if (author != null) { if (engine.pageExists(author)) { author = "" + engine.beautifyTitle(author) + ""; } } else { author = "AnonymousCoward"; } sb.append("By " + author + "  "); sb.append( "Permalink"); String commentPageName = TextUtil.replaceString(p.getName(), "blogentry", "comments"); if (hasComments) { int numComments = guessNumberOfComments(engine, commentPageName); // // We add the number of comments to the URL so that // the user's browsers would realize that the page // has changed. // sb.append("  "); sb.append( "Comments? (" + numComments + ")"); } sb.append("
\n"); // // Done, close // sb.append("
\n"); } //end for...all blog entry... sb.append("
\n"); //Add category head... StringBuffer categorysb = new StringBuffer("

Category

"); //combine category head and blog list result categorysb.append(sb); return categorysb.toString(); } private int guessNumberOfComments(WikiEngine engine, String commentpage) throws ProviderException { String pagedata = engine.getPureText(commentpage, WikiProvider.LATEST_VERSION); return TextUtil.countSections(pagedata); } /** * Attempts to locate all pages that correspond to the * blog entry pattern. Will only consider the days on the dates; not the hours and minutes. * * Returns a list of pages with their FIRST revisions. */ public List findBlogEntries( PageManager mgr, //String baseName, Date start, Date end, String category) throws ProviderException { Collection everyone = mgr.getAllPages(); //log.info("------Page Counts:" + everyone.size()); ArrayList result = new ArrayList(); //baseName = makeEntryPage(baseName); SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT); for (Iterator i = everyone.iterator(); i.hasNext();) { WikiPage p = (WikiPage) i.next(); String pageName = p.getName(); String categoryName = ""; boolean isInCategory = true; //empty category cfg will be ";;" if (2 >= category.length()) { isInCategory = true; } else { int index = pageName.indexOf("_blogentry_"); categoryName = pageName.substring(0, index >= 0 ? index : 0); if (0 >= categoryName.length()) { isInCategory = true; } else { if (0 <= category.indexOf(";" + categoryName + ";")) { isInCategory = true; } else { isInCategory = false; } } } //all blog entry... int index = pageName.indexOf("_blogentry_"); if (isInCategory && 0 < index) { String baseName = pageName.substring(0,index + 11); //if (pageName.startsWith(baseName)) { // // Check the creation date from the page name. // We do this because RCSFileProvider is very slow at getting a // specific page version. // try { //log.debug("Checking: "+pageName); int firstScore = pageName.indexOf('_', baseName.length() - 1); if (firstScore != -1 && firstScore + 1 < pageName.length()) { int secondScore = pageName.indexOf('_', firstScore + 1); if (secondScore != -1) { String creationDate = pageName.substring(firstScore + 1, secondScore); //log.debug(" Creation date: "+creationDate); Date pageDay = fmt.parse(creationDate); // // Add the first version of the page into the list. This way // the page modified date becomes the page creation date. // if (pageDay != null && pageDay.after(start) && pageDay.before(end)) { WikiPage firstVersion = mgr.getPageInfo(pageName, 1); result.add(firstVersion); } } } } catch (Exception e) { log.debug( "Page name :" + pageName + " was suspected as a blog entry but it isn't because of parsing errors", e); } } } return result; } /** * Reverse comparison. */ private class PageDateComparator implements Comparator { public int compare(Object o1, Object o2) { if (o1 == null || o2 == null) { return 0; } WikiPage page1 = (WikiPage) o1; WikiPage page2 = (WikiPage) o2; return page2.getLastModified().compareTo(page1.getLastModified()); } } }