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;
037import java.util.stream.Collectors;
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    /**
069     *  {@inheritDoc}
070     */
071    @Override
072    public String execute( final Context context, final Map< String, String > params ) throws PluginException {
073        final ReferenceManager refmgr = context.getEngine().getManager( ReferenceManager.class );
074        String pageName = params.get( PARAM_PAGE );
075        final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE );
076
077        StringBuilder result = new StringBuilder( 256 );
078
079        if( pageName == null ) {
080            pageName = context.getPage().getName();
081        }
082
083        final Page page = context.getEngine().getManager( PageManager.class ).getPage( pageName );
084        if( page != null ) {
085            Collection< String > links  = refmgr.findReferrers( page.getName() );
086            String wikitext;
087
088            super.initialize( context, params );
089
090            final int items = TextUtil.parseIntParameter( params.get( PARAM_MAX ), ALL_ITEMS );
091
092            String extras = TextUtil.replaceEntities( params.get( PARAM_EXTRAS ) );
093            if( extras == null ) {
094                extras = rb.getString( "referringpagesplugin.more" );
095            }
096
097            LOG.debug( "Fetching referring pages for {} with a max of {}", page.getName(), items );
098
099            if( links != null && !links.isEmpty()) {
100                links = filterAndSortCollection( links.stream().filter( name -> mayView( context, name ) ).collect( Collectors.toList() ) );
101                wikitext = wikitizeCollection( links, m_separator, items );
102
103                result.append( applyColumnsStyle( makeHTML( context, wikitext ) ) );
104
105                if( items < links.size() && items > 0 ) {
106                    final Object[] args = { "" + ( links.size() - items ) };
107                    extras = MessageFormat.format( extras, args );
108
109                    result.append( "<br />" )
110                          .append( "<a class='morelink' href='" )
111                          .append( context.getURL( ContextEnum.PAGE_INFO.getRequestContext(), page.getName() ) )
112                          .append( "' " )
113                          .append( ">" )
114                          .append( extras )
115                          .append( "</a><br />" );
116                }
117            }
118
119            // If nothing was left after filtering or during search
120            if( links == null || links.isEmpty()) {
121                wikitext = rb.getString( "referringpagesplugin.nobody" );
122                result.append( makeHTML( context, wikitext ) );
123            } else  if( m_show.equals( PARAM_SHOW_VALUE_COUNT ) ) {
124                result = new StringBuilder();
125                result.append( links.size() );
126                if( m_lastModified ) {
127                    result.append( " (" ).append( m_dateFormat.format( m_dateLastModified ) ).append( ")" );
128                }
129            }
130
131            return result.toString();
132        }
133
134        return "";
135    }
136
137}