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