📄 weblogplugin.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package com.ecyrd.jspwiki.plugin;import java.text.DateFormat;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.Iterator;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.*;import com.ecyrd.jspwiki.auth.AuthorizationManager;import com.ecyrd.jspwiki.auth.permissions.PagePermission;import com.ecyrd.jspwiki.parser.PluginContent;import com.ecyrd.jspwiki.preferences.Preferences;import com.ecyrd.jspwiki.preferences.Preferences.TimeFormat;import com.ecyrd.jspwiki.providers.ProviderException;/** * <p>Builds a simple weblog. * The pageformat can use the following params:</p> * <p>%p - Page name</p> * <p>Parameters:</p> * <ul> * <li><b>page</b> - which page is used to do the blog; default is the current page.</li> * <li><b>entryFormat</b> - how to display the date on pages, using the J2SE SimpleDateFormat * syntax. Defaults to the current locale's DateFormat.LONG format * for the date, and current locale's DateFormat.SHORT for the time. * Thus, for the US locale this will print dates similar to * this: September 4, 2005 11:54 PM</li> * <li><b>days</b> - how many days the weblog aggregator should show. If set to * "all", shows all pages.</li> * <li><b>pageformat</b> - What the entry pages should look like.</li> * <li><b>startDate</b> - Date when to start. Format is "ddMMyy."</li> * <li><b>maxEntries</b> - How many entries to show at most.</li> * </ul> * <p>The "days" and "startDate" can also be sent in HTTP parameters, * and the names are "weblog.days" and "weblog.startDate", respectively.</p> * <p>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.</p> * @since 1.9.21 */// FIXME: Add "entries" param as an alternative to "days".// FIXME: Entries arrive in wrong order.public class WeblogPlugin implements WikiPlugin, ParserStagePlugin{ private static Logger log = Logger.getLogger(WeblogPlugin.class); private static final Pattern HEADINGPATTERN; /** How many days are considered by default. Default value is {@value} */ private static final int DEFAULT_DAYS = 7; private static final String DEFAULT_PAGEFORMAT = "%p_blogentry_"; /** The default date format used in the blog entry page names. */ public static final String DEFAULT_DATEFORMAT = "ddMMyy"; /** Parameter name for the startDate. Value is <tt>{@value}</tt>. */ public static final String PARAM_STARTDATE = "startDate"; /** Parameter name for the entryFormat. Value is <tt>{@value}</tt>. */ public static final String PARAM_ENTRYFORMAT = "entryFormat"; /** Parameter name for the days. Value is <tt>{@value}</tt>. */ public static final String PARAM_DAYS = "days"; /** Parameter name for the allowComments. Value is <tt>{@value}</tt>. */ public static final String PARAM_ALLOWCOMMENTS = "allowComments"; /** Parameter name for the maxEntries. Value is <tt>{@value}</tt>. */ public static final String PARAM_MAXENTRIES = "maxEntries"; /** Parameter name for the page. Value is <tt>{@value}</tt>. */ public static final String PARAM_PAGE = "page"; /** The attribute which is stashed to the WikiPage attributes to check if a page * is a weblog or not. You may check for its presence. */ public static final String ATTR_ISWEBLOG = "weblogplugin.isweblog"; static { // This is a pretty ugly, brute-force regex. But it will do for now... HEADINGPATTERN = Pattern.compile("(<h[1-4][^>]*>)(.*)(</h[1-4]>)", Pattern.CASE_INSENSITIVE); } /** * Create an entry name based on the blogname, a date, and an entry number. * * @param pageName Name of the blog * @param date The date (in ddMMyy format) * @param entryNum The entry number. * @return A formatted page name. */ public static String makeEntryPage( String pageName, String date, String entryNum ) { return TextUtil.replaceString(DEFAULT_PAGEFORMAT,"%p",pageName)+date+"_"+entryNum; } /** * Return just the basename for entires without date and entry numebr. * * @param pageName The name of the blog. * @return A formatted name. */ public static String makeEntryPage( String pageName ) { return TextUtil.replaceString(DEFAULT_PAGEFORMAT,"%p",pageName); } /** * Returns the entry page without the entry number. * * @param pageName Blog name. * @param date The date. * @return A base name for the blog entries. */ public static String makeEntryPage( String pageName, String date ) { return TextUtil.replaceString(DEFAULT_PAGEFORMAT,"%p",pageName)+date; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public String execute( WikiContext context, Map params ) throws PluginException { Calendar startTime; Calendar stopTime; int numDays = DEFAULT_DAYS; WikiEngine engine = context.getEngine(); AuthorizationManager mgr = engine.getAuthorizationManager(); // // Parse parameters. // String days; DateFormat entryFormat; 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( ( params.get(PARAM_ENTRYFORMAT)) == null ) { entryFormat = Preferences.getDateFormat( context, TimeFormat.DATETIME ); } else { entryFormat = new SimpleDateFormat( (String)params.get(PARAM_ENTRYFORMAT) ); } if( days != null ) { if( 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 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -