/* JSPWiki - a JSP-based WikiWiki clone. Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi) This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser 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 */ package com.ecyrd.jspwiki.plugin; import com.ecyrd.jspwiki.*; import com.ecyrd.jspwiki.providers.ProviderException; import org.apache.log4j.Logger; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.*; /** * Builds a simple weblog. *
* The pageformat can use the following params:
* %p - Page name
*
* Parameters
*
* The weblog plugin also adds an attribute to each page it is on: "weblogplugin.isweblog" is set to "true". This can be used to quickly peruse pages which have weblogs. * @since 1.9.21 */ // FIXME: Add "entries" param as an alternative to "days". // FIXME: Entries arrive in wrong order. public class WeblogPlugin implements WikiPlugin, InitializablePlugin { 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"; 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"; public static final String ATTR_ISWEBLOG = "weblogplugin.isweblog"; 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; } /** * Just sets the "I am a weblog" mark. */ public void initialize( WikiContext context, Map params ) { context.getPage().setAttribute(ATTR_ISWEBLOG, "true"); } 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; 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 ); // // 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; } } // // Mark this to be a weblog // context.getPage().setAttribute(ATTR_ISWEBLOG, "true"); // // 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 ); StringBuffer sb = new StringBuffer(); try { List blogEntries = findBlogEntries( engine.getPageManager(), weblogName, startTime.getTime(), stopTime.getTime() ); // Rolf Mueller: begin-of-change creation date from filename // Collections.sort( blogEntries, new PageDateComparator() ); Collections.sort( blogEntries, new PageFileNameDateComparator() ); // SimpleDateFormat entryDateFmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm"); SimpleDateFormat entryDateFmt = new SimpleDateFormat("dd-MMM-yyyy"); SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT); // Rolf Mueller: end-of-change creation date from filename sb.append("