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     */
019    package org.apache.wiki.plugin;
020    
021    import java.text.MessageFormat;
022    import java.util.Collection;
023    import java.util.Map;
024    import java.util.ResourceBundle;
025    
026    import org.apache.log4j.Logger;
027    import org.apache.wiki.ReferenceManager;
028    import org.apache.wiki.WikiContext;
029    import org.apache.wiki.WikiPage;
030    import org.apache.wiki.api.exceptions.PluginException;
031    import org.apache.wiki.api.plugin.WikiPlugin;
032    import org.apache.wiki.preferences.Preferences;
033    import 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     */
052    public 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            StringBuffer result = new StringBuffer( 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   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                String extras = params.get( PARAM_EXTRAS );
098                if( extras == null )
099                {
100                    extras = rb.getString("referringpagesplugin.more");
101                }
102                
103                if( log.isDebugEnabled() )
104                    log.debug( "Fetching referring pages for "+page.getName()+
105                               " with a max of "+items);
106            
107                if( links != null && links.size() > 0 )
108                {
109                    links = filterAndSortCollection( links );
110                    wikitext = wikitizeCollection( links, m_separator, items );
111    
112                    result.append( makeHTML( context, wikitext ) );
113                    
114                    if( items < links.size() && items > 0 )
115                    {
116                        Object[] args = { "" + ( links.size() - items) };
117                        extras = MessageFormat.format(extras, args);
118                        
119                        result.append( "<br />" );
120                        result.append( "<a class='morelink' href='"+context.getURL( WikiContext.INFO, page.getName() )+"' ");
121                        result.append( ">"+extras+"</a><br />");
122                    }
123                }
124    
125                //
126                // If nothing was left after filtering or during search
127                //
128                if (links == null || links.size() == 0)
129                {
130                    wikitext = rb.getString("referringpagesplugin.nobody");
131                    
132                    result.append( makeHTML( context, wikitext ) );
133                }
134                else
135                {
136                    if( m_show.equals( PARAM_SHOW_VALUE_COUNT ) )
137                    {
138                        result = new StringBuffer();
139                        result.append( links.size() );
140                        if( m_lastModified )
141                        {
142                            result.append( " (" + m_dateFormat.format( m_dateLastModified ) + ")" );
143                        }
144                    }
145                }
146                
147                return result.toString();
148            }
149    
150            return "";
151        }
152    
153    }