WordPress Comments to Wiki

How to Add WordPress Comments to Wiki

In this post, I would like to share to you all about how we can add wordpress comments to Wiki. See on the details below:

The Abstract of Adding Comments to Wiki

This is a wiki extention which integrates WordPress comments. MediaWiki is used to create structured documents. Content is continually added and often refined. Multiple authors can cooperate to create a more accurate or informative wiki document, however, users must actually create an account and log in. In some cases it would be worthwhile to get transitory information from viewers instead of content added by authors. To do this, it is important to offer these viewers a convienent way to submit comments. WordPress excels at providing one of the best interfaces to do this. It has an administrative section which allows the site owner to mederate comments posted by viewers. Now the wiki site owner can moderate comments, possibly getting information or direction from comments of the viewers. This extention is an attempt of melding some of the great aspects of MedaWiki and WordPress together.

This mediawiki extention allows wordpress comments to be integrated in any page on the wiki.

You Will Need This to Add Comments to Wiki

  • MediaWiki 1.9 or newer
  • WordPress (Any version, the latest is the best)
  • Both installed on the same mysql db

Installation

  • copy the below code as WordPressComments.php into the extensions directory
  • add the following to your LocalSettings.php
include('extensions/WordpressComments.php');
  • create a post in WordPress
  • in a wiki page which you would like comments from your WordPress site, add the following in an edit, where

<<title_of_your_comment_post>> is the title of a Post in WordPress

<wp:comments>
wp_comment=<<title_of_your_comment_post>>
wp_url=<<url base of your wordpress install>>
</wp:comments>

e.g.

<wp:comments>
wp_comment=Wordpress Comments To Wiki
wp_url=http://blog.gizmogarden.com
</wp:comments>
  • the following css can be dropped into your custom or main_mono.css
.commentlist li, #commentform input, #commentform textarea {
	font: 0.9em 'Lucida Grande', Verdana, Arial, Sans-Serif;
	}

.commentlist li {
	font-weight: bold;
	}

.commentlist cite, .commentlist cite a {
	font-weight: bold;
	font-style: normal;
	font-size: 1.1em;
	}

.commentlist p {
	font-weight: normal;
	line-height: 1.5em;
	text-transform: none;
	}

#commentform p {
	font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
	}

.commentmetadata {
	font-weight: normal;
	}

#sidebar {
	font: 1em 'Lucida Grande', Verdana, Arial, Sans-Serif;
	}

small, #sidebar ul ul li, #sidebar ul ol li, .nocomments, .postmetadata, blockquote, strike {
	color: #777;
	}

code {
	font: 1.1em 'Courier New', Courier, Fixed;
	}


.alt {
	background-color: #f8f8f8;
	border-top: 1px solid #ddd;
	border-bottom: 1px solid #ddd;
	}

 todo

  • default to page name of wiki as post name in WordPress
  • make extension which does not require installation on the same db
  • merge other aspects of WordPress blog into Wiki extension
  • have extension control aspects of comment display
  • use wiki css classes default as control to comments display
  • how-to merging styles MediaWiki and WordPress
  • Leonardo – Ajax post support – no need to refresh
  • users/namespace support – this is already supported
  • Leonardo – limit number of comments
  • Leonardo – Rss feeds per article
  • Leonardo – logged in wiki users only allow comments

bugs

 WordPressComments.php

<?php
define('WORDPRESSCOMMENTS_VERSION','0.9.0, 2007-12-16');
$wgExtensionFunctions[] = "wordpresscomments";

function wordpresscomments(){
	global $wgParser ;
	$wgParser->setHook ( "wp:comments" , 'get_wordpress_comments' ) ;
}

