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.logging.log4j.LogManager; 022import org.apache.logging.log4j.Logger; 023import org.apache.wiki.api.core.Context; 024import org.apache.wiki.api.core.ContextEnum; 025import org.apache.wiki.api.core.Page; 026import org.apache.wiki.api.exceptions.PluginException; 027import org.apache.wiki.api.plugin.Plugin; 028import org.apache.wiki.pages.PageManager; 029import org.apache.wiki.preferences.Preferences; 030import org.apache.wiki.references.ReferenceManager; 031import org.apache.wiki.util.TextUtil; 032 033import java.text.MessageFormat; 034import java.util.Collection; 035import java.util.Map; 036import java.util.ResourceBundle; 037 038/** 039 * Displays the pages referring to the current page. 040 * 041 * Parameters: 042 * <ul> 043 * <li><b>max</b> - How many items to show.</li> 044 * <li><b>extras</b> - How to announce extras.</li> 045 * <li><b>page</b> - Which page to get the table of contents from.</li> 046 * </ul> 047 * 048 * From AbstractReferralPlugin: 049 * <ul> 050 * <li><b>separator</b> - How to separate generated links; default is a wikitext line break, producing a vertical list.</li> 051 * <li><b>maxwidth</b> - maximum width, in chars, of generated links.</li> 052 * </ul> 053 */ 054public class ReferringPagesPlugin extends AbstractReferralPlugin { 055 056 private static final Logger LOG = LogManager.getLogger( ReferringPagesPlugin.class ); 057 058 /** Parameter name for setting the maximum items to show. Value is <tt>{@value}</tt>. */ 059 public static final String PARAM_MAX = "max"; 060 061 /** Parameter name for setting the text to show when the maximum items is overruled. Value is <tt>{@value}</tt>. */ 062 public static final String PARAM_EXTRAS = "extras"; 063 064 /** Parameter name for choosing the page. Value is <tt>{@value}</tt>. */ 065 public static final String PARAM_PAGE = "page"; 066 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public String execute( final Context context, final Map< String, String > params ) throws PluginException { 072 final ReferenceManager refmgr = context.getEngine().getManager( ReferenceManager.class ); 073 String pageName = params.get( PARAM_PAGE ); 074 final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE ); 075 076 StringBuilder result = new StringBuilder( 256 ); 077 078 if( pageName == null ) { 079 pageName = context.getPage().getName(); 080 } 081 082 final Page page = context.getEngine().getManager( PageManager.class ).getPage( pageName ); 083 if( page != null ) { 084 Collection< String > links = refmgr.findReferrers( page.getName() ); 085 String wikitext; 086 087 super.initialize( context, params ); 088 089 final int items = TextUtil.parseIntParameter( params.get( PARAM_MAX ), ALL_ITEMS ); 090 091 String extras = TextUtil.replaceEntities( params.get( PARAM_EXTRAS ) ); 092 if( extras == null ) { 093 extras = rb.getString( "referringpagesplugin.more" ); 094 } 095 096 LOG.debug( "Fetching referring pages for {} with a max of {}", page.getName(), items ); 097 098 if( links != null && links.size() > 0 ) { 099 links = filterAndSortCollection( links ); 100 wikitext = wikitizeCollection( links, m_separator, items ); 101 102 result.append( applyColumnsStyle( makeHTML( context, wikitext ) ) ); 103 104 if( items < links.size() && items > 0 ) { 105 final Object[] args = { "" + ( links.size() - items ) }; 106 extras = MessageFormat.format( extras, args ); 107 108 result.append( "<br />" ) 109 .append( "<a class='morelink' href='" ) 110 .append( context.getURL( ContextEnum.PAGE_INFO.getRequestContext(), page.getName() ) ) 111 .append( "' " ) 112 .append( ">" ) 113 .append( extras ) 114 .append( "</a><br />" ); 115 } 116 } 117 118 // If nothing was left after filtering or during search 119 if( links == null || links.size() == 0 ) { 120 wikitext = rb.getString( "referringpagesplugin.nobody" ); 121 result.append( makeHTML( context, wikitext ) ); 122 } else if( m_show.equals( PARAM_SHOW_VALUE_COUNT ) ) { 123 result = new StringBuilder(); 124 result.append( links.size() ); 125 if( m_lastModified ) { 126 result.append( " (" ).append( m_dateFormat.format( m_dateLastModified ) ).append( ")" ); 127 } 128 } 129 130 return result.toString(); 131 } 132 133 return ""; 134 } 135 136}