52°F

Aaron Parecki

  • Articles
  • Notes
  • Photos
  • MediaWiki Comments via Wordpress

    June 27, 2009

    Introduction

    This is a modification of an existing MediaWiki plugin, found here

    The modifications include:

    • Visitors are immediately redirected to the mediawiki page after commenting
    • Wordpress posts are automatically created
    • Wordpress base URL is specified in LocalSettings.php rather than in each MediaWiki article

    Changes

    This is a brief description of the changes necessary to make this work. However some code needed to be moved around, so please verify with the entire code at the bottom before making any changes. This "changes" section is only intended to give you a vague idea of the changes involved.

    in function get_wordpress_comments:

    $wp_post_title = (isset($param["wp_post_title"]))? $param["wp_post_title"] : $wgParser->mTitle->mTextform;
    $wp_url = (isset($param["wp_url"]))? $param["wp_url"] : $wgWpBaseURL;
    

    a little farther down:

     if ($number < 1) {
      # Insert a new Wordpress Post with the specified title
      $sql = 'INSERT INTO wp_posts
       (post_author, post_date, post_date_gmt, post_content, post_title, post_status, comment_status,
         post_modified, post_modified_gmt, post_type)
        VALUES
       ("1", "'.date('Y-m-d H:i:s').'", "'.gmdate('Y-m-d H:i:s').'", "", "'.mysql_escape_string($wp_post_title).'", "publish", "open",
         "'.date('Y-m-d H:i:s').'", "'.gmdate('Y-m-d H:i:s').'", "post")';
      mysql_query($sql);
    
      # get the id of the post and update the wp_posts table
      $last_id = mysql_insert_id();
      $sql = 'UPDATE wp_posts SET guid="'.$wp_url.'/?p='.$last_id.'" WHERE id='.$last_id;
      mysql_query($sql);
     }
    

    in get_wordpress_reply_form()

    $ret .= "<input type=\"hidden\" name=\"redirect_to\" value=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . "\" />";
    

    Code

    <?php
    /**
     * WordPress Comments extension
     * For documentation, please see http://www.mediawiki.org/wiki/Extension:WordPress_Comments
     *
     * @ingroup Extensions
     * @author Greg Perry
     * @version 0.9.0
     */
    define('WORDPRESSCOMMENTS_VERSION','0.9.0, 2007-12-1');
    
    //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' => '<tt><wp:comments></tt> 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)
    {
     $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]);
       }
      }
     }
    
     global $wgParser, $wgDBserver, $wgDBname;
     global $wgDBuser, $wgDBpassword, $wgPageName, $wgWpBaseURL;
    
     $wp_post_title = (isset($param["wp_post_title"]))? $param["wp_post_title"] : $wgParser->mTitle->mTextform;
     $wp_url = (isset($param["wp_url"]))? $param["wp_url"] : $wgWpBaseURL;
    
     $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");
     mysql_query("SET NAMES utf8");
     mysql_query("SET CHARACTER_SET utf8");
    
     $sql = " SELECT * FROM wp_posts WHERE post_title LIKE \"".mysql_escape_string($wp_post_title)."\" AND post_status = 'publish' ";
     $result = mysql_query($sql);
     $number = mysql_num_rows($result);
     $wp_post_ID = 0;
     $ret = "";
    
     if ($number < 1) {
      # Insert a new Wordpress Post with the specified title
      $sql = 'INSERT INTO wp_posts
       (post_author, post_date, post_date_gmt, post_content, post_title, post_status, comment_status,
         post_modified, post_modified_gmt, post_type)
        VALUES
       ("1", "'.date('Y-m-d H:i:s').'", "'.gmdate('Y-m-d H:i:s').'", "", "'.mysql_escape_string($wp_post_title).'", "publish", "open",
         "'.date('Y-m-d H:i:s').'", "'.gmdate('Y-m-d H:i:s').'", "post")';
      mysql_query($sql);
    
      # get the id of the post and update the wp_posts table
      $last_id = mysql_insert_id();
      $sql = 'UPDATE wp_posts SET guid="'.$wp_url.'/?p='.$last_id.'" WHERE id='.$last_id;
      mysql_query($sql);
     }
    
     $sql = " SELECT * FROM wp_posts WHERE post_title LIKE \"".mysql_escape_string($wp_post_title)."\" AND post_status = 'publish' ";
     $result = mysql_query($sql);
     $number = mysql_num_rows($result);
     $wp_post_ID = 0;
     $ret = "";
    
     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_num_rows($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 .= ">";
       if( mysql_result($result,$i,"comment_author_url") )
        $ret .= " <cite><a href='". mysql_result($result,$i,"comment_author_url") ."' rel='external nofollow'>". mysql_result($result,$i,"comment_author") ."</a></cite> Says:";
       else
        $ret .= " <cite>". mysql_result($result,$i,"comment_author") ."</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 = "<div class=\"wpCommentForm\">";
     $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=\"5\" 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 .= "<input type=\"hidden\" name=\"redirect_to\" value=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . "\" />";
     $ret .= "</p>";
     $ret .= "</form>";
     $ret .= "</div>\n";
     }
     return $ret;
    }
    
    ?>
    
    Sat, Jun 27, 2009 5:17pm -07:00 #past-projects #mediawiki #wordpress
    2 mentions

    Other Mentions

    • mypropertynation.com
      Property Nation
      Wed, May 29, 2019 1:46pm -07:00 (via tinyurl.com)
    • industrialtalk.com
      IndustrialTalk
      Thu, May 30, 2019 3:53am -07:00 (via www.instagram.com)
Posted in /articles

Hi, I'm Aaron Parecki, Director of Identity Standards at Okta, and co-founder of IndieWebCamp. I maintain oauth.net, write and consult about OAuth, and participate in the OAuth Working Group at the IETF. I also help people learn about video production and livestreaming. (detailed bio)

I've been tracking my location since 2008 and I wrote 100 songs in 100 days. I've spoken at conferences around the world about owning your data, OAuth, quantified self, and explained why R is a vowel. Read more.

  • Director of Identity Standards at Okta
  • IndieWebCamp Founder
  • OAuth WG Editor
  • OpenID Board Member

  • 🎥 YouTube Tutorials and Reviews
  • 🏠 We're building a triplex!
  • ⭐️ Life Stack
  • ⚙️ Home Automation
  • All
  • Articles
  • Bookmarks
  • Notes
  • Photos
  • Replies
  • Reviews
  • Trips
  • Videos
  • Contact
© 1999-2025 by Aaron Parecki. Powered by p3k. This site supports Webmention.
Except where otherwise noted, text content on this site is licensed under a Creative Commons Attribution 3.0 License.
IndieWebCamp Microformats Webmention W3C HTML5 Creative Commons
WeChat ID
aaronpk_tv