Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

Comments

Summary: Comment addon - comments in separate files
Version: 14 April 2005
Prerequisites:
Status:
Maintainer: Ilya Zverev

This is not a page to leave comments to all cookbooks!! It is the page for the recipe "Comments"!

This recipe may be obsolete: It uses the deprecated mailposts script, and the maintainer has not been available for assistance. It also had some serious drawbacks, even when it was current. You may do better to look at other recipes in the Comments category.


This is a backup from Google Cache 2007-02-10, because the history of this version of the page wasn't available.

Description

This module enables comment functionality on all pages in the wiki. It doesn't mess page content, because comments are stored in separate files. Users have ability to alter their comments right after posting. Wiki markup is supported. Installation

To install the software, unpack the zip file and copy its contents to your pmwiki directory.

Add the following line into your local/config.php file:

    include_once('cookbook/comments.php');

Then, to display comments you should call PmWiki with action=comments. For example you may add the following line in your pmwiki.tmpl template file, if you're using standard template:

    <a href='$PageUrl?action=comments#comments'>
        Comment (<!--function:DisplayCommentsCount -->)
    </a>

If you want comments always to be displayed in pages, Add the following line into your local/config.php file after including comments.php:

  PatchCommentBrowseFmt();

To be able to remove comments you also need to set the default password for this action:

    $DefaultPasswords['removecomment'] = crypt('yourpassword');

See also Passwords and PasswordsAdmin for information on using passwords in PmWiki. Usage

To display comments call pmwiki.php script with action=comments. Or simply open any page, if the comments are shown by default. Under the page text there will be a bunch of already written comments (if any) and the form to enter new comment. It looks like:

    Enter your comment:
    [                                             ]
    [                                             ]
    [                                             ]
    [                                             ]
    [                                             ]
    [                                             ]
    Author: [          ]  [Submit]

After entering comment text and author name (if not entered, it's substituted by author's ip address) and submitting the form buttons are replaced by those:

    Author: [          ]  [Update] [Delete] [Reset]

User can now edit his recently posted comment and submit changes by pressing Update button. If the text is completely deleted and posted, the recent comment is deleted. The same effect can be reached by pressing Delete. To add a completely new comment, Reset button should be pressed to clear all the hidden fields.

The behaviour seems a bit hard to understand, but it's useful and easy when one's got used to it. As for beginners, they could probably need some advice, and it should be written in the PmWiki.CommentsFooter page. For example, it may contain the following text:

The meaning of buttons:

    * Submit — submits a new comment;
    * Update — updates recently submitted comment's text;
    * Delete — removes last comment;
    * Reset — clears text and hidden fields allowing to enter a new comment. 

The one useful feature is that wiki markup is supported in the comment text. Configuration

In the script there are two functions that set default values for the styling variables. The first one, InitComments(), uses plain wiki markup to style comments. If you are not afraid of using CSS markup, you may leave a call to InitCommentsStyled() uncommented.

Variables

$CommentStartFmt

Defines comment list header.

$NoCommentsFmt

Defines text that is displayed when there's no comments for the page. By default it looks like $CommentStartFmt, but saying 'No comments'.

$CommentFmt

Defines how the comment is displayed. Has following variables that are substituted: $Author — author's name or ip; $Time — date and time of the comment (formatted with $TimeFmt); $Text — the comment itself.

$CommentEditFmt

Contains the comment edit form excepting buttons. Before changing this variable you must learn how it works by examining the default value.

$NewCommentButtonsFmt

Contains Submit button, which sends the new comment data.

$EditCommentButtonsFmt

Contains Update, Delete and Reset buttons used to alter posted comment.

$CommentsFooterFmt

By default displays PmWiki.CommentsFooter page under the edit form. Is referred in the $CommentEditFmt.

$RecentComments vList pages where notifications about recently added comments are stored, just as $RecentChanges array. By default contains Main.AllRecentComments and $Group.RecentComments. $CommentsFileName

The name of the function which takes page name as parameter and return the file name where page comments are stored. Default function returns "$WikiDir/$pagename.Comments".

