Added Commenter Profile Page to MovableType

If you check the site careful enough, you will see a small icon besides the name of the commenter. It is newly added feature – Commenter Profile:

screen-comment.png

The link on the icon will lead you to the profile page of the commenter, so you understand what comments he/she has posted before.

Since it is based on display name only, don’t always take it for granted that people with same display name are the same person. If you found your name are too common, choose a more unique name.

Code?

Below are the code I used. It is very massy, since I didn’t spent time to make it workable for others. It just fit the needs of this blog. I wrote it as if I were writting a blog entry. When what I try to express is completed, the code is completed. So take whatever you can take from the code, but don’t expect it to work on your MovableType directly. For those who don’t like tech stuff, sorry for posting techie again.


<?php

function dirify($s) {

$s = strtolower($s);   ## lower-case.

$s = strip_tags($s);       ## remove HTML tags.

$s = preg_replace('!&[^;\s]+;!','',$s); ## remove HTML entities.

$s = preg_replace('![^\w\s]!','',$s);   ## remove non-word/space chars.

$s = preg_replace('!\s+!','_',$s);       ## change space chars to underscores.

return $s;

}

$mt_blog_users = "http://home.wangjianshuo.com/users/";

$mt_blog_archive = "http://home.wangjianshuo.com/archives/";

$file = '<Location of mt-config.cgi file>';

$cfg = array();

if ($fp = file($file)) {

foreach ($fp as $line) {

// search through the file

if (!ereg('^\s*\#',$line)) {

// ignore lines starting with the hash symbol

if (preg_match('/^\s*([^ ]+)[ ](.*)(\r|n)?$/', $line, $regs)) {

$key = trim($regs[1]);

$value = trim($regs[2]);

$cfg[$key] = $value;

}

}

}

} else {

die("Unable to open configuration file $file");

}

$db = mysql_connect($cfg['DBHost'], $cfg['DBUser'],

$cfg['DBPassword']) or die ('I cannot connect to MySQL.');

mysql_select_db($cfg['Database']);

$query = "SELECT comment_author,

count(comment_author) as comment_count

FROM mt_comment

GROUP BY comment_author

ORDER BY comment_count";

$result = mysql_query($query);

$comment_author_previous = "";

$comment_author_next = "";

while ($row = mysql_fetch_array($result)) {

if($row['comment_author'] == $user) {

$comment_author_previous = $comment_author_previous_temp;

}

if($comment_author_previous_temp == $user) {

$comment_author_next = $row['comment_author'];

}

$comment_author_previous_temp = $row['comment_author'];

}

echo "<small><a href='$mt_blog_users$comment_author_next.htm'>« $comment_author_next</a> |";

echo " <a href='http://home.wangjianshuo.com/scripts/mt-user/mt-all-user.php'>Commenters</a> ";

echo "| <a href='$mt_blog_users$comment_author_previous.htm'>$comment_author_previous »</a></small>";

$user or $user = 'Jian Shuo Wang';

$query = "SELECT entry_id, entry_title,

comment_id, comment_url,

entry_excerpt, comment_text,

comment_entry_id,

date_format(entry_created_on, '%Y%m%d') as comment_entry_ymd,

date_format(comment_created_on, '%h%i') as comment_hourmin,

comment_created_on

FROM mt_comment, mt_entry

WHERE comment_entry_id = entry_id

AND comment_author = '$user'

ORDER BY comment_id DESC

LIMIT 300";

$comment_url = "";

$comment_count = 0;

$html = "";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {

$comment_entry_id = $row['comment_entry_id'];

$html = $html . "<p>" . (nl2br($row['comment_text'])) .

"<p><small>Posted by $user at <

a href='http://home.wangjianshuo.com/users/" .

$row['comment_id'] . ".htm' target=_blank>" .

$row['comment_created_on'] .

"</a> on <a target=_blank href=$mt_blog_archive" .

$row['comment_entry_ymd'] .

"_" . dirify($row['entry_title']) .

".htm#" . $row['comment_hourmin'] .

">" . $row['entry_title'] .

"</a></small><hr>";

$comment_url or $comment_url = $row['comment_url'];

$comment_count = $comment_count + 1;

}

echo "<h1>$user</h1><a href='$comment_url' target=_blank>$comment_url</a> ";

echo "Posted <b>$comment_count</b> comment(s)";

echo $html;

mysql_free_result($result);

mysql_close();

?>

3 thoughts on “Added Commenter Profile Page to MovableType

Leave a Reply

Your email address will not be published. Required fields are marked *