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.util.Collection;
022import java.util.Iterator;
023import java.util.Map;
024
025import org.apache.wiki.ReferenceManager;
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.api.exceptions.PluginException;
028import org.apache.wiki.util.TextUtil;
029
030/**
031 * Plugin for displaying pages that are not linked to in other pages.
032 * Uses the ReferenceManager.
033 * <p>
034 *  Parameters  (from AbstractReferralPlugin):
035 *  <ul>
036 *  <li><b>separator</b> - how to separate generated links; default is a wikitext line break,  producing a vertical list</li>
037 * <li><b> maxwidth</b> - maximum width, in chars, of generated links.</li>
038 * </ul>
039 *
040 */
041public class UnusedPagesPlugin
042    extends AbstractReferralPlugin
043{
044    /**
045     *  If set to "true", attachments are excluded from display.  Value is {@value}.
046     */
047    public static final String PARAM_EXCLUDEATTS = "excludeattachments";
048
049    /**
050     *  {@inheritDoc}
051     */
052    @SuppressWarnings("unchecked")
053    public String execute( WikiContext context, Map<String, String> params )
054        throws PluginException
055    {
056        ReferenceManager refmgr = context.getEngine().getReferenceManager();
057        Collection<String> links = refmgr.findUnreferenced();
058        //
059        // filter out attachments if "excludeattachments" was requested:
060        //
061        String prop = params.get( PARAM_EXCLUDEATTS );
062        if( TextUtil.isPositive(prop) ) 
063        {
064            //  remove links to attachments (recognizable by a slash in it)
065            //  FIXME: In 3.0, this assumption is going to fail. FIXME3.0
066            Iterator iterator = links.iterator();
067            while( iterator.hasNext() ) 
068            {
069                String link = (String) iterator.next();
070                if (link.indexOf("/")!=-1) 
071                {
072                    iterator.remove();
073                }
074            }
075        }
076
077        super.initialize( context, params );
078
079        links = filterAndSortCollection( links );
080
081        String wikitext = null;
082        
083        if (m_show.equals(PARAM_SHOW_VALUE_COUNT))
084        {
085            wikitext = "" + links.size();
086            if (m_lastModified && links.size()!=0)
087            {
088                wikitext = links.size() + " (" + m_dateFormat.format(m_dateLastModified) + ")";
089            }
090        }
091        else
092        {
093            wikitext = wikitizeCollection( links, m_separator, ALL_ITEMS );
094        }        
095        return makeHTML( context, wikitext );
096    }
097
098}
099