I just fixed a couple more Movable Type things. I reckon I must have the only working copy of MT5 in the whole world. Today I did a number of fixes to the new-fangled comment pagination feature.
Comment pagination is pretty cool. We’ve all been to blog pages with hundreds and hundreds of comments, and seen how such a huge page can clobber your browser. But the Movable Type comment pagination lets you only show a limited number of the most recent comments on your page, then uses AJAX to navigate to newer or older comments, without reloading the whole page.
The only problem was, it didn’t work. There were two or three problems I had to track down and fix. First of all, my blog (at seangleeson.com) is on a different domain from my MT installation (at mt5.gleeson.us). But the comment pagination script relies on an XMLHttpRequest, and as we all know, cross-domain XMLHttpRequests are illegal! The blog page was requesting a batch of comments from mt5.gleeson.us/mt-comments.cgi, and getting denied.
I fixed this problem by putting a very simple PHP script at seangleeson.com/commentlisting.php. All this script does is act as a go-between, a middleman. It gets the request from the blog page, passes it along to the MT script, gets an answer from the MT script, and passes it back to the blog page. So now the blog has a legal place to send its XMLHttpRequest.
Here is the code of commentlisting.php:
<?php
$R = 'void(0);';
$E = $_GET['e'];
$L = ($_GET['l'] ? $_GET['l'] : '50');
$O = ($_GET['o'] ? $_GET['o'] : '0');
if ($E) {
ob_start();
$U = "http://mt5.gleeson.us/mt-comments.cgi?";
$U .= "__mode=comment_listing&direction=ascend";
$U .= "&entry_id=".$E."&limit=".$L."&offset=".$O;
$C = curl_init();
curl_setopt($C, CURLOPT_URL, $U);
curl_exec($C);
curl_close($C);
$R = ob_get_clean();
}
header('Last-Modified: '.gmdate(DATE_RFC1123, time() - 6));
header('Expires: '.gmdate(DATE_RFC1123, time() + 6));
header('Accept-Ranges: bytes');
header('Content-Length: '.strlen($R));
header('Content-Type: text/javascript; charset=utf-8');
echo $R;
?>
And once that was in place, all I had to do was edit the JavaScript, to send its requests to commentlisting.php instead of mt-comments.cgi, thusly:
jsonUrl = [
"<$mt:BlogURL$>commentlisting.php?e=",
entryID,
"&l=",
commentsPerPage,
"&o=",
_getCommentOffset()
].join('');
And then it worked. Well, it worked after I wrote my True Comment Number plugin.
Leave a comment