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