There are times we want to seperate the discussion from the originally arranged, well-organized wiki page. Or we just want to make it simpler for people who don't know any about wiki/wiki formats to leave comments.

This patch is to create a new macro, Comment, which can make your readers easier and quicklier to give comments which will be appended to an arbitrary page, without disarranging the original article.

This macro is implemented on [MiGi's Tavi Site] (See the bottom of that page for an immediate example)

Features

Usage

[[Comment (the page for comment to be appended) (use template. default:1)]]

The parentheses () means the paremeter is optional.

There are 3 methods(interfaces) to carry out the comment area:

Method 1: append comments to the page

The fast way to create commenting textarea. The comments by your readers are appended to the last of the same page.

usage:

[[Comment]]

Which will create a form like this:

Leave Comments: [___________________(Input Field)_________________][Save][Preview][]Use Wiki Format

Method 2 & Method 3

You can let comments appended to other pages just fill the second parameter:

[[Comment DisscussionPage]]

See [here] for details and examples.

http://runx.org


How to patch

See the followings or refer to [MiGi:TaviComment/ForProgrammer]

Files to be modified

Because this mocro is related to viewing, saving and previewing, the affected files are quite many. However the process is straight.

./config.php

To make Tavi know this new macro. Add the line following "$ViewMacroEngine = array_merge($ViewMacroEngine, array(":


$ViewMacroEngine = array_merge($ViewMacroEngine, array(
             'Comment'       => 'view_macro_comment' // add this line

./parse/macros.php

Append to the last:


require_once('macros/comment.php'); //MiGi,040319

./macro/comment.php

Create this file. Create the directory "macro" if it is not in your Tavi directory.


<?php
/***************************************************************************
 * comment.php, version 0.0.3 (compatible to Tavi 0.25,0.26)               *
 * Last modified: 04.28.2006                                               *
 * Contributed by: MiGi <migichen@gmail.com>                               *
 ***************************************************************************/

if (!defined('TMPL_AddComments')){
    define('TMPL_AddComments', "Leave Your Comments: ");
    define('TMPL_CommentWikiFormat', 'Use Wiki Format');
}
switch (LANGUAGE_CODE)
{
    case 'zh': case 'zh_tw':
        $comment_samplePage = 'TaviComment/SamplePage';
        break;
    default:
        $comment_samplePage = 'lang_en:TaviComment/SamplePage';
        break;
}

function view_macro_comment($args)
{
    global $comment_samplePage; 
    global $ParseEngine, $page, $pagestore;
    
    if ($page == $comment_samplePage) //in the sample page, common mocro is disabled
        return '[[comment]]';//commentPage;

    //{default
    $commentPage = $page; // write to current page
    $comment_useSamplePage = 0;
    //}default
    if ($args!='') {
        $arr = split(" ", $args);
        if (count($arr)>=1) {$comment_useSamplePage = 1; $commentPage = $arr[0]; }
        if (count($arr)>=2) {$comment_useSamplePage = $arr[1]; }
            
    }
    // transclude page: $comment_samplePage
    if ($comment_useSamplePage && $commentPage!=$page) {
        $pg = $pagestore->page($comment_samplePage);
          $pg->read();
        $commentBody = 
            '<div id="body">'.
                  parseText( (preg_replace('/\\[\\[comment\\]\\]/i', $commentPage,  $pg->text))
                    , $ParseEngine, $args).
            '</div>';

    }else
        $commentBody = '';
        
    $r = //'<script src=./macro/comment.js></script>'.
    '<form method="post" action="'. saveURL($commentPage) . '">'.
        '<div class="comment">'.
              $commentBody.
            '<div id="foot">'.
                TMPL_AddComments.'<input id="text" type="text" name="GuestComment" size="70"/>'.
                '<input type="submit" name="Save" value="'.TMPL_ButtonSave.'"  />'.
                '<input type="submit" name="Preview" value="'.TMPL_ButtonPreview.'" />'.
                '<input type="checkbox" name="WikiFormat">'.TMPL_CommentWikiFormat.'</checkbox>'.
                '<input type="hidden" name="archive" value="1" />'.
                '<input type="hidden" name="referrer" value="'.viewURL($page).'" />'. /*refer to the original page, modified 04.26.2006*/
            '</div>'.
        '</div>'.
    '</form>';
    return $r;
    

}

// called by ./action/save.php, ./action/preview.php
function comment_modifyDocument($document, $comment, $useWikiFormat)
{
    global $UserName, $REMOTE_ADDR, $TimeZoneOff;
    // add \n to $document
      $endingChar = $document[strlen($document)-1];
      if ($endingChar!="\n")
          $document.="\n";
      
      // check comment, remove slashes added by server, anti-spam // 04.28.2006
      $comment = stripslashes(trim(strtok($comment, "\n")));
      if (empty($comment)) return $document;      

      // get date-time
    $daystr=strftime("%Y.%m.%d %H:%M",time()+$TimeZoneOff * 60);

    // write comment into $document:
    if ($useWikiFormat)
          $document .= "* ". $comment;
      else 
          $document .= "* ```". $comment."```";
      $document .=     " ~ ". ($UserName?"(($UserName))":gethostbyaddr($REMOTE_ADDR)) .
              " - ". $daystr;
      
      //print $HTTP_COOKIE_VARS[$CookieName];
      // return
      return $document;
}
?>


Note

In order to provide multi-language interface, in this site $comment_samplePage is given according to the language detected formerly from the accepted language of visiters' browser.

switch (LANGUAGE_CODE)
{
	case 'zh': case 'zh_tw':
		$comment_samplePage = 'TaviComment/SamplePage';
		break;
	default:
		$comment_samplePage = 'lang_en:TaviComment/SamplePage';
		break;
}

./action/save.php

Add the lines between if($pg->exists()){...} and $document = substr($document, 0, $MaxPostLen);


  if (isset($_POST['GuestComment'])) {
     include_once(\\\"./macros/comment.php\\\");
      $document =  comment_modifyDocument($pg->text, $_POST['GuestComment'], isset($_POST['WikiFormat'])?TRUE:FALSE);
  }

The part of UseCaptcha?. Add && !isset($_POST['GuestComment']) into the if-statement.

(Tavi0.25 or below is unnecessary)


  if ($UseCaptcha && !isset($_POST['GuestComment'])/*MiGi*/) {

After the modification above, save.php should look like this: (TaviComment/ForPgrammer/action_save.php)? (Tavi 0.26)

./action/preview.php


  if (isset($_POST['GuestComment'])) {
     include_once(\\\"./macros/comment.php\\\");
      $document =  comment_modifyDocument($pg->text, $_POST['GuestComment'], isset($_POST['WikiFormat'])?TRUE:FALSE);
  }

./template/save.php

Modify the file as the followings:


<?
function template_save($args)
{
//{ MiGi: Add here: refer to referrer, for [[comment]] macro
global $referrer;
  if (!empty($referrer))
    header('Location: ' . $referrer);
  else 
//} MiGi: refer to referrer, for [[comment]] macro
    header('Location: ' . viewURL($args['page']));
}
?>


./template/wiki.css (optional)

There are three CSS tags you can change. You can leave it after all others are done.

/*comments macro*/
div.comment{  /*the frame of comment */
}
div.comment #body{  /*the style of template interfate*/
}
div.comment #text { /*input area*/
}

./lang/lang_XX.php (optional)

For multi-language usage, you can add the following language patches into each files in lang directory:

Therefore the prompts will be translated:

For example, if you want the page showed in Chinese, add the followings in the bottom of lang_zh_tw.php:

setConst('TMPL_AddComments', "&#25105;&#20063;&#35201;&#30041;&#35328;: "); // add comments:
setConst('TMPL_CommentWikiFormat',  '&#20351;&#29992;Wiki&#26684;&#24335;'); // using Wiki style

Now You Can Run Your Tavi Site

Now, try typing [[Comment]] in your SandBox or anywhere. Save it, and you can see a textbox informing to add comment. If you try to leave any comments in the textbox, the comments will be appended to the bottom of the same page. If anything go wrong, check your codes. Don't do the followings if this test fails.

Before we go any further, to make description easier to understand, we first define comment macro in user phase and code phase:

define - user phase
means we are talking about the comment macro you and users from the Internet can add by inputing the following line in any wiki page:
[[comment ...]]
define - code phase
means the PHP codes of comment macro you have added into your site.

Create template page

Create a page named TaviComment/SamplePage if you did not change the page name defined by $comment_samplePage in ./macro/comment.php

After that, fill in the page like this:

(([[Comment]]|Comments)) 
[[Transclude [[Comment]] ]]
Last modified: [[! (([[Comment]]))]]

Note

For example:
[[comment TaviComment/comment]]

It will become the followings after the process of the comment macro:

((TaviComment/comment|Comments)) 
[[Transclude TaviComment/comment ]]
Last modified: [[! ((TaviComment/comment))]]

In code phase, it will be passed to Tavi's ParseEngine and rendered to the correspoding html format.

FAQ

  1. Q:Why don't we just save the template page in files or codes, so that the template can be used by the macro directly?
    A: Currently it seems only site managers need to modify template page. However, in the future the Namespace feature may be included to Tavi, and then it will possibly be easier for different languages, topics, or authors to have different templates. Thus it will provide more flexibility.