Internationalization

Several strings in default values are made i18nable, and some of those strings interfere with the standard ones. I think this is not critical because the meaning is the same. The strings are:

    * Comments (in $CommentStartFmt)
    * No comments (in $NoCommentsFmt)
    * Enter your comment
    * Author
    * Submit
    * Update
    * Delete
    * Reset
    * Set new comment removing password: 

Storage

Comments are stored in $WikiDir along with wiki pages. The name of comments file for the page in constructed by adding .Comments extension to the page file name. For example, for Main.HomePage comments file will be named Main.HomePage.Comments. If you want to override this behaviour (for example, store comments in a separate directory), write your own comment-file-name-constructing function and specify its name in the $CommentsFileName variable.

Each comment occupies exactly one line in the file. The line has the following format:

    UID|IP|Author|Time|Text

The meaning of fields is:

  • UID — the unique id of the comment allowing its author to alter it right after posting. Probably it will allow changing all comments by the author in the future;
  • IP — the ip address of the comment author;
  • Author — the author's name;
  • Time — the unix-timestamp of the comment, is converted to normal time using $TimeFmt when the comment is displayed;
  • Text — the comment text itself. All newlines are replaced with the $Newline symbol. Note that $Newline is not stored along with comments, so it will be a disaster when you change it.

History

Apr 14, 2005

  • commentsmailed.php version added

Dec 8, 2004

  • Updated to work in PmWiki 2.0.beta6

Oct 29, 2004

  • Handling \r chars in input.
  • CommentsCount() function caused warnings.
  • Now allows authorized users to delete comments.
  • Added forgotten fclose() in HandlePostComment{}.

Sep 29, 2004

  • original version.

Revision: Commentsmailed.php

I decided to modify Ilya's original script so that I could receive e-mail notifications about comments through the MailPosts script. To use this, simply replace

    include_once('cookbook/comments.php');

in config.php with

    include_once('cookbook/commentsmailed.php');

Aside from integrating with MailPosts, the only other change I made was to prettify the links placed in the RecentComments pages, so that they show the name of the page, rather than the naked hyperlink. I still can't figure out how to place them in reverse chronological order; even though Ilya's code is identical to that used to create the RecentChanges pages, the order is reversed. Weird... - David A Spitzley

Comments & Bugs

  • $EnablePostAuthorRequired does not appear to be tested;
  • when using the browser Back Button (following comment Submit), this may cause Post Data to insert a copy of the comment.
  • problem with function commentsFileName. As the source is when i downloaded it, it was recording all my comments in a file "Object.Comments". It is probably not a bug as there is another return statement commented. But when i tried to tweak, i found $WikiDir returning "Object" probably not much here, could be fixed easily.
  • No password is required to post. This is normally what would be expected with a Comments feature, but there is no way to prevent automated comment-spam.
  • On some installations, at least, ever robot's visit to a "comment" link is treated as an added comment - a "Groupname.Pagename.Comments" file is either created or incremented.
  • Comments do not appear on (All)RecentChanges; the (All)RecentComments pages are reverse-sorted.
  • Comments do not appear on activity mailposts (which script is deprecated anyhow). David Spitzley's version - the "commentsmailed" recipe - will send notification if your site has the mailposts script working, but then all robot "touches" are sent as activity - which makes the notifications useless if the robots are very busy generating false positives.

okay i tried to figure it out but i couldn't (looong time i've been doin' php). So my workaround is to replace the return statement with : return "wiki.d/".$pagename.".Comments";

i also tried :

 return $WikiDir->dirfmt.'/'.$pagename.".Comments";

but it doesn't work.

I faced this problem too. here is the fix. change function as follows

  function CommentsFileName($pagename) {
  global $WikiDir;
  //	return $WikiDir.'/'.$pagename.".Comments";
  $pagefile = FmtPageName($WikiDir->dirfmt,$pagename);
  error_log("trying to save to ".$pagefile."comments");
  return $pagefile.".Comments";
  }

