#!/usr/bin/perl # mt-comment-stop.pl - v0.01 # stop people leaving MT comments on a post when one hasn't been left # for a period of time - for mysql DB powered blogs # USAGE ./mt-comment-stop.pl [directory your blog lives in] # (typically in a nightly crontab) # (C) James Powell / March 2004 - http://www.webscalability.com/ use DBI; use strict; use Data::Dumper; my $days_to_close = 50; # number of days to mark comments closed on my $bloghome = $ARGV[0]; die unless (-d $bloghome); # get database connection params my ($db, $dbuser, $dbpass); open(MTCFG, "$bloghome/mt.cfg"); while () { chomp; if (/^Database +(.+)$/) { $db = $1; } elsif (/^DBUser +(.+)$/) { $dbuser = $1; } } close(MTCFG); open(MTDBPASS, "$bloghome/mt-db-pass.cgi"); $dbpass = ; chomp $dbpass; close(MTDBPASS); my $dbh = DBI->connect("DBI:mysql:database=$db", $dbuser, $dbpass) or die "Can't connect to DB"; # get list of comment ids to leave unmarked my $sth = $dbh->prepare("select distinct(comment_entry_id) from mt_comment where now() < date_add(comment_created_on, interval $days_to_close DAY)"); $sth->execute; my @cids_to_leave; while (my ($cid) = $sth->fetchrow_array) { push @cids_to_leave, $cid; } $sth->finish; if ($ENV{DEBUG}) { print "CIDS to leave: " . Dumper(\@cids_to_leave); } my $cidstoleave = join ',', @cids_to_leave; my $sql = "update mt_entry set entry_allow_comments = 2 where entry_allow_comments = 1 and entry_id not in ($cidstoleave) and now() > date_add(entry_created_on, interval $days_to_close DAY)"; if ($ENV{DEBUG}) { print "SQL: $sql\n"; } else { $dbh->do($sql); } $dbh->disconnect; exit;