// FIXME: The compiled pattern strings should really be cached somehow.
  public void initialize(WikiContext context, Map params) throws PluginException {
    m_dateFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME);
    m_engine = context.getEngine();
    m_maxwidth = TextUtil.parseIntParameter((String) params.get(PARAM_MAXWIDTH), Integer.MAX_VALUE);
    if (m_maxwidth < 0) m_maxwidth = 0;

    String s = (String) params.get(PARAM_SEPARATOR);

    if (s != null) {
      m_separator = s;
      // pre-2.1.145 there was a separator at the end of the list
      // if they set the parameters, we use the new format of
      // before Item1 after separator before Item2 after separator before Item3 after
      m_after = "";
    }

    s = (String) params.get(PARAM_BEFORE);

    if (s != null) {
      m_before = s;
    }

    s = (String) params.get(PARAM_AFTER);

    if (s != null) {
      m_after = s;
    }

    s = (String) params.get(PARAM_EXCLUDE);

    if (s != null) {
      try {
        PatternCompiler pc = new GlobCompiler();

        String[] ptrns = StringUtils.split(s, ",");

        m_exclude = new Pattern[ptrns.length];

        for (int i = 0; i < ptrns.length; i++) {
          m_exclude[i] = pc.compile(ptrns[i]);
        }
      } catch (MalformedPatternException e) {
        throw new PluginException("Exclude-parameter has a malformed pattern: " + e.getMessage());
      }
    }

    // TODO: Cut-n-paste, refactor
    s = (String) params.get(PARAM_INCLUDE);

    if (s != null) {
      try {
        PatternCompiler pc = new GlobCompiler();

        String[] ptrns = StringUtils.split(s, ",");

        m_include = new Pattern[ptrns.length];

        for (int i = 0; i < ptrns.length; i++) {
          m_include[i] = pc.compile(ptrns[i]);
        }
      } catch (MalformedPatternException e) {
        throw new PluginException("Include-parameter has a malformed pattern: " + e.getMessage());
      }
    }

    // log.debug( "Requested maximum width is "+m_maxwidth );
    s = (String) params.get(PARAM_SHOW);

    if (s != null) {
      if (s.equalsIgnoreCase("count")) {
        m_show = "count";
      }
    }

    s = (String) params.get(PARAM_LASTMODIFIED);

    if (s != null) {
      if (s.equalsIgnoreCase("true")) {
        if (m_show.equals("count")) {
          m_lastModified = true;
        } else {
          throw new PluginException(
              "showLastModified=true is only valid if show=count is also specified");
        }
      }
    }

    initSorter(context, params);
  }
