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