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 java.text.MessageFormat;
022import java.util.Collection;
023import java.util.Map;
024import java.util.ResourceBundle;
025import java.util.TreeMap;
026
027import org.apache.wiki.ReferenceManager;
028import org.apache.wiki.WikiContext;
029import org.apache.wiki.api.exceptions.PluginException;
030import org.apache.wiki.api.plugin.WikiPlugin;
031import org.apache.wiki.preferences.Preferences;
032import org.apache.wiki.util.TextUtil;
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    public String execute(WikiContext context, Map<String, String> params) throws PluginException {
049        ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
050
051        ReferenceManager referenceManager = context.getEngine().getReferenceManager();
052
053        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        StringBuilder resultHTML = new StringBuilder();
060
061        Collection<String> uncreatedPages = referenceManager.findUncreated();
062
063        super.initialize(context, params);
064
065        Collection<String> result = null;
066
067        TreeMap< String, String > sortedMap = new TreeMap<>();
068        if (uncreatedPages != null) {
069            for (String uncreatedPageName : uncreatedPages) {
070                Collection<String> referrers = referenceManager.findReferrers(uncreatedPageName);
071                if (referrers != null) {
072                    for (String referringPage : referrers) {
073                        sortedMap.put(referringPage, "");
074                    }
075                }
076            }
077            result = sortedMap.keySet();
078        }
079
080        result = super.filterAndSortCollection(result);
081
082        String wikitext = wikitizeCollection(result, m_separator, items);
083
084        resultHTML.append(makeHTML(context, wikitext));
085
086        // add the more.... text
087        if (items < result.size() && items > 0) {
088            Object[] args = {"" + (result.size() - items)};
089            extras = MessageFormat.format(extras, args);
090
091            resultHTML.append("<br/>" + extras + "<br/>");
092        }
093        return resultHTML.toString();
094    }
095
096}