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