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