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.api.plugin.Plugin;
024import org.apache.wiki.preferences.Preferences;
025import org.apache.wiki.references.ReferenceManager;
026import org.apache.wiki.util.TextUtil;
027
028import java.text.MessageFormat;
029import java.util.Collection;
030import java.util.Map;
031import java.util.ResourceBundle;
032import java.util.TreeMap;
033/**
034 *  <p>Lists all pages containing links to Undefined Pages (pages containing dead links).</p>
035 *
036 *  An original idea from Gregor Hagedorn.
037 *
038 *  @since 2.10.0
039 */
040public class ReferringUndefinedPagesPlugin extends AbstractReferralPlugin {
041
042    /** Parameter name for setting the maximum items to show.  Value is <tt>{@value}</tt>. */
043    public static final String PARAM_MAX = "max";
044
045    /** Parameter name for setting the text to show when the maximum items is overruled. Value is <tt>{@value}</tt>. */
046    public static final String PARAM_EXTRAS = "extras";
047
048    @Override
049    public String execute( final Context context, final Map<String, String> params) throws PluginException {
050        final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
051        final ReferenceManager referenceManager = context.getEngine().getManager( ReferenceManager.class );
052
053        final int items = TextUtil.parseIntParameter(params.get(PARAM_MAX), ALL_ITEMS);
054        String extras = params.get(PARAM_EXTRAS);
055        if (extras == null) {
056            extras = rb.getString("referringundefinedpagesplugin.more");
057        }
058
059        final Collection< String > uncreatedPages = referenceManager.findUncreated();
060        super.initialize( context, params );
061        Collection< String > result = null;
062
063        final TreeMap< String, String > sortedMap = new TreeMap<>();
064        if( uncreatedPages != null ) {
065            for( final String uncreatedPageName : uncreatedPages ) {
066                final Collection< String > referrers = referenceManager.findReferrers( uncreatedPageName );
067                if( referrers != null ) {
068                    for( final String referringPage : referrers ) {
069                        sortedMap.put( referringPage, "" );
070                    }
071                }
072            }
073            result = sortedMap.keySet();
074        }
075
076        result = super.filterAndSortCollection( result );
077
078        final String wikitext = wikitizeCollection( result, m_separator, items );
079        final StringBuilder resultHTML = new StringBuilder();
080        resultHTML.append( applyColumnsStyle( makeHTML( context, wikitext ) ) );
081
082        // add the more.... text
083        if( items < result.size() && items > 0 ) {
084            final Object[] args = { "" + ( result.size() - items ) };
085            extras = MessageFormat.format( extras, args );
086
087            resultHTML.append( "<br/>" ).append( extras ).append( "<br/>" );
088        }
089        return resultHTML.toString();
090    }
091
092}