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    public String execute( WikiContext context, Map<String, String> params )
053        throws PluginException
054    {
055        ReferenceManager refmgr = context.getEngine().getReferenceManager();
056        Collection<String> links = refmgr.findUnreferenced();
057        //
058        // filter out attachments if "excludeattachments" was requested:
059        //
060        String prop = params.get( PARAM_EXCLUDEATTS );
061        if( TextUtil.isPositive(prop) )
062        {
063            //  remove links to attachments (recognizable by a slash in it)
064            Iterator< String > iterator = links.iterator();
065            while( iterator.hasNext() )
066            {
067                String link = iterator.next();
068                if (link.indexOf("/")!=-1)
069                {
070                    iterator.remove();
071                }
072            }
073        }
074
075        super.initialize( context, params );
076
077        links = filterAndSortCollection( links );
078
079        String wikitext = null;
080
081        if (m_show.equals(PARAM_SHOW_VALUE_COUNT))
082        {
083            wikitext = "" + links.size();
084            if (m_lastModified && links.size()!=0)
085            {
086                wikitext = links.size() + " (" + m_dateFormat.format(m_dateLastModified) + ")";
087            }
088        }
089        else
090        {
091            wikitext = wikitizeCollection( links, m_separator, ALL_ITEMS );
092        }
093        return makeHTML( context, wikitext );
094    }
095
096}
097