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.wiki.api.core.Context;
022import org.apache.wiki.api.exceptions.PluginException;
023import org.apache.wiki.references.ReferenceManager;
024import org.apache.wiki.util.TextUtil;
025
026import java.util.Collection;
027import java.util.Locale;
028import java.util.Map;
029import java.util.ResourceBundle;
030
031/**
032 * Plugin for displaying pages that are not linked to in other pages.
033 * Uses the ReferenceManager.
034 * <p>
035 *  Parameters  (from AbstractReferralPlugin):
036 *  <ul>
037 *  <li><b>separator</b> - how to separate generated links; default is a wikitext line break,  producing a vertical list</li>
038 * <li><b> maxwidth</b> - maximum width, in chars, of generated links.</li>
039 * </ul>
040 */
041public class UnusedPagesPlugin extends AbstractReferralPlugin {
042
043    /** If set to "true", attachments are excluded from display.  Value is {@value}. */
044    public static final String PARAM_EXCLUDEATTS = "excludeattachments";
045
046    @Override
047    public String getDisplayName(Locale locale) {
048        final ResourceBundle rb = ResourceBundle.getBundle(PluginManager.PLUGIN_I18N_RESOURCE, locale);
049        return rb.getString(this.getClass().getSimpleName());
050    } 
051    
052    /**
053     *  {@inheritDoc}
054     */
055    @Override
056    public String execute( final Context context, final Map< String, String > params ) throws PluginException {
057        final ReferenceManager refmgr = context.getEngine().getManager( ReferenceManager.class );
058        Collection< String > links = refmgr.findUnreferenced();
059
060        // filter out attachments if "excludeattachments" was requested:
061        final String prop = params.get( PARAM_EXCLUDEATTS );
062        if( TextUtil.isPositive( prop ) ) {
063            // remove links to attachments (recognizable by a slash in it)
064            links.removeIf( link -> link.contains( "/" ) );
065        }
066
067        super.initialize( context, params );
068        links = filterAndSortCollection( links );
069
070        String wikitext;
071        if( m_show.equals( PARAM_SHOW_VALUE_COUNT ) ) {
072            wikitext = "" + links.size();
073            if( m_lastModified && !links.isEmpty()) {
074                wikitext = links.size() + " (" + m_dateFormat.format( m_dateLastModified ) + ")";
075            }
076            return makeHTML( context, wikitext );
077        } else {
078            wikitext = wikitizeCollection( links, m_separator, ALL_ITEMS );
079            return applyColumnsStyle( makeHTML( context, wikitext ) );
080        }
081    }
082
083}
084