%META:TOPICINFO{author="ProjectContributor" date="1734083408" format="1.1" version="1"}%
%META:TOPICPARENT{name="Plugins"}%
---+!! Comment Plugin
%FORMFIELD{"Description"}%

Comment Plugin lets users quickly post comments to a page without an edit/preview/save cycle.

%TOC{title="On this page:"}%

Related topics: CommentPluginTemplates, %SANDBOXWEB%.CommentPluginExamples

---++ Features

Inserts an edit box into the page that allows users to type in and save comments. Comments can be made
   * in different formats (as defined by a template),
   * in both forward and reverse chronological order,  
   * signed or unsigned, dated or undated (as defined by a template),
   * in other topics, or other positions within the current topic.

Supports the definition of custom templates for prompts and for formatting
what is placed into topics.

Supports custom access controls, allowing you to use it to add content to
topics where the commenting user doesn't have CHANGE permissions.

Uses Javascript (if available) to ensure feedback is as rapid as possible.

---++ Syntax
#StandardAttrs
Write =%<nop>COMMENT{<i>attributes</i>}%= anywhere in a topic.
%INCLUDE{"VarCOMMENT" section="comment_attributes"}%
(See also [[#MoreAttrs][additional attributes]])

---+++ Positioning the comment
=%COMMENT= supports several ways to specify _where_ a comment should be inserted in the target topic. This is referred to as the _location_ of the comment.

---++++ Location relative to <code>%COMMENT</code> tag
The default _location_ is the =%COMMENT= tag itself. For example:
<verbatim class="tml">
%COMMENT{type="below"}%
</verbatim>
will add comments in the current topic, directly below the =%COMMENT= tag.

---++++ Location relative to an anchor
The =target= attribute may specify a web, and may also specify an anchor within the target topic; for example, 
<verbatim class="tml">
%COMMENT{type="above" target="%USERSWEB%.PersonalRemarks#InsertHere"}%
</verbatim>
This uses a standard in-topic anchor as the insertion location. See %SYSTEMWEB%.EditingShorthand for more about Foswiki anchors.

---+++ Default templates
Templates are used to define the "comment style" i.e. how comments appear in the page. The default is to add comments in "Blog like" style using bulleted lists, with the most recent comment at the top, but many other styles are available such as tables or Wiki thread mode comments. It is easy to define your own customer styles as well.

A set of default comment templates are shipped with the plugin - see also CommentPluginTemplates:
| *Template type* | *Description* |
| =top= | Comments, signed and dated (server time), added at top of the topic (the anchor is ignored) |
| =bottom= | Comments, signed and dated (server time), added at end of the target topic (the anchor is ignored) |
| =above= | Comments, signed and dated (server time), added immediately before the target anchor, or the =%COMMENT= if no anchor is specified |
| =bulletabove= | Comments, signed and dated (server time), bullet point added immediately before the target anchor, or the =%COMMENT= if no anchor is specified (inputsize="20" can be used to set the input length) |
| =below= | Comments, signed and dated (server time), added immediately below the target anchor, or the =%COMMENT= if no anchor is specified |
| =after= | Comments, signed and dated (server time), added added before the comment box, after the last comment |
| =belowthreadmode= | Comments, signed and dated, added recurse after comment box |
| =threadmode= | Wiki thread mode comment, signed and dated (server time) |
| =tableprepend= | Comments, signed and dated (server time), formatted as an HTML table row, added below the anchor (which must be in an HTML &lt;table>) |
| =tableappend= | Comments, signed and dated (server time), formatted as an HTML table row, added above the anchor (which must be in an HTML &lt;table>) |
| =action= | Action added to action table directly above comment box (see <a href="#Installation">Plugin Installation Instructions</a> below for important notes) |
| =table= | Tablerows adding on end |
| =toctalk= | Talk using TOC adding on end |
| =bookmark= | Create a list of annotated bookmarks |
| =return= | Post to a different topic and return |

Your local installation may add more template types as well - see [[#TemPlates][Customization]], below.

#TemPlates
---++ Customization
Customization of the comment plugin requires
   * familiarity with HTML forms
   * some familiarity with the [[%SYSTEMWEB%.SkinTemplates][Skin Templates]].

To define a comment type, you have to provide at least two simple template definitions in the template file; one for the prompt box, and one for the generated output. If we have a template type "mytype", these are named =PROMPT:mytype= and =OUTPUT:mytype= respectively. See =comments.tmpl= in the templates directory for examples.

The plugin picks up these template definitions from a standard template file, =templates/comments.tmpl=. This allows different templates to be defined for different Foswiki skins.

---+++ Defining custom templates
By default, =templates/comments.tmpl= includes the topic %SYSTEMWEB%.CommentPluginTemplate, which contains all the shipped standard templates and in turn includes %SYSTEMWEB%.<nop>UserCommentsTemplate that can include non-standard customizations.

This allows for several levels of customization:
   1 To *override all default templates, everywhere*, change =comments.tmpl= to include a different topic (this customization will be lost next time you upgrade, though).
   1 To add *site-wide local template customizations*, add them to %SYSTEMWEB%.UserCommentsTemplate (create if it does not exist yet). You can redefine the standard templates here if you want, and your definitions will override the standard definitions.
   1 To override templates on a *web-by-web basis*, add a topic =UserCommentsTemplate= to the web (this will replace %SYSTEMWEB%.<nop>UserCommentsTemplate)
   1 To override templates *for a specific skin*, add them to %SYSTEMWEB%.<nop>UserComments&lt;Skin&gt;Template (where &lt;Skin&gt; is the name of the skin with the first letter capitalized, e.g. Pattern)

You can also define a *comment template in a topic*, by passing the topic location with =templatetopic=. For example:

<verbatim class="tml">
%COMMENT{
   type="blogpost"
   templatetopic="BlogPostCommentTemplate"
   target="%TOPIC%"
   button="Add comment"
}%
</verbatim>

=templatetopic= accepts =topic= or =web.topic= syntax. See an example in [[%SANDBOXWEB%.CommentPluginExamples#TemplateTopic][CommentPluginExamples:templatetopic]].



<div class="foswikiHelp"> %X% Templates are picked up by following the [[SkinTemplates#FindingTemplates][standard rules for locating template files]]. Note that you can use =%TMPL:INCLUDE= to include other files of templates.</div>

---+++ Customization example
Provide both a =PROMPT= and an =OUTPUT= definition:

<verbatim class="tml">
%TMPL:DEF{PROMPT:myComment}%%TMPL:P{promptbox}%%TMPL:END%
%TMPL:DEF{OUTPUT:myComment}%%TMPL:P{outputoneliner}%%POS:TOP%
%TMPL:END%
</verbatim>

Call your custom comment with:

<verbatim class="tml">
%COMMENT{type="myComment"}%
</verbatim>

---+++ The =PROMPT= template
The =PROMPT= template defines the contents of an HTML form that is used to capture the comment. This form invokes the comment generator when submitted. Parameters to the comment generator are defined using standard HTML input fields, such as =input=, =textarea= and =select=. The user enters values for these parameters, and these are then available when the =OUTPUT= template is expanded, in the form of <code>%<nop>URLPARAM%</code>s.

---+++ The =FORM= template
The =FORM= template can optionally be provided if you want to explicitly
define the form (the <code>&lt;form&gt;</code> and <code>&lt;/form&gt;</code> tHTML ags) that wraps around the =PROMPT= template. If you don't define
a =FORM=template, one is automatically generated for you (unless the =noform="on"= parameter is given).

#MoreAttrs
---++++ Providing attribute values
If an attribute is given to the =%COMMENT= tag that is not one of the [[#StandardAttrs][standard attributes]], then that attribute is taken as the name of a parameter to be expanded in the =PROMPT= template. Expressions in the template of the form <code>%</code><i>param</i><code>|</code><i>default</i><code>%</code> (e.g. <code>%rows|3%</code>, <code>%button|Push me%</code>) are expanded to the values given in the <code>%COMMENT</code>. For example, if the =PROMPT= template 'example' contains:
<verbatim class="tml">
<textarea rows="%rows|3%" cols="%cols|50%" value="%tval|Rubbish%">
</verbatim>
and the %COMMENT tag is:
<verbatim class="tml">
%COMMENT{type="example" cols="75"}%
</verbatim>
then the template will be expanded as
<verbatim class="tml">
<textarea rows="3" cols="75" value="Rubbish">
</verbatim>

---++++ Special macros
As well as support for all the usual macros in templates, the following special macros are supported in the =PROMPT= definition:
    | *Macro* | *Description* |
    | =%<nop>DISABLED%= | Set to 'disabled' when you cannot comment (e.g. in preview mode). |
    | =%<nop>MESSAGE%= | The text specified by =default=. This may be overridden by a helpful message when the prompt is DISABLED. |

Within a =FORM= definition, the body of the prompt is expanded to replace
the =%<nop>COMMENTPROMPT%= macro.

*EXPERT* Note that when a comment is saved, the =rest= script is invoked on the target topic, with a number of parameters provided by the comment form. Normally the CommentPlugin will provide these fields in the form, but experts can also provide the fields themselves in order to get finer control over what is submitted, or you might want to define your own HTML forms that do comment submission. The parameters that the CommentPlugin recognises are as follows:
| *CGI parameter* | *Description* |
| =comment_action= | Must be =save= to get the CommentPlugin to perform |
| =comment_type= | Type of the OUTPUT template |
| =comment_index= | Zero-based index of the %COMMENT in the source topic. Used to place a post relative to an existing %COMMENT. |
| =comment_anchor= | Anchor taken from the =target= spec |
| =comment_nonotify= | As passed to %COMMENT |
| =comment_remove= | Zero-based index of a %COMMENT to remove from the target topic |
| =comment_nopost= | As passed to %COMMENT |
| =comment_templatetopic= | As passed to %COMMENT |
| =comment_target= | Alternate save topic as passed to %COMMENT |
Note that =comment_anchor= overrides =comment_index=. Example, shows an "I Approve" button that adds your approval signature to the end of the topic:
<verbatim class="tml">
<form method="post" action="%SCRIPTURL{restauth}%/CommentPlugin/comment" >
<input type="submit" value="I Approve" />
<input type="hidden" name="topic" value="%WEB%/%TOPIC%" />
<input type="hidden" name="redirectto" value="%WEB%/%TOPIC%" />
<input type="hidden" name="comment_action" value="save" />
<input type="hidden" name="comment_type" value="bottom" />
<input type="hidden" name="comment" value="I Approve" />
</form>
</verbatim>

---++++ Customisation example with custom form template
<div class="foswikiAlert"> %X% *This method has critical differences from the version shipped with Foswiki 1.1.4!* (Identified by %X%) </div>
Write a custom form in a topic.
   * %X% In the form, the =action= must be =%<nop>SCRIPTURL{"rest"}%/CommentPlugin/comment=.   The save script used in previous versions will not work.
   * %X% In the form, the =topic= must be set to a valid, existing =Web.Topic=.  =topic= is validated by the =rest= script which will reject the request if invalid.
   * %X% In the form, if the target topic will be provided by user input, use the =comment_target= input field to identify the target, otherwise it defaults to =topic= setting.  =comment_target= is more flexible and can omit the Web part.
   * In the form set the location of the prompt with =%<nop>COMMENTPROMPT%=; the prompt will be positioned here.
   * In %COMMENT use parameter =noform="on"=
   * In %COMMENT use parameter =templatetopic= to point to the topic with the form template

Example form:
<verbatim class="tml">
%TMPL:DEF{FORM:example}%
<form method="post" action="%SCRIPTURL{rest}%/CommentPlugin/comment" enctype="application/x-www-form-urlencoded" name="examplecomment" id="examplecomment">
<input type="hidden" name="topic"  value="%BASEWEB%/%BASETOPIC%" /> 
<input type="hidden" name="redirectto" value="%BASEWEB%.%BASETOPIC%" />
%COMMENTPROMPT%
</form>
%TMPL:END%
</verbatim>

Example comment:
<verbatim class="tml">
%COMMENT{
   type="example"
   templatetopic="%SANDBOXWEB%.CommentPluginTemplateExample"
   target="%TOPIC%"
   button="Add comment"
}%
</verbatim>

---+++ The =OUTPUT= template
The =OUTPUT= template defines the format for the text that actually gets embedded into the topic. All the usual macros are available in the =PROMPT= definition,
but note that a subset of macros are expanded _when the comment is inserted in the text_, so time, date and username will refer to the time and date when the comment was made, and the user who made it.

The expanded variables are:
   * =%<nop>DATE%= Signature-format date [[VarDATE]]
   * =%<nop>SERVERTIME%= See [[VarSERVERTIME]]
   * =%<nop>GMTIME%= See [[VarGMTIME]]
   * =%<nop>USERNAME%= Base login name - [[VarUSERNAME]]
   * =%<nop>WIKINAME%= Wiki name - [[VarWIKINAME]]
   * =%<nop>WIKIUSERNAME%= Wiki name with prepended web - [[VarWIKIUSERNAME]]
   * =%<nop>URLPARAM{...}%= - Parameters to the current CGI query - [[VarURLPARAM]]
   * =%<nop>NOP%= No-op - [[VarNOP]]

All other Macros are inserted into the topic un-expanded and are expanded by standard rendering when the topic containing the comment is viewed.

In addition to Macros, there is one "token" expanded during output.  =$encodeguest= will be set to "off" when the commenting user is logged in, and set to "entity" for anonymous comments.
This is used in combination with the =%<nop>URLPARAM%= macro.  Any user input from untrusted users should be entity encoded.  For example:
<verbatim>
%TMPL:DEF{outputoneliner}%   * %URLPARAM{"comment" encode="$encodeguest"}% -- %WIKIUSERNAME% - %GMTIME{"$day $month $year"}%%TMPL:END%
</verbatim>

There are also four position tags that are used to indicate where the comment should be placed, relative to the location defined in the =%COMMENT= tag:

    | =%<nop>POS:TOP%= | If present, comments will be inserted *at the top of the topic* i.e. before any other text |
    | =%<nop>POS:BOTTOM%= | If present, comments will be inserted *at the end of the topic* i.e. after all existing text |
    | =%<nop>POS:BEFORE%= | If present, comments will be inserted <b>immediately before the =%<nop>COMMENT%= tag</b> |
    | =%<nop>POS:AFTER%= | If present, comments will be inserted <b>immediately after the =%<nop>COMMENT%= tag</b> |
Note that these position tags are obviously mutually exclusive. If you define more than one, the result is undefined. If none is present, the default is taken from the plugin setting =DEFAULT_TYPE=

    | =%<nop>COMMENTPROMPT%= | Use with a custom form. If present, the comment prompt will be positioned here. |
    
All the [[TemplateTopics#TemplateTopicsVars][usual macros]] that can be used in a topic template can also be used in an =OUTPUT= template.

---+++ Custom access controls
Using =configure=, the plugin can be configured to use a different access control domain than the default CHANGE.
This allows you to use the plugin to add content to topics where the commenting user does not have CHANGE (or even VIEW) access.

<div class="foswikiHelp">%X% *Caution* If you configure this plugin to use =COMMENT= permission instead of =CHANGE=, you should ensure that every web has the desired
default permission by configuring =ALLOWWEBCOMMENT= or =DENYWEBCOMMENT= in every !WebPreferences topic.  You should also configure appropriate defaults in the =_default= Template web.
If default permissions are not configured, then commenting will be permitted.
</div>

---++ Settings
Two [[%SYSTEMWEB%.PreferenceSettings][preference settings]] are recognised by the CommentPlugin:
| *Preference* | *Default* | *Description* |
| =%<nop>COMMENTPLUGIN_TEMPLATES%= | =comments= | Name of template file in the 'templates' directory that contains the comment templates. The default 'comments.tmpl' automatically includes user templates from %SYSTEMWEB%.CommentPluginTemplate, which in turn includes %SYSTEMWEB%.UserCommentsTemplate. |
| =%<nop>COMMENTPLUGIN_DEFAULT_TYPE%= | =above= | Default template type |
These can be set in %LOCALSITEPREFS%, in each web's <nop>%WEBPREFSTOPIC%, or in individual topics.

#Installation
---++ Installation Instructions
You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server.

Open configure, and open the "Extensions" section. "Extensions Operation and Maintenance" Tab -&gt; "Install, Update or Remove extensions" Tab.  Click the "Search for Extensions" button.  
Enter part of the extension name or description and press search.   Select the desired extension(s) and click install. If an extension is already installed, it will *not* show up in the
search results.

You can also install from the shell by running the extension installer as the web server user: (Be sure to run as the webserver user, not as root!)
<verbatim>
cd /path/to/foswiki
perl tools/extension_installer <NameOfExtension> install
</verbatim>

If you have any problems, or if the extension isn't available in =configure=, then you can still install manually from the command-line. See https://foswiki.org/Support/ManuallyInstallingExtensions for more help.


Note that if you want to use the =action= template then you must also:
   1 Install the Foswiki:Extensions/ActionTrackerPlugin;
   1 Put the !CommentPlugin *before* the !ActionTrackerPlugin in the ={PluginsOrder}= configuration option (in =configure=)

---++ Upgrade Instructions

This plugin has been significantly changed from the =10 April 2011= version shipped with Foswiki 1.1.4.  
   * The plugin uses a =rest= handler to update topics.  By default this script does not require authentication, and can now permit anonymous commenting.  Examine the [[%SCRIPTURL{"configure"}%]] ( =Security and Authentication= tab, =Login= sub-tab ) configuration ={AuthScripts}= setting, and the scripts requiring authentication in the Apache configuration.
   * The plugin no longer processes %COMMENT macros during save.  If you have developed any forms using the [[%SANDBOXWEB%.CommentPluginExamples#NoForm][=noform= and =templatetopic= examples]] they must be revised.

---++ Change History
%TABLE{columnwidths="7em" tablewidth="100%"}%
|  2.95 (06 Aug 2023) | Foswikitask:Item15151: add warnings and strictness |
|  2.94 (13 Oct 2020) | Foswikitask:Item14941: only load comment.js and comment.css on pages where it is required |
|  2.93 (22 Jan 2018) | Foswikitask:Item14554: remove link to unused wikiring.com domain.<br/>\
                        Foswikitask:Item14605: Unescaped brace in Regular Expression,   Perl 5.27.8. |
|  2.92 (21 Jan 2017) | Foswikitask:Item14009: Comment plugin does not properly identify location to insert comment when specified as an anchor. |
|  2.91 (08 Apr 2016) | Foswikitask:Item14022: Update Foswiki.org links to https. Released with Foswiki 2.1.1. |
|  2.9 (03 Feb 2016) | Foswikitask:Item13854: Set ALLOWTOPICVIEW = "*" on Comment Template.  *Requires compatibility patch on Foswiki 1.x or Foswiki 1.1.10.* |
|  2.8 (15 Nov 2015) | Foswikitask:Item13852: Warn that COMMENT ACLs should be configured if enabled. Also suppress guest session warning on Foswiki 1.1. |
|  2.7 (29 Sep 2015) | Foswikitask:Item13576: Disable fields during submit, then clear input fields after comment is posted.<br/>\
                       Foswikitask:Item13764: Entity encode any comments by guest users. |
|  2.6 (24 Jul 2015) | Foswikitask:Item13502: Expand macros when returning an inline Ajax response. |
|  2.5 (24 Jun 2015) | Foswikitask:Item13474: Unicode fix |
|  2.4 (14 Jun 2015) | Foswikitask:Item13423: Perl 5.20 deprecations<br />\
                        Foswikitask:Item13345: CGI deprecations<br />\
                        Foswikitask:Item13287: Regular expression fixes<br />\
                        Foswikitask:Item13125: CGI parm calling changes |
|  2.3 (18 Dec 2014) | Foswikitask:Item12855: Core extensions should require JQueryPlugin 6.00<br />\
                        Foswikitask:Item13125: CGI changes for multi-value parameters<br />\
                        Foswikitask:Item13096: incorrect handling of unauthorised response<br />\
                        Foswikitask:Item13105: Eliminate sort from tables in Macros<br />\
                        Foswikitask:Item13106: removed blockquote where possible<br />\
                        Foswikitask:Item13027: Most try..catch clauses were catching Error::Simple s/b Error<br />\
                        Foswikitask:Item11737: Change hardcoded Main to =%<nop>USERSWEB%= Replace Sandbox with =%<nop>SANDBOXWEB%= <br />\
                        Foswikitask:Item12900: Implement some session handling improvements |
|  2.2 (26 Sep 2014) | Foswikitask:Item12931: add a highlight to JS-added comments; fix no-JS comments |
|  2.1 (25 Apr 2014) | Foswikitask:Item10191: Recoded REST as a proper JQuery plugin, with correctly formatted insertions. Note that the old =location= parameter has been *removed*, as it never worked properly, could cause damage to topics, and could not work with client comments. If you were using it, apologies, but it had to go. Correct use of validation and authentication. Ready for 2.0. |
|  2.0.3 (22 Feb 2012) | Foswikitask:Item11448 - Implement {GuestCanComment}. If enabled, guests can comment on topics that they can not edit. |
|  2.0.2 (30 Jan 2012) 12887 | Foswikitask:Item11447 - "noform" examples not working - add comment_target parameter.|
|  2.0.1 (15 Jan 2012) | Foswikitask:Item11443 - better handling if target topic or web do not exist. |
|  10 Apr 2011 | Foswikitask:Item10263 - remove wrap="soft" to make templates validate. |
|  19 Nov 2010 | Foswikitask:Item10050 - CommentPlugin prompt must not add newlines that prevent COMMENT inside TML tables |
|  21 Oct 2010 | Foswikitask:Item3499 - support COMMENT (and other) ACL preferences%BR%\
                 Foswikitask:Item9592 - added AJAX example%BR%\
                 Foswikitask:Item9601 - Prevent uprev of topic for every comment%BR%\
                 Foswikitask:Item4423 - Spreadsheet calculations do not execute within comment templates%BR%\
                 Foswikitask:Item9568 (and others) - Redesigned core. Change commonTagsHandler to registered COMMENT macro, convert to REST handler instead of save script with beforeSaveHandler%BR%\
                 Foswikitask:Item5897 - if COMMENT target topic is missing, it should be created. |
|  21 Oct 2010 | Foswikitask:Item9857 - Restored the !CommentPluginTemplate to how it was before. All the changes done to make it look nice causes many errors. Never again add newlines, never again add trailing \ on existing templates. It does not always work. |
|  31 Jul 2010 | Foswikitask:Item9415 - Documentation updates |
|  27 May 2010 | Moved example topic Sandbox.CommentPluginExamples to Sandbox web. | 
|  24 Feb 2010 | Foswikitask:Item8611 - block comments resulting from calls to saveTopic from within an afterSaveHandler |
|  12 Sep 2009 | Foswikitask:Item8269 - Targeting anchors broken by Item727 is fixed. |
|  04 Jun 2009 | Foswikitask:Item1668 - The action template used with !ActionTrackerPlugin now uses new syntax ending with %ENDACTION. This makes each action item appear on a new line. Additionally new lines are now correctly saved as html br tags and not as html encoded br tag <br /> Foswikitask:Item1640 - Templates shipped with the plugin no longer encodes the user date entered when the date is saved so that it is possible for the user to use macros. |
|  15 Jan 2008 | Foswikitask:Item727 - data loss issue when missing anchor or location |
|  13 Jan 2009 | Foswikitask:Item736 - Make !CommentPlugin aware of CompareRevisionsAddOn bin script. |
|  16 Dec 2008 | Foswiki version |
|  06 Mar 2002 | initial commit |

%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Foswiki:Main.CrawfordCurrie inspired by the work of TWiki:Main.DavidWeller and TWiki:Main.PeterMasiar"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2004, Foswiki:Main.CrawfordCurrie<br />&copy; 2004-2023 Foswiki:%25SYSTEMWEB%25.ProjectContributor"}%
%META:FIELD{name="Description" title="Description" value="Quickly post comments to a page without an edit/save cycle"}%
%META:FIELD{name="Home" title="Home" value="http://foswiki.org/Extensions/CommentPlugin"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Release" title="Release" value="18 Dec 2025"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/distro"}%
%META:FIELD{name="Support" title="Support" value="http://foswiki.org/Support/CommentPlugin"}%
%META:FIELD{name="Version" title="Version" value="2.95"}%
