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