function get_wordpress_comments($data)
{

	$param = array();
	$info = explode("\n", $data);
	if(is_array($info)){
		foreach($info as $lin){
			$line = explode("=",$lin, 2);
			if(count($line)==2){
				$param[trim($line[0])] = trim($line[1]);
			}
		}
	}
	$wp_post_title = (isset($param["wp_post_title"]))? $param["wp_post_title"] : ""; // TODO - default to page name
	$wp_url = (isset($param["wp_url"]))? $param["wp_url"] : "";

	global $wgParser;
	global $wgDBserver;
	global $wgDBname;
	global $wgDBuser;
	global $wgDBpassword;
	global $wgPageName;

	$wgParser->disableCache();

	mysql_connect($wgDBserver,$wgDBuser,$wgDBpassword) or die("Unable toconnect to database" . mysql_error());
	@mysql_select_db("$wgDBname") or die("Unable to select database $wgDBname");

	$sql = "  SELECT * FROM wp_posts WHERE post_title LIKE '$wp_post_title%' AND post_status = 'publish'  ";
	$result = mysql_query($sql);
	$number = mysql_numrows($result);
	$wp_post_ID = 0;
	$ret = "";

	if ($number < 1)
	{
		#$ret .=  "no records found"; -- TODO Configurable
		$ret .= "could not find wordpress article with a title like [".$wp_post_title."%]" ;
	} else if ($number == 1){
		$wp_post_ID = mysql_result($result, 0,"ID");
		$sql = " SELECT * FROM wp_comments INNER JOIN wp_posts ON comment_post_ID = ID WHERE  ID = $wp_post_ID AND comment_approved = '1'  ORDER BY comment_date ";
		$result = mysql_query($sql);
		$number = mysql_numrows($result);
		$i = 0;
		$ret = "";
		#$ret .=  "[".$sql ."]";
		#$ret .=  "[".$wp_post_ID ."]";


		if ($number < 1)
		{
			#$ret .=  "no records found"; -- TODO Configurable
		}
		else
		{
			$ret .=  "<ul class=\"commentlist\">";
			while ($number > $i)
			{
				$ret .=  "<li id=\"comment-". mysql_result($result,$i,"comment_ID") ."\"  ";
	 			$ret .=  (($i % 2) ? " " : " class=\"alt\" ");
				$ret .=  ">";
				$ret .=  "	<cite><a href='https://www.gizmogarden.com' rel='external nofollow'>". mysql_result($result,$i,"comment_author") ."</a></cite> Says:";
				$ret .=  "				<br />";
				$ret .=  "	<small class=\"commentmetadata\"><a href=\"#comment-". mysql_result($result,$i,"comment_ID") ."\" title=\"\">". date("M d Y g:i a", strtotime(mysql_result($result,$i,"comment_date")))  ."</a> </small><br>";
				$ret .=  "	<p>" . mysql_result($result,$i,"comment_content") . "</p>";
				$ret .=  "</li>";
				$i++;
			}
			$ret .=  "</ul>";

		}

		$reply_form = true;
		if ($reply_form)
		{
			$ret .= get_wordpress_reply_form($wp_post_ID, $wp_url);
		}


	} else {
		$ret .=  "more than one wordpress post match";
		$ret .= "sql [".$sql."]";
	}

	return  $ret;
}

function get_wordpress_reply_form ($wp_post_ID, $wp_url)
{

	if ($wp_post_ID > 0)
	{
		$ret = "";
		$ret .=  "<h3 id=\"respond\">Leave a Comment</h3>";
		$ret .=  "<form action=\"$wp_url/wp-comments-post.php\" method=\"post\" id=\"commentform\">";
		$ret .=  "<p><input type=\"text\" name=\"author\" id=\"author\" value=\"\" size=\"22\" tabindex=\"1\" />";
		$ret .=  "<label for=\"author\"><small>Name (required)</small></label></p>";
		$ret .=  "<p><input type=\"text\" name=\"email\" id=\"email\" value=\"\" size=\"22\" tabindex=\"2\" />";
		$ret .=  "<label for=\"email\"><small>Mail (will not be published) (required)</small></label></p>";
		$ret .=  "<p><input type=\"text\" name=\"url\" id=\"url\" value=\"\" size=\"22\" tabindex=\"3\" />";
		$ret .=  "<label for=\"url\"><small>Website</small></label></p>";
		$ret .=  "<p><textarea name=\"comment\" id=\"comment\" cols=\"100%\" rows=\"10\" tabindex=\"4\"></textarea></p>";
		$ret .=  "<p><input name=\"submit\" type=\"submit\" id=\"submit\" tabindex=\"5\" value=\"Submit Comment\" />";
		$ret .=  "<input type=\"hidden\" name=\"comment_post_ID\" value=\"$wp_post_ID\" />";
		$ret .=  "</p>";
		$ret .=  "</form>";
		$ret .=  "	</div>";
		$ret .=  "<hr />";
		$ret .=  "<div id=\"footer\">";
		$ret .=  "	<p>";
		$ret .=  "		GizmoGarden is proudly powered by";
		$ret .=  "		<a href=\"http://wordpress.org/\">WordPress</a>";
		$ret .=  "";
		$ret .=  "		<br /><a href=\"http://blog.gizmogarden.com/?feed=rss2\">Entries (RSS)</a>";
		$ret .=  "		and <a href=\"http://blog.gizmogarden.com/?feed=comments-rss2\">Comments (RSS)</a>.";
		$ret .=  "	</p>";
		$ret .=  "</div>";
		$ret .=  "</div>";
	}
	return $ret;
}

?>

Leave a Reply

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