Views
MediaWiki Wordpress Comment Extension Code
From Shadowfax
Contents |
Background
Greg Perry's excellent MediaWiki extension allowing Wordpress to be used as a comment engine for MediaWiki no longer seems to be available on his site, so I have taken the liberty of reproducing the code below, with some minor adjustments. I'm sure that it should be relatively easy to make this work in different ways, so feel free to contact me if you have ideas of a better way!
I've also been given a copy of Greg's original code, which is now presented here. I have yet to retest this or provide usage notes.
Disclaimer
It is worth backing up the appropriate files before you make any changes - and I disclaim any responsibility if you break your wordpress or MediaWiki installation. You have been warned!
The code is presented on an "as is" basis, but if you run into any problems, please contact me (send a PM to shadowfax on the MediaWiki Users Forum).
Simple Usage
There are two very different ways of using this extension. The easiest (and least flexible) is to use it in the way that Greg Parry originally intended.
To do this follow the installation instructions below. Then, on any wiki page that you require a comments form, add the following markup:
<wp:comments> wp_post_title=YourBlogPostTitle wp_url=http://yourblogurl.com </wp:comments>
You will need to ensure that a blog post exists with that name on your Wordpress blog. With this method, one blog post is required for each MediaWiki article that requires comments.
Note that if Wordpress is installed in a different database you also need to change the Wordpress Database Details.
Complex Usage
The second, more complex usage is to allow one blog post to contain all the comments, and place the comments form on the mediawiki template. HOWEVER before you do that you will need to make some modifications to the WordPress database and WordPress code. This is not for the faint-hearted.
Note that if Wordpress is installed in a different database you also need to change the Wordpress Database Details.
Once these changes are made there are four variables in the code which must be set to configure the way that the Wordpress Comments Engine works on your wiki.
These are shown below
// Edit these configuration variables to reflect your installation as required $wp_post_title="MediaWiki Comments"; $wp_url="http://blog.mydomain.com/"; $pageComments=false; $useTemplate=false;
$wp_post_title
This is the name of the post on your wordpress blog which the comments will be appended to. It must be the name of a real post on the blog.
$wp_url
This is the URL that your Wordpress Blog can be reached
$pageComments
If you are happy for each mediawiki page that requires comments to have its own blog entry then leave this as false. If, however, you want one blog post to be shared across all mediawiki pages then this needs to set to True. The wordpress database modification must also be done for this to work.
$useTemplate
If you are integrating the comments into the template then set this true, otherwise leave it to be false
Wordpress Database Details
The code assumes that MediaWiki and the Wordpress share the same database, username and password. If this is not the case it is easy to fix by setting the $dbServer, $dbName, $dbUser, and $dbPassword variables at lines 17-20 of the script to have the details of the Wordpress database. eg change the lines
// If the Wordpress database does not share the same database, username and password // as the MediaWiki installation these need to be changed to have the wordpress values $dbUser = $wgDBuser; $dbPassword = $wgDBpassword; $dbServer = $wgDBserver; $dbName = $wgDBname;
to
// Wordpress database details $dbUser = "myUserName"; $dbPassword = "myPassord"; $dbServer = "myServerName"; $dbName = "myWordpressDatabaseName";
The code
Add the following to LocalSettings.php
require_once ('extensions/WordpressComments.php');
Save the following as WordpressComments.php in MediaWiki extensions directory and edit the global config declarations to fit your wiki.
<?php /** * WordPress Comments extension * For documentation, please see: * http://shadowfax.org.uk/wiki/MediaWiki_Wordpress_Comment_Extension_Code * * Version 0.10.0 minor changes by Ian Coleman. Based on 0.9.0 by Greg Perry * @ingroup Extensions * @author Greg Perry * @version 0.10.1 */ define('WORDPRESSCOMMENTS_VERSION','0.10.1, 2011-10-20'); // Edit these configuration variables to reflect your installation as required $wp_post_title="Wordpress Comments"; $wp_url="http://blog.mydomain.com/"; $pageComments=false; $useTemplate=false; // If the Wordpress database does not share the same database, username and password // as the MediaWiki installation these need to be changed to have the wordpress values $dbUser = $wgDBuser; $dbPassword = $wgDBpassword; $dbServer = $wgDBserver; $dbName = $wgDBname; //Extension credits that show up on Special:Version $wgExtensionCredits['parserhook'][] = array( 'name' => 'WordPress Comments', 'url' => 'http://www.mediawiki.org/wiki/Extension:WordPress_Comments', 'version' => WORDPRESSCOMMENTS_VERSION, 'author' => 'Greg Perry', 'description' => 'Parser hook to show WordPress comments on a MediaWiki.' ); //Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, //as per r35980 if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) { $wgHooks['ParserFirstCallInit'][] = 'wordpresscomments'; } else { $wgExtensionFunctions[] = 'wordpresscomments'; } function wordpresscomments(){ global $wgParser; $wgParser->setHook ( 'wp:comments', 'get_wordpress_comments' ); return true; } function get_wordpress_comments($data) { global $wp_post_title,$wp_url, $pageComments; global $wgParser, $wgPageName; global $dbUser, $dbPassword, $dbServer, $dbName; global $wgTitle; $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]);} } } if (isset($param['wp_post_title'])) $wp_post_title = $param['wp_post_title']; if (isset($param['wp_url'])) $wp_url = $param['wp_url']; $wgParser->disableCache(); mysql_connect($dbServer,$dbUser,$dbPassword) or die("Unable toconnect to database" . mysql_error()); @mysql_select_db("$dbName") or die("Unable to select database $dbName"); mysql_query("SET NAMES utf8"); mysql_query("SET CHARACTER_SET utf8"); $sql = " SELECT * FROM wp_posts WHERE post_title LIKE '$wp_post_title' AND post_status = 'publish' "; $result = mysql_query($sql); $number = mysql_num_rows($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 <b>".$wp_post_title."</b>" ; } else if ($number == 1) { $wp_post_ID = mysql_result($result, 0,"ID"); if ($pageComments) { $pgTitle = $wgTitle->getText(); $sql = " SELECT * FROM wp_comments WHERE comment_post_ID = $wp_post_ID AND comment_approved = '1' and wikipage='$pgTitle' ORDER BY comment_date desc"; } else $sql = " SELECT * FROM wp_comments WHERE comment_post_ID = $wp_post_ID AND comment_approved = '1' ORDER BY comment_date desc"; $result = mysql_query($sql); $number = mysql_num_rows($result); $i = 0; $ret = "<br /><div id='comment-outer'>\n<hr />\n<div id='comment-list'>\n"; $ret .= get_wordpress_reply_form($wp_post_ID, $wp_url); if ($number >= 1) { $ret .= "<h3>Comments</h3>"; while ($number > $i) { $ret .= "<div class='comment-item'>\n"; $ret .= "<span class='comment-author'><a href='" . mysql_result($result,$i,"comment_author_url") . "' rel='external nofollow'>" . mysql_result($result,$i,"comment_author") ."</a></span> "; $ret .= mysql_result($result,$i,"comment_content") . "<br />\n"; $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>\n"; $ret .= "</div>\n"; $i++; } } $ret .= "</div>\n</div>\n"; } else { $ret .= "more than one wordpress post match"; $ret .= "sql [".$sql."]"; } if ($useTemplate) echo $ret; else return $ret; } function get_wordpress_reply_form ($wp_post_ID, $wp_url) { global $wgTitle, $pageComments; if ($wp_post_ID > 0) { $ret .= "<h3 id=\"respond\">Leave your comment</h3>\n"; $ret .= "<form action=\"" . $wp_url ."wp-comments-post.php\" method=\"post\" id=\"commentform\">\n"; $ret .= "<p><input type=\"text\" name=\"author\" id=\"author\" value=\"\" size=\"22\" tabindex=\"1\" class='comment-inp' />\n"; $ret .= "<label for=\"author\">Name (required)</label></p>\n"; $ret .= "<p><input type=\"text\" name=\"email\" id=\"email\" value=\"\" size=\"22\" tabindex=\"2\" class='comment-inp' />\n"; $ret .= "<label for=\"email\">Mail (required but will not be published)</label></p>\n"; $ret .= "<p><input type=\"text\" name=\"url\" id=\"url\" value=\"\" size=\"22\" tabindex=\"3\" class='comment-inp' />\n"; $ret .= "<label for=\"url\">Website</label></p>\n"; $ret .= "<p><textarea name=\"comment\" id=\"comment\" cols=\"100%\" rows=\"4\" tabindex=\"4\" class='comment-inp' ></textarea></p>\n"; $ret .= "<p><input name=\"submit\" type=\"submit\" id=\"submit\" tabindex=\"5\" value=\"Post Comment\" />\n"; $ret .= "<input type=\"hidden\" name=\"comment_post_ID\" value=\"$wp_post_ID\" />\n"; if ($pageComments) { $ret .= "<input type=\"hidden\" name=\"wikipage\" value=\"" . $wgTitle->getText() . "\" size=\"80\"/>\n"; } $ret .= "<input type=\"hidden\" name=\"redirect_to\" value=\"" . $wgTitle->getFullURL() . "\" size=\"80\"/>\n"; $ret .= "</p>\n"; $ret .= "</form>\n"; $ret .= " "; } return $ret; }
Leave your comment
Comments
Ki
Nov 22 2011 8:44 pm
Thanks for getting back to me right away. After running around in circles for hours, I realized I had "wpcomment" instead of "wp_post_title". My mistake, I must have copied something from somewhere to the wrong place along the way.
Your plugin is working great now, thanks for pointing me in the right direction!!
Nov 17 2011 12:10 am
Nov 16 2011 4:26 pm
so for example it ignores this:
wp_post_title=YourBlogPostTitle
wp_url=http://yourblogurl.com
and uses this instead for the title:
$wp_post_title="Wordpress Comments";
What should I do?
Thanks!!
Nov 16 2011 3:14 pm
Oct 20 2011 5:26 pm
May 29 2011 9:18 am
May 18 2011 2:26 pm
Mar 14 2011 8:06 am
Jan 30 2011 11:45 am
Dec 17 2010 9:24 am