I too couldn't get the comments working right out of the box (at least not with PmWiki 2.0 beta19). The above hack does the trick, but so does this one that Jeffrey Barke gave me. Which one is better? (Replace this in comments.php on line 207)

  function CommentsFileName($pagename) {
  global $WorkDir;
  return $WorkDir.'/'.$pagename.".Comments";
  }

It seems to me that this depends on pmwiki version as pm has changed $WorkDir definition. In pmwiki 2.0beta37 comments are not saved at all. You should change comments.php as follows to fix this

  function CommentsFileName($pagename) {
  global $WorkDir;
  return $WorkDir->pagefile($pagename).".Comments";
  }
  • Is there a way to have recent comments listed at the top of the AllRecentComments page, rather than the bottom? It would be more consistent with the RecentChanges format. Also, I'd like to use the recent-comments page as a WikiTrail and subscribe to it as an RSS feed, but the feed produced by ?action=rss has no items. Any idea how to fix it? GrahamL
  • bug in the AllRecentComments and RecentComments pages generation. The syntax for wikilinks shoud be updated to and not example.com text?.
  • A working example can be found at the Milwaukee Data Consortium's website.
  • I can't see any comments on this site, could you be a bit more specific on where the example can be found. DonT. Try here: http://www.nonprofitcentermilwaukee.org/data/index.php/PmWiki/BasicEditing?action=comments

Is there a way to show the comments by default, not only with action=comments?

  • I don't seem to be able to use any Wiki markup in the comments. Any ideas?
  • Comments don't seem to be picked up by the search either.
  • These comments appear to be global to the site, not specific to a page, or have I done something wrong?
    • AllRecentComments lists the pages on which comments have most recently been added, for the whole site, it's analagous to AllRecentChanges; Group/RecentComments lists all comments by wikigroup. Create a link or use ?action=comments#comments in your browser's address bar to view the comments (if any) on a particular page. Does that address your issue at all?
  • I'm also having this problem. I'm using <a href='$PageUrl?action=comments#comments'> as a link within each page, but its showing me the same comments on each page. I'm using PmWiki 2.1.5.
  • The comments engine doesn't work when $EnablePathInfo is set to 1, i.e. if pmwiki uses index.php/Main/Page instead of index.php?n=/Main/Page. With this setting, all comments are added to the Site/PageNotFound page.
  • Does it work with the newest 2.1 Beta Releases?
    • It works, but $PageName in the Forms definition must be replaced by small characters 2 times ($pagename) uudo
  • Q1: Is it possible to always show the commentbox (instead of using action=commentbox)?
  • Q2: I use commentsmailed, it seems that I always get a mail when somebody clicks on the commentbox link for the first time each page, even if no comment is entered. How can I get only a mail when something is entered?
    • My PmWiki | commentsmailed.php - uudo May 29, 2006, at 05:41 AM
  • Perhaps it's just the sample provided, but it looks like comments containing raw XHTML, including the <script> tag are allowed. I've created an annoying JavaScript alert as an example...
  • The function CommentsFileName($pagename) misuses the $WikiDir global. This change makes comments for a particular page appear only on that page
 function CommentsFileName($pagename) {
 global $WikiDir;
 return $WikiDir->pagefile($pagename.".Comments");
 // return $WikiDir.'/'.$pagename.".Comments";
 // return $WikiDir.".Comments";
 }
  • Q: commentsmailed.php uses the deprecated MailPost module. How hard would it be to convert it to use the Notify Module?
  • line 181: I think it's better to use <br /> rather than \n
   $comment = explode("|",str_replace($Newline, "\n", rtrim(fgets($comfile, 4096))));

Author

I'm Ilya Zverev, you can mail to zverik at int.spb.ru if you are experiencing problems with this script. I'm also subscribed to Pmwiki-users list: use it if you think that the question (and the answer) may be interesting to others.

Script was updated for PmWiki 2.0.beta6 by Jeffrey Barke (jbarke@milwaukeedept.org).

Edit - History - Print - Recent Changes - Search
Page last modified on May 27, 2008, at 09:04 PM