Class IfPlugin

  • All Implemented Interfaces:
    Plugin

    public class IfPlugin
    extends java.lang.Object
    implements Plugin
    The IfPlugin allows parts of a WikiPage to be executed conditionally, and is intended as a flexible way of customizing a page depending on certain conditions. Do not use it as a security mechanism to conditionally hide content from users (use page ACLs for that). You can also use shorthand "If" to run it. Parameters:
    • group - A "|" -separated list of group names.
    • user - A "|" -separated list of user names.
    • ip - A "|" -separated list of ip addresses.
    • var - A wiki variable
    • page - A page name
    • contains - A Perl5 regexp pattern
    • is - A Perl5 regexp pattern
    • exists - "true" or "false".

    If any of them match, the body of the plugin is executed. You can negate the content by prefixing it with a "!". For example, to greet all admins, put the following in your LeftMenu:

      [{If group='Admin'
    
      Hello, Admin, and your mighty powers!}]
      

    In order to send a message to everybody except Jack use

      [{If user='!Jack'
    
      %%warning
      Jack's surprise birthday party at eleven!
      %%}]
      

    Note that you can't use "!Jack|!Jill", because for Jack, !Jill matches; and for Jill, !Jack matches. These are not regular expressions (though they might become so in the future).

    To check for page content, use

      [{If page='TestPage' contains='xyzzy'
    
      Page contains the text "xyzzy"}]
      

    The difference between "contains" and "is" is that "is" is always an exact match, whereas "contains" just checks if a pattern is available.

    To check for page existence, use

      [{If page='TestPage' exists='true'
    
      Page "TestPage" exists.}]
      

    With the same mechanism, it's also possible to test for the existence of a variable - just use "var" instead of "page".

    Another caveat is that the plugin body content is not counted towards ReferenceManager links. So any links do not appear on any reference lists. Depending on your position, this may be a good or a bad thing.

    Calling Externally

    The functional, decision-making part of this plugin may be called from other code (e.g., other plugins) since it is available as a static method ifInclude(Context,Map). Note that the plugin body may contain references to other plugins.

    Since:
    2.6
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String PARAM_CONTAINS
      The parameter name for setting the contents of the page to check.
      static java.lang.String PARAM_EXISTS
      The parameter name for checking whether a page/var exists.
      static java.lang.String PARAM_GROUP
      The parameter name for setting the group to check.
      static java.lang.String PARAM_IP
      The parameter name for setting the ip address to check.
      static java.lang.String PARAM_IS
      The parameter name for setting the exact content to check.
      static java.lang.String PARAM_PAGE
      The parameter name for setting the page name to check.
      static java.lang.String PARAM_USER
      The parameter name for setting the user id to check.
      static java.lang.String PARAM_VAR
      The parameter name for setting the variable name to check.
    • Constructor Summary

      Constructors 
      Constructor Description
      IfPlugin()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String execute​(Context context, java.util.Map<java.lang.String,​java.lang.String> params)
      This is the main entry point for any plugin.
      static boolean ifInclude​(Context context, java.util.Map<java.lang.String,​java.lang.String> params)
      Returns a boolean result based on processing the WikiContext and parameter Map as according to the rules stated in the IfPlugin documentation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PARAM_GROUP

        public static final java.lang.String PARAM_GROUP
        The parameter name for setting the group to check. Value is "group".
        See Also:
        Constant Field Values
      • PARAM_USER

        public static final java.lang.String PARAM_USER
        The parameter name for setting the user id to check. Value is "user".
        See Also:
        Constant Field Values
      • PARAM_IP

        public static final java.lang.String PARAM_IP
        The parameter name for setting the ip address to check. Value is "ip".
        See Also:
        Constant Field Values
      • PARAM_PAGE

        public static final java.lang.String PARAM_PAGE
        The parameter name for setting the page name to check. Value is "page".
        See Also:
        Constant Field Values
      • PARAM_CONTAINS

        public static final java.lang.String PARAM_CONTAINS
        The parameter name for setting the contents of the page to check. Value is "contains".
        See Also:
        Constant Field Values
      • PARAM_VAR

        public static final java.lang.String PARAM_VAR
        The parameter name for setting the variable name to check. Value is "var".
        See Also:
        Constant Field Values
      • PARAM_IS

        public static final java.lang.String PARAM_IS
        The parameter name for setting the exact content to check. Value is "is".
        See Also:
        Constant Field Values
      • PARAM_EXISTS

        public static final java.lang.String PARAM_EXISTS
        The parameter name for checking whether a page/var exists. Value is "exists".
        See Also:
        Constant Field Values
    • Method Detail

      • execute

        public java.lang.String execute​(Context context,
                                        java.util.Map<java.lang.String,​java.lang.String> params)
                                 throws PluginException
        This is the main entry point for any plugin. The parameters are parsed, and a special parameter called "_body" signifies the name of the plugin body, i.e. the part of the plugin that is not a parameter of the form "key=value". This has been separated using an empty line.

        Note that it is preferred that the plugin returns XHTML-compliant HTML (i.e. close all tags, use <br /> instead of <br>, etc.

        Specified by:
        execute in interface Plugin
        Parameters:
        context - The current WikiContext.
        params - A Map which contains key-value pairs. Any parameter that the user has specified on the wiki page will contain String-String parameters, but it is possible that at some future date, JSPWiki will give you other things that are not Strings.
        Returns:
        HTML, ready to be included into the rendered page.
        Throws:
        PluginException - In case anything goes wrong.
      • ifInclude

        public static boolean ifInclude​(Context context,
                                        java.util.Map<java.lang.String,​java.lang.String> params)
                                 throws PluginException
        Returns a boolean result based on processing the WikiContext and parameter Map as according to the rules stated in the IfPlugin documentation. As a static method this may be called by other classes.
        Parameters:
        context - The current WikiContext.
        params - The parameter Map which contains key-value pairs.
        Returns:
        True, if the condition holds.
        Throws:
        PluginException - If something goes wrong