Пример #2
0
    /**
     * Count a page hit, present a pages' counter or output a list of pagecounts.
     *
     * @param context
     * @param params
     * @throws com.ecyrd.jspwiki.plugin.PluginException
     * @return String Wiki page snippet
     * @throws PluginException Malformed pattern parameter.
     * @concurrency concurrent
     */
    public String execute(WikiContext context, Map params) throws PluginException {
      WikiEngine engine = context.getEngine();
      WikiPage page = context.getPage();
      String result = STR_EMPTY;

      if (null != page) {
        // get parameters
        String pagename = page.getName();
        String count = (String) params.get(PARAM_COUNT);
        String show = (String) params.get(PARAM_SHOW);
        int entries =
            TextUtil.parseIntParameter((String) params.get(PARAM_MAX_ENTRIES), Integer.MAX_VALUE);
        final int max =
            TextUtil.parseIntParameter((String) params.get(PARAM_MAX_COUNT), Integer.MAX_VALUE);
        final int min =
            TextUtil.parseIntParameter((String) params.get(PARAM_MIN_COUNT), Integer.MIN_VALUE);
        String sort = (String) params.get(PARAM_SORT);
        String body = (String) params.get(PluginManager.PARAM_BODY);
        Pattern[] exclude = compileGlobs(PARAM_EXCLUDE, (String) params.get(PARAM_EXCLUDE));
        Pattern[] include = compileGlobs(PARAM_INCLUDE, (String) params.get(PARAM_INCLUDE));
        Pattern[] refer = compileGlobs(PARAM_REFER, (String) params.get(PARAM_REFER));
        PatternMatcher matcher =
            (null != exclude || null != include || null != refer) ? new Perl5Matcher() : null;
        boolean increment = false;

        // increment counter?
        if (STR_YES.equals(count)) {
          increment = true;
        } else {
          count = null;
        }

        // default increment counter?
        if ((null == show || STR_NONE.equals(show)) && null == count) {
          increment = true;
        }

        // filter on referring pages?
        Collection referrers = null;

        if (null != refer) {
          ReferenceManager refManager = engine.getReferenceManager();

          Iterator iter = refManager.findCreated().iterator();

          while (null != iter && iter.hasNext()) {

            String name = (String) iter.next();
            boolean use = false;

            for (int n = 0; !use && n < refer.length; n++) {
              use = matcher.matches(name, refer[n]);
            }

            if (use) {
              Collection refs = engine.getReferenceManager().findReferrers(name);

              if (null != refs && !refs.isEmpty()) {
                if (null == referrers) {
                  referrers = new HashSet();
                }
                referrers.addAll(refs);
              }
            }
          }
        }

        synchronized (this) {
          Counter counter = (Counter) counters.get(pagename);

          // only count in view mode, keep storage values in sync
          if (increment && WikiContext.VIEW.equalsIgnoreCase(context.getRequestContext())) {
            if (null == counter) {
              counter = new Counter();
              counters.put(pagename, counter);
            }
            counter.increment();
            storage.setProperty(pagename, counter.toString());
            dirty = true;
          }

          if (null == show || STR_NONE.equals(show)) {
            // nothing to show

          } else if (PARAM_COUNT.equals(show)) {
            // show page count
            result = counter.toString();

          } else if (null != body && 0 < body.length() && STR_LIST.equals(show)) {
            // show list of counts
            String header = STR_EMPTY;
            String line = body;
            String footer = STR_EMPTY;
            int start = body.indexOf(STR_SEPARATOR);

            // split body into header, line, footer on ----
            // separator
            if (0 < start) {
              header = body.substring(0, start);

              start = skipWhitespace(start + STR_SEPARATOR.length(), body);

              int end = body.indexOf(STR_SEPARATOR, start);

              if (start >= end) {
                line = body.substring(start);

              } else {
                line = body.substring(start, end);

                end = skipWhitespace(end + STR_SEPARATOR.length(), body);

                footer = body.substring(end);
              }
            }

            // sort on name or count?
            Map sorted = counters;

            if (null != sort && PARAM_COUNT.equals(sort)) {
              sorted = new TreeMap(compareCountDescending);

              sorted.putAll(counters);
            }

            // build a messagebuffer with the list in wiki markup
            StringBuffer buf = new StringBuffer(header);
            MessageFormat fmt = new MessageFormat(line);
            Object[] args = new Object[] {pagename, STR_EMPTY, STR_EMPTY};
            Iterator iter = sorted.entrySet().iterator();

            while (null != iter && 0 < entries && iter.hasNext()) {

              Entry entry = (Entry) iter.next();
              String name = (String) entry.getKey();

              // check minimum count
              final int value = ((Counter) entry.getValue()).getValue();
              boolean use = min <= value && value <= max;

              // did we specify a refer-to page?
              if (use && null != referrers) {

                use = referrers.contains(name);
              }

              // did we specify what pages to include?
              if (use && null != include) {
                use = false;

                for (int n = 0; !use && n < include.length; n++) {

                  use = matcher.matches(name, include[n]);
                }
              }

              // did we specify what pages to exclude?
              if (use && null != exclude) {
                for (int n = 0; use && n < exclude.length; n++) {

                  use &= !matcher.matches(name, exclude[n]);
                }
              }

              if (use) {
                args[1] = engine.beautifyTitle(name);
                args[2] = entry.getValue();

                fmt.format(args, buf, null);

                entries--;
              }
            }
            buf.append(footer);

            // let the engine render the list
            result = engine.textToHTML(context, buf.toString());
          }
        }
      }
      return result;
    }