001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.plugin; 020 021import org.apache.commons.lang3.StringUtils; 022import org.apache.logging.log4j.LogManager; 023import org.apache.logging.log4j.Logger; 024import org.apache.wiki.WikiContext; 025import org.apache.wiki.api.core.Context; 026import org.apache.wiki.api.core.ContextEnum; 027import org.apache.wiki.api.core.Engine; 028import org.apache.wiki.api.core.Page; 029import org.apache.wiki.api.exceptions.PluginException; 030import org.apache.wiki.api.plugin.Plugin; 031import org.apache.wiki.attachment.Attachment; 032import org.apache.wiki.auth.AuthorizationManager; 033import org.apache.wiki.auth.permissions.PermissionFactory; 034import org.apache.wiki.i18n.InternationalizationManager; 035import org.apache.wiki.pages.PageManager; 036import org.apache.wiki.preferences.Preferences; 037import org.apache.wiki.preferences.Preferences.TimeFormat; 038import org.apache.wiki.render.RenderingManager; 039import org.apache.wiki.util.TextUtil; 040import org.apache.wiki.util.XHTML; 041import org.apache.wiki.util.XhtmlUtil; 042import org.jdom2.Element; 043 044import java.text.DateFormat; 045import java.text.SimpleDateFormat; 046import java.util.Calendar; 047import java.util.Collection; 048import java.util.Date; 049import java.util.GregorianCalendar; 050import java.util.Map; 051import java.util.stream.Collectors; 052 053 054/** 055 * Returns the Recent Changes in the wiki being a date-sorted list of page names. 056 * 057 * <p>Parameters: </p> 058 * <ul> 059 * <li><b>since</b> - show changes from the last n days, for example since=5 shows only the pages that were changed in the last five days</li> 060 * <li><b>format</b> - (full|compact) : if "full", then display a long version with all possible info. If "compact", then be as compact as possible.</li> 061 * <li><b>timeFormat</b> - the time format to use, the default is "HH:mm:ss"</li> 062 * <li><b>dateFormat</b> - the date format to use, the default is "dd.MM.yyyy"</li> 063 * </ul> 064 */ 065public class RecentChangesPlugin extends AbstractReferralPlugin implements Plugin { 066 067 private static final Logger LOG = LogManager.getLogger( RecentChangesPlugin.class ); 068 069 /** Parameter name for the separator format. Value is <tt>{@value}</tt>. */ 070 public static final String PARAM_FORMAT = "format"; 071 /** Parameter name for the separator timeFormat. Value is <tt>{@value}</tt>. */ 072 public static final String PARAM_TIME_FORMAT = "timeFormat"; 073 /** Parameter name for the separator dateFormat. Value is <tt>{@value}</tt>. */ 074 public static final String PARAM_DATE_FORMAT = "dateFormat"; 075 076 /** How many days we show by default. */ 077 private static final int DEFAULT_DAYS = 100*365; 078 public static final String DEFAULT_TIME_FORMAT ="HH:mm:ss"; 079 public static final String DEFAULT_DATE_FORMAT ="dd.MM.yyyy"; 080 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 public String execute( final Context context, final Map< String, String > params ) throws PluginException { 087 final int since = TextUtil.parseIntParameter( params.get( "since" ), DEFAULT_DAYS ); 088 String spacing = "4"; 089 boolean showAuthor = true; 090 boolean showChangenote = true; 091 String tablewidth = "4"; 092 093 final Engine engine = context.getEngine(); 094 095 // 096 // Which format we want to see? 097 // 098 if( "compact".equals( params.get( PARAM_FORMAT ) ) ) { 099 spacing = "0"; 100 showAuthor = false; 101 showChangenote = false; 102 tablewidth = "2"; 103 } 104 105 final Calendar sincedate = new GregorianCalendar(); 106 sincedate.add( Calendar.DAY_OF_MONTH, -since ); 107 108 LOG.debug("Calculating recent changes from "+sincedate.getTime()); 109 110 // FIXME: Should really have a since date on the getRecentChanges method. 111 Collection< Page > changes = engine.getManager( PageManager.class ).getRecentChanges(); 112 super.initialize( context, params ); 113 changes = filterWikiPageCollection( changes ); 114 115 // The list is shown to everybody, so it holds only the pages and attachments that the viewer may view. 116 final AuthorizationManager authorizationManager = engine.getManager( AuthorizationManager.class ); 117 changes = changes.stream() 118 .filter( page -> authorizationManager.checkPermission( context.getWikiSession(), 119 PermissionFactory.getPagePermission( page, "view" ) ) ) 120 .collect( Collectors.toList() ); 121 122 if ( changes != null ) { 123 Date olddate = new Date( 0 ); 124 125 final DateFormat fmt = getDateFormat( context, params ); 126 final DateFormat tfmt = getTimeFormat( context, params ); 127 128 final Element rt = XhtmlUtil.element( XHTML.table ); 129 rt.setAttribute( XHTML.ATTR_class, "recentchanges" ); 130 rt.setAttribute( XHTML.ATTR_cellpadding, spacing ); 131 132 for( final Page pageref : changes ) { 133 final Date lastmod = pageref.getLastModified(); 134 135 if( lastmod.before( sincedate.getTime() ) ) { 136 break; 137 } 138 139 if( !isSameDay( lastmod, olddate ) ) { 140 final Element row = XhtmlUtil.element( XHTML.tr ); 141 final Element col = XhtmlUtil.element( XHTML.td ); 142 col.setAttribute( XHTML.ATTR_colspan, tablewidth ); 143 col.setAttribute( XHTML.ATTR_class, "date" ); 144 col.addContent( XhtmlUtil.element( XHTML.b, fmt.format( lastmod ) ) ); 145 146 rt.addContent( row ); 147 row.addContent( col ); 148 olddate = lastmod; 149 } 150 151 final String href = context.getURL( pageref instanceof Attachment ? ContextEnum.PAGE_ATTACH.getRequestContext() 152 : ContextEnum.PAGE_VIEW.getRequestContext(), pageref.getName() ); 153 Element link = XhtmlUtil.link( href, engine.getManager( RenderingManager.class ).beautifyTitle( pageref.getName() ) ); 154 final Element row = XhtmlUtil.element( XHTML.tr ); 155 final Element col = XhtmlUtil.element( XHTML.td ); 156 col.setAttribute( XHTML.ATTR_width, "30%" ); 157 col.addContent( link ); 158 159 // 160 // Add the direct link to the attachment info. 161 // 162 if( pageref instanceof Attachment ) { 163 link = XhtmlUtil.link( context.getURL( WikiContext.INFO, pageref.getName() ), null ); 164 link.setAttribute( XHTML.ATTR_class, "infolink" ); 165 166 final Element img = XhtmlUtil.img( context.getURL( WikiContext.NONE, "images/attachment_small.png" ), null ); 167 link.addContent( img ); 168 169 col.addContent( link ); 170 } 171 172 row.addContent( col ); 173 rt.addContent( row ); 174 175 if( pageref instanceof Attachment ) { 176 final Element td = XhtmlUtil.element( XHTML.td, tfmt.format( lastmod ) ); 177 td.setAttribute( XHTML.ATTR_class, "lastchange" ); 178 row.addContent( td ); 179 } else { 180 final Element infocol = XhtmlUtil.element( XHTML.td ); 181 infocol.setAttribute( XHTML.ATTR_class, "lastchange" ); 182 infocol.addContent( XhtmlUtil.link( context.getURL( WikiContext.DIFF, pageref.getName(), "r1=-1" ), 183 tfmt.format( lastmod ) ) ); 184 row.addContent( infocol ); 185 } 186 187 // 188 // Display author information. 189 // 190 if( showAuthor ) { 191 final String author = pageref.getAuthor(); 192 193 final Element authorinfo = XhtmlUtil.element( XHTML.td ); 194 authorinfo.setAttribute( XHTML.ATTR_class, "author" ); 195 196 if( author != null ) { 197 if( engine.getManager( PageManager.class ).wikiPageExists( author ) ) { 198 authorinfo.addContent( XhtmlUtil.link( context.getURL( WikiContext.VIEW, author ), author ) ); 199 } else { 200 authorinfo.addContent( author ); 201 } 202 } else { 203 authorinfo.addContent( Preferences.getBundle( context, InternationalizationManager.CORE_BUNDLE ) 204 .getString( "common.unknownauthor" ) ); 205 } 206 207 row.addContent( authorinfo ); 208 } 209 210 // Change note 211 if( showChangenote ) { 212 final String changenote = pageref.getAttribute( Page.CHANGENOTE ); 213 final Element td_changenote = XhtmlUtil.element( XHTML.td, changenote ); 214 td_changenote.setAttribute( XHTML.ATTR_class, "changenote" ); 215 row.addContent( td_changenote ); 216 } 217 218 // Revert note 219/* 220 if( context.hasAdminPermissions() ) 221 { 222 row.addElement( new td("Revert") ); 223 } 224 */ 225 } 226 return XhtmlUtil.serialize( rt, XhtmlUtil.EXPAND_EMPTY_NODES ); 227 } 228 return ""; 229 } 230 231 232 private boolean isSameDay( final Date a, final Date b ) { 233 final Calendar aa = Calendar.getInstance(); aa.setTime( a ); 234 final Calendar bb = Calendar.getInstance(); bb.setTime( b ); 235 236 return aa.get( Calendar.YEAR ) == bb.get( Calendar.YEAR ) 237 && aa.get( Calendar.DAY_OF_YEAR ) == bb.get( Calendar.DAY_OF_YEAR ); 238 } 239 240 241 // TODO: Ideally the default behavior should be to return the default format for the default 242 // locale, but that is at odds with the 1st version of this plugin. We seek to preserve the 243 // behaviour of that first version, so to get the default format, the user must explicitly do 244 // something like: dateFormat='' timeformat='' which is a odd, but probably okay. 245 private DateFormat getTimeFormat( final Context context, final Map< String, String > params ) { 246 final String formatString = get( params, DEFAULT_TIME_FORMAT, PARAM_TIME_FORMAT ); 247 if( StringUtils.isBlank( formatString ) ) { 248 return Preferences.getDateFormat( context, TimeFormat.TIME ); 249 } 250 251 return new SimpleDateFormat( formatString ); 252 } 253 254 private DateFormat getDateFormat( final Context context, final Map< String, String > params ) { 255 final String formatString = get( params, DEFAULT_DATE_FORMAT, PARAM_DATE_FORMAT ); 256 if( StringUtils.isBlank( formatString ) ) { 257 return Preferences.getDateFormat( context, TimeFormat.DATE ); 258 } 259 260 return new SimpleDateFormat( formatString ); 261 } 262 263 private String get( final Map< String, String > params, final String defaultValue, final String paramName ) { 264 final String value = params.get( paramName ); 265 return value == null ? defaultValue : value; 266 } 267 268}