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