Login | Register

Programming

CommentAPI for Movable Type

Jan/13/2008 05:53 PM

I was going through some things on my site today, fixing up some bugs and such. One thing I noticed was that I never rewrote my CommentAPI support after I changed the way comments were handled on this site. I don't believe anyone ever posted a comment using CommentAPI, but I thought it was kind of nice to learn how it worked and to provide some support for it. It's somewhat of an older standard, and never really caught on, but I think it is really handy. Anyhow, I thought I'd release the code I had come up with. It still needs a little work to be perfect, but this did allow comments to come in on my Movable Type 3.2 system.

Here's PHP that took the comments in and submitted them to Movable Type:

<?php
//Created by Cameron Bulock 07/12/2005 - 09/06/2006
//Here is my implementation of CommentAPI for use with Movable Type

//URL to MT's comment CGI
$comment_url = 'domain/comments.cgi'

//Grab the PostID
$postid = $_GET['id'];
//Read in POST data containing the XML
$input = $GLOBALS['HTTP_RAW_POST_DATA'];

//Use SimpleXML to parse the input
$xml = simplexml_load_string($input);

//Format the data to post to MT
$url = urlencode($xml->link);
$entry = urlencode($xml->description);
$author = $xml->author;
$name = urlencode($xml->creator);

//This should work, but I haven't been able to fully test, currently just using $author for the author field, but $creator should be MT's equivalent to author
$dc = $xml->children('http://purl.org/dc/elements/1.1/');
$creator = urlencode($dc->$creator);

//pull email address out of authors field
preg_match("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $author, $match);
$email = $match[0];

$data = "entry_id=".$postid."&email=".$email."&url=".$url."&author=".urlencode($author)."&text=".$entry."&post= Post ";

//Setting up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $comment_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

//Run cURL
$process = curl_exec($ch);
curl_close($ch);

//Return a 'Created' HTTP response.  This is currently just assumed even if not successful
header('HTTP/1.1 201 Created');

//The server response needs to be reworked and more informative, see: http://www.imc.org/atom-protocol/mail-archive/msg01384.html
?>

After you have the code on your site, you also need to update your RSS feed to support CommentAPI. There are two things that need to be changed. First, the <RSS> tag needs to have the following added to it:

xmlns:wfw="http://wellformedweb.org/CommentAPI/"

Here's an example of what the full tag will probably look like:

<rss version="2.0" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:content="http://purl.org/rss/1.0/modules/content/" 
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
> 

That may or may not be how yours will look, depending on if you use any other namespace extensions to RSS. Mine actually is quite a bit larger.
Then, you need to add the following lines inside of the <MTEntries> tag:

<wfw:comment>
<$MTBlogURL$>comments/post.php?id=<$MTEntryID$>
</wfw:comment>

Of course, change the path to whatever you have named your PHP file.

Please be aware, this code is freely available for whatever use you'd like. Though I have tested and successfully used it myself in a couple different programs that supported CommentAPI, I don't know for a fact that the support is complete. For one thing, there is no error checking. Also, the HTTP responses don't fully support all the responses that should be sent. Also, since it uses SimpleXML, PHP5 is required to run this. There is no guarantees, and I can't be responsible for the code. I'm just offering this as I planned to release it once it was more complete, but as I am no longer using Movable Type's comment system, I won't be using this anymore and it's may be of some use to someone.

New DataAdmin Documentation

Dec/17/2007 02:35 PM

So, I've finally decided to create some decent documention for the DataAdmin PHP class I wrote. Before, a lame text file was all there was. There is now a wiki available. The current state of the information is about the same as it was before in the text file, but it's now much more organized and easier to browse through. I plan to add more full coding examples and such soon. Additionally, the login system for the wiki has been hooked into the login system for this site, so if you have an account here, you can also use the same info for the wiki.

DataAdmin Class 1.08 Update

Jun/25/2007 07:22 PM

I made a small little update to the DataAdmin class today. This is the first update in close to a year. All I did was add the ability to set a variable 'editable' to FALSE which then disables being able to click on data to edit it. This was simply a feature I needed for part of a backend update to this site. I still have some major changes I plan to make to this class, probably for a 1.1 version. The big things I want are the ability to add callback before and after running various processes. For instance, an image database might need to actually delete the image off the disk after deleting the entry from the database, so a callback could be triggered to handle that. My image backend on this site basically doesn't allow deleting images for that reason right now.
The other feature I want to add is for the class to actually handle taking XML data, performing database controls, then returning XML. That way apps could be designed however they want and still have full transparency from handling the raw database. This goes somewhat against the original intentions of this class, which was the ability to quickly develop a frontend to a database with almost no HTML coding, but this will allow more flexibility and sites could use a hybrid of the current setup and one where the HTML code is fully their own. I also will probably add the ability to have just PHP array variables to be sent back as results and also quite likely JSON to allow for some nice AJAX database controls.
I'd also even like to allow for a plugin type system where current functions in the class could be extended and added new feature by the outside code, but this would require more PHP knowledge on my end, and I don't really see too much of a new for that. There are just times when it seems like certain things really don't need to be added to the main class, they could just be patched in for the particular implementation I am using.

Database Admin PHP Class

May/11/2007 12:10 AM

I've got around to releasing a PHP class that I use for MySQL database administration. It's released under the LGPL. I plan to do a more detailed write-up about this soon. Basically, this class will allow you to add, edit, delete, and cross-reference data from databases and requires only minutes to setup with basically no HTML to do.

The code can be found here:
Database Admin Class

Validating Web Pages

Oct/23/2005 04:23 PM

Thought I would share this bit of code as some people might find it useful. Ever since I started working to make sure my pages were compatible, I have come up with a number of things to help me to accomplish this. Now, every page on this site should be fully xHTML compliant and I have setup a number of backend protections to stop most things from causing the sites to stop validating. One thing I wrote was a simple PHP function that can be used in any script that gives some feedback as to if a website is valid or not.

function validate_page($url)
{
$url = urlencode($url);
$site = "http://validator.w3.org/check?uri=$url";
$data = implode("", file($site));
$result = preg_match_all ("/\<h2 class\=\"valid\"\>/", $data, $matches);
return $result;
}
Usage: $page = "http://www.cbulock.com/";
$result = validate_page($page);

Basically, give the function a URL, it will send back either a 1 or 0 depending on if it's a valid page or not. If this is something useful, go ahead and use it, it's free to use